mariadb -> postgres
This commit is contained in:
1 parent
21964fa986
commit
e51bb86dc0
36 files changed
+650
-464
No files matched your search
+11
-16
@@ -36,9 +36,10 @@ async def create_new_encryption_key(db_session, user_id: int = None) -> KnownKey
|
||||
meta={"I_user_id": user_id} if user_id else None,
|
||||
created=datetime.now()
|
||||
)
|
||||
from sqlalchemy import select
|
||||
db_session.add(new_key)
|
||||
db_session.commit()
|
||||
new_key = db_session.query(KnownKey).filter(KnownKey.seed_hash == new_seed_hash).first()
|
||||
await db_session.commit()
|
||||
new_key = (await db_session.execute(select(KnownKey).where(KnownKey.seed_hash == new_seed_hash))).scalars().first()
|
||||
assert new_key, "Key not created"
|
||||
return new_key
|
||||
|
||||
@@ -46,9 +47,10 @@ async def create_new_encryption_key(db_session, user_id: int = None) -> KnownKey
|
||||
async def create_encrypted_content(
|
||||
db_session, decrypted_content: StoredContent,
|
||||
) -> StoredContent:
|
||||
encrypted_content = db_session.query(StoredContent).filter(
|
||||
from sqlalchemy import select
|
||||
encrypted_content = (await db_session.execute(select(StoredContent).where(
|
||||
StoredContent.id == decrypted_content.decrypted_content_id
|
||||
).first()
|
||||
))).scalars().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
|
||||
@@ -57,10 +59,8 @@ async def create_encrypted_content(
|
||||
if decrypted_content.key is None:
|
||||
key = await create_new_encryption_key(db_session, user_id=decrypted_content.user_id)
|
||||
decrypted_content.key_id = key.id
|
||||
db_session.commit()
|
||||
decrypted_content = db_session.query(StoredContent).filter(
|
||||
StoredContent.id == decrypted_content.id
|
||||
).first()
|
||||
await db_session.commit()
|
||||
decrypted_content = (await db_session.execute(select(StoredContent).where(StoredContent.id == decrypted_content.id))).scalars().first()
|
||||
assert decrypted_content.key_id, "Key not assigned"
|
||||
|
||||
decrypted_path = os.path.join(UPLOADS_DIR, decrypted_content.hash)
|
||||
@@ -72,9 +72,7 @@ async def create_encrypted_content(
|
||||
encrypted_bin = cipher.encrypt(decrypted_bin)
|
||||
encrypted_hash_bin = sha256(encrypted_bin).digest()
|
||||
encrypted_hash = b58encode(encrypted_hash_bin).decode()
|
||||
encrypted_content = db_session.query(StoredContent).filter(
|
||||
StoredContent.hash == encrypted_hash
|
||||
).first()
|
||||
encrypted_content = (await db_session.execute(select(StoredContent).where(StoredContent.hash == encrypted_hash))).scalars().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
|
||||
@@ -99,19 +97,16 @@ async def create_encrypted_content(
|
||||
created=datetime.now(),
|
||||
)
|
||||
db_session.add(encrypted_content)
|
||||
db_session.commit()
|
||||
await db_session.commit()
|
||||
|
||||
encrypted_path = os.path.join(UPLOADS_DIR, encrypted_hash)
|
||||
async with aiofiles.open(encrypted_path, mode='wb') as file:
|
||||
await file.write(encrypted_bin)
|
||||
|
||||
encrypted_content = db_session.query(StoredContent).filter(
|
||||
StoredContent.hash == encrypted_hash
|
||||
).first()
|
||||
encrypted_content = (await db_session.execute(select(StoredContent).where(StoredContent.hash == encrypted_hash))).scalars().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
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user