Speed up replies
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Tyler Perkins 2023-11-03 19:40:38 -04:00
parent 3977c7896f
commit d2dd08b654
1 changed files with 9 additions and 14 deletions

View File

@ -1,5 +1,5 @@
from fastapi import APIRouter, Depends, HTTPException, Header, BackgroundTasks
from fastapi.responses import FileResponse
from fastapi.responses import FileResponse, Response
from typing import List, Optional
from pydantic import BaseModel
@ -44,14 +44,16 @@ async def background(background_tasks: BackgroundTasks):
random_object_name = random.choice(object_names)
print(random_object_name)
file_response = None
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
client.fget_object(bucket, random_object_name, temp_file.name)
temp_file_name = temp_file.name
try:
response = client.get_object(bucket, random_object_name)
file_response = Response(response.data, media_type="image/*")
finally:
response.close()
response.release_conn()
background_tasks.add_task(delete_temp_file, temp_file_name)
return FileResponse(temp_file_name)
return file_response
@ -59,10 +61,3 @@ async def background(background_tasks: BackgroundTasks):
traceback.print_exc()
raise HTTPException(status_code=500, detail=f"Unknown error. Check logs {err}")
print(f"Error occurued processing background {err}")
def delete_temp_file(path: str):
try:
os.remove(path)
except Exception as e:
print(f"Error deleting file {path} : {e}")