makerworld.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. """MakerWorld integration routes.
  2. User pastes a MakerWorld URL → Bambuddy resolves it → shows plate list →
  3. one-click import/print. The URL-paste flow covers the actual discovery
  4. pattern (Reddit/YouTube/shared links) without needing to replicate
  5. MakerWorld's whole search UI.
  6. Search/browse endpoints are intentionally NOT exposed: the public-facing
  7. ``design/search`` endpoint returns empty results from server-originated
  8. requests (see memory/makerworld-integration.md for the investigation).
  9. """
  10. from __future__ import annotations
  11. import logging
  12. import os
  13. from fastapi import APIRouter, Depends, HTTPException, Query
  14. from fastapi.responses import Response
  15. from sqlalchemy import select
  16. from sqlalchemy.ext.asyncio import AsyncSession
  17. from backend.app.api.routes.cloud import get_stored_token
  18. from backend.app.api.routes.library import save_3mf_bytes_to_library
  19. from backend.app.core.auth import RequirePermissionIfAuthEnabled
  20. from backend.app.core.database import get_db
  21. from backend.app.core.permissions import Permission
  22. from backend.app.models.library import LibraryFile, LibraryFolder
  23. from backend.app.models.user import User
  24. from backend.app.schemas.makerworld import (
  25. MakerWorldImportRequest,
  26. MakerWorldImportResponse,
  27. MakerWorldRecentImport,
  28. MakerWorldResolvedModel,
  29. MakerWorldResolveRequest,
  30. MakerWorldStatus,
  31. )
  32. from backend.app.services.makerworld import (
  33. MakerWorldAuthError,
  34. MakerWorldError,
  35. MakerWorldForbiddenError,
  36. MakerWorldNotFoundError,
  37. MakerWorldService,
  38. MakerWorldUnavailableError,
  39. MakerWorldUrlError,
  40. )
  41. logger = logging.getLogger(__name__)
  42. router = APIRouter(prefix="/makerworld", tags=["makerworld"])
  43. _SOURCE_TYPE = "makerworld"
  44. async def _build_service(db: AsyncSession, user: User | None) -> MakerWorldService:
  45. """Construct a per-request MakerWorldService seeded with the caller's
  46. stored Bambu Cloud bearer token when available.
  47. Mirrors ``cloud.build_authenticated_cloud`` — the token is entirely
  48. optional; anonymous calls (metadata, URL resolution) still work.
  49. """
  50. token, _email, _region = await get_stored_token(db, user)
  51. return MakerWorldService(auth_token=token)
  52. def _canonical_url(model_id: int, profile_id: int | None = None) -> str:
  53. """Build a stable source_url we use for dedupe.
  54. Dedupe is keyed per *plate* (profile) rather than per model, since the
  55. ``/iot-service/.../profile/{profileId}`` download returns a specific
  56. plate — not the full multi-plate zip — so two different plates of the
  57. same design should become two separate library entries. Canonical
  58. shape uses the locale-free path with the ``#profileId-`` fragment so
  59. all URL variants of the same plate still collapse (e.g. ``/en/models/
  60. 123-slug?from=search#profileId-456`` and ``/de/models/123#profileId-
  61. 456`` both map to ``https://makerworld.com/models/123#profileId-
  62. 456``). Plate-less imports (legacy or whole-design) keep the old
  63. model-only shape for backwards compatibility with existing rows.
  64. """
  65. if profile_id:
  66. return f"https://makerworld.com/models/{model_id}#profileId-{profile_id}"
  67. return f"https://makerworld.com/models/{model_id}"
  68. def _map_service_error(exc: MakerWorldError) -> HTTPException:
  69. """Translate service exceptions into HTTP responses."""
  70. if isinstance(exc, MakerWorldUrlError):
  71. return HTTPException(status_code=400, detail=str(exc))
  72. if isinstance(exc, MakerWorldAuthError):
  73. return HTTPException(status_code=401, detail=str(exc))
  74. if isinstance(exc, MakerWorldForbiddenError):
  75. # 403 forwards MakerWorld's own refusal message (content-gated,
  76. # region-locked, requires points, etc.) — UI surfaces it verbatim.
  77. return HTTPException(status_code=403, detail=str(exc))
  78. if isinstance(exc, MakerWorldNotFoundError):
  79. return HTTPException(status_code=404, detail=str(exc))
  80. if isinstance(exc, MakerWorldUnavailableError):
  81. return HTTPException(status_code=502, detail=str(exc))
  82. return HTTPException(status_code=500, detail=f"MakerWorld error: {exc}")
  83. @router.get("/thumbnail")
  84. async def proxy_thumbnail(
  85. url: str = Query(..., description="MakerWorld CDN image URL (makerworld.bblmw.com or public-cdn.bblmw.com)"),
  86. ):
  87. """Proxy a MakerWorld CDN thumbnail.
  88. The SPA's ``img-src`` CSP only allows ``'self' data: blob:`` — hotlinking
  89. from makerworld.bblmw.com is blocked. This endpoint refetches the image
  90. server-side and returns it with a long cache window.
  91. **Unauthenticated on purpose**: ``<img>`` tags can't send Authorization
  92. headers, so requiring a Bearer token here would break the whole feature
  93. (browsers would get 401 on every image, rendering as broken-image
  94. placeholders). The thumbnails being proxied are MakerWorld's *public*
  95. CDN — any visitor to makerworld.com can fetch them without auth — so no
  96. data is exposed. The SSRF guard inside ``fetch_thumbnail`` restricts
  97. the upstream host to the MakerWorld CDN allowlist, so this can't be
  98. abused as a generic open proxy.
  99. URLs are content-addressable (filename contains a hash), so the
  100. aggressive ``immutable`` cache-control is safe.
  101. """
  102. service = MakerWorldService()
  103. try:
  104. payload, content_type = await service.fetch_thumbnail(url)
  105. except MakerWorldError as exc:
  106. raise _map_service_error(exc) from exc
  107. finally:
  108. await service.close()
  109. return Response(
  110. content=payload,
  111. media_type=content_type,
  112. headers={
  113. "Cache-Control": "public, max-age=86400, immutable",
  114. },
  115. )
  116. @router.get("/status", response_model=MakerWorldStatus)
  117. async def get_status(
  118. db: AsyncSession = Depends(get_db),
  119. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.MAKERWORLD_VIEW),
  120. ):
  121. """Report whether the caller can import 3MFs (needs a Bambu Cloud token)."""
  122. token, _email, _region = await get_stored_token(db, current_user)
  123. has_token = bool(token)
  124. return MakerWorldStatus(has_cloud_token=has_token, can_download=has_token)
  125. @router.post("/resolve", response_model=MakerWorldResolvedModel)
  126. async def resolve_url(
  127. body: MakerWorldResolveRequest,
  128. db: AsyncSession = Depends(get_db),
  129. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.MAKERWORLD_VIEW),
  130. ):
  131. """Resolve a MakerWorld URL to full model metadata + plate list.
  132. The response also tells the caller which (if any) LibraryFile rows already
  133. exist for the same model URL, so the UI can show an "Already imported"
  134. badge and skip a redundant download.
  135. """
  136. try:
  137. model_id, profile_id = MakerWorldService.parse_url(body.url)
  138. except MakerWorldError as exc:
  139. raise _map_service_error(exc) from exc
  140. service = await _build_service(db, current_user)
  141. try:
  142. design = await service.get_design(model_id)
  143. instances_envelope = await service.get_design_instances(model_id)
  144. except MakerWorldError as exc:
  145. raise _map_service_error(exc) from exc
  146. finally:
  147. await service.close()
  148. # MakerWorld's instances payload is ``{"total": N, "hits": [...]}``; callers
  149. # only care about the hits, and we normalise the null case to an empty list
  150. # so the frontend doesn't have to handle null vs [] both ways.
  151. instances = instances_envelope.get("hits") or []
  152. if not isinstance(instances, list):
  153. instances = []
  154. # Find every library row whose source_url is either the model-level
  155. # canonical URL (legacy whole-model imports) or any plate-level URL
  156. # (``...#profileId-{n}``) under this model. The frontend surfaces this
  157. # to mark imported plates in the instance picker.
  158. model_prefix = _canonical_url(model_id)
  159. existing_q = await db.execute(
  160. select(LibraryFile.id).where(
  161. (LibraryFile.source_url == model_prefix) | (LibraryFile.source_url.like(f"{model_prefix}#profileId-%"))
  162. )
  163. )
  164. already_imported = [row[0] for row in existing_q.all()]
  165. return MakerWorldResolvedModel(
  166. model_id=model_id,
  167. profile_id=profile_id,
  168. design=design,
  169. instances=instances,
  170. already_imported_library_ids=already_imported,
  171. )
  172. @router.post("/import", response_model=MakerWorldImportResponse)
  173. async def import_instance(
  174. body: MakerWorldImportRequest,
  175. db: AsyncSession = Depends(get_db),
  176. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.MAKERWORLD_IMPORT),
  177. ):
  178. """Download a specific MakerWorld instance (plate configuration) and save
  179. the 3MF into the library.
  180. De-duplicates by canonicalised source URL — if the same MakerWorld model
  181. was imported before (any plate), that existing LibraryFile is returned and
  182. no new download happens.
  183. """
  184. if body.folder_id is not None:
  185. folder_q = await db.execute(select(LibraryFolder).where(LibraryFolder.id == body.folder_id))
  186. target_folder = folder_q.scalar_one_or_none()
  187. if target_folder is None:
  188. raise HTTPException(status_code=404, detail="Folder not found")
  189. if target_folder.is_external and target_folder.external_readonly:
  190. raise HTTPException(
  191. status_code=403,
  192. detail="Cannot import into a read-only external folder",
  193. )
  194. effective_folder_id: int | None = body.folder_id
  195. else:
  196. # Default destination: a dedicated top-level "MakerWorld" folder. Keeps
  197. # imports out of the library root so power users can still organise
  198. # manually in subfolders, and auto-creates the folder on the first
  199. # import so users don't have to set it up themselves.
  200. mw_folder_q = await db.execute(
  201. select(LibraryFolder).where(
  202. LibraryFolder.name == "MakerWorld",
  203. LibraryFolder.parent_id.is_(None),
  204. LibraryFolder.is_external.is_(False),
  205. )
  206. )
  207. mw_folder = mw_folder_q.scalar_one_or_none()
  208. if mw_folder is None:
  209. mw_folder = LibraryFolder(name="MakerWorld", parent_id=None)
  210. db.add(mw_folder)
  211. await db.flush()
  212. effective_folder_id = mw_folder.id
  213. service = await _build_service(db, current_user)
  214. # YASTL#51's iot-service endpoint needs the *alphanumeric* modelId
  215. # (e.g. "US2bb73b106683e5"), not the integer design id from /models/{N}.
  216. # Fetch design metadata to resolve it, and — in the same call — pick a
  217. # default profileId from the response if the frontend didn't specify one.
  218. try:
  219. design = await service.get_design(body.model_id)
  220. except MakerWorldError as exc:
  221. await service.close()
  222. raise _map_service_error(exc) from exc
  223. alphanumeric_model_id = design.get("modelId")
  224. if not isinstance(alphanumeric_model_id, str) or not alphanumeric_model_id:
  225. await service.close()
  226. raise HTTPException(
  227. status_code=502,
  228. detail="MakerWorld design metadata missing the modelId field",
  229. )
  230. profile_id = body.profile_id
  231. if profile_id is None:
  232. for instance in design.get("instances") or []:
  233. pid = instance.get("profileId")
  234. if isinstance(pid, int) and pid > 0:
  235. profile_id = pid
  236. break
  237. if profile_id is None:
  238. try:
  239. envelope = await service.get_design_instances(body.model_id)
  240. except MakerWorldError as exc:
  241. await service.close()
  242. raise _map_service_error(exc) from exc
  243. for hit in envelope.get("hits") or []:
  244. pid = hit.get("profileId")
  245. if isinstance(pid, int) and pid > 0:
  246. profile_id = pid
  247. break
  248. if profile_id is None:
  249. await service.close()
  250. raise HTTPException(
  251. status_code=502,
  252. detail="MakerWorld returned no instances for this model",
  253. )
  254. # Canonical URL includes profile_id so each plate gets its own library
  255. # entry (see ``_canonical_url`` docstring).
  256. source_url = _canonical_url(body.model_id, profile_id)
  257. try:
  258. manifest = await service.get_profile_download(profile_id, alphanumeric_model_id)
  259. except MakerWorldError as exc:
  260. await service.close()
  261. raise _map_service_error(exc) from exc
  262. signed_url = manifest.get("url")
  263. # Basename-strip any path components from the upstream filename so a
  264. # malicious response (``name: "../../evil.3mf"``) can't persist a suspect
  265. # string into the library row or the UI. On-disk storage uses a UUID
  266. # filename regardless (see library.py), so this is defence-in-depth.
  267. raw_name = manifest.get("name")
  268. if isinstance(raw_name, str) and raw_name.strip():
  269. suggested_name = os.path.basename(raw_name.strip()) or f"makerworld-{body.model_id}.3mf"
  270. else:
  271. suggested_name = f"makerworld-{body.model_id}.3mf"
  272. if not signed_url or not isinstance(signed_url, str):
  273. await service.close()
  274. raise HTTPException(status_code=502, detail="MakerWorld did not return a download URL")
  275. # Dedupe check upfront so we don't burn bandwidth re-downloading.
  276. if source_url:
  277. existing_q = await db.execute(select(LibraryFile).where(LibraryFile.source_url == source_url).limit(1))
  278. existing_row = existing_q.scalar_one_or_none()
  279. if existing_row is not None:
  280. await service.close()
  281. return MakerWorldImportResponse(
  282. library_file_id=existing_row.id,
  283. filename=existing_row.filename,
  284. folder_id=existing_row.folder_id,
  285. profile_id=profile_id,
  286. was_existing=True,
  287. )
  288. try:
  289. file_bytes, download_filename = await service.download_3mf(signed_url)
  290. except MakerWorldError as exc:
  291. await service.close()
  292. raise _map_service_error(exc) from exc
  293. finally:
  294. await service.close()
  295. # Prefer the server-provided human-readable filename; the signed URL's
  296. # path ends in a UUID that's not meaningful to users.
  297. filename = suggested_name if suggested_name.endswith(".3mf") else download_filename
  298. library_file, was_existing = await save_3mf_bytes_to_library(
  299. db,
  300. file_bytes=file_bytes,
  301. filename=filename,
  302. folder_id=effective_folder_id,
  303. source_type=_SOURCE_TYPE,
  304. source_url=source_url,
  305. owner_id=current_user.id if current_user else None,
  306. )
  307. return MakerWorldImportResponse(
  308. library_file_id=library_file.id,
  309. filename=library_file.filename,
  310. folder_id=library_file.folder_id,
  311. profile_id=profile_id,
  312. was_existing=was_existing,
  313. )
  314. @router.get("/recent-imports", response_model=list[MakerWorldRecentImport])
  315. async def recent_imports(
  316. limit: int = 10,
  317. db: AsyncSession = Depends(get_db),
  318. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.MAKERWORLD_VIEW),
  319. ):
  320. """Last N MakerWorld imports, newest first.
  321. Surfaces files whose ``source_type`` is ``"makerworld"`` so the MakerWorld
  322. page can show a 'Recent imports' sidebar that persists across resolves.
  323. ``limit`` is clamped to ``[1, 50]`` to keep payloads sensible.
  324. """
  325. _ = current_user # permission gate only
  326. capped = max(1, min(50, int(limit)))
  327. result = await db.execute(
  328. select(LibraryFile)
  329. .where(LibraryFile.source_type == _SOURCE_TYPE)
  330. .order_by(LibraryFile.created_at.desc())
  331. .limit(capped)
  332. )
  333. rows = result.scalars().all()
  334. return [
  335. MakerWorldRecentImport(
  336. library_file_id=row.id,
  337. filename=row.filename,
  338. folder_id=row.folder_id,
  339. thumbnail_path=row.thumbnail_path,
  340. source_url=row.source_url,
  341. created_at=row.created_at.isoformat() if row.created_at else "",
  342. )
  343. for row in rows
  344. ]