docs: add memory experiment results to report
This commit is contained in:
+26
-6
@@ -242,7 +242,8 @@ async def list_messages(chat_id: UUID, user: User = Depends(get_current_user), l
|
||||
async def upload_attachment(chat_id: UUID, request: Request, filename: str = Query(..., min_length=1, max_length=255), user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db)):
|
||||
chat = await chats_repo.get_if_member(db, chat_id, user.id)
|
||||
if not chat: raise HTTPException(404, "Chat not found")
|
||||
|
||||
if "/" in filename or "\\" in filename or "\x00" in filename:
|
||||
raise HTTPException(400, "Invalid filename")
|
||||
content = await request.body()
|
||||
mime = validate_image(content)
|
||||
|
||||
@@ -250,12 +251,26 @@ async def upload_attachment(chat_id: UUID, request: Request, filename: str = Que
|
||||
ext = ".png" if mime == "image/png" else ".jpg"
|
||||
s3_key = f"{chat_id}/{uuid.uuid4()}{ext}"
|
||||
|
||||
s3.put_object(Bucket=BUCKET, Key=s3_key, Body=content, ContentType=mime)
|
||||
# 1. Загрузка в S3 (с обработкой недоступности S3 -> 503)
|
||||
try:
|
||||
s3.put_object(Bucket=BUCKET, Key=s3_key, Body=content, ContentType=mime)
|
||||
except Exception:
|
||||
raise HTTPException(503, "S3 Storage unavailable")
|
||||
|
||||
# 2. Сохранение в БД (с компенсацией при ошибке -> удаление из S3)
|
||||
att = Attachment(chat_id=chat_id, owner_id=user.id, filename=filename, content_type=mime, size_bytes=len(content), sha256=sha256, status="ready", s3_key=s3_key)
|
||||
db.add(att)
|
||||
await db.commit()
|
||||
await db.refresh(att)
|
||||
try:
|
||||
await db.commit()
|
||||
await db.refresh(att)
|
||||
except Exception:
|
||||
await db.rollback()
|
||||
try:
|
||||
s3.delete_object(Bucket=BUCKET, Key=s3_key)
|
||||
except Exception:
|
||||
pass # если S3 тоже упал, удалит фоновый скрипт (reconciler)
|
||||
raise HTTPException(500, "Database error while saving attachment")
|
||||
|
||||
return _attachment_json(att)
|
||||
|
||||
@app.get("/api/v1/attachments/{attachment_id}")
|
||||
@@ -273,8 +288,13 @@ async def download_attachment(attachment_id: UUID, user: User = Depends(get_curr
|
||||
chat = await chats_repo.get_if_member(db, att.chat_id, user.id)
|
||||
if not chat: raise HTTPException(404, "Attachment not found")
|
||||
|
||||
obj = s3.get_object(Bucket=BUCKET, Key=att.s3_key)
|
||||
content = obj["Body"].read()
|
||||
# 3. Скачивание из S3 (с обработкой недоступности S3 -> 503)
|
||||
try:
|
||||
obj = s3.get_object(Bucket=BUCKET, Key=att.s3_key)
|
||||
content = obj["Body"].read()
|
||||
except Exception:
|
||||
raise HTTPException(503, "S3 Storage unavailable")
|
||||
|
||||
return Response(content=content, media_type=att.content_type)
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user