Spaces:
Runtime error
Runtime error
File size: 2,241 Bytes
644f326 | 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 57 58 | import os
import shutil
from typing import List
from fastapi import Request, UploadFile, File, HTTPException
from src.core.logger import logger
from src.constants import INGESTION_FOLDER_NAME, PUBLIC_DIRECTOR_NAME
PUBLIC_TEMP_DIR = os.path.join(PUBLIC_DIRECTOR_NAME, INGESTION_FOLDER_NAME)
os.makedirs(PUBLIC_TEMP_DIR, exist_ok=True)
async def multer_middleware(
request: Request,
file: UploadFile = File(default=None),
):
saved_paths: List[str] = []
with logger.span("📂 Upload middleware processing"):
try:
if not file:
logger.debug("⚠️ No file uploaded in request")
yield []
return
thread_id = getattr(request.state, "thread_id", "default")
dest_dir = os.path.join(PUBLIC_TEMP_DIR, str(thread_id))
os.makedirs(dest_dir, exist_ok=True)
file_path = os.path.join(dest_dir, file.filename)
logger.info("💾 Storing uploaded file", file_name=file.filename, destination=file_path)
with open(file_path, "wb") as f:
shutil.copyfileobj(file.file, f)
saved_paths.append(file_path)
logger.info("✅ Uploaded file stored successfully", file_path=file_path)
yield saved_paths
except HTTPException:
raise
except Exception as e:
logger.error("❌ Failed to store uploaded file", error=str(e))
raise HTTPException(status_code=500, detail=str(e))
finally:
for file_path in saved_paths:
try:
if os.path.exists(file_path):
os.remove(file_path)
logger.info("🧹 Cleaned up temporary upload file", file_path=file_path)
except Exception as clean_err:
logger.error("❌ Failed to clean up temporary file", file_path=file_path, error=str(clean_err))
if saved_paths:
dest_dir = os.path.dirname(saved_paths[0])
if os.path.exists(dest_dir) and not os.listdir(dest_dir):
os.rmdir(dest_dir)
logger.debug("🧹 Cleaned up empty upload directory", directory=dest_dir)
|