moved three.js viewer to static folder

modified upload.html and main.py accordingly
This commit is contained in:
2026-02-27 22:46:20 +01:00
parent 93cb11d7bb
commit 22268781cb
4 changed files with 50 additions and 6 deletions

View File

@@ -17,3 +17,6 @@ mit folgender URL im Browser ist der Einstiegpunkt:
http://127.0.0.1:8001/static/upload.html http://127.0.0.1:8001/static/upload.html
Ein Viewer für die hochgeladenen STEP files
http://127.0.0.1:8001/static/viewer.html

51
main.py
View File

@@ -1,17 +1,58 @@
from fastapi import FastAPI import os
import tempfile
import uuid
from fastapi import FastAPI, UploadFile, File, HTTPException
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from pathlib import Path
app = FastAPI() app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static") 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("/") @app.get("/")
def read_root(): def read_root():
return {"Hello": "World"} return {"Hello": "World"}
@app.get("/health")
def health():
return {"status": "ok"}
@app.get("/items/{item_id}") @app.post("/upload")
def read_item(item_id: int, q: str | None = None): async def upload_file(file: UploadFile = File(...)):
return {"item_id": item_id, "q": q} 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,
}

View File

@@ -22,7 +22,7 @@
</div> </div>
<script> <script>
const API_URL = "http://127.0.0.1:8000/upload"; const API_URL = "http://127.0.0.1:8001/upload";
const fileInput = document.getElementById("fileInput"); const fileInput = document.getElementById("fileInput");
const uploadBtn = document.getElementById("uploadBtn"); const uploadBtn = document.getElementById("uploadBtn");