refac: robust file upload failed handling

This commit is contained in:
Timothy Jaeryang Baek 2025-09-24 12:17:01 -05:00
parent 05732de898
commit 23a51f2d01
2 changed files with 208 additions and 180 deletions

View file

@ -130,6 +130,17 @@ class FilesTable:
except Exception:
return None
def get_file_by_id_and_user_id(self, id: str, user_id: str) -> Optional[FileModel]:
with get_db() as db:
try:
file = db.query(File).filter_by(id=id, user_id=user_id).first()
if file:
return FileModel.model_validate(file)
else:
return None
except Exception:
return None
def get_file_metadata_by_id(self, id: str) -> Optional[FileMetadataResponse]:
with get_db() as db:
try:

View file

@ -1407,8 +1407,13 @@ def process_file(
form_data: ProcessFileForm,
user=Depends(get_verified_user),
):
try:
if user.role == "admin":
file = Files.get_file_by_id(form_data.file_id)
else:
file = Files.get_file_by_id_and_user_id(form_data.file_id, user.id)
if file:
try:
collection_name = form_data.collection_name
@ -1421,7 +1426,9 @@ def process_file(
try:
# /files/{file_id}/data/content/update
VECTOR_DB_CLIENT.delete_collection(collection_name=f"file-{file.id}")
VECTOR_DB_CLIENT.delete_collection(
collection_name=f"file-{file.id}"
)
except:
# Audio file upload pipeline
pass
@ -1602,6 +1609,11 @@ def process_file(
except Exception as e:
log.exception(e)
Files.update_file_data_by_id(
file.id,
{"status": "failed"},
)
if "No pandoc was found" in str(e):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
@ -1613,6 +1625,11 @@ def process_file(
detail=str(e),
)
else:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
)
class ProcessTextForm(BaseModel):
name: str