лаба на 3
This commit is contained in:
+51
-3
@@ -14,6 +14,7 @@ from fastapi.security import (
|
||||
HTTPBearer,
|
||||
)
|
||||
from botocore.exceptions import BotoCoreError, ClientError
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.exc import IntegrityError, SQLAlchemyError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||
@@ -552,7 +553,7 @@ async def get_messages_endpoint(
|
||||
@app.post(
|
||||
"/api/v1/chats/{chat_id}/attachments",
|
||||
response_model=Attachment,
|
||||
status_code=201,
|
||||
status_code=202,
|
||||
)
|
||||
async def upload_attachment_endpoint(
|
||||
chat_id: UUID,
|
||||
@@ -600,7 +601,7 @@ async def upload_attachment_endpoint(
|
||||
|
||||
attachment_id = uuid4()
|
||||
extension = "png" if content_type == "image/png" else "jpg"
|
||||
s3_key = f"attachments/{chat_id}/{attachment_id}.{extension}"
|
||||
s3_key = f"staging/{chat_id}/{attachment_id}.{extension}"
|
||||
|
||||
try:
|
||||
await put_object(
|
||||
@@ -628,6 +629,17 @@ async def upload_attachment_endpoint(
|
||||
s3_key=s3_key,
|
||||
)
|
||||
|
||||
await session.execute(
|
||||
text("""
|
||||
INSERT INTO attachment_jobs (id, attachment_id)
|
||||
VALUES (:job_id, :attachment_id)
|
||||
"""),
|
||||
{
|
||||
"job_id": uuid4(),
|
||||
"attachment_id": attachment_id,
|
||||
},
|
||||
)
|
||||
|
||||
await session.commit()
|
||||
|
||||
except BaseException:
|
||||
@@ -688,6 +700,7 @@ async def get_attachment_content_endpoint(
|
||||
attachment_id: UUID,
|
||||
current_user: User = Depends(get_current_user),
|
||||
session: AsyncSession = Depends(get_db_session),
|
||||
variant: str = Query(default="original"),
|
||||
):
|
||||
result = await get_attachment(
|
||||
session=session,
|
||||
@@ -704,6 +717,41 @@ async def get_attachment_content_endpoint(
|
||||
|
||||
attachment, s3_key = result
|
||||
|
||||
if attachment.status != "ready":
|
||||
raise APIError(
|
||||
status_code=409,
|
||||
code="conflict",
|
||||
message="Attachment is not ready",
|
||||
)
|
||||
|
||||
if variant == "thumbnail":
|
||||
selected = next(
|
||||
(item for item in attachment.variants if item.name == "thumbnail"),
|
||||
None,
|
||||
)
|
||||
|
||||
if selected is None:
|
||||
raise APIError(
|
||||
status_code=404,
|
||||
code="not_found",
|
||||
message="Variant not found",
|
||||
)
|
||||
|
||||
s3_key = (
|
||||
f"variants/{attachment.chat_id}/{attachment_id}/thumbnail.jpg"
|
||||
)
|
||||
media_type = selected.content_type
|
||||
|
||||
elif variant == "original":
|
||||
media_type = attachment.content_type
|
||||
|
||||
else:
|
||||
raise APIError(
|
||||
status_code=404,
|
||||
code="not_found",
|
||||
message="Variant not found",
|
||||
)
|
||||
|
||||
try:
|
||||
body = await get_object(s3_key)
|
||||
except (BotoCoreError, ClientError, OSError, TimeoutError) as exc:
|
||||
@@ -715,7 +763,7 @@ async def get_attachment_content_endpoint(
|
||||
|
||||
return Response(
|
||||
content=body,
|
||||
media_type=attachment.content_type,
|
||||
media_type=media_type,
|
||||
headers={
|
||||
"Cache-Control": "private, no-store",
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
|
||||
Reference in New Issue
Block a user