1- import uuid
1+ import logging
22from typing import Any
3+ import uuid
34
45import boto3
56from fastapi import APIRouter , HTTPException
67from fastapi .responses import RedirectResponse
78from sqlmodel import func , select
89
9- from app .api .deps import AwsDep , CurrentUser , SessionDep
10- from app .core .config import Settings
10+ from app .api .deps import CurrentUser , SessionDep , s3_client
11+ from app .core .config import settings
1112from app .models .attachments import (
1213 Attachment ,
1314 AttachmentCreate ,
1819)
1920from app .models import Message
2021
22+ logger = logging .getLogger (__name__ )
23+
2124router = APIRouter (prefix = "/attachments" , tags = ["attachments" ])
2225
2326
@@ -36,7 +39,7 @@ def read_attachments(
3639 return AttachmentsPublic (data = attachments , count = count )
3740
3841@router .get ("/{id}/content" )
39- def read_attachment_content (session : SessionDep , aws_client : AwsDep , current_user : CurrentUser , id : uuid .UUID ) -> Any :
42+ def read_attachment_content (session : SessionDep , current_user : CurrentUser , id : uuid .UUID ) -> Any :
4043 """
4144 Get attachment content by ID.
4245 """
@@ -45,16 +48,17 @@ def read_attachment_content(session: SessionDep, aws_client: AwsDep, current_use
4548 raise HTTPException (status_code = 404 , detail = "Attachment not found" )
4649
4750 try :
48- presigned_url = aws_client .generate_presigned_url (
51+ presigned_url = s3_client .generate_presigned_url (
4952 "get_object" ,
5053 Params = {
51- "Bucket" : Settings .AWS_S3_ATTACHMENTS_BUCKET ,
54+ "Bucket" : settings .AWS_S3_ATTACHMENTS_BUCKET ,
5255 "Key" : attachment .storage_path ,
5356 "ContentDisposition" : f"attachment; filename={ attachment .file_name } " ,
5457 },
5558 ExpiresIn = 3600 ,
5659 )
5760 except Exception as e :
61+ logger .exception (e )
5862 raise HTTPException (status_code = 500 , detail = "Could not generate presigned URL" )
5963
6064 return RedirectResponse (status_code = 302 , url = presigned_url )
@@ -73,7 +77,7 @@ def read_attachment(session: SessionDep, current_user: CurrentUser, id: uuid.UUI
7377
7478@router .post ("/" , response_model = AttachmentCreatePublic )
7579def create_attachment (
76- * , session : SessionDep , aws_client : AwsDep , current_user : CurrentUser , attachment_in : AttachmentCreate
80+ * , session : SessionDep , current_user : CurrentUser , attachment_in : AttachmentCreate
7781) -> Any :
7882 """
7983 Create a new attachment.
@@ -84,19 +88,21 @@ def create_attachment(
8488 session .refresh (attachment )
8589
8690 try :
87- presigned_upload_url = aws_client .generate_presigned_url (
91+ presigned_upload_url = s3_client .generate_presigned_url (
8892 "put_object" ,
8993 Params = {
90- "Bucket" : Settings .AWS_S3_ATTACHMENTS_BUCKET ,
94+ "Bucket" : settings .AWS_S3_ATTACHMENTS_BUCKET ,
9195 "Key" : attachment .storage_path ,
9296 "ContentDisposition" : f"attachment; filename={ attachment .file_name } " ,
9397 },
9498 ExpiresIn = 3600 ,
9599 )
96100 except Exception as e :
101+ logger .exception (e )
97102 raise HTTPException (status_code = 500 , detail = "Could not generate presigned URL" )
98103
99- resmodel = AttachmentCreatePublic .model_validate (attachment )
100- resmodel .upload_url = presigned_upload_url
104+ resmodel = AttachmentCreatePublic .model_validate (
105+ attachment , update = {"upload_url" : presigned_upload_url }
106+ )
101107
102108 return resmodel
0 commit comments