archive.py 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  1. import hashlib
  2. import json
  3. import logging
  4. import os
  5. import re
  6. import shutil
  7. import zipfile
  8. from datetime import date, datetime, time, timezone
  9. from pathlib import Path
  10. from defusedxml import ElementTree as ET
  11. from sqlalchemy import and_, or_, select, text
  12. from sqlalchemy.ext.asyncio import AsyncSession
  13. from backend.app.core.config import settings
  14. from backend.app.models.archive import PrintArchive
  15. from backend.app.models.filament import Filament
  16. from backend.app.models.printer import Printer
  17. from backend.app.utils.safe_path import PathTraversalError, safe_join_under
  18. logger = logging.getLogger(__name__)
  19. def _copy_and_fsync(src: Path, dst: Path, chunk_size: int = 1024 * 1024) -> None:
  20. """Copy src to dst with an explicit chunked read/write and fsync the dst.
  21. Replacement for shutil.copy2 in the archive pipeline. shutil.copy2 uses
  22. Linux sendfile(), which on some kernels/filesystems has returned a short
  23. count on the first call and truncated the destination for larger 3MF
  24. uploads (#1032, observed on Raspberry Pi OS bookworm / armv7l). An
  25. explicit loop with fsync avoids that path and guarantees the dest bytes
  26. are on disk before the caller inspects them as a ZIP.
  27. """
  28. with src.open("rb") as rf, dst.open("wb") as wf:
  29. while True:
  30. buf = rf.read(chunk_size)
  31. if not buf:
  32. break
  33. wf.write(buf)
  34. wf.flush()
  35. os.fsync(wf.fileno())
  36. shutil.copystat(src, dst)
  37. def resolve_display_stem(filename: str) -> str:
  38. """Return a clean human-readable stem from a 3MF/gcode filename.
  39. Bambu Studio's "Send to printer" dialog typically writes files like
  40. ``Plate_1.gcode.3mf`` (a sliced gcode payload wrapped in a 3MF container).
  41. The naive ``Path(filename).stem`` only drops the last suffix, leaving
  42. ``Plate_1.gcode`` — which then surfaces in the archive UI as a confusing
  43. ``Plate_1.gcode`` rather than ``Plate_1`` (#1152 follow-up).
  44. Strip the recognised print-format suffixes in order:
  45. - ``.gcode.3mf`` → bare stem (Bambu Studio FTP send)
  46. - ``.3mf`` → bare stem
  47. - ``.gcode`` → bare stem (rare standalone gcode upload)
  48. Anything else passes through unchanged.
  49. """
  50. name = Path(filename).name # drop any path components
  51. lower = name.lower()
  52. for suffix in (".gcode.3mf", ".3mf", ".gcode"):
  53. if lower.endswith(suffix):
  54. return name[: -len(suffix)]
  55. return Path(name).stem
  56. def peek_plate_index_in_3mf(file_path: Path) -> int | None:
  57. """Return the plate index recorded inside a Bambu 3MF, or None.
  58. Reads only ``Metadata/slice_info.config`` to keep this cheap — used by
  59. the print-start callback to verify that the 3MF we just downloaded over
  60. FTP actually matches the plate the printer is running (#1204). The full
  61. ThreeMFParser does much more work and runs later inside ArchiveService.
  62. """
  63. try:
  64. with zipfile.ZipFile(file_path, "r") as zf:
  65. if "Metadata/slice_info.config" not in zf.namelist():
  66. return None
  67. content = zf.read("Metadata/slice_info.config").decode()
  68. root = ET.fromstring(content)
  69. plate = root.find(".//plate")
  70. if plate is None:
  71. return None
  72. for meta in plate.findall("metadata"):
  73. if meta.get("key") == "index":
  74. value = meta.get("value")
  75. if value:
  76. try:
  77. return int(value)
  78. except ValueError:
  79. return None
  80. except Exception:
  81. return None
  82. return None
  83. _PLATE_SUFFIX_RE = re.compile(r"^(.*?)(\s*-\s*Plate\s+|_plate_)(\d+)$", re.IGNORECASE)
  84. def swap_plate_suffix(name: str | None, target_plate: int) -> str | None:
  85. """Return ``name`` with its trailing plate number replaced, or None.
  86. Bambu Studio names multi-plate uploads ``"<Project> - Plate <N>"`` (and
  87. a lowercase ``"_plate_<N>"`` variant exists too — see
  88. test_print_start_expected_promotion). When MQTT subtask_name lags
  89. across consecutive plates of the same model (#1204) the suffix points
  90. at the previous plate; swapping it gives us the correct upload to
  91. re-fetch from FTP. Returns None if no recognised suffix is present.
  92. """
  93. if not name:
  94. return None
  95. m = _PLATE_SUFFIX_RE.match(name)
  96. if not m:
  97. return None
  98. base, separator, _ = m.groups()
  99. return f"{base}{separator}{target_plate}"
  100. class ThreeMFParser:
  101. """Parser for Bambu Lab 3MF files."""
  102. def __init__(self, file_path: Path, plate_number: int | None = None):
  103. self.file_path = file_path
  104. self.plate_number = plate_number # Which plate was printed (1, 2, 3, etc.)
  105. self.metadata: dict = {}
  106. def parse(self) -> dict:
  107. """Extract metadata from 3MF file."""
  108. try:
  109. with zipfile.ZipFile(self.file_path, "r") as zf:
  110. self._parse_slice_info(zf) # Now sets self.plate_number from slice_info
  111. self._parse_project_settings(zf)
  112. self._parse_gcode_header(zf)
  113. self._parse_3dmodel(zf)
  114. self._extract_thumbnail(zf) # Uses correct plate_number for thumbnail
  115. # Enhance print_name with plate info if this is a multi-plate export
  116. plate_index = self.metadata.get("_plate_index")
  117. if plate_index and plate_index > 1:
  118. # Append plate number to distinguish from other plates
  119. existing_name = self.metadata.get("print_name", "")
  120. if existing_name and f"Plate {plate_index}" not in existing_name:
  121. self.metadata["print_name"] = f"{existing_name} - Plate {plate_index}"
  122. # ALWAYS prefer slice_info values - they contain ONLY filaments actually used in print
  123. # project_settings contains ALL configured filaments (AMS slots), not just used ones
  124. if self.metadata.get("_slice_filament_type"):
  125. self.metadata["filament_type"] = self.metadata["_slice_filament_type"]
  126. if self.metadata.get("_slice_filament_color"):
  127. self.metadata["filament_color"] = self.metadata["_slice_filament_color"]
  128. # Clean up internal keys
  129. self.metadata.pop("_slice_filament_type", None)
  130. self.metadata.pop("_slice_filament_color", None)
  131. self.metadata.pop("_plate_index", None)
  132. except Exception as e:
  133. # Return whatever metadata was extracted before the error, but
  134. # surface the failure so corrupted / truncated 3MF archives are
  135. # visible in support bundles (#1032).
  136. logger.warning(
  137. "ThreeMFParser: failed to parse %s: %s(%s) — returning partial metadata",
  138. self.file_path,
  139. type(e).__name__,
  140. e,
  141. )
  142. return self.metadata
  143. def _parse_slice_info(self, zf: zipfile.ZipFile):
  144. """Parse slice_info.config for print settings and printable objects."""
  145. try:
  146. if "Metadata/slice_info.config" in zf.namelist():
  147. content = zf.read("Metadata/slice_info.config").decode()
  148. root = ET.fromstring(content)
  149. # Extract printer_model_id from plate metadata
  150. # Format: <plate><metadata key="printer_model_id" value="C11" /></plate>
  151. for meta in root.findall(".//metadata"):
  152. key = meta.get("key")
  153. value = meta.get("value")
  154. if key == "printer_model_id" and value:
  155. from backend.app.utils.printer_models import normalize_printer_model_id
  156. normalized = normalize_printer_model_id(value)
  157. if normalized:
  158. self.metadata["sliced_for_model"] = normalized
  159. break
  160. # Find the plate element (single-plate exports only have one plate)
  161. plate = root.find(".//plate")
  162. if plate is not None:
  163. # Extract metadata from plate element
  164. for meta in plate.findall("metadata"):
  165. key = meta.get("key")
  166. value = meta.get("value")
  167. if key == "index" and value:
  168. # Extract plate index - this tells us which plate was exported
  169. try:
  170. extracted_index = int(value)
  171. # Set plate_number if not already set from filename
  172. if not self.plate_number:
  173. self.plate_number = extracted_index
  174. # Store in metadata for print_name generation
  175. self.metadata["_plate_index"] = extracted_index
  176. except ValueError:
  177. pass # Skip non-numeric plate index
  178. elif key == "prediction" and value:
  179. self.metadata["print_time_seconds"] = int(value)
  180. elif key == "weight" and value:
  181. self.metadata["filament_used_grams"] = float(value)
  182. elif key == "curr_bed_type" and value:
  183. self.metadata["bed_type"] = value
  184. # Extract printable objects for skip object functionality
  185. # Objects are stored as <object identify_id="123" name="Part1" skipped="false" />
  186. printable_objects = {}
  187. for obj in plate.findall("object"):
  188. identify_id = obj.get("identify_id")
  189. name = obj.get("name")
  190. skipped = obj.get("skipped", "false")
  191. # Only include objects that are not pre-skipped
  192. if identify_id and name and skipped.lower() != "true":
  193. try:
  194. printable_objects[int(identify_id)] = name
  195. except ValueError:
  196. pass # Skip objects with non-numeric identify_id
  197. if printable_objects:
  198. self.metadata["printable_objects"] = printable_objects
  199. # Get filament info from filaments ACTUALLY USED in the print
  200. # slice_info has <filament id="1" type="PLA" color="#FFFFFF" used_g="100" />
  201. # Only include filaments where used_g > 0
  202. filaments = root.findall(".//filament")
  203. if filaments:
  204. # Collect unique filament types and colors for filaments that are actually used
  205. types = []
  206. colors = []
  207. for f in filaments:
  208. # Check if this filament is actually used in the print
  209. used_g = f.get("used_g", "0")
  210. try:
  211. used_amount = float(used_g)
  212. except (ValueError, TypeError):
  213. used_amount = 0
  214. # Only include if used_g > 0 (filament is actually consumed)
  215. if used_amount > 0:
  216. ftype = f.get("type")
  217. fcolor = f.get("color")
  218. if ftype and ftype not in types:
  219. types.append(ftype)
  220. if fcolor and fcolor not in colors:
  221. colors.append(fcolor)
  222. if types:
  223. self.metadata["_slice_filament_type"] = ", ".join(types)
  224. if colors:
  225. self.metadata["_slice_filament_color"] = ",".join(colors)
  226. # Collect per-slot filament usage for tracking & notifications
  227. filament_slots = []
  228. for f in filaments:
  229. slot_id = f.get("id")
  230. used_g_str = f.get("used_g", "0")
  231. try:
  232. used_g = float(used_g_str)
  233. except (ValueError, TypeError):
  234. used_g = 0
  235. if used_g > 0 and slot_id:
  236. filament_slots.append(
  237. {
  238. "slot_id": int(slot_id),
  239. "used_g": round(used_g, 2),
  240. "type": f.get("type", ""),
  241. "color": f.get("color", ""),
  242. }
  243. )
  244. if filament_slots:
  245. self.metadata["filament_slots"] = filament_slots
  246. except Exception:
  247. pass # Skip unparseable slice_info metadata
  248. def _parse_project_settings(self, zf: zipfile.ZipFile):
  249. """Parse project settings for print configuration."""
  250. try:
  251. if "Metadata/project_settings.config" in zf.namelist():
  252. content = zf.read("Metadata/project_settings.config").decode()
  253. try:
  254. data = json.loads(content)
  255. self._extract_filament_info(data)
  256. self._extract_print_settings(data)
  257. except json.JSONDecodeError:
  258. pass # Skip malformed project_settings JSON
  259. except Exception:
  260. pass # Skip unreadable project settings file
  261. def _parse_gcode_header(self, zf: zipfile.ZipFile):
  262. """Parse G-code file header for total layer count and printer model."""
  263. try:
  264. # Look for plate_1.gcode or similar
  265. gcode_files = [f for f in zf.namelist() if f.endswith(".gcode")]
  266. if not gcode_files:
  267. return
  268. # Read first 4KB of G-code (header contains metadata)
  269. gcode_path = gcode_files[0]
  270. with zf.open(gcode_path) as f:
  271. header = f.read(4096).decode("utf-8", errors="ignore")
  272. # Look for "; total layer number: XX" pattern
  273. match = re.search(r";\s*total\s+layer\s+number[:\s]+(\d+)", header, re.IGNORECASE)
  274. if match:
  275. self.metadata["total_layers"] = int(match.group(1))
  276. # Total filament usage. The slicer writes the print's totals into
  277. # the G-code header ("; total filament weight [g] : 126.26"). Only
  278. # a fallback — slice_info.config is more authoritative when present
  279. # — but it covers sliced outputs whose slice_info lacks per-filament
  280. # used_g, and it's the slicer's own figure regardless.
  281. if "filament_used_grams" not in self.metadata:
  282. match = re.search(r";\s*total\s+filament\s+weight\s*\[g\]\s*:\s*([\d.]+)", header, re.IGNORECASE)
  283. if match:
  284. self.metadata["filament_used_grams"] = float(match.group(1))
  285. if "filament_used_mm" not in self.metadata:
  286. match = re.search(r";\s*total\s+filament\s+length\s*\[mm\]\s*:\s*([\d.]+)", header, re.IGNORECASE)
  287. if match:
  288. self.metadata["filament_used_mm"] = float(match.group(1))
  289. # Look for printer_model in gcode header (fallback if not found in slice_info)
  290. # Format: "; printer_model = Bambu Lab X1 Carbon" or "; printer_model = X1C"
  291. if "sliced_for_model" not in self.metadata:
  292. match = re.search(r";\s*printer_model\s*=\s*(.+)", header, re.IGNORECASE)
  293. if match:
  294. from backend.app.utils.printer_models import normalize_printer_model
  295. raw_model = match.group(1).strip()
  296. self.metadata["sliced_for_model"] = normalize_printer_model(raw_model)
  297. except Exception:
  298. pass # G-code header parsing is best-effort; metadata may come from other sources
  299. def _extract_filament_info(self, data: dict):
  300. """Extract filament info, preferring non-support filaments."""
  301. try:
  302. filament_types = data.get("filament_type", [])
  303. filament_colors = data.get("filament_colour", [])
  304. filament_is_support = data.get("filament_is_support", [])
  305. if not filament_types:
  306. return
  307. # Collect all non-support filaments
  308. non_support_types = []
  309. non_support_colors = []
  310. for i, ftype in enumerate(filament_types):
  311. is_support = filament_is_support[i] if i < len(filament_is_support) else "0"
  312. if is_support == "0":
  313. if ftype and ftype not in non_support_types:
  314. non_support_types.append(ftype)
  315. if i < len(filament_colors) and filament_colors[i]:
  316. color = filament_colors[i]
  317. if color not in non_support_colors:
  318. non_support_colors.append(color)
  319. # Fallback to first filament if all are support
  320. if not non_support_types and filament_types:
  321. non_support_types = [filament_types[0]]
  322. if not non_support_colors and filament_colors:
  323. non_support_colors = [filament_colors[0]]
  324. # Store filament type(s)
  325. if non_support_types:
  326. self.metadata["filament_type"] = ", ".join(non_support_types)
  327. # Store all colors as comma-separated (for multi-color display)
  328. if non_support_colors:
  329. self.metadata["filament_color"] = ",".join(non_support_colors)
  330. except Exception:
  331. pass # Filament info is optional; fall back to slice_info values
  332. def _extract_print_settings(self, data: dict):
  333. """Extract print settings from JSON config."""
  334. try:
  335. # Layer height - usually an array, get first value
  336. if "layer_height" in data:
  337. val = data["layer_height"]
  338. if isinstance(val, list) and val:
  339. self.metadata["layer_height"] = float(val[0])
  340. elif isinstance(val, (int, float, str)):
  341. self.metadata["layer_height"] = float(val)
  342. # Nozzle diameter
  343. if "nozzle_diameter" in data:
  344. val = data["nozzle_diameter"]
  345. if isinstance(val, list) and val:
  346. self.metadata["nozzle_diameter"] = float(val[0])
  347. elif isinstance(val, (int, float, str)):
  348. self.metadata["nozzle_diameter"] = float(val)
  349. # Bed temperature - first layer or regular
  350. for key in ["bed_temperature_initial_layer", "bed_temperature"]:
  351. if key in data:
  352. val = data[key]
  353. if isinstance(val, list) and val:
  354. self.metadata["bed_temperature"] = int(float(val[0]))
  355. elif isinstance(val, (int, float, str)):
  356. self.metadata["bed_temperature"] = int(float(val))
  357. break
  358. # Nozzle temperature
  359. for key in ["nozzle_temperature_initial_layer", "nozzle_temperature"]:
  360. if key in data:
  361. val = data[key]
  362. if isinstance(val, list) and val:
  363. self.metadata["nozzle_temperature"] = int(float(val[0]))
  364. elif isinstance(val, (int, float, str)):
  365. self.metadata["nozzle_temperature"] = int(float(val))
  366. break
  367. # Printer model (extract and normalize)
  368. if "printer_model" in data:
  369. from backend.app.utils.printer_models import normalize_printer_model
  370. self.metadata["sliced_for_model"] = normalize_printer_model(data["printer_model"])
  371. # Build plate type — only set from project_settings if slice_info didn't already
  372. # provide it (slice_info is more authoritative as it reflects the exported plate).
  373. if "bed_type" not in self.metadata and "curr_bed_type" in data:
  374. val = data["curr_bed_type"]
  375. if isinstance(val, str) and val.strip():
  376. self.metadata["bed_type"] = val.strip()
  377. except Exception:
  378. pass # Print settings are optional; missing values are left unset
  379. def _extract_settings_from_content(self, content: str):
  380. """Extract print settings from config content."""
  381. settings_map = {
  382. "layer_height": ("layer_height", float),
  383. "nozzle_diameter": ("nozzle_diameter", float),
  384. "bed_temperature": ("bed_temperature", int),
  385. "nozzle_temperature": ("nozzle_temperature", int),
  386. }
  387. for key, (search_key, converter) in settings_map.items():
  388. if key not in self.metadata:
  389. try:
  390. # Try JSON format
  391. if f'"{search_key}"' in content:
  392. start = content.find(f'"{search_key}"')
  393. value_start = content.find(":", start) + 1
  394. value_end = content.find(",", value_start)
  395. if value_end == -1:
  396. value_end = content.find("}", value_start)
  397. value = content[value_start:value_end].strip().strip('"')
  398. self.metadata[key] = converter(value)
  399. except (ValueError, TypeError):
  400. pass # Skip settings with unconvertible values
  401. def _parse_3dmodel(self, zf: zipfile.ZipFile):
  402. """Parse 3D/3dmodel.model for MakerWorld metadata."""
  403. try:
  404. model_path = "3D/3dmodel.model"
  405. if model_path not in zf.namelist():
  406. return
  407. content = zf.read(model_path).decode("utf-8", errors="ignore")
  408. # Parse XML metadata elements
  409. # MakerWorld adds metadata like: <metadata name="Designer">username</metadata>
  410. metadata_pattern = r'<metadata\s+name="([^"]+)"[^>]*>([^<]*)</metadata>'
  411. matches = re.findall(metadata_pattern, content)
  412. makerworld_fields = {}
  413. for name, value in matches:
  414. makerworld_fields[name] = value.strip()
  415. # Check for direct MakerWorld URL in content
  416. url_pattern = r'https?://makerworld\.com/[^\s<>"\']+/models/(\d+)'
  417. url_match = re.search(url_pattern, content)
  418. if url_match:
  419. self.metadata["makerworld_url"] = url_match.group(0)
  420. self.metadata["makerworld_model_id"] = url_match.group(1)
  421. # Extract model ID from DSM reference in image URLs
  422. # Format: https://makerworld.bblmw.com/makerworld/model/DSM00000001275614/...
  423. # The numeric part (1275614) is the MakerWorld model ID
  424. if "makerworld_url" not in self.metadata:
  425. dsm_pattern = r"DSM0+(\d+)"
  426. dsm_match = re.search(dsm_pattern, content)
  427. if dsm_match:
  428. model_id = dsm_match.group(1)
  429. self.metadata["makerworld_url"] = f"https://makerworld.com/en/models/{model_id}"
  430. self.metadata["makerworld_model_id"] = model_id
  431. # Store designer info
  432. if "Designer" in makerworld_fields:
  433. self.metadata["designer"] = makerworld_fields["Designer"]
  434. if "Title" in makerworld_fields:
  435. self.metadata["print_name"] = makerworld_fields["Title"]
  436. except Exception:
  437. pass # MakerWorld/3dmodel metadata is optional
  438. def _extract_thumbnail(self, zf: zipfile.ZipFile):
  439. """Extract thumbnail image from 3MF.
  440. If a plate_number was specified, try to use that plate's thumbnail first.
  441. """
  442. thumbnail_paths = []
  443. # If a specific plate was printed, try that thumbnail first
  444. if self.plate_number:
  445. thumbnail_paths.append(f"Metadata/plate_{self.plate_number}.png")
  446. # Fallback to default paths
  447. thumbnail_paths.extend(
  448. [
  449. "Metadata/plate_1.png",
  450. "Metadata/thumbnail.png",
  451. "Metadata/model_thumbnail.png",
  452. # Project-wide thumbnail BambuStudio embeds at upload time. We
  453. # only reach this when BS hasn't written a per-plate
  454. # ``Metadata/plate_N.png`` — most notably the #1493 cross-class
  455. # re-slice path where ``--arrange`` rearranges objects but the
  456. # CLI then doesn't emit a fresh per-plate preview. The
  457. # ``_middle`` size is the editor-quality variant (~500 KB);
  458. # ``_small`` and ``_3mf`` are smaller alternates if it's not
  459. # present. Without this fallback the re-sliced archive cards
  460. # render without a cover image.
  461. "Auxiliaries/.thumbnails/thumbnail_middle.png",
  462. "Auxiliaries/.thumbnails/thumbnail_small.png",
  463. "Auxiliaries/.thumbnails/thumbnail_3mf.png",
  464. ]
  465. )
  466. for thumb_path in thumbnail_paths:
  467. if thumb_path in zf.namelist():
  468. self.metadata["_thumbnail_data"] = zf.read(thumb_path)
  469. self.metadata["_thumbnail_ext"] = ".png"
  470. break
  471. def extract_printable_objects_from_3mf(
  472. data: bytes, plate_number: int | None = None, include_positions: bool = False
  473. ) -> dict[int, str] | dict[int, dict] | tuple[dict[int, dict], list | None]:
  474. """Extract printable objects from 3MF file bytes.
  475. This is a lightweight function used during print start to get the list
  476. of objects that can be skipped.
  477. Args:
  478. data: Raw bytes of the 3MF file
  479. plate_number: Which plate was printed (1-based), or None for first plate
  480. include_positions: If True, return tuple of (objects dict, bbox_all)
  481. Returns:
  482. If include_positions=False: Dictionary mapping identify_id (int) to object name (str)
  483. If include_positions=True: Tuple of (dict mapping identify_id to {name, x, y}, bbox_all list or None)
  484. """
  485. from io import BytesIO
  486. printable_objects: dict = {}
  487. bbox_all: list | None = None
  488. try:
  489. with zipfile.ZipFile(BytesIO(data), "r") as zf:
  490. if "Metadata/slice_info.config" not in zf.namelist():
  491. return printable_objects
  492. content = zf.read("Metadata/slice_info.config").decode()
  493. root = ET.fromstring(content)
  494. # Find the correct plate
  495. if plate_number:
  496. plate = root.find(f".//plate[@plate_idx='{plate_number}']")
  497. if plate is None:
  498. plate = root.find(".//plate")
  499. else:
  500. plate = root.find(".//plate")
  501. if plate is None:
  502. return printable_objects
  503. # Get actual plate index from metadata (sliced files only have one plate)
  504. plate_idx = plate_number or 1
  505. for meta in plate.findall("metadata"):
  506. if meta.get("key") == "index":
  507. try:
  508. plate_idx = int(meta.get("value", "1"))
  509. except ValueError:
  510. pass # Use default plate_idx if value is non-numeric
  511. break
  512. # Load position data from plate_N.json if we need positions
  513. # Build a lookup by name - use list to handle duplicate names
  514. bbox_by_name: dict[str, list[list]] = {}
  515. if include_positions:
  516. plate_json_path = f"Metadata/plate_{plate_idx}.json"
  517. if plate_json_path in zf.namelist():
  518. try:
  519. plate_json = json.loads(zf.read(plate_json_path).decode())
  520. # Get bbox_all - the bounding box of all objects (used for image bounds)
  521. bbox_all = plate_json.get("bbox_all")
  522. for bbox_obj in plate_json.get("bbox_objects", []):
  523. obj_name = bbox_obj.get("name")
  524. bbox = bbox_obj.get("bbox", [])
  525. if obj_name and len(bbox) >= 4:
  526. if obj_name not in bbox_by_name:
  527. bbox_by_name[obj_name] = []
  528. bbox_by_name[obj_name].append(bbox)
  529. except (json.JSONDecodeError, KeyError):
  530. pass # Position data is optional; objects will lack x/y coordinates
  531. # Extract objects from slice_info.config
  532. for obj in plate.findall("object"):
  533. identify_id = obj.get("identify_id")
  534. name = obj.get("name")
  535. skipped = obj.get("skipped", "false")
  536. if identify_id and name and skipped.lower() != "true":
  537. try:
  538. obj_id = int(identify_id)
  539. if include_positions:
  540. x, y = None, None
  541. # Match by name - pop first bbox to handle duplicates
  542. bboxes = bbox_by_name.get(name)
  543. if bboxes:
  544. bbox = bboxes.pop(0)
  545. # Calculate center from bbox [x_min, y_min, x_max, y_max]
  546. x = (bbox[0] + bbox[2]) / 2
  547. y = (bbox[1] + bbox[3]) / 2
  548. printable_objects[obj_id] = {"name": name, "x": x, "y": y}
  549. else:
  550. printable_objects[obj_id] = name
  551. except ValueError:
  552. pass # Skip objects with non-numeric identify_id
  553. except Exception:
  554. pass # Return empty dict if 3MF is corrupt or unreadable
  555. if include_positions:
  556. return printable_objects, bbox_all
  557. return printable_objects
  558. class ProjectPageParser:
  559. """Parser for extracting project page data from Bambu Lab 3MF files."""
  560. def __init__(self, file_path: Path):
  561. self.file_path = file_path
  562. def parse(self, archive_id: int) -> dict:
  563. """Extract project page metadata and images from 3MF file."""
  564. import html
  565. result = {
  566. "title": None,
  567. "description": None,
  568. "designer": None,
  569. "designer_user_id": None,
  570. "license": None,
  571. "copyright": None,
  572. "creation_date": None,
  573. "modification_date": None,
  574. "origin": None,
  575. "profile_title": None,
  576. "profile_description": None,
  577. "profile_cover": None,
  578. "profile_user_id": None,
  579. "profile_user_name": None,
  580. "design_model_id": None,
  581. "design_profile_id": None,
  582. "design_region": None,
  583. "model_pictures": [],
  584. "profile_pictures": [],
  585. "thumbnails": [],
  586. }
  587. try:
  588. with zipfile.ZipFile(self.file_path, "r") as zf:
  589. # Parse 3D/3dmodel.model for metadata
  590. model_path = "3D/3dmodel.model"
  591. if model_path in zf.namelist():
  592. content = zf.read(model_path).decode("utf-8", errors="ignore")
  593. # Extract metadata elements using regex
  594. # Format: <metadata name="Key">Value</metadata> or <metadata name="Key" />
  595. metadata_pattern = r'<metadata\s+name="([^"]+)"[^>]*>([^<]*)</metadata>'
  596. matches = re.findall(metadata_pattern, content)
  597. field_mapping = {
  598. "Title": "title",
  599. "Description": "description",
  600. "Designer": "designer",
  601. "DesignerUserId": "designer_user_id",
  602. "License": "license",
  603. "Copyright": "copyright",
  604. "CreationDate": "creation_date",
  605. "ModificationDate": "modification_date",
  606. "Origin": "origin",
  607. "ProfileTitle": "profile_title",
  608. "ProfileDescription": "profile_description",
  609. "ProfileCover": "profile_cover",
  610. "ProfileUserId": "profile_user_id",
  611. "ProfileUserName": "profile_user_name",
  612. "DesignModelId": "design_model_id",
  613. "DesignProfileId": "design_profile_id",
  614. "DesignRegion": "design_region",
  615. }
  616. for name, value in matches:
  617. if name in field_mapping:
  618. # Decode HTML entities multiple times (content is often triple-encoded)
  619. decoded = value.strip()
  620. prev = None
  621. while prev != decoded:
  622. prev = decoded
  623. decoded = html.unescape(decoded)
  624. # Normalize non-breaking spaces to regular spaces
  625. decoded = decoded.replace("\xa0", " ")
  626. result[field_mapping[name]] = decoded if decoded else None
  627. # List images in Auxiliaries folder
  628. from urllib.parse import quote
  629. for name in zf.namelist():
  630. if name.startswith("Auxiliaries/Model Pictures/"):
  631. filename = name.split("/")[-1]
  632. if filename:
  633. result["model_pictures"].append(
  634. {
  635. "name": filename,
  636. "path": name,
  637. "url": f"/api/v1/archives/{archive_id}/project-image/{quote(name, safe='')}",
  638. }
  639. )
  640. elif name.startswith("Auxiliaries/Profile Pictures/"):
  641. filename = name.split("/")[-1]
  642. if filename:
  643. result["profile_pictures"].append(
  644. {
  645. "name": filename,
  646. "path": name,
  647. "url": f"/api/v1/archives/{archive_id}/project-image/{quote(name, safe='')}",
  648. }
  649. )
  650. elif name.startswith("Auxiliaries/.thumbnails/"):
  651. filename = name.split("/")[-1]
  652. if filename:
  653. result["thumbnails"].append(
  654. {
  655. "name": filename,
  656. "path": name,
  657. "url": f"/api/v1/archives/{archive_id}/project-image/{quote(name, safe='')}",
  658. }
  659. )
  660. except Exception as e:
  661. result["_error"] = str(e)
  662. return result
  663. def get_image(self, image_path: str) -> tuple[bytes, str] | None:
  664. """Extract an image from the 3MF file.
  665. Returns tuple of (image_data, content_type) or None if not found.
  666. """
  667. try:
  668. with zipfile.ZipFile(self.file_path, "r") as zf:
  669. if image_path in zf.namelist():
  670. data = zf.read(image_path)
  671. # Determine content type from extension
  672. ext = image_path.lower().split(".")[-1]
  673. content_types = {
  674. "png": "image/png",
  675. "jpg": "image/jpeg",
  676. "jpeg": "image/jpeg",
  677. "webp": "image/webp",
  678. "gif": "image/gif",
  679. }
  680. content_type = content_types.get(ext, "application/octet-stream")
  681. return (data, content_type)
  682. except Exception:
  683. pass # Return None if image cannot be extracted from 3MF
  684. return None
  685. def update_metadata(self, updates: dict) -> bool:
  686. """Update project page metadata in the 3MF file.
  687. Args:
  688. updates: Dict with fields to update (title, description, designer, etc.)
  689. Returns:
  690. True if successful, False otherwise.
  691. """
  692. import html
  693. import tempfile
  694. try:
  695. # Read the 3MF file
  696. with zipfile.ZipFile(self.file_path, "r") as zf_read:
  697. # Find and read the 3dmodel.model file
  698. model_path = "3D/3dmodel.model"
  699. if model_path not in zf_read.namelist():
  700. return False
  701. content = zf_read.read(model_path).decode("utf-8")
  702. # Update metadata fields
  703. field_mapping = {
  704. "title": "Title",
  705. "description": "Description",
  706. "designer": "Designer",
  707. "license": "License",
  708. "copyright": "Copyright",
  709. "profile_title": "ProfileTitle",
  710. "profile_description": "ProfileDescription",
  711. }
  712. for field, xml_name in field_mapping.items():
  713. if field in updates and updates[field] is not None:
  714. new_value = html.escape(updates[field])
  715. # Replace existing metadata or we'd need to add it
  716. pattern = rf'(<metadata\s+name="{xml_name}"[^>]*>)[^<]*(</metadata>)'
  717. replacement = rf"\g<1>{new_value}\g<2>"
  718. content = re.sub(pattern, replacement, content)
  719. # Write to a temporary file first
  720. with tempfile.NamedTemporaryFile(delete=False, suffix=".3mf") as tmp:
  721. tmp_path = Path(tmp.name)
  722. # Create new zip with updated content
  723. with zipfile.ZipFile(tmp_path, "w", zipfile.ZIP_DEFLATED) as zf_write:
  724. for item in zf_read.namelist():
  725. if item == model_path:
  726. zf_write.writestr(item, content.encode("utf-8"))
  727. else:
  728. zf_write.writestr(item, zf_read.read(item))
  729. # Replace original file with updated one
  730. shutil.move(tmp_path, self.file_path)
  731. return True
  732. except Exception:
  733. # Clean up temp file if it exists
  734. if "tmp_path" in locals() and tmp_path.exists():
  735. tmp_path.unlink()
  736. return False
  737. async def _null_print_log_thumbnail_paths(db: AsyncSession, archive_id: int) -> None:
  738. """NULL thumbnail_path on PrintLogEntry rows linked to *archive_id*.
  739. Called from both soft- and hard-delete paths before the archive's files
  740. leave disk. The FK on PrintLogEntry.archive_id is ON DELETE SET NULL so
  741. log rows survive the archive — without this clear, their cached
  742. thumbnail_path would still point at a deleted file and the print-log
  743. view would 404-storm on every render (#1348 follow-up). Lazy-NULL on
  744. the GET route self-heals stragglers (e.g. failed prints that never had
  745. a thumbnail written), but eager clear here avoids the one-time storm.
  746. """
  747. from sqlalchemy import update as sa_update
  748. from backend.app.models.print_log import PrintLogEntry
  749. await db.execute(sa_update(PrintLogEntry).where(PrintLogEntry.archive_id == archive_id).values(thumbnail_path=None))
  750. async def _cancel_pending_queue_items(db: AsyncSession, archive_id: int) -> None:
  751. """Cancel pending queue items pointing at *archive_id* (#1348 follow-up).
  752. Called from ``soft_delete_archive`` only — hard-delete is covered by the
  753. ``ON DELETE CASCADE`` on ``print_queue.archive_id``. A queue item
  754. pointing at an archive whose 3MF has been removed from disk can never
  755. actually dispatch, so cancelling at delete time both (a) tells the user
  756. why the item disappeared from the pending list, and (b) stops the queue
  757. page from 404-storming the archive thumbnail / plates / plate-thumbnail
  758. endpoints when the row is rendered. Only ``pending`` items are touched;
  759. ``printing`` is a rare race the printer-side fail-path catches, and
  760. completed / failed / cancelled rows are historical and untouched.
  761. """
  762. from sqlalchemy import update as sa_update
  763. from backend.app.models.print_queue import PrintQueueItem
  764. await db.execute(
  765. sa_update(PrintQueueItem)
  766. .where(PrintQueueItem.archive_id == archive_id, PrintQueueItem.status == "pending")
  767. .values(status="cancelled", waiting_reason="Source archive deleted")
  768. )
  769. class ArchiveService:
  770. """Service for archiving print jobs."""
  771. def __init__(self, db: AsyncSession):
  772. self.db = db
  773. @staticmethod
  774. def compute_file_hash(file_path: Path) -> str:
  775. """Compute SHA256 hash of a file for duplicate detection."""
  776. sha256 = hashlib.sha256()
  777. with open(file_path, "rb") as f:
  778. # Read in chunks to handle large files
  779. for chunk in iter(lambda: f.read(8192), b""):
  780. sha256.update(chunk)
  781. return sha256.hexdigest()
  782. async def get_duplicate_hashes_and_names(self) -> tuple[set[str], set[tuple[str, str]]]:
  783. """Get all content hashes and (print name, hash) pairs that appear more than once.
  784. For hashes: returns all hashes with > 1 archive (true duplicates).
  785. For name/hash pairs: returns only pairs that have > 1 archive
  786. (i.e., same file archived multiple times, not different files with same name).
  787. Returns a tuple of (duplicate_hashes, duplicate_name_hash_pairs).
  788. """
  789. from sqlalchemy import func
  790. # Soft-deleted archives don't appear in the listing (#1343), so they
  791. # mustn't influence the duplicate-group counts either — otherwise a
  792. # group with 1 live + 4 soft-deleted would still be flagged as a
  793. # duplicate even though the user only sees one row.
  794. result = await self.db.execute(
  795. select(PrintArchive.content_hash)
  796. .where(PrintArchive.content_hash.isnot(None), PrintArchive.deleted_at.is_(None))
  797. .group_by(PrintArchive.content_hash)
  798. .having(func.count(PrintArchive.id) > 1)
  799. )
  800. duplicate_hashes = {row[0] for row in result.all()}
  801. # Find print names that have multiple archives with the SAME hash
  802. # This avoids marking different files with the same name as duplicates
  803. result = await self.db.execute(
  804. select(func.lower(PrintArchive.print_name), PrintArchive.content_hash)
  805. .where(
  806. PrintArchive.print_name.isnot(None),
  807. PrintArchive.content_hash.isnot(None),
  808. PrintArchive.deleted_at.is_(None),
  809. )
  810. .group_by(func.lower(PrintArchive.print_name), PrintArchive.content_hash)
  811. .having(func.count(PrintArchive.id) > 1)
  812. )
  813. duplicate_name_hash_pairs = {(row[0], row[1]) for row in result.all()}
  814. return duplicate_hashes, duplicate_name_hash_pairs
  815. async def find_duplicates(
  816. self,
  817. archive_id: int,
  818. content_hash: str | None = None,
  819. print_name: str | None = None,
  820. makerworld_model_id: str | None = None,
  821. ) -> list[dict]:
  822. """Find duplicate archives based on hash or name matching.
  823. Returns list of dicts with id, print_name, created_at, match_type.
  824. """
  825. duplicates = []
  826. # First, find exact matches by content hash
  827. if content_hash:
  828. result = await self.db.execute(
  829. select(PrintArchive)
  830. .where(
  831. and_(
  832. PrintArchive.content_hash == content_hash,
  833. PrintArchive.id != archive_id,
  834. PrintArchive.deleted_at.is_(None),
  835. )
  836. )
  837. .order_by(PrintArchive.created_at.desc())
  838. .limit(10)
  839. )
  840. for archive in result.scalars().all():
  841. duplicates.append(
  842. {
  843. "id": archive.id,
  844. "print_name": archive.print_name,
  845. "created_at": archive.created_at,
  846. "match_type": "exact",
  847. }
  848. )
  849. # Then, find similar matches by print name or MakerWorld ID
  850. # Prefer strict name+hash matching when hash exists; fallback to name-only for legacy/manual
  851. # archives that may not have a content_hash.
  852. if print_name or makerworld_model_id:
  853. conditions = [PrintArchive.id != archive_id, PrintArchive.deleted_at.is_(None)]
  854. name_conditions = []
  855. if print_name:
  856. if content_hash:
  857. # Match if print names are similar AND have the same hash (same file)
  858. name_conditions.append(
  859. and_(PrintArchive.print_name.ilike(print_name), PrintArchive.content_hash == content_hash)
  860. )
  861. else:
  862. # Fallback for archives without hash data: match by print name only.
  863. name_conditions.append(PrintArchive.print_name.ilike(print_name))
  864. if makerworld_model_id:
  865. # Match by MakerWorld model ID stored in extra_data
  866. from backend.app.core.db_dialect import is_sqlite
  867. if is_sqlite():
  868. from sqlalchemy import func
  869. name_conditions.append(
  870. func.json_extract(PrintArchive.extra_data, "$.makerworld_model_id") == str(makerworld_model_id)
  871. )
  872. else:
  873. name_conditions.append(
  874. text("(extra_data::jsonb->>'makerworld_model_id') = :mw_id").bindparams(
  875. mw_id=str(makerworld_model_id)
  876. )
  877. )
  878. if name_conditions:
  879. conditions.append(or_(*name_conditions))
  880. result = await self.db.execute(
  881. select(PrintArchive).where(and_(*conditions)).order_by(PrintArchive.created_at.desc()).limit(10)
  882. )
  883. for archive in result.scalars().all():
  884. # Don't add if already in duplicates (exact match)
  885. if not any(d["id"] == archive.id for d in duplicates):
  886. duplicates.append(
  887. {
  888. "id": archive.id,
  889. "print_name": archive.print_name,
  890. "created_at": archive.created_at,
  891. "match_type": "similar",
  892. }
  893. )
  894. return duplicates
  895. async def archive_print(
  896. self,
  897. printer_id: int | None,
  898. source_file: Path,
  899. print_data: dict | None = None,
  900. created_by_id: int | None = None,
  901. original_filename: str | None = None,
  902. project_id: int | None = None,
  903. subtask_id: str | None = None,
  904. prefer_filename_for_name: bool = False,
  905. ) -> PrintArchive | None:
  906. """Archive a 3MF file with metadata.
  907. Args:
  908. printer_id: ID of the printer (optional)
  909. source_file: Path to the 3MF file
  910. print_data: Print data from MQTT (optional)
  911. created_by_id: User ID who created this archive (optional, for user tracking)
  912. original_filename: Original human-readable filename (optional, for library files
  913. stored with UUID names)
  914. project_id: Project to associate this archive with (optional, set when triggered
  915. from the project view)
  916. subtask_id: MQTT-provided task identifier (optional). Used to match an
  917. existing archive across a backend restart mid-print so the
  918. original row can be resumed instead of cancelled (#972).
  919. prefer_filename_for_name: When True, use the uploaded filename stem as the
  920. archive's display name even if the 3MF embeds a `print_name` in its
  921. metadata. Used by virtual-printer flows so users who rename a job in
  922. BambuStudio's "send to printer" dialog see that name instead of the
  923. creator-baked title (#1152).
  924. """
  925. # Verify printer exists if specified
  926. if printer_id is not None:
  927. result = await self.db.execute(select(Printer).where(Printer.id == printer_id))
  928. printer = result.scalar_one_or_none()
  929. if not printer:
  930. return None
  931. # Create archive directory structure
  932. timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
  933. display_stem = resolve_display_stem(original_filename if original_filename else source_file.name)
  934. archive_name = f"{timestamp}_{display_stem}"
  935. # Use "unassigned" folder for archives without a printer
  936. printer_folder = str(printer_id) if printer_id is not None else "unassigned"
  937. archive_dir = (
  938. settings.archive_dir / printer_folder / archive_name
  939. ) # SEC-PATH-OK: printer_folder = str(int|None) → digits or "unassigned"; archive_name = f"{timestamp}_{display_stem}" where resolve_display_stem strips path components via Path(filename).name
  940. archive_dir.mkdir(parents=True, exist_ok=True)
  941. # Copy 3MF file with an explicit fsync'd loop (avoids a sendfile
  942. # short-read quirk that silently truncated 3MF archives on some
  943. # platforms — see _copy_and_fsync and #1032).
  944. dest_file = archive_dir / source_file.name
  945. _copy_and_fsync(source_file, dest_file)
  946. # If we just archived a 3MF, verify the dest is a valid ZIP before
  947. # going any further. Staying quiet here is how #1032 escaped review —
  948. # the archive row was written but every later zipfile.ZipFile() call
  949. # on the dest failed with "File is not a zip file".
  950. if (
  951. source_file.suffix.lower() == ".3mf"
  952. and zipfile.is_zipfile(source_file)
  953. and not zipfile.is_zipfile(dest_file)
  954. ):
  955. try:
  956. src_size = source_file.stat().st_size
  957. dst_size = dest_file.stat().st_size
  958. except OSError:
  959. src_size = dst_size = -1
  960. logger.error(
  961. "Archive copy corrupted 3MF: src=%s (%s bytes, valid ZIP) -> dst=%s (%s bytes, NOT a ZIP). Refusing to create archive row.",
  962. source_file,
  963. src_size,
  964. dest_file,
  965. dst_size,
  966. )
  967. # Narrow cleanup: remove only the truncated file and the archive
  968. # directory if it's now empty. archive_dir was created with
  969. # exist_ok=True so it could in theory pre-date this call (e.g.
  970. # same-second same-filename collision); rmtree would be too broad.
  971. try:
  972. dest_file.unlink()
  973. except OSError:
  974. pass
  975. try:
  976. archive_dir.rmdir()
  977. except OSError:
  978. pass # directory not empty — leave untouched
  979. return None
  980. # Compute content hash for duplicate detection
  981. content_hash = self.compute_file_hash(dest_file)
  982. # Extract plate number from filename (e.g., "plate_5" from "/data/Metadata/plate_5.gcode")
  983. plate_number = None
  984. if print_data:
  985. filename = print_data.get("filename", "")
  986. match = re.search(r"plate_(\d+)", filename)
  987. if match:
  988. plate_number = int(match.group(1))
  989. # Parse 3MF metadata
  990. parser = ThreeMFParser(dest_file, plate_number=plate_number)
  991. metadata = parser.parse()
  992. # Save thumbnail if present
  993. thumbnail_path = None
  994. if "_thumbnail_data" in metadata:
  995. thumb_file = archive_dir / f"thumbnail{metadata['_thumbnail_ext']}"
  996. thumb_file.write_bytes(metadata["_thumbnail_data"])
  997. thumbnail_path = str(thumb_file.relative_to(settings.base_dir))
  998. del metadata["_thumbnail_data"]
  999. del metadata["_thumbnail_ext"]
  1000. # Merge with print data from MQTT
  1001. if print_data:
  1002. metadata["_print_data"] = print_data
  1003. # Determine status and timestamps
  1004. status = print_data.get("status", "completed") if print_data else "archived"
  1005. started_at = datetime.now(timezone.utc) if status == "printing" else None
  1006. completed_at = datetime.now(timezone.utc) if status in ("completed", "failed", "archived") else None
  1007. # Calculate cost based on filament usage and type
  1008. cost = None
  1009. filament_grams = metadata.get("filament_used_grams")
  1010. filament_type = metadata.get("filament_type")
  1011. if filament_grams and filament_type:
  1012. # For multi-material prints, use the first filament type for cost calculation
  1013. primary_type = filament_type.split(",")[0].strip()
  1014. # Look up filament cost_per_kg from database
  1015. filament_result = await self.db.execute(select(Filament).where(Filament.type == primary_type).limit(1))
  1016. filament = filament_result.scalar_one_or_none()
  1017. if filament:
  1018. cost = round((filament_grams / 1000) * filament.cost_per_kg, 2)
  1019. else:
  1020. # Use default filament cost from settings
  1021. from backend.app.api.routes.settings import get_setting
  1022. default_cost_setting = await get_setting(self.db, "default_filament_cost")
  1023. default_cost_per_kg = float(default_cost_setting) if default_cost_setting else 25.0
  1024. cost = round((filament_grams / 1000) * default_cost_per_kg, 2)
  1025. # Calculate quantity from printable objects count
  1026. # printable_objects is a dict of {identify_id: name} for non-skipped objects
  1027. quantity = 1 # Default to 1
  1028. printable_objects = metadata.get("printable_objects")
  1029. if printable_objects and isinstance(printable_objects, dict):
  1030. quantity = len(printable_objects)
  1031. logger.debug("Auto-detected %s parts from 3MF printable objects", quantity)
  1032. # Create archive record
  1033. archive = PrintArchive(
  1034. printer_id=printer_id,
  1035. filename=original_filename or source_file.name,
  1036. file_path=str(dest_file.relative_to(settings.base_dir)),
  1037. file_size=dest_file.stat().st_size,
  1038. content_hash=content_hash,
  1039. thumbnail_path=thumbnail_path,
  1040. print_name=display_stem if prefer_filename_for_name else (metadata.get("print_name") or display_stem),
  1041. print_time_seconds=metadata.get("print_time_seconds"),
  1042. filament_used_grams=metadata.get("filament_used_grams"),
  1043. filament_type=metadata.get("filament_type"),
  1044. filament_color=metadata.get("filament_color"),
  1045. layer_height=metadata.get("layer_height"),
  1046. total_layers=metadata.get("total_layers"),
  1047. nozzle_diameter=metadata.get("nozzle_diameter"),
  1048. bed_temperature=metadata.get("bed_temperature"),
  1049. bed_type=metadata.get("bed_type"),
  1050. nozzle_temperature=metadata.get("nozzle_temperature"),
  1051. sliced_for_model=metadata.get("sliced_for_model"),
  1052. makerworld_url=metadata.get("makerworld_url"),
  1053. designer=metadata.get("designer"),
  1054. status=status,
  1055. started_at=started_at,
  1056. completed_at=completed_at,
  1057. cost=cost,
  1058. quantity=quantity,
  1059. extra_data=metadata,
  1060. created_by_id=created_by_id,
  1061. project_id=project_id,
  1062. subtask_id=subtask_id,
  1063. )
  1064. self.db.add(archive)
  1065. await self.db.commit()
  1066. await self.db.refresh(archive)
  1067. return archive
  1068. async def get_archive(self, archive_id: int) -> PrintArchive | None:
  1069. """Get an archive by ID with relationships loaded."""
  1070. from sqlalchemy.orm import selectinload
  1071. result = await self.db.execute(
  1072. select(PrintArchive)
  1073. .options(selectinload(PrintArchive.created_by), selectinload(PrintArchive.project))
  1074. .where(PrintArchive.id == archive_id)
  1075. )
  1076. return result.scalar_one_or_none()
  1077. async def update_archive_status(
  1078. self,
  1079. archive_id: int,
  1080. status: str,
  1081. completed_at: datetime | None = None,
  1082. failure_reason: str | None = None,
  1083. ) -> bool:
  1084. """Update the status of an archive."""
  1085. archive = await self.get_archive(archive_id)
  1086. if not archive:
  1087. return False
  1088. archive.status = status
  1089. if completed_at:
  1090. archive.completed_at = completed_at
  1091. if failure_reason:
  1092. archive.failure_reason = failure_reason
  1093. await self.db.commit()
  1094. return True
  1095. async def list_archives(
  1096. self,
  1097. printer_id: int | None = None,
  1098. project_id: int | None = None,
  1099. date_from: date | None = None,
  1100. date_to: date | None = None,
  1101. limit: int = 50,
  1102. offset: int = 0,
  1103. ) -> list[PrintArchive]:
  1104. """List archives with optional filtering."""
  1105. from sqlalchemy.orm import selectinload
  1106. query = (
  1107. select(PrintArchive)
  1108. .options(selectinload(PrintArchive.project), selectinload(PrintArchive.created_by))
  1109. # Hide soft-deleted rows from the listings (#1343). The stats
  1110. # endpoint deliberately does NOT add this filter so deleted
  1111. # archives keep contributing to Quick Stats.
  1112. .where(PrintArchive.deleted_at.is_(None))
  1113. .order_by(PrintArchive.created_at.desc())
  1114. )
  1115. if printer_id:
  1116. query = query.where(PrintArchive.printer_id == printer_id)
  1117. if project_id:
  1118. query = query.where(PrintArchive.project_id == project_id)
  1119. if date_from:
  1120. dt_from = datetime.combine(date_from, time.min, tzinfo=timezone.utc)
  1121. query = query.where(PrintArchive.created_at >= dt_from)
  1122. if date_to:
  1123. dt_to = datetime.combine(date_to, time.max, tzinfo=timezone.utc)
  1124. query = query.where(PrintArchive.created_at <= dt_to)
  1125. query = query.limit(limit).offset(offset)
  1126. result = await self.db.execute(query)
  1127. return list(result.scalars().all())
  1128. async def soft_delete_archive(self, archive_id: int) -> bool:
  1129. """Soft-delete an archive (#1343).
  1130. Removes the archive's files from disk (it disappears from the listings
  1131. and frees the storage) but flips the row's ``deleted_at`` so the stats
  1132. endpoint keeps counting its filament / energy / time / cost. The user
  1133. can opt into a hard delete via the "Also remove from statistics"
  1134. checkbox in the delete dialog — that path calls ``delete_archive``
  1135. instead and removes the row entirely.
  1136. """
  1137. archive = await self.get_archive(archive_id)
  1138. if not archive:
  1139. return False
  1140. if archive.deleted_at is not None:
  1141. # Already soft-deleted; nothing to do. The files were purged on
  1142. # the first soft-delete pass so there is nothing left on disk.
  1143. return True
  1144. dir_to_delete = self._resolve_archive_dir_for_delete(archive)
  1145. await _null_print_log_thumbnail_paths(self.db, archive_id)
  1146. await _cancel_pending_queue_items(self.db, archive_id)
  1147. archive.deleted_at = datetime.now(timezone.utc)
  1148. await self.db.commit()
  1149. if dir_to_delete:
  1150. shutil.rmtree(dir_to_delete, ignore_errors=True)
  1151. return True
  1152. def _resolve_archive_dir_for_delete(self, archive: PrintArchive) -> Path | None:
  1153. """Return the on-disk directory that backs *archive*, after the same
  1154. two safety checks ``delete_archive`` enforces.
  1155. Extracted so soft-delete and hard-delete share the path-resolution
  1156. rules. Returns ``None`` when nothing should be removed from disk
  1157. (no file_path, path outside archive_dir, or path not deep enough).
  1158. """
  1159. if not archive.file_path or not archive.file_path.strip():
  1160. logger.error(
  1161. f"SECURITY: Refusing to delete files for archive {archive.id} - "
  1162. f"file_path is empty or invalid: '{archive.file_path}'"
  1163. )
  1164. return None
  1165. file_path = settings.base_dir / archive.file_path
  1166. if not file_path.exists():
  1167. return None
  1168. archive_dir = file_path.parent
  1169. try:
  1170. relative_path = archive_dir.resolve().relative_to(settings.archive_dir.resolve())
  1171. except ValueError:
  1172. logger.error(
  1173. f"SECURITY: Refusing to delete archive {archive.id} - "
  1174. f"path {archive_dir} is outside archive directory {settings.archive_dir}"
  1175. )
  1176. return None
  1177. if len(relative_path.parts) < 1:
  1178. logger.error(
  1179. f"SECURITY: Refusing to delete archive {archive.id} - "
  1180. f"path {archive_dir} is not deep enough inside archive directory"
  1181. )
  1182. return None
  1183. return archive_dir
  1184. async def delete_archive(self, archive_id: int) -> bool:
  1185. """Delete an archive and its files."""
  1186. archive = await self.get_archive(archive_id)
  1187. if not archive:
  1188. return False
  1189. # Resolve the directory to delete BEFORE committing the DB change
  1190. dir_to_delete: Path | None = None
  1191. if archive.file_path and archive.file_path.strip():
  1192. file_path = settings.base_dir / archive.file_path
  1193. if file_path.exists():
  1194. archive_dir = file_path.parent
  1195. # Safety check 1: archive_dir must be inside archive_dir
  1196. try:
  1197. archive_dir.resolve().relative_to(settings.archive_dir.resolve())
  1198. except ValueError:
  1199. logger.error(
  1200. f"SECURITY: Refusing to delete archive {archive_id} - "
  1201. f"path {archive_dir} is outside archive directory {settings.archive_dir}"
  1202. )
  1203. await self.db.delete(archive)
  1204. await self.db.commit()
  1205. return True
  1206. # Safety check 2: archive_dir must be at least 1 level deep inside archive_dir
  1207. try:
  1208. relative_path = archive_dir.resolve().relative_to(settings.archive_dir.resolve())
  1209. if len(relative_path.parts) < 1:
  1210. logger.error(
  1211. f"SECURITY: Refusing to delete archive {archive_id} - "
  1212. f"path {archive_dir} is not deep enough inside archive directory"
  1213. )
  1214. await self.db.delete(archive)
  1215. await self.db.commit()
  1216. return True
  1217. except ValueError:
  1218. pass # Already handled above
  1219. dir_to_delete = archive_dir
  1220. else:
  1221. logger.error(
  1222. f"SECURITY: Refusing to delete files for archive {archive_id} - "
  1223. f"file_path is empty or invalid: '{archive.file_path}'"
  1224. )
  1225. # NULL stale thumbnail_path on linked PrintLogEntries before the FK
  1226. # SET-NULL cascade fires. The on-disk file is about to be removed by
  1227. # the rmtree below, so the path on any surviving log entry (archive_id
  1228. # gets SET NULL by the FK) would otherwise point at a missing file
  1229. # and produce 404 storms in the print-log view (#1348-followup).
  1230. await _null_print_log_thumbnail_paths(self.db, archive_id)
  1231. # Delete database record FIRST — if the commit fails (e.g. database locked
  1232. # during concurrent bulk deletes), the files stay on disk and nothing is lost.
  1233. await self.db.delete(archive)
  1234. await self.db.commit()
  1235. # Only delete files AFTER the DB commit succeeds to avoid orphaned records
  1236. if dir_to_delete:
  1237. shutil.rmtree(dir_to_delete, ignore_errors=True)
  1238. return True
  1239. async def attach_timelapse(
  1240. self,
  1241. archive_id: int,
  1242. timelapse_data: bytes,
  1243. filename: str = "timelapse.mp4",
  1244. ) -> bool:
  1245. """Attach a timelapse video to an archive.
  1246. Non-MP4 videos (e.g. AVI from P1S) are saved as-is and a background
  1247. task converts them to MP4 for browser compatibility.
  1248. """
  1249. import asyncio
  1250. archive = await self.get_archive(archive_id)
  1251. if not archive:
  1252. return False
  1253. # Get archive directory
  1254. file_path = (
  1255. settings.base_dir / archive.file_path
  1256. ) # SEC-PATH-OK: archive.file_path is DB-stored, set by archive_print() under settings.archive_dir
  1257. archive_dir = file_path.parent
  1258. # Save timelapse - use thread pool to avoid blocking event loop
  1259. # (timelapse files can be 100MB+, sync write blocks for seconds).
  1260. # `filename` ultimately comes from a printer's FTP listing (compromised-
  1261. # printer threat model) or a query param on /archives/{id}/timelapse/select;
  1262. # the safe-join helper rejects ``..`` segments and absolute paths so a
  1263. # crafted name can't escape the archive directory. Use http=False so a
  1264. # service-layer reject surfaces as a return False (matching the existing
  1265. # not-found contract) rather than a 400 raised from inside a background
  1266. # task.
  1267. try:
  1268. timelapse_file = safe_join_under(archive_dir, filename, http=False)
  1269. except PathTraversalError:
  1270. logger.warning(
  1271. "Refusing to attach timelapse with unsafe filename %r to archive %s",
  1272. filename,
  1273. archive_id,
  1274. )
  1275. return False
  1276. await asyncio.to_thread(timelapse_file.write_bytes, timelapse_data)
  1277. # Update archive record
  1278. archive.timelapse_path = str(timelapse_file.relative_to(settings.base_dir))
  1279. await self.db.commit()
  1280. # For non-MP4 videos (e.g. AVI from P1S), kick off background conversion
  1281. if not filename.lower().endswith(".mp4"):
  1282. asyncio.create_task(
  1283. _convert_timelapse_to_mp4(archive_id, timelapse_file),
  1284. name=f"timelapse-convert-{archive_id}",
  1285. )
  1286. return True
  1287. async def _convert_timelapse_to_mp4(archive_id: int, source_path: Path) -> None:
  1288. """Background task: convert non-MP4 timelapse (e.g. AVI from P1S) to MP4.
  1289. Runs with low CPU priority (-threads 1, nice) so it doesn't starve
  1290. other processes on resource-constrained devices like Raspberry Pi.
  1291. """
  1292. import asyncio
  1293. from backend.app.core.database import async_session
  1294. from backend.app.services.camera import get_ffmpeg_path
  1295. logger = logging.getLogger(__name__)
  1296. ffmpeg = get_ffmpeg_path()
  1297. if not ffmpeg:
  1298. logger.info(
  1299. "FFmpeg not available, skipping timelapse conversion for archive %s (file saved as %s)",
  1300. archive_id,
  1301. source_path.suffix,
  1302. )
  1303. return
  1304. mp4_path = source_path.with_suffix(".mp4")
  1305. try:
  1306. cmd = [
  1307. ffmpeg,
  1308. "-y",
  1309. "-i",
  1310. str(source_path),
  1311. "-c:v",
  1312. "libx264",
  1313. "-preset",
  1314. "fast",
  1315. "-crf",
  1316. "23",
  1317. "-threads",
  1318. "1",
  1319. "-movflags",
  1320. "+faststart",
  1321. str(mp4_path),
  1322. ]
  1323. # Try with nice for lower CPU priority (standard on Linux/macOS)
  1324. try:
  1325. process = await asyncio.create_subprocess_exec(
  1326. "nice",
  1327. "-n",
  1328. "19",
  1329. *cmd,
  1330. stdout=asyncio.subprocess.PIPE,
  1331. stderr=asyncio.subprocess.PIPE,
  1332. )
  1333. except FileNotFoundError:
  1334. # nice not available (e.g. Windows), run without
  1335. process = await asyncio.create_subprocess_exec(
  1336. *cmd,
  1337. stdout=asyncio.subprocess.PIPE,
  1338. stderr=asyncio.subprocess.PIPE,
  1339. )
  1340. _, stderr = await process.communicate()
  1341. if process.returncode != 0:
  1342. logger.warning(
  1343. "Timelapse conversion failed for archive %s: %s",
  1344. archive_id,
  1345. stderr.decode()[-500:],
  1346. )
  1347. if mp4_path.exists():
  1348. mp4_path.unlink()
  1349. return
  1350. # Update DB path to the new MP4 file
  1351. async with async_session() as db:
  1352. from backend.app.models.archive import PrintArchive
  1353. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  1354. archive = result.scalar_one_or_none()
  1355. if archive:
  1356. archive.timelapse_path = str(mp4_path.relative_to(settings.base_dir))
  1357. await db.commit()
  1358. # Remove original non-MP4 file
  1359. if source_path.exists():
  1360. source_path.unlink()
  1361. logger.info(
  1362. "Converted timelapse to MP4 for archive %s (%s → %s)",
  1363. archive_id,
  1364. source_path.name,
  1365. mp4_path.name,
  1366. )
  1367. except Exception as e:
  1368. logger.warning("Timelapse conversion error for archive %s: %s", archive_id, e)
  1369. if mp4_path.exists():
  1370. mp4_path.unlink()