Отделить обработку вложений от приёма файла.
Upload отвечает 202 после staging и записи задания, worker через RabbitMQ собирает thumbnail, crop и optimized и переживает разрыв до publish. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+40
-8
@@ -5,7 +5,7 @@ import re
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import JSONResponse, Response
|
||||
|
||||
from app import attachments, cache, config, db, direct, images, rediscli, s3, sessions, store
|
||||
from app import attachments, cache, config, db, direct, images, jobs, rediscli, s3, sessions, store
|
||||
from app.cursor_codec import decode_cursor, encode_cursor
|
||||
from app.errors import BASIC_CHALLENGE, BEARER_CHALLENGE, AppError, error_response
|
||||
from app.passwords import dummy_hash, hash_password, verify_password
|
||||
@@ -309,7 +309,7 @@ async def upload_attachment(request: Request) -> Response:
|
||||
except images.ImageRejected as exc:
|
||||
return error_response(exc.status, exc.message, rid)
|
||||
created = await attachments.create_attachment(chat_id, user["id"], filename, content_type, data)
|
||||
return _json(created, rid, 201)
|
||||
return _json(created, rid, 202)
|
||||
|
||||
|
||||
async def begin_direct_upload(request: Request) -> Response:
|
||||
@@ -360,7 +360,7 @@ async def complete_direct_upload(request: Request) -> Response:
|
||||
if not isinstance(digest, str) or re.fullmatch(r"[a-f0-9]{64}", digest) is None:
|
||||
return error_response(400, "invalid checksum", rid)
|
||||
created = await direct.complete_upload(upload_id, user["id"], digest)
|
||||
return _json(created, rid, 201)
|
||||
return _json(created, rid, 202)
|
||||
|
||||
|
||||
async def get_attachment(request: Request) -> Response:
|
||||
@@ -372,7 +372,7 @@ async def get_attachment(request: Request) -> Response:
|
||||
row = await attachments.get_for_member(attachment_id, user["id"])
|
||||
if row is None:
|
||||
return error_response(404, "attachment not found", rid)
|
||||
return _json(attachments._public(row), rid)
|
||||
return _json(await attachments.present(row), rid)
|
||||
|
||||
|
||||
async def download_attachment(request: Request) -> Response:
|
||||
@@ -381,14 +381,46 @@ async def download_attachment(request: Request) -> Response:
|
||||
attachment_id = request.path_params["attachment_id"]
|
||||
if validate_uuid(attachment_id):
|
||||
return error_response(400, "invalid attachment id", rid)
|
||||
variant = request.query_params.get("variant")
|
||||
if variant not in (None, "original"):
|
||||
variant = request.query_params.get("variant") or "original"
|
||||
if variant not in ("original", "thumbnail", "crop", "optimized"):
|
||||
return error_response(400, "invalid variant", rid)
|
||||
row = await attachments.get_for_member(attachment_id, user["id"])
|
||||
if row is None:
|
||||
return error_response(404, "attachment not found", rid)
|
||||
payload = await s3.get_bytes(row["object_key"])
|
||||
return Response(payload, status_code=200, media_type=row["content_type"], headers={"X-Request-Id": rid})
|
||||
if row["status"] != "ready":
|
||||
return error_response(409, "attachment is not ready", rid)
|
||||
if variant == "original":
|
||||
payload = await s3.get_bytes(row["object_key"])
|
||||
media_type = row["content_type"]
|
||||
else:
|
||||
async with db.require().acquire() as conn:
|
||||
found = await conn.fetchrow(
|
||||
"""
|
||||
SELECT content_type, object_key
|
||||
FROM attachment_variants
|
||||
WHERE attachment_id = $1 AND pipeline_version = 1 AND name = $2
|
||||
""",
|
||||
row["id"],
|
||||
variant,
|
||||
)
|
||||
if found is None:
|
||||
return error_response(404, "attachment not found", rid)
|
||||
payload = await s3.get_bytes(found["object_key"])
|
||||
media_type = found["content_type"]
|
||||
return Response(payload, status_code=200, media_type=media_type, headers={"X-Request-Id": rid})
|
||||
|
||||
|
||||
async def lab_replay(request: Request) -> Response:
|
||||
rid = request.state.request_id
|
||||
if not config.lab_mode():
|
||||
return error_response(404, "not found", rid)
|
||||
attachment_id = request.path_params["attachment_id"]
|
||||
if validate_uuid(attachment_id):
|
||||
return error_response(400, "invalid attachment id", rid)
|
||||
result = await jobs.replay_attachment(attachment_id)
|
||||
if result["result"] == "missing":
|
||||
return error_response(404, "attachment not found", rid)
|
||||
return _json(result, rid)
|
||||
|
||||
|
||||
async def lab_sweep(request: Request) -> Response:
|
||||
|
||||
Reference in New Issue
Block a user