Spaces:
Runtime error
Runtime error
File size: 2,010 Bytes
99fdb5f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | import os
from fastapi.responses import FileResponse
def send_file(path: str):
if not os.path.exists(path):
return {"error": "File not found"}
return FileResponse(
path,
filename=os.path.basename(path),
media_type="video/mp4", # ✅ REQUIRED FOR PLAYBACK
headers={
"Accept-Ranges": "bytes"
}
)
# utils/response_utils.py (or wherever send_file is defined)
from fastapi.responses import FileResponse, JSONResponse
import os
import shutil
import uuid
# ... (other imports)
def send_file(file_path: str, file_size: int = None): # ✅ ADD file_size PARAMETER
"""
Sends the processed file back, and cleans up the file after transmission.
Returns a JSON response with URL and size for the frontend.
"""
if not os.path.exists(file_path):
raise HTTPException(status_code=500, detail="Output file not found after processing.")
# 1. Determine the output URL path (e.g., /output/filename.mp4)
# This logic depends on how your FastAPI serves static files.
# We use a simple path relative to the UPLOAD_DIR
filename = os.path.basename(file_path)
output_url = f"/uploads/{filename}" # Adjust this to your static file serving path
# 2. Get size if not provided
if file_size is None:
file_size = os.path.getsize(file_path)
# 3. Use FileResponse to send the file and clean it up (using background tasks)
# The actual implementation of cleaning up after FileResponse needs a BackgroundTask
# 4. Return the required JSON structure to the frontend
# Note: The frontend uses the URL and size from this JSON response.
return JSONResponse(content={
"url": output_url,
"size": file_size,
"message": "File processed successfully"
})
# Note: If your send_file is designed to return the FileResponse directly,
# you would need to adjust the frontend or use FastAPI's BackgroundTasks
# to clean up the file after it is served.
|