35 lines
1014 B
Python
35 lines
1014 B
Python
# app/routers/upload.py
|
|
|
|
from fastapi import APIRouter, UploadFile, File, HTTPException
|
|
import os
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("/upload")
|
|
async def upload_file(file: UploadFile = File(...)):
|
|
if not file.filename.endswith('.xlsx'):
|
|
raise HTTPException(status_code=400, detail="Invalid file format. Please upload an .xlsx file.")
|
|
|
|
save_path = os.path.join("uploads", file.filename)
|
|
os.makedirs(os.path.dirname(save_path), exist_ok=True)
|
|
with open(save_path, "wb") as buffer:
|
|
buffer.write(await file.read())
|
|
|
|
# Validate the file (add your validation logic here)
|
|
is_valid, summary, error = validate_file(save_path)
|
|
if not is_valid:
|
|
raise HTTPException(status_code=400, detail=error)
|
|
|
|
return summary
|
|
|
|
|
|
def validate_file(file_path: str):
|
|
# Implement your file validation logic here
|
|
# For demo purpose, assuming it always succeeds
|
|
summary = {
|
|
"dewars": 5,
|
|
"pucks": 10,
|
|
"samples": 100,
|
|
}
|
|
return True, summary, None |