tradingprintable/auction-labels-api
0
1import os
2import tempfile
3from fastapi import FastAPI, UploadFile, File, HTTPException
4from fastapi.responses import FileResponse
5
6from auction_label_exact import AuctionLabelGenerator, LayoutSpec, load_fonts
7
8app = FastAPI(title="Auction Labels API", version="1.0.0")
9
10
11@app.get("/health")
12def health():
13 return {"status": "ok"}
14
15
16@app.post("/generate")
17async def generate(file: UploadFile = File(...)):
18 filename = (file.filename or "").lower()
19 if not (filename.endswith(".xlsx") or filename.endswith(".xls")):
20 raise HTTPException(status_code=400, detail="Please upload an Excel file (.xlsx or .xls).")
21
22 content = await file.read()
23 if not content:
24 raise HTTPException(status_code=400, detail="Uploaded file is empty.")
25
26 # IMPORTANT: don't use TemporaryDirectory() because it gets deleted before FileResponse serves the file.
27 tmpdir = tempfile.mkdtemp(prefix="auction-labels-")
28 input_path = os.path.join(tmpdir, file.filename or "input.xlsx")
29 output_path = os.path.join(tmpdir, "auction_labels.pdf")
30
31 with open(input_path, "wb") as f:
32 f.write(content)
33
34 fonts = load_fonts()
35 gen = AuctionLabelGenerator(layout=LayoutSpec(), fonts=fonts)
36 gen.validate_required_fonts()
37 gen.generate(input_path, output_path)
38
39 return FileResponse(
40 output_path,
41 media_type="application/pdf",
42 filename="auction_labels.pdf",
43 )
44 