fix: add comprehensive debugging and validation for avatar uploads
- Add detailed error extraction from API responses - Validate file content is not empty before saving - Verify file was actually saved to disk - Check saved file size matches expectations - Add extensive logging for debugging upload issues - Improve error messages with specific details Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
@@ -116,9 +116,37 @@ async def upload_avatar(
|
||||
try:
|
||||
contents = await file.read()
|
||||
print(f"File size: {len(contents)} bytes")
|
||||
print(f"File content preview: {contents[:50]}...") # First 50 bytes for debugging
|
||||
|
||||
# Validate that we actually got content
|
||||
if len(contents) == 0:
|
||||
print("Empty file content received")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail="Empty file content received"
|
||||
)
|
||||
|
||||
with open(file_path, "wb") as buffer:
|
||||
buffer.write(contents)
|
||||
print("File saved successfully")
|
||||
|
||||
# Verify file was saved
|
||||
if not os.path.exists(file_path):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="Failed to verify saved file"
|
||||
)
|
||||
|
||||
file_size = os.path.getsize(file_path)
|
||||
print(f"Saved file size: {file_size} bytes")
|
||||
|
||||
if file_size == 0:
|
||||
os.remove(file_path)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail="Saved file is empty"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Failed to save file: {str(e)}")
|
||||
raise HTTPException(
|
||||
|
||||
Reference in New Issue
Block a user