Files
HartOMat/backend/app/domains/media/router.py
T
Hartmut a70cb55d01 feat(N): workflow pipeline, 3D viewer, worker management, QC tests
- workflow_builder.py: fix broken stubs, add render_order_line_still_task
  (resolves step_path from DB instead of passing order_line_id as step_path)
- domains/rendering/tasks.py: add render_order_line_still_task,
  export_gltf_for_order_line_task, export_blend_for_order_line_task,
  generate_gltf_geometry_task (trimesh STL→GLB, no Blender needed)
- tasks/step_tasks.py: add generate_gltf_geometry_task for CadFile GLB export
- cad router: POST /{id}/generate-gltf-geometry endpoint (admin/PM)
- worker router: GET /celery-workers + POST /scale (docker compose subprocess)
- Dockerfile: pip install -e "[dev]" to enable pytest
- docker-compose.yml: docker socket + compose file mount on backend
- ThreeDViewer.tsx: mode toggle (geometry/production), wireframe, env presets,
  download buttons (GLB + .blend)
- CadPreview.tsx: load gltf_geometry/gltf_production/blend_production assets
  from MediaAsset table and pass URLs to ThreeDViewer
- ProductDetail.tsx: "View 3D" button → /cad/:id, "Generate GLB" button
- media router/service: cad_file_id filter on GET /api/media
- WorkerManagement.tsx: new page with worker status, queue depth, scale controls
- App.tsx + Layout.tsx: /workers route + sidebar link (admin/PM)
- tests: test_rendering_service.py, test_orders_service.py (backend)
- tests: WorkerActivity.test.tsx, WorkerManagement.test.tsx (frontend)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-06 22:56:53 +01:00

104 lines
3.2 KiB
Python

"""MediaAsset router — /api/media."""
import io
import uuid
import zipfile
from fastapi import APIRouter, Depends, HTTPException, Query
from fastapi.responses import StreamingResponse
from sqlalchemy.ext.asyncio import AsyncSession
from app.database import get_db
from app.domains.media.models import MediaAssetType
from app.domains.media.schemas import MediaAssetOut
from app.domains.media import service
router = APIRouter(prefix="/api/media", tags=["media"])
@router.get("/", response_model=list[MediaAssetOut])
async def list_assets(
product_id: uuid.UUID | None = None,
order_line_id: uuid.UUID | None = None,
cad_file_id: uuid.UUID | None = None,
asset_type: MediaAssetType | None = None,
skip: int = Query(0, ge=0),
limit: int = Query(50, ge=1, le=500),
db: AsyncSession = Depends(get_db),
):
assets = await service.list_media_assets(
db,
product_id=product_id,
order_line_id=order_line_id,
cad_file_id=cad_file_id,
asset_type=asset_type,
skip=skip,
limit=limit,
)
for a in assets:
a.download_url = service.get_download_url(a)
return assets
@router.get("/{asset_id}", response_model=MediaAssetOut)
async def get_asset(asset_id: uuid.UUID, db: AsyncSession = Depends(get_db)):
asset = await service.get_media_asset(db, asset_id)
if not asset:
raise HTTPException(404, "Asset not found")
asset.download_url = service.get_download_url(asset)
return asset
@router.get("/{asset_id}/download")
async def download_asset(asset_id: uuid.UUID, db: AsyncSession = Depends(get_db)):
from fastapi.responses import RedirectResponse
asset = await service.get_media_asset(db, asset_id)
if not asset:
raise HTTPException(404, "Asset not found")
url = service.get_download_url(asset)
if url:
return RedirectResponse(url)
raise HTTPException(404, "File not available")
@router.post("/zip")
async def zip_download(
asset_ids: list[uuid.UUID],
db: AsyncSession = Depends(get_db),
):
assets = []
for aid in asset_ids:
a = await service.get_media_asset(db, aid)
if a:
assets.append(a)
if not assets:
raise HTTPException(404, "No assets found")
def generate():
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf:
from app.core.storage import get_storage
storage = get_storage()
for a in assets:
ext = (a.mime_type or "").split("/")[-1] or "bin"
fname = f"{a.asset_type.value}_{a.id}.{ext}"
try:
data = storage.download_bytes(a.storage_key)
zf.writestr(fname, data)
except Exception:
pass
yield buf.getvalue()
return StreamingResponse(
generate(),
media_type="application/zip",
headers={"Content-Disposition": "attachment; filename=media-export.zip"},
)
@router.delete("/{asset_id}")
async def archive_asset(asset_id: uuid.UUID, db: AsyncSession = Depends(get_db)):
asset = await service.archive_media_asset(db, asset_id)
if not asset:
raise HTTPException(404, "Asset not found")
return {"ok": True}