Files
mtusi2026lab6/src/attachments.py
T

145 lines
4.9 KiB
Python

from uuid import uuid4
from src.errors import DomainError, not_found
from src.pipeline import enqueue, fault
def dto(row, variants=()):
return {
k: row[k]
for k in [
"id",
"chat_id",
"owner_id",
"filename",
"content_type",
"size_bytes",
"sha256",
"created_at",
"updated_at",
]
} | {
"status": row["state"],
"variants": [
{
k: v[k]
for k in [
"name",
"content_type",
"size_bytes",
"sha256",
"width",
"height",
]
}
for v in variants
if v["name"] != "original"
],
"error_code": row["error_code"] if row["state"] == "failed" else None,
}
class Attachments:
def __init__(self, *, database, store, instance):
self.database, self.store, self.instance = database, store, instance
def upload(
self,
*,
chat_id,
owner_id,
filename,
content_type,
size,
sha256,
stream,
):
identity, job_id = uuid4(), uuid4()
key = f"objects/staging/{identity}/original"
with self.database.connection() as conn:
conn.execute(
"INSERT INTO upload_reservations(id,object_key) VALUES(%s,%s)",
(identity, key),
)
self.store.put(
key=key,
stream=stream,
size=size,
content_type=content_type,
sha256=sha256,
)
fault(self.database, chat_id, "api_after_staging")
with self.database.connection() as conn, conn.transaction():
reservation = conn.execute(
"SELECT * FROM upload_reservations WHERE id=%s AND expires_at>clock_timestamp() FOR UPDATE",
(identity,),
).fetchone()
if reservation is None:
raise DomainError(
503, "unavailable", "Upload reservation expired"
)
row = conn.execute(
"INSERT INTO attachments(id,chat_id,owner_id,filename,content_type,size_bytes,sha256,object_key,state,uploader_instance,expires_at) "
"VALUES(%s,%s,%s,%s,%s,%s,%s,%s,'queued',%s,clock_timestamp()) RETURNING *",
(
identity,
chat_id,
owner_id,
filename,
content_type,
size,
sha256,
key,
self.instance,
),
).fetchone()
conn.execute(
"INSERT INTO jobs(id,attachment_id,pipeline_version,source_key,state) VALUES(%s,%s,1,%s,'queued')",
(job_id, identity, key),
)
enqueue(conn, job_id)
conn.execute(
"DELETE FROM upload_reservations WHERE id=%s", (identity,)
)
fault(self.database, chat_id, "api_after_commit")
# Captured committed snapshot remains queued even if worker already finished.
return dto(row)
def row(self, *, identity, actor_id):
with self.database.connection() as conn:
row = conn.execute(
"SELECT a.* FROM attachments a JOIN members m ON m.chat_id=a.chat_id AND m.user_id=%s WHERE a.id=%s",
(actor_id, identity),
).fetchone()
if row is None:
raise not_found()
return row
def metadata(self, *, identity, actor_id):
row = self.row(identity=identity, actor_id=actor_id)
variants = []
if row["state"] == "ready":
with self.database.connection() as conn:
variants = conn.execute(
"SELECT * FROM attachment_variants WHERE attachment_id=%s AND pipeline_version=%s ORDER BY name",
(identity, row["published_version"]),
).fetchall()
return dto(row, variants)
def content(self, *, identity, actor_id, variant):
row = self.row(identity=identity, actor_id=actor_id)
if row["state"] != "ready":
raise DomainError(409, "conflict", "Attachment is not ready")
with self.database.connection() as conn:
data = conn.execute(
"SELECT * FROM attachment_variants WHERE attachment_id=%s AND pipeline_version=%s AND name=%s",
(identity, row["published_version"], variant),
).fetchone()
if data is None:
raise not_found()
if self.store.head(key=data["object_key"]) is None:
raise DomainError(503, "unavailable", "Variant unavailable")
return {**data, "filename": row["filename"]}, self.store.get(
key=data["object_key"]
)