makerworld.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. """MakerWorld API service.
  2. Thin async client for MakerWorld's ``/api/v1/design-service/*`` endpoints.
  3. Lets Bambuddy resolve a MakerWorld URL, enumerate plate/profile metadata, and
  4. download the 3MF bundle so users can import and print MakerWorld models
  5. without leaving the app.
  6. The endpoints and header set were reverse-engineered from the
  7. `kloshi-io/makerworld-api-reverse` TypeScript project (Apache-2.0) and
  8. cross-validated against live MakerWorld traffic. Authenticated calls reuse
  9. Bambuddy's existing Bambu Cloud bearer token (same SSO backend — no separate
  10. OAuth flow needed).
  11. Only interoperability — not affiliated with or endorsed by MakerWorld or
  12. Bambu Lab, and not intended to circumvent any access control.
  13. """
  14. from __future__ import annotations
  15. import asyncio
  16. import logging
  17. import re
  18. from typing import Any
  19. from urllib.parse import urlparse
  20. import httpx
  21. logger = logging.getLogger(__name__)
  22. # API base: ``api.bambulab.com/v1/design-service`` — the same Bambu Cloud
  23. # backend that the MakerWorld web UI talks to, but not behind Cloudflare
  24. # (the website ``makerworld.com`` is, and plain httpx requests there get
  25. # fingerprinted as bot traffic and served "Please log in"). Confirmed by
  26. # Pr0zak/YASTL#51 and verified with direct curl.
  27. MAKERWORLD_API_BASE = "https://api.bambulab.com/v1/design-service"
  28. MAKERWORLD_HOST = "makerworld.com" # Used only for URL parsing (input validation)
  29. MAKERWORLD_CDN_HOSTS = ("makerworld.bblmw.com", "public-cdn.bblmw.com")
  30. # Hosts that the iot-service download endpoint may return presigned URLs
  31. # for. Besides MakerWorld's own CDN, Bambu Cloud also issues AWS S3
  32. # presigned URLs (e.g. ``s3.us-west-2.amazonaws.com``) — confirmed by
  33. # Pr0zak/YASTL#52. The suffix check matches any regional S3 endpoint.
  34. _ALLOWED_DOWNLOAD_SUFFIXES = (".amazonaws.com",)
  35. # Browser-like headers. ``api.bambulab.com`` accepts minimal headers cleanly;
  36. # the Referer is kept so MakerWorld origin checks don't fail anywhere the
  37. # same client hits ``makerworld.com``.
  38. _CLIENT_HEADERS = {
  39. "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:147.0) Gecko/20100101 Firefox/147.0",
  40. "Accept": "text/html,application/json,*/*",
  41. "Accept-Language": "en-US,en;q=0.9",
  42. "Referer": "https://makerworld.com/",
  43. }
  44. _MODEL_ID_RE = re.compile(r"/models/(\d+)")
  45. _PROFILE_ID_RE = re.compile(r"#profileId[-=](\d+)")
  46. _MAX_3MF_BYTES = 200 * 1024 * 1024 # 200 MB hard cap
  47. _MAX_THUMBNAIL_BYTES = 10 * 1024 * 1024 # 10 MB hard cap — MakerWorld's "thumbnails" can be 2–3 MB source images
  48. _IMAGE_EXT_TO_MIME = {
  49. ".png": "image/png",
  50. ".jpg": "image/jpeg",
  51. ".jpeg": "image/jpeg",
  52. ".gif": "image/gif",
  53. ".webp": "image/webp",
  54. ".bmp": "image/bmp",
  55. }
  56. # Content types we refuse even if the URL extension looks image-y — prevents
  57. # forwarding an upstream error page or JSON blob with image framing.
  58. _REFUSED_THUMBNAIL_MIMES = ("text/html", "text/plain", "application/json")
  59. _shared_http_client: httpx.AsyncClient | None = None
  60. def set_shared_http_client(client: httpx.AsyncClient | None) -> None:
  61. """Register an app-scoped ``httpx.AsyncClient`` for service reuse.
  62. Same pattern as ``bambu_cloud.set_shared_http_client`` — lets the FastAPI
  63. lifespan share one connection pool across per-request service instances.
  64. """
  65. global _shared_http_client
  66. _shared_http_client = client
  67. class MakerWorldError(Exception):
  68. """Base exception for MakerWorld API errors."""
  69. class MakerWorldAuthError(MakerWorldError):
  70. """Raised when the endpoint requires a Bambu Cloud token and we don't have
  71. one (or the one we sent was rejected). True auth failure."""
  72. class MakerWorldForbiddenError(MakerWorldError):
  73. """Raised when MakerWorld refuses access despite valid authentication —
  74. content-gated (points required, purchase required, region restricted,
  75. early-access, etc.). The message includes MakerWorld's own reason text
  76. when provided."""
  77. class MakerWorldNotFoundError(MakerWorldError):
  78. """Raised when a design / profile / instance doesn't exist."""
  79. class MakerWorldUnavailableError(MakerWorldError):
  80. """Raised on 5xx, network errors, or malformed payloads."""
  81. class MakerWorldUrlError(MakerWorldError):
  82. """Raised when a URL isn't a makerworld.com model page."""
  83. async def _download_s3_urllib(url: str, filename_fallback: str) -> tuple[bytes, str]:
  84. """Fetch an AWS S3 presigned URL without touching the query string.
  85. ``urllib.request`` passes the URL to the transport verbatim — which is
  86. essential for S3 presigned URLs where the signature is computed over
  87. the exact query-string bytes. httpx's ``URL`` class and curl_cffi's
  88. libcurl layer both normalise encodings and produce
  89. ``SignatureDoesNotMatch`` 400s from S3.
  90. Runs the blocking urllib call in a thread executor so we don't stall
  91. the event loop.
  92. """
  93. from urllib.request import HTTPRedirectHandler, Request, build_opener
  94. # Don't follow redirects: the host allowlist above is only enforced on
  95. # the initial URL. A 302 from S3 to any other host would otherwise
  96. # transparently bypass the allowlist — so insist S3 resolve directly.
  97. class _NoRedirect(HTTPRedirectHandler):
  98. def redirect_request(self, *args, **kwargs): # type: ignore[override]
  99. return None
  100. opener = build_opener(_NoRedirect)
  101. def _blocking_fetch() -> bytes:
  102. req = Request(url, headers={"User-Agent": _CLIENT_HEADERS["User-Agent"]})
  103. with opener.open(req, timeout=60.0) as resp:
  104. if resp.status != 200:
  105. raise MakerWorldUnavailableError(f"3MF download returned HTTP {resp.status}")
  106. data = b""
  107. while True:
  108. chunk = resp.read(65536)
  109. if not chunk:
  110. break
  111. data += chunk
  112. if len(data) > _MAX_3MF_BYTES:
  113. raise MakerWorldUnavailableError(f"3MF exceeds {_MAX_3MF_BYTES // (1024 * 1024)} MB cap")
  114. return data
  115. try:
  116. data = await asyncio.to_thread(_blocking_fetch)
  117. except MakerWorldUnavailableError:
  118. raise
  119. except Exception as exc: # noqa: BLE001 — urllib throws a zoo of exceptions
  120. raise MakerWorldUnavailableError(f"S3 download failed: {exc}") from exc
  121. return data, filename_fallback
  122. def _extract_upstream_error(response: httpx.Response) -> str | None:
  123. """Pull MakerWorld's own error text out of a 4xx/5xx response body.
  124. MakerWorld returns ``{"code": N, "error": "text"}`` on auth/perm failures
  125. and sometimes ``{"message": "..."}`` on other errors. Returns ``None`` if
  126. the body isn't JSON or doesn't have a recognised error field — callers
  127. should fall back to a generic message in that case.
  128. """
  129. try:
  130. data = response.json()
  131. except ValueError:
  132. return None
  133. if not isinstance(data, dict):
  134. return None
  135. for key in ("error", "message", "detail"):
  136. value = data.get(key)
  137. if isinstance(value, str) and value.strip():
  138. return value.strip()
  139. return None
  140. class MakerWorldService:
  141. """Per-request MakerWorld API client.
  142. Mirrors ``BambuCloudService``'s construction pattern so callers can
  143. instantiate per request, reuse the shared connection pool in production,
  144. inject a client in tests, and close the client only if they own it.
  145. """
  146. def __init__(
  147. self,
  148. client: httpx.AsyncClient | None = None,
  149. auth_token: str | None = None,
  150. ):
  151. if client is not None:
  152. self._client = client
  153. self._owns_client = False
  154. elif _shared_http_client is not None:
  155. self._client = _shared_http_client
  156. self._owns_client = False
  157. else:
  158. self._client = httpx.AsyncClient(timeout=30.0)
  159. self._owns_client = True
  160. self._auth_token = auth_token
  161. async def close(self) -> None:
  162. if self._owns_client:
  163. await self._client.aclose()
  164. def _headers(self) -> dict[str, str]:
  165. headers = dict(_CLIENT_HEADERS)
  166. if self._auth_token:
  167. headers["Authorization"] = f"Bearer {self._auth_token}"
  168. return headers
  169. async def _get_json(self, path: str) -> dict[str, Any]:
  170. """GET ``{MAKERWORLD_API_BASE}{path}`` returning the decoded JSON body.
  171. Raises ``MakerWorld{Auth,Forbidden,NotFound,Unavailable}Error`` based
  172. on status. Retries once on 418 (Cloudflare bot-detection) with a
  173. short backoff — that flagging is often request-scoped and clears on
  174. a subsequent call; hammering beyond one retry provokes a stronger
  175. block, so we stop there and surface a useful error.
  176. """
  177. url = f"{MAKERWORLD_API_BASE}{path}"
  178. for attempt in range(2):
  179. try:
  180. response = await self._client.get(url, headers=self._headers(), timeout=30.0)
  181. except httpx.TimeoutException as exc:
  182. raise MakerWorldUnavailableError(f"MakerWorld request timed out: {exc}") from exc
  183. except httpx.HTTPError as exc:
  184. raise MakerWorldUnavailableError(f"MakerWorld request failed: {exc}") from exc
  185. if response.status_code == 418 and attempt == 0:
  186. logger.info("MakerWorld returned 418 for %s; retrying once after backoff", path)
  187. await asyncio.sleep(1.5)
  188. continue
  189. break
  190. # 401: genuine auth failure — token expired, malformed, not accepted.
  191. # 403: MakerWorld accepted the token but refuses the specific resource
  192. # — usually content gating (points-redeemable, purchase-required,
  193. # region-restricted, early-access). These must surface differently
  194. # because the UI remedy is completely different: 401 → re-login,
  195. # 403 → user has to go to MakerWorld and meet the access requirement.
  196. if response.status_code == 401:
  197. upstream = _extract_upstream_error(response)
  198. raise MakerWorldAuthError(upstream or f"MakerWorld rejected the Bambu Cloud token for {path}")
  199. if response.status_code == 403:
  200. upstream = _extract_upstream_error(response)
  201. raise MakerWorldForbiddenError(
  202. upstream
  203. or f"MakerWorld refused access to {path} — the model may require purchase, points redemption, or be region-restricted"
  204. )
  205. if response.status_code == 404:
  206. raise MakerWorldNotFoundError(f"MakerWorld resource not found: {path}")
  207. if response.status_code == 418:
  208. # MakerWorld's anti-abuse layer challenges the source IP with a
  209. # CAPTCHA (``{"captchaId":"...","error":"We need to confirm..."}``).
  210. # This is application-level, not Cloudflare-edge, and clears
  211. # on its own within 1–4 hours of quiet traffic. There's no
  212. # server-side solve — CAPTCHAs are intentionally unsolvable
  213. # without a real browser. Surface the upstream message so the
  214. # user can recognise it and reach for the "Open on MakerWorld"
  215. # fallback instead of thinking the feature is broken.
  216. upstream = _extract_upstream_error(response)
  217. if upstream and "robot" in upstream.lower():
  218. raise MakerWorldUnavailableError(
  219. f"MakerWorld is challenging this IP with a CAPTCHA ({upstream}). "
  220. "This usually clears within a few hours. In the meantime, use "
  221. "'Open on MakerWorld' below to download the 3MF manually."
  222. )
  223. raise MakerWorldUnavailableError(
  224. f"MakerWorld blocked the request (HTTP 418) for {path}. "
  225. "Try again in a few minutes, or use 'Open on MakerWorld' to import manually."
  226. )
  227. if response.status_code == 429:
  228. raise MakerWorldUnavailableError(
  229. f"MakerWorld rate-limited the request (HTTP 429) for {path}. Try again shortly."
  230. )
  231. if response.status_code >= 500:
  232. raise MakerWorldUnavailableError(f"MakerWorld server error (HTTP {response.status_code}) for {path}")
  233. if response.status_code != 200:
  234. raise MakerWorldUnavailableError(f"MakerWorld unexpected status {response.status_code} for {path}")
  235. try:
  236. data = response.json()
  237. except ValueError as exc:
  238. raise MakerWorldUnavailableError(f"MakerWorld returned non-JSON for {path}") from exc
  239. if not isinstance(data, dict):
  240. raise MakerWorldUnavailableError(
  241. f"MakerWorld returned unexpected JSON shape for {path}: {type(data).__name__}"
  242. )
  243. return data
  244. # ------------------------------------------------------------------ URL parse
  245. @staticmethod
  246. def parse_url(url: str) -> tuple[int, int | None]:
  247. """Extract ``(model_id, profile_id_or_None)`` from a MakerWorld URL.
  248. Accepts any of:
  249. - ``https://makerworld.com/en/models/1400373``
  250. - ``https://makerworld.com/en/models/1400373-slug-with-dashes``
  251. - ``https://makerworld.com/en/models/1400373#profileId-1452154``
  252. - ``makerworld.com/models/1400373`` (scheme optional)
  253. Rejects non-makerworld hosts.
  254. """
  255. if not url or not isinstance(url, str):
  256. raise MakerWorldUrlError("URL is empty or not a string")
  257. candidate = url.strip()
  258. if "://" not in candidate:
  259. candidate = "https://" + candidate
  260. try:
  261. parsed = urlparse(candidate)
  262. except ValueError as exc:
  263. raise MakerWorldUrlError(f"Could not parse URL: {exc}") from exc
  264. host = (parsed.hostname or "").lower()
  265. if host != MAKERWORLD_HOST and not host.endswith("." + MAKERWORLD_HOST):
  266. raise MakerWorldUrlError(f"Not a MakerWorld URL (host={host!r}); expected makerworld.com")
  267. model_match = _MODEL_ID_RE.search(parsed.path)
  268. if not model_match:
  269. raise MakerWorldUrlError("URL does not contain a /models/{id} segment")
  270. model_id = int(model_match.group(1))
  271. profile_id: int | None = None
  272. if parsed.fragment:
  273. profile_match = _PROFILE_ID_RE.search("#" + parsed.fragment)
  274. if profile_match:
  275. profile_id = int(profile_match.group(1))
  276. return model_id, profile_id
  277. # ---------------------------------------------------------------- endpoints
  278. async def get_design(self, model_id: int) -> dict[str, Any]:
  279. """Fetch full model metadata. Works anonymously.
  280. Returns the MakerWorld ``design`` object — title, summary, creator,
  281. license, tags, coverUrl, instances[] with profileId+cover per plate,
  282. categories, etc.
  283. """
  284. return await self._get_json(f"/design/{int(model_id)}")
  285. async def get_design_instances(self, model_id: int) -> dict[str, Any]:
  286. """Fetch list of profiles/instances for a model. Works anonymously.
  287. Returns ``{"total": N, "hits": [{id, profileId, title, cover,
  288. instanceCreator, instanceFilaments, needAms, ...}, ...]}``.
  289. """
  290. return await self._get_json(f"/design/{int(model_id)}/instances")
  291. async def get_profile(self, profile_id: int) -> dict[str, Any]:
  292. """Fetch a single profile's summary (designId/modelId/title/cover/
  293. instanceId). Works anonymously.
  294. """
  295. return await self._get_json(f"/profile/{int(profile_id)}")
  296. async def get_profile_download(self, profile_id: int, model_id: str) -> dict[str, Any]:
  297. """Fetch the signed 3MF download URL for a specific MakerWorld profile.
  298. Note on ``model_id`` — this is MakerWorld's internal alphanumeric
  299. identifier (e.g. ``"US2bb73b106683e5"``), **not** the integer
  300. ``designId`` that appears in the ``/models/{N}`` URL. Callers must
  301. fetch the design first (``get_design(design_id)``) and pass the
  302. ``modelId`` field from the response.
  303. Returns ``{"url": "https://makerworld.bblmw.com/...?at=<unix>
  304. &exp=<unix>&key=<hmac>&uid=<int>", ...}``. URL is short-lived (~5
  305. min); download immediately.
  306. Hits ``api.bambulab.com/v1/iot-service/api/user/profile/{profileId}
  307. ?model_id={modelId}`` with the stored Bambu Cloud bearer. This is the
  308. endpoint Pr0zak/YASTL#51 reverse-engineered — it lives on the
  309. ``api.bambulab.com`` backend (not Cloudflare-protected
  310. ``makerworld.com``), accepts the same long-lived bearer users already
  311. sign in with, and mints the signed CDN URL that the browser would
  312. otherwise fetch via session cookies. This is the only known non-
  313. cookie path to a download URL, after ruling out ``/design-service/``
  314. endpoints on ``makerworld.com`` (cookie-gated) and the now-dead
  315. ``/instance/{id}/f3mf?type=download`` shape.
  316. """
  317. if not self._auth_token:
  318. raise MakerWorldAuthError("Downloading files from MakerWorld requires a Bambu Cloud login")
  319. url = f"https://api.bambulab.com/v1/iot-service/api/user/profile/{int(profile_id)}"
  320. headers = dict(_CLIENT_HEADERS)
  321. headers["Authorization"] = f"Bearer {self._auth_token}"
  322. try:
  323. response = await self._client.get(
  324. url,
  325. headers=headers,
  326. params={"model_id": str(model_id)},
  327. timeout=30.0,
  328. )
  329. except httpx.TimeoutException as exc:
  330. raise MakerWorldUnavailableError(f"Bambu Lab API request timed out: {exc}") from exc
  331. except httpx.HTTPError as exc:
  332. raise MakerWorldUnavailableError(f"Bambu Lab API request failed: {exc}") from exc
  333. if response.status_code == 401:
  334. upstream = _extract_upstream_error(response)
  335. raise MakerWorldAuthError(
  336. upstream or "Bambu Lab rejected the token — sign in again in Settings → Bambu Cloud"
  337. )
  338. if response.status_code == 403:
  339. upstream = _extract_upstream_error(response)
  340. raise MakerWorldForbiddenError(upstream or f"Bambu Lab refused access to profile {profile_id}")
  341. if response.status_code == 404:
  342. raise MakerWorldNotFoundError(f"MakerWorld profile not found: {profile_id}")
  343. if response.status_code != 200:
  344. raise MakerWorldUnavailableError(
  345. f"Bambu Lab API unexpected status {response.status_code} for profile {profile_id}"
  346. )
  347. try:
  348. data = response.json()
  349. except ValueError as exc:
  350. raise MakerWorldUnavailableError(f"Bambu Lab API returned non-JSON for profile {profile_id}") from exc
  351. if not isinstance(data, dict):
  352. raise MakerWorldUnavailableError(f"Bambu Lab API returned unexpected JSON shape for profile {profile_id}")
  353. return data
  354. async def download_3mf(self, signed_url: str) -> tuple[bytes, str]:
  355. """Fetch the 3MF bytes from a signed MakerWorld CDN URL.
  356. Validates that the URL's host is one of the known MakerWorld CDN hosts
  357. (SSRF guard — pattern matches ``_spoolman_helpers.assert_safe_spoolman_url``).
  358. Enforces a 200 MB cap so a single bad response can't exhaust disk.
  359. Returns ``(file_bytes, suggested_filename)``.
  360. """
  361. try:
  362. parsed = urlparse(signed_url)
  363. except ValueError as exc:
  364. raise MakerWorldUrlError(f"Invalid download URL: {exc}") from exc
  365. host = (parsed.hostname or "").lower()
  366. is_allowed = host in MAKERWORLD_CDN_HOSTS or any(host.endswith(suffix) for suffix in _ALLOWED_DOWNLOAD_SUFFIXES)
  367. if not is_allowed:
  368. raise MakerWorldUrlError(f"Refusing to download from non-MakerWorld host: {host!r}")
  369. # Filename fallback from the signed path (before query string)
  370. path_tail = parsed.path.rsplit("/", 1)[-1] or "model.3mf"
  371. # Presigned S3 URLs (``s3.<region>.amazonaws.com``) compute the
  372. # signature over exact query-string bytes. Both httpx and curl_cffi
  373. # re-serialize the URL through ``urllib.parse.urlencode`` which
  374. # normalises encodings — breaks the signature and yields HTTP 400
  375. # ``SignatureDoesNotMatch`` (confirmed, and matches Pr0zak/YASTL#52's
  376. # analysis). ``urllib.request`` transmits the URL verbatim, so we
  377. # use it for S3 hosts and keep httpx for MakerWorld's own CDN.
  378. if host.endswith(".amazonaws.com"):
  379. return await _download_s3_urllib(signed_url, path_tail)
  380. # The signed URL's query-string IS the credential — don't send the
  381. # Bambu Cloud bearer to the CDN too. Strips Authorization/x-bbl-* and
  382. # keeps only User-Agent, matching what ``_download_s3_urllib`` does.
  383. cdn_headers = {"User-Agent": _CLIENT_HEADERS["User-Agent"]}
  384. try:
  385. async with self._client.stream(
  386. "GET", signed_url, headers=cdn_headers, timeout=60.0, follow_redirects=False
  387. ) as response:
  388. if response.status_code != 200:
  389. raise MakerWorldUnavailableError(f"3MF download returned HTTP {response.status_code}")
  390. chunks: list[bytes] = []
  391. total = 0
  392. async for chunk in response.aiter_bytes():
  393. total += len(chunk)
  394. if total > _MAX_3MF_BYTES:
  395. raise MakerWorldUnavailableError(f"3MF exceeds {_MAX_3MF_BYTES // (1024 * 1024)} MB cap")
  396. chunks.append(chunk)
  397. return b"".join(chunks), path_tail
  398. except httpx.TimeoutException as exc:
  399. raise MakerWorldUnavailableError(f"3MF download timed out: {exc}") from exc
  400. except httpx.HTTPError as exc:
  401. raise MakerWorldUnavailableError(f"3MF download failed: {exc}") from exc
  402. async def fetch_thumbnail(self, url: str) -> tuple[bytes, str]:
  403. """Fetch a MakerWorld CDN image (thumbnail / cover / plate preview).
  404. Used by the ``/makerworld/thumbnail`` proxy so the frontend doesn't
  405. have to hotlink MakerWorld's CDN directly — avoids loosening the
  406. SPA's ``img-src`` CSP and keeps users' IP addresses out of
  407. MakerWorld's access logs.
  408. Validates that the URL's host is one of the known MakerWorld CDN
  409. hosts (SSRF guard — same allowlist as :meth:`download_3mf`). Caps
  410. payload at 5 MB. Returns ``(bytes, content_type)``; content type
  411. defaults to ``image/jpeg`` if the upstream didn't set one.
  412. """
  413. try:
  414. parsed = urlparse(url)
  415. except ValueError as exc:
  416. raise MakerWorldUrlError(f"Invalid thumbnail URL: {exc}") from exc
  417. host = (parsed.hostname or "").lower()
  418. if host not in MAKERWORLD_CDN_HOSTS:
  419. raise MakerWorldUrlError(f"Refusing to fetch thumbnail from non-MakerWorld host: {host!r}")
  420. # ``follow_redirects=False``: the host allowlist above is only
  421. # meaningful on the initial URL. A 302 from the CDN to any other host
  422. # would otherwise be followed transparently (including RFC1918 /
  423. # metadata endpoints), so we insist upstream resolve the asset
  424. # directly. A redirect response surfaces as ``MakerWorldUnavailable``
  425. # below.
  426. try:
  427. response = await self._client.get(url, headers=self._headers(), timeout=20.0, follow_redirects=False)
  428. except httpx.TimeoutException as exc:
  429. raise MakerWorldUnavailableError(f"Thumbnail request timed out: {exc}") from exc
  430. except httpx.HTTPError as exc:
  431. raise MakerWorldUnavailableError(f"Thumbnail request failed: {exc}") from exc
  432. if response.status_code != 200:
  433. raise MakerWorldUnavailableError(f"Thumbnail fetch returned HTTP {response.status_code}")
  434. # MakerWorld's CDN serves real PNG/JPG files with
  435. # ``Content-Type: application/octet-stream`` (they use
  436. # ``Content-Disposition: attachment; filename="...png"`` instead). So
  437. # we can't just trust the header — derive the MIME from the URL's
  438. # file extension and only fall back to the header if the URL doesn't
  439. # carry one. Reject text/* / json outright regardless of extension
  440. # so an upstream error page can't slip through as "image/png".
  441. upstream_type = response.headers.get("content-type", "").split(";")[0].strip().lower()
  442. if upstream_type in _REFUSED_THUMBNAIL_MIMES:
  443. raise MakerWorldUnavailableError(f"Thumbnail upstream returned non-image content-type: {upstream_type!r}")
  444. path_lower = parsed.path.lower()
  445. ext_mime: str | None = None
  446. for ext, mime in _IMAGE_EXT_TO_MIME.items():
  447. if path_lower.endswith(ext):
  448. ext_mime = mime
  449. break
  450. if upstream_type.startswith("image/"):
  451. content_type = upstream_type
  452. elif ext_mime is not None:
  453. content_type = ext_mime
  454. else:
  455. # No image extension and no image/* content-type — can't confidently
  456. # serve this as an image, so refuse.
  457. raise MakerWorldUnavailableError(
  458. f"Thumbnail upstream returned {upstream_type!r} and URL has no image extension"
  459. )
  460. payload = response.content
  461. if len(payload) > _MAX_THUMBNAIL_BYTES:
  462. raise MakerWorldUnavailableError(f"Thumbnail exceeds {_MAX_THUMBNAIL_BYTES // (1024 * 1024)} MB cap")
  463. return payload, content_type