59 lines
1.5 KiB
Python
59 lines
1.5 KiB
Python
import os
|
|
import tempfile
|
|
import uuid
|
|
|
|
from fastapi import FastAPI, UploadFile, File, HTTPException
|
|
from fastapi.staticfiles import StaticFiles
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
app = FastAPI()
|
|
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
|
# Für lokales Testen mit separatem Frontend (z.B. file:// oder anderer Port)
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"], # in Produktion einschränken!
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
UPLOAD_DIR = Path("uploads")
|
|
UPLOAD_DIR.mkdir(exist_ok=True)
|
|
|
|
|
|
@app.get("/")
|
|
def read_root():
|
|
return {"Hello": "World"}
|
|
|
|
@app.get("/health")
|
|
def health():
|
|
return {"status": "ok"}
|
|
|
|
@app.post("/upload")
|
|
async def upload_file(file: UploadFile = File(...)):
|
|
filename = (file.filename or "").lower()
|
|
safe_name = ""
|
|
if not (filename.endswith(".step") or filename.endswith(".stp")):
|
|
raise HTTPException(status_code=400, detail="Upload a .step or .stp file")
|
|
|
|
# eindeutiger Dateiname, um Kollisionen zu vermeiden
|
|
suffix = Path(file.filename).suffix
|
|
safe_name = f"{uuid.uuid4().hex}{suffix}"
|
|
target = UPLOAD_DIR / safe_name
|
|
|
|
# Datei speichern (streaming)
|
|
with target.open("wb") as f:
|
|
while chunk := await file.read(1024 * 1024):
|
|
f.write(chunk)
|
|
|
|
return {
|
|
"original_filename": file.filename,
|
|
"stored_as": safe_name,
|
|
"content_type": file.content_type,
|
|
"size_bytes": target.stat().st_size,
|
|
}
|