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:
parent
75520729de
commit
9bf72ab444
2 changed files with 28 additions and 18 deletions
|
|
@ -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))
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue