bfd58e3419
Bug A: Media Library thumbnails were gray because <img src> cannot send JWT auth headers. Added useAuthBlob() hook (fetch + createObjectURL) in MediaBrowser.tsx. Also fixed publish_asset Celery task to populate product_id + cad_file_id on MediaAsset for thumbnail fallback resolution. Bug B: Product dimensions now shown in Product Details card with Ruler icon and "from CAD" label when cad_mesh_attributes.dimensions_mm exists. Bug C: Replaced 128×128 CAD thumbnail with InlineCadViewer component. Queries gltf_geometry MediaAssets, fetches GLB via auth fetch → blob URL → Three.js Canvas with OrbitControls. Falls back to thumbnail + "Load 3D Model" button. Polling when GLB generation is in progress. Bug D: trimesh was in [cad] optional extra but Dockerfile only installed [dev]. Changed to pip install -e ".[dev,cad]" — trimesh now available in backend container, GLB + Colors export works. Also added bbox extraction (STL-first numpy parsing) in render_step_thumbnail and admin "Re-extract CAD Metadata" bulk endpoint. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
89 lines
2.9 KiB
Python
89 lines
2.9 KiB
Python
"""MediaAsset service."""
|
|
import uuid
|
|
from sqlalchemy import select, update as sql_update
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from app.domains.media.models import MediaAsset, MediaAssetType
|
|
|
|
|
|
_SORT_COLUMNS = {
|
|
"created_at": MediaAsset.created_at,
|
|
"file_size_bytes": MediaAsset.file_size_bytes,
|
|
"storage_key": MediaAsset.storage_key,
|
|
}
|
|
|
|
|
|
async def list_media_assets(
|
|
db: AsyncSession,
|
|
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,
|
|
asset_types: list[MediaAssetType] | None = None,
|
|
is_archived: bool | None = False,
|
|
skip: int = 0,
|
|
limit: int = 50,
|
|
sort_by: str = "created_at",
|
|
sort_dir: str = "desc",
|
|
) -> list[MediaAsset]:
|
|
from sqlalchemy import asc, desc
|
|
col = _SORT_COLUMNS.get(sort_by, MediaAsset.created_at)
|
|
order = desc(col) if sort_dir == "desc" else asc(col)
|
|
q = select(MediaAsset).order_by(order)
|
|
if product_id:
|
|
q = q.where(MediaAsset.product_id == product_id)
|
|
if order_line_id:
|
|
q = q.where(MediaAsset.order_line_id == order_line_id)
|
|
if cad_file_id:
|
|
q = q.where(MediaAsset.cad_file_id == cad_file_id)
|
|
if asset_types:
|
|
q = q.where(MediaAsset.asset_type.in_(asset_types))
|
|
elif asset_type is not None:
|
|
q = q.where(MediaAsset.asset_type == asset_type)
|
|
if is_archived is not None:
|
|
q = q.where(MediaAsset.is_archived == is_archived)
|
|
q = q.offset(skip).limit(limit)
|
|
result = await db.execute(q)
|
|
return list(result.scalars().all())
|
|
|
|
|
|
async def get_media_asset(db: AsyncSession, asset_id: uuid.UUID) -> MediaAsset | None:
|
|
result = await db.execute(select(MediaAsset).where(MediaAsset.id == asset_id))
|
|
return result.scalar_one_or_none()
|
|
|
|
|
|
async def create_media_asset(db: AsyncSession, **kwargs) -> MediaAsset:
|
|
asset = MediaAsset(**kwargs)
|
|
db.add(asset)
|
|
await db.commit()
|
|
await db.refresh(asset)
|
|
return asset
|
|
|
|
|
|
async def archive_media_asset(db: AsyncSession, asset_id: uuid.UUID) -> MediaAsset | None:
|
|
await db.execute(
|
|
sql_update(MediaAsset).where(MediaAsset.id == asset_id).values(is_archived=True)
|
|
)
|
|
await db.commit()
|
|
return await get_media_asset(db, asset_id)
|
|
|
|
|
|
async def delete_media_asset(db: AsyncSession, asset_id: uuid.UUID) -> bool:
|
|
asset = await get_media_asset(db, asset_id)
|
|
if not asset:
|
|
return False
|
|
await db.delete(asset)
|
|
await db.commit()
|
|
return True
|
|
|
|
|
|
def get_download_url(asset: MediaAsset) -> str | None:
|
|
"""Return a backend proxy URL so the browser can always download the file."""
|
|
return f"/api/media/{asset.id}/download"
|
|
|
|
|
|
def get_thumbnail_url(asset: MediaAsset) -> str | None:
|
|
"""Return CAD thumbnail URL if asset has a cad_file_id."""
|
|
if asset.cad_file_id:
|
|
return f"/api/cad/{asset.cad_file_id}/thumbnail"
|
|
return None
|