refac: robust file upload failed handling
This commit is contained in:
@@ -130,6 +130,17 @@ class FilesTable:
|
|||||||
except Exception:
|
except Exception:
|
||||||
return None
|
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]:
|
def get_file_metadata_by_id(self, id: str) -> Optional[FileMetadataResponse]:
|
||||||
with get_db() as db:
|
with get_db() as db:
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -1407,8 +1407,13 @@ def process_file(
|
|||||||
form_data: ProcessFileForm,
|
form_data: ProcessFileForm,
|
||||||
user=Depends(get_verified_user),
|
user=Depends(get_verified_user),
|
||||||
):
|
):
|
||||||
try:
|
if user.role == "admin":
|
||||||
file = Files.get_file_by_id(form_data.file_id)
|
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
|
collection_name = form_data.collection_name
|
||||||
|
|
||||||
@@ -1421,7 +1426,9 @@ def process_file(
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
# /files/{file_id}/data/content/update
|
# /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:
|
except:
|
||||||
# Audio file upload pipeline
|
# Audio file upload pipeline
|
||||||
pass
|
pass
|
||||||
@@ -1602,6 +1609,11 @@ def process_file(
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.exception(e)
|
log.exception(e)
|
||||||
|
Files.update_file_data_by_id(
|
||||||
|
file.id,
|
||||||
|
{"status": "failed"},
|
||||||
|
)
|
||||||
|
|
||||||
if "No pandoc was found" in str(e):
|
if "No pandoc was found" in str(e):
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_400_BAD_REQUEST,
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
@@ -1613,6 +1625,11 @@ def process_file(
|
|||||||
detail=str(e),
|
detail=str(e),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class ProcessTextForm(BaseModel):
|
class ProcessTextForm(BaseModel):
|
||||||
name: str
|
name: str
|
||||||
|
|||||||
Reference in New Issue
Block a user