fix: improve file name generation in upload_file function to prevent files with the same name (#3550)

* fix: improve file name generation in upload_file function to prevent files with the same name

* [autofix.ci] apply automated fixes

* fix: improve file name generation in upload_file function

* [autofix.ci] apply automated fixes

* fix: improve file name generation in upload_file function

* fix: improve file path generation in test_upload_file function

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
anovazzi1 2024-08-26 15:35:19 -03:00 • committed by GitHub
commit 9bf72ab444
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 28 additions and 18 deletions

View file

@ -1,3 +1,4 @@
from datetime import datetime
import hashlib
from http import HTTPStatus
from io import BytesIO
@ -45,10 +46,12 @@ async def upload_file(
try:
flow_id_str = str(flow_id)
file_content = await file.read()
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
file_name = file.filename or hashlib.sha256(file_content).hexdigest()
full_file_name = f"{timestamp}_{file_name}"
folder = flow_id_str
await storage_service.save_file(flow_id=folder, file_name=file_name, data=file_content)
return UploadFileResponse(flowId=flow_id_str, file_path=f"{folder}/{file_name}")
await storage_service.save_file(flow_id=folder, file_name=full_file_name, data=file_content)
return UploadFileResponse(flowId=flow_id_str, file_path=f"{folder}/{full_file_name}")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))