74 lines
2.0 KiB
Python
74 lines
2.0 KiB
Python
import os
|
|
import tempfile
|
|
import uuid
|
|
import subprocess
|
|
|
|
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,
|
|
}
|
|
@app.post("/upload/{file_id}/convert")
|
|
async def convert_obj(file_id: str):
|
|
inpath = UPLOAD_DIR / Path(file_id)
|
|
|
|
#check for target
|
|
|
|
outpath = UPLOAD_DIR / Path(file_id).with_suffix(".obj")
|
|
result = subprocess.run(["freecadcmd", "step_to_obj_headless.py", inpath, outpath])
|
|
|
|
return {
|
|
"file_id": file_id,
|
|
"inpath": inpath,
|
|
"outpath": outpath,
|
|
"status": result.__str__()
|
|
} |