Files
mtusi2026lab6/app/direct.py
T
dawuduk 14ab803529 Перенести рабочую реализацию lab5 в skeleton лабораторной 6.
API, worker, брокер и стенд остаются базой, контракт lab6 не менялся.
2026-11-17 15:15:42 +03:00

204 lines
7.0 KiB
Python

"""Presigned PUT that does not replace the proxy upload endpoint."""
from __future__ import annotations
import asyncio
import hashlib
from datetime import datetime, timedelta, timezone
from uuid import UUID, uuid4
from app import config, db, images, s3
from app.attachments import _compensate, _public
from app import jobs
from app.errors import AppError
from app.store import format_ts
def _aware(value: datetime) -> datetime:
if value.tzinfo is None:
return value.replace(tzinfo=timezone.utc)
return value
async def begin_upload(
chat_id: str,
owner_id: str,
filename: str,
content_type: str,
size_bytes: int,
) -> dict:
upload_id = uuid4()
key = f"originals/{upload_id}"
created = datetime.now(timezone.utc)
expires_at = created + timedelta(seconds=config.PRESIGN_TTL_SECONDS)
async with db.require().acquire() as conn:
async with conn.transaction():
await conn.execute(
"""
INSERT INTO blob_writes (object_key, committed, created_at)
VALUES ($1, FALSE, $2)
""",
key,
created,
)
await conn.execute(
"""
INSERT INTO direct_uploads (
id, chat_id, owner_id, filename, content_type, size_bytes,
object_key, status, expires_at, created_at
)
VALUES ($1, $2, $3, $4, $5, $6, $7, 'pending', $8, $9)
""",
upload_id,
UUID(chat_id),
UUID(owner_id),
filename,
content_type,
size_bytes,
key,
expires_at,
created,
)
try:
url = await asyncio.to_thread(s3.presign_put, key, content_type, size_bytes)
except Exception:
await _compensate(key)
async with db.require().acquire() as conn:
await conn.execute(
"UPDATE direct_uploads SET status = 'aborted' WHERE id = $1 AND status = 'pending'",
upload_id,
)
raise
return {
"upload_id": str(upload_id),
"method": "PUT",
"url": url,
"headers": {"Content-Type": content_type},
"expires_at": format_ts(expires_at),
"size_bytes": size_bytes,
}
async def _abort_pending(upload_id: UUID, key: str) -> None:
async with db.require().acquire() as conn:
changed = await conn.fetchval(
"""
UPDATE direct_uploads
SET status = 'aborted'
WHERE id = $1 AND status = 'pending'
RETURNING id
""",
upload_id,
)
if changed is not None:
await _compensate(key)
async def complete_upload(upload_id: str, owner_id: str, claimed_sha: str) -> dict:
async with db.require().acquire() as conn:
row = await conn.fetchrow(
"""
SELECT id, chat_id, owner_id, filename, content_type, size_bytes,
object_key, status, expires_at
FROM direct_uploads
WHERE id = $1 AND owner_id = $2
""",
UUID(upload_id),
UUID(owner_id),
)
if row is None:
raise AppError(404, "upload not found")
if row["status"] != "pending":
raise AppError(409, "upload is already finished")
if _aware(row["expires_at"]) <= datetime.now(timezone.utc):
await _abort_pending(row["id"], row["object_key"])
raise AppError(409, "upload signature expired")
actual_size = await s3.head_size(row["object_key"])
if actual_size is None:
raise AppError(409, "upload is incomplete")
if actual_size != row["size_bytes"] or actual_size > config.MAX_UPLOAD_BYTES:
await _abort_pending(row["id"], row["object_key"])
raise AppError(400, "uploaded size does not match")
data = await s3.get_bytes(row["object_key"])
digest = hashlib.sha256(data).hexdigest()
if len(data) != row["size_bytes"] or digest != claimed_sha:
await _abort_pending(row["id"], row["object_key"])
raise AppError(400, "checksum does not match")
try:
images.inspect_image(data, row["content_type"])
except images.ImageRejected as exc:
await _abort_pending(row["id"], row["object_key"])
raise AppError(exc.status, exc.message) from None
created = datetime.now(timezone.utc)
try:
async with db.require().acquire() as conn:
async with conn.transaction():
locked = await conn.fetchrow(
"""
SELECT status, object_key
FROM direct_uploads
WHERE id = $1 AND owner_id = $2
FOR UPDATE
""",
row["id"],
UUID(owner_id),
)
if locked is None or locked["status"] != "pending":
raise AppError(409, "upload is already finished")
intent = await conn.fetchrow(
"SELECT committed FROM blob_writes WHERE object_key = $1 FOR UPDATE",
row["object_key"],
)
if intent is None or intent["committed"]:
raise AppError(409, "upload is already finished")
await conn.execute(
"""
INSERT INTO attachments (
id, chat_id, owner_id, filename, content_type, size_bytes,
sha256, object_key, status, error_code, created_at, updated_at
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 'queued', NULL, $9, $9)
""",
row["id"],
row["chat_id"],
row["owner_id"],
row["filename"],
row["content_type"],
len(data),
digest,
row["object_key"],
created,
)
await jobs.insert_job(conn, row["id"], created)
await conn.execute(
"UPDATE blob_writes SET committed = TRUE WHERE object_key = $1",
row["object_key"],
)
await conn.execute(
"UPDATE direct_uploads SET status = 'completed' WHERE id = $1",
row["id"],
)
except AppError:
raise
except Exception:
raise AppError(503, "service unavailable") from None
jobs.kick_publish()
return _public(
{
"id": row["id"],
"chat_id": row["chat_id"],
"owner_id": row["owner_id"],
"filename": row["filename"],
"content_type": row["content_type"],
"size_bytes": len(data),
"sha256": digest,
"status": "queued",
"error_code": None,
"created_at": created,
"updated_at": created,
}
)