This commit is contained in:
Матвей
2026-10-26 15:49:31 +03:00
parent 4f6e69cfac
commit a1bb6f8bde
18 changed files with 820 additions and 195 deletions
+8 -6
View File
@@ -83,10 +83,6 @@ async def upload(chat_id: UUID, request: Request, filename: str = Query(...),
attachment_id, job_id = uuid4(), uuid4()
key = f"staging/{attachment_id}/original"
async with store.attachment_transaction() as connection:
await s3.put(key, file, size, content_type)
# Disabled by default; models a crash after PUT, before INSERT.
if os.environ.get("UPLOAD_FAULT_AFTER_S3_PUT") == "1":
raise ApiError(503, "unavailable", "Инъецирован отказ после S3 PUT")
try:
max_pending = int(os.environ.get("MAX_PENDING_JOBS", "100"))
except ValueError as error:
@@ -94,6 +90,9 @@ async def upload(chat_id: UUID, request: Request, filename: str = Query(...),
if max_pending < 1:
raise ApiError(503, "unavailable", "Некорректный лимит очереди")
await store.ensure_attachment_queue_capacity(connection, max_pending)
await s3.put(key, file, size, content_type)
if os.environ.get("UPLOAD_FAULT_AFTER_S3_PUT") == "1":
raise ApiError(503, "unavailable", "Инъецирован отказ после S3 PUT")
attachment = await store.create_attachment(
attachment_id, chat_id, user["id"], filename, content_type,
size, digest.hexdigest(), key, job_id, connection=connection,
@@ -130,9 +129,12 @@ async def download(attachment_id: UUID, request: Request,
if not matching:
raise ApiError(404, "not_found", "Вариант не найден")
item = matching[0]
suffix = {"thumbnail": "thumbnail.jpg", "crop": "crop.jpg", "optimized": "optimized.webp"}[variant]
object_key = await store.pool.fetchval(
'SELECT object_key FROM attachment_variants WHERE attachment_id=$1 AND pipeline_version=$2 AND name=$3',
attachment_id, attachment['pipeline_version'], variant,
)
data = await request.app.state.s3.download_key(
f"variants/{attachment_id}/{suffix}", item["size_bytes"], item["sha256"]
object_key, item["size_bytes"], item["sha256"]
)
content_type = item["content_type"]
return Response(data, media_type=content_type,