dev@locazia: fix encrypt content
This commit is contained in:
1 parent
b8251a456b
commit
e00a25f713
2 files changed
+41
-28
No files matched your search
+37
-28
@@ -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,
|
||||
}
|
||||
)
|
||||
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")
|
||||
|
||||
metadata_content, is_new = await create_new_content(
|
||||
db_session, "local/content_bin", metadata_bin, filename="metadata.json",
|
||||
meta={'content_type': 'application/json'},
|
||||
)
|
||||
return metadata_content
|
||||
Reference in new issue
Block a user