dev@locazia: fix encrypt content
This commit is contained in:
parent
b8251a456b
commit
e00a25f713
|
|
@ -10,6 +10,7 @@ from app.core._config import UPLOADS_DIR
|
|||
from app.core._crypto.cipher import AESCipher
|
||||
from app.core.models.keys import KnownKey
|
||||
from app.core.models.node_storage import StoredContent
|
||||
from app.core.logger import make_log
|
||||
|
||||
|
||||
async def create_new_encryption_key(db_session, user_id: int = None) -> KnownKey:
|
||||
|
|
@ -48,6 +49,7 @@ async def create_encrypted_content(
|
|||
StoredContent.id == decrypted_content.id
|
||||
).first()
|
||||
if encrypted_content:
|
||||
make_log("create_encrypted_content", f"(d={decrypted_content.cid.serialize_v2()}) => (e={encrypted_content.cid.serialize_v2()}): already exist (found by decrypted content)", level="debug")
|
||||
return encrypted_content
|
||||
|
||||
encrypted_content = None
|
||||
|
|
@ -74,6 +76,7 @@ async def create_encrypted_content(
|
|||
StoredContent.hash == encrypted_hash
|
||||
).first()
|
||||
if encrypted_content:
|
||||
make_log("create_encrypted_content", f"(d={decrypted_content.cid.serialize_v2()}) => (e={encrypted_content.cid.serialize_v2()}): already exist (found by encrypted_hash)", level="debug")
|
||||
return encrypted_content
|
||||
|
||||
encrypted_content = None
|
||||
|
|
@ -106,6 +109,7 @@ async def create_encrypted_content(
|
|||
StoredContent.hash == encrypted_hash
|
||||
).first()
|
||||
assert encrypted_content, "Content not created"
|
||||
make_log("create_encrypted_content", f"(d={decrypted_content.cid.serialize_v2()}) => (e={encrypted_content.cid.serialize_v2()}): created new content/bin", level="debug")
|
||||
return encrypted_content
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
import json
|
||||
import asyncio
|
||||
import os
|
||||
import aiofiles
|
||||
from hashlib import sha256
|
||||
|
||||
from base58 import b58encode
|
||||
|
|
@ -7,12 +9,42 @@ from datetime import datetime, timedelta
|
|||
from httpx import AsyncClient
|
||||
|
||||
from app.core.logger import make_log
|
||||
from app.core._config import PROJECT_HOST
|
||||
from app.core._config import PROJECT_HOST, UPLOADS_DIR
|
||||
from app.core._crypto.signer import Signer
|
||||
from app.core._secrets import hot_seed
|
||||
from app.core.models.node_storage import StoredContent
|
||||
|
||||
|
||||
async def create_new_content(
|
||||
db_session, type: str, content_bin: bytes, **kwargs
|
||||
) -> [StoredContent, bool]: # return content, is_new
|
||||
assert type.startswith("local/"), "Invalid type"
|
||||
kwargs = {k: v for k, v in kwargs.items() if not (k in ['id', 'content_id', 'created', 'onchain_index'])}
|
||||
content_hash_bin = sha256(content_bin).digest()
|
||||
content_hash_b58 = b58encode(content_hash_bin).decode()
|
||||
|
||||
new_content = db_session.query(StoredContent).filter(StoredContent.hash == content_hash_b58).first()
|
||||
if new_content:
|
||||
return new_content, False
|
||||
|
||||
new_content = StoredContent(
|
||||
type=type,
|
||||
hash=content_hash_b58,
|
||||
**kwargs,
|
||||
created=datetime.now(),
|
||||
|
||||
)
|
||||
db_session.add(new_content)
|
||||
db_session.commit()
|
||||
new_content = db_session.query(StoredContent).filter(StoredContent.hash == content_hash).first()
|
||||
assert new_content, "Content not created (through utils)"
|
||||
content_filepath = os.path.join(UPLOADS_DIR, content_hash_b58)
|
||||
async with aiofiles.open(content_filepath, 'wb') as file:
|
||||
await file.write(content_bin)
|
||||
|
||||
return new_content, True
|
||||
|
||||
|
||||
async def create_metadata_for_item(
|
||||
db_session,
|
||||
title: str = None,
|
||||
|
|
@ -48,31 +80,8 @@ async def create_metadata_for_item(
|
|||
metadata_hash = sha256(metadata_bin).digest()
|
||||
metadata_hash_b58 = b58encode(metadata_hash).decode()
|
||||
|
||||
metadata_content = None
|
||||
init_ts = datetime.now()
|
||||
async with AsyncClient() as client:
|
||||
while not metadata_content:
|
||||
response = await client.post(
|
||||
f"{PROJECT_HOST}/api/v1/storage",
|
||||
files={"file": ('metadata.json', metadata_bin, 'json')},
|
||||
headers={
|
||||
'X-Service-Signature': signer.sign(metadata_hash),
|
||||
'X-Message-Hash': metadata_hash_b58,
|
||||
}
|
||||
metadata_content, is_new = await create_new_content(
|
||||
db_session, "local/content_bin", metadata_bin, filename="metadata.json",
|
||||
meta={'content_type': 'application/json'},
|
||||
)
|
||||
try:
|
||||
assert response.status_code == 200
|
||||
except BaseException as e:
|
||||
make_log("create_metadata_for_item", f"Res: {response.text}, Error: {e}", level='error')
|
||||
raise e
|
||||
|
||||
response_json = response.json()
|
||||
metadata_sha256 = response_json['content_sha256']
|
||||
|
||||
metadata_content = db_session.query(StoredContent).filter(StoredContent.hash == metadata_sha256).first()
|
||||
|
||||
await asyncio.sleep(.3)
|
||||
if (datetime.now() - init_ts) > timedelta(seconds=10):
|
||||
raise Exception("Metadata not created")
|
||||
|
||||
return metadata_content
|
||||
|
|
|
|||
Loading…
Reference in New Issue