makerworld.py 18 KB

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