archive.py 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. import hashlib
  2. import json
  3. import logging
  4. import re
  5. import shutil
  6. import zipfile
  7. from datetime import datetime
  8. from pathlib import Path
  9. from xml.etree import ElementTree as ET
  10. from sqlalchemy import and_, or_, select
  11. from sqlalchemy.ext.asyncio import AsyncSession
  12. from backend.app.core.config import settings
  13. from backend.app.models.archive import PrintArchive
  14. from backend.app.models.filament import Filament
  15. from backend.app.models.printer import Printer
  16. logger = logging.getLogger(__name__)
  17. class ThreeMFParser:
  18. """Parser for Bambu Lab 3MF files."""
  19. def __init__(self, file_path: Path, plate_number: int | None = None):
  20. self.file_path = file_path
  21. self.plate_number = plate_number # Which plate was printed (1, 2, 3, etc.)
  22. self.metadata: dict = {}
  23. def parse(self) -> dict:
  24. """Extract metadata from 3MF file."""
  25. try:
  26. with zipfile.ZipFile(self.file_path, "r") as zf:
  27. self._parse_slice_info(zf) # Now sets self.plate_number from slice_info
  28. self._parse_project_settings(zf)
  29. self._parse_gcode_header(zf)
  30. self._parse_3dmodel(zf)
  31. self._extract_thumbnail(zf) # Uses correct plate_number for thumbnail
  32. # Enhance print_name with plate info if this is a multi-plate export
  33. plate_index = self.metadata.get("_plate_index")
  34. if plate_index and plate_index > 1:
  35. # Append plate number to distinguish from other plates
  36. existing_name = self.metadata.get("print_name", "")
  37. if existing_name and f"Plate {plate_index}" not in existing_name:
  38. self.metadata["print_name"] = f"{existing_name} - Plate {plate_index}"
  39. # ALWAYS prefer slice_info values - they contain ONLY filaments actually used in print
  40. # project_settings contains ALL configured filaments (AMS slots), not just used ones
  41. if self.metadata.get("_slice_filament_type"):
  42. self.metadata["filament_type"] = self.metadata["_slice_filament_type"]
  43. if self.metadata.get("_slice_filament_color"):
  44. self.metadata["filament_color"] = self.metadata["_slice_filament_color"]
  45. # Clean up internal keys
  46. self.metadata.pop("_slice_filament_type", None)
  47. self.metadata.pop("_slice_filament_color", None)
  48. self.metadata.pop("_plate_index", None)
  49. except Exception:
  50. pass
  51. return self.metadata
  52. def _parse_slice_info(self, zf: zipfile.ZipFile):
  53. """Parse slice_info.config for print settings and printable objects."""
  54. try:
  55. if "Metadata/slice_info.config" in zf.namelist():
  56. content = zf.read("Metadata/slice_info.config").decode()
  57. root = ET.fromstring(content)
  58. # Extract printer_model_id from plate metadata
  59. # Format: <plate><metadata key="printer_model_id" value="C11" /></plate>
  60. for meta in root.findall(".//metadata"):
  61. key = meta.get("key")
  62. value = meta.get("value")
  63. if key == "printer_model_id" and value:
  64. from backend.app.utils.printer_models import normalize_printer_model_id
  65. normalized = normalize_printer_model_id(value)
  66. if normalized:
  67. self.metadata["sliced_for_model"] = normalized
  68. break
  69. # Find the plate element (single-plate exports only have one plate)
  70. plate = root.find(".//plate")
  71. if plate is not None:
  72. # Extract metadata from plate element
  73. for meta in plate.findall("metadata"):
  74. key = meta.get("key")
  75. value = meta.get("value")
  76. if key == "index" and value:
  77. # Extract plate index - this tells us which plate was exported
  78. try:
  79. extracted_index = int(value)
  80. # Set plate_number if not already set from filename
  81. if not self.plate_number:
  82. self.plate_number = extracted_index
  83. # Store in metadata for print_name generation
  84. self.metadata["_plate_index"] = extracted_index
  85. except ValueError:
  86. pass
  87. elif key == "prediction" and value:
  88. self.metadata["print_time_seconds"] = int(value)
  89. elif key == "weight" and value:
  90. self.metadata["filament_used_grams"] = float(value)
  91. # Extract printable objects for skip object functionality
  92. # Objects are stored as <object identify_id="123" name="Part1" skipped="false" />
  93. printable_objects = {}
  94. for obj in plate.findall("object"):
  95. identify_id = obj.get("identify_id")
  96. name = obj.get("name")
  97. skipped = obj.get("skipped", "false")
  98. # Only include objects that are not pre-skipped
  99. if identify_id and name and skipped.lower() != "true":
  100. try:
  101. printable_objects[int(identify_id)] = name
  102. except ValueError:
  103. pass
  104. if printable_objects:
  105. self.metadata["printable_objects"] = printable_objects
  106. # Get filament info from filaments ACTUALLY USED in the print
  107. # slice_info has <filament id="1" type="PLA" color="#FFFFFF" used_g="100" />
  108. # Only include filaments where used_g > 0
  109. filaments = root.findall(".//filament")
  110. if filaments:
  111. # Collect unique filament types and colors for filaments that are actually used
  112. types = []
  113. colors = []
  114. for f in filaments:
  115. # Check if this filament is actually used in the print
  116. used_g = f.get("used_g", "0")
  117. try:
  118. used_amount = float(used_g)
  119. except (ValueError, TypeError):
  120. used_amount = 0
  121. # Only include if used_g > 0 (filament is actually consumed)
  122. if used_amount > 0:
  123. ftype = f.get("type")
  124. fcolor = f.get("color")
  125. if ftype and ftype not in types:
  126. types.append(ftype)
  127. if fcolor and fcolor not in colors:
  128. colors.append(fcolor)
  129. if types:
  130. self.metadata["_slice_filament_type"] = ", ".join(types)
  131. if colors:
  132. self.metadata["_slice_filament_color"] = ",".join(colors)
  133. except Exception:
  134. pass
  135. def _parse_project_settings(self, zf: zipfile.ZipFile):
  136. """Parse project settings for print configuration."""
  137. try:
  138. if "Metadata/project_settings.config" in zf.namelist():
  139. content = zf.read("Metadata/project_settings.config").decode()
  140. try:
  141. data = json.loads(content)
  142. self._extract_filament_info(data)
  143. self._extract_print_settings(data)
  144. except json.JSONDecodeError:
  145. pass
  146. except Exception:
  147. pass
  148. def _parse_gcode_header(self, zf: zipfile.ZipFile):
  149. """Parse G-code file header for total layer count and printer model."""
  150. import re
  151. try:
  152. # Look for plate_1.gcode or similar
  153. gcode_files = [f for f in zf.namelist() if f.endswith(".gcode")]
  154. if not gcode_files:
  155. return
  156. # Read first 4KB of G-code (header contains metadata)
  157. gcode_path = gcode_files[0]
  158. with zf.open(gcode_path) as f:
  159. header = f.read(4096).decode("utf-8", errors="ignore")
  160. # Look for "; total layer number: XX" pattern
  161. match = re.search(r";\s*total\s+layer\s+number[:\s]+(\d+)", header, re.IGNORECASE)
  162. if match:
  163. self.metadata["total_layers"] = int(match.group(1))
  164. # Look for printer_model in gcode header (fallback if not found in slice_info)
  165. # Format: "; printer_model = Bambu Lab X1 Carbon" or "; printer_model = X1C"
  166. if "sliced_for_model" not in self.metadata:
  167. match = re.search(r";\s*printer_model\s*=\s*(.+)", header, re.IGNORECASE)
  168. if match:
  169. from backend.app.utils.printer_models import normalize_printer_model
  170. raw_model = match.group(1).strip()
  171. self.metadata["sliced_for_model"] = normalize_printer_model(raw_model)
  172. except Exception:
  173. pass
  174. def _extract_filament_info(self, data: dict):
  175. """Extract filament info, preferring non-support filaments."""
  176. try:
  177. filament_types = data.get("filament_type", [])
  178. filament_colors = data.get("filament_colour", [])
  179. filament_is_support = data.get("filament_is_support", [])
  180. if not filament_types:
  181. return
  182. # Collect all non-support filaments
  183. non_support_types = []
  184. non_support_colors = []
  185. for i, ftype in enumerate(filament_types):
  186. is_support = filament_is_support[i] if i < len(filament_is_support) else "0"
  187. if is_support == "0":
  188. if ftype and ftype not in non_support_types:
  189. non_support_types.append(ftype)
  190. if i < len(filament_colors) and filament_colors[i]:
  191. color = filament_colors[i]
  192. if color not in non_support_colors:
  193. non_support_colors.append(color)
  194. # Fallback to first filament if all are support
  195. if not non_support_types and filament_types:
  196. non_support_types = [filament_types[0]]
  197. if not non_support_colors and filament_colors:
  198. non_support_colors = [filament_colors[0]]
  199. # Store filament type(s)
  200. if non_support_types:
  201. self.metadata["filament_type"] = ", ".join(non_support_types)
  202. # Store all colors as comma-separated (for multi-color display)
  203. if non_support_colors:
  204. self.metadata["filament_color"] = ",".join(non_support_colors)
  205. except Exception:
  206. pass
  207. def _extract_print_settings(self, data: dict):
  208. """Extract print settings from JSON config."""
  209. try:
  210. # Layer height - usually an array, get first value
  211. if "layer_height" in data:
  212. val = data["layer_height"]
  213. if isinstance(val, list) and val:
  214. self.metadata["layer_height"] = float(val[0])
  215. elif isinstance(val, (int, float, str)):
  216. self.metadata["layer_height"] = float(val)
  217. # Nozzle diameter
  218. if "nozzle_diameter" in data:
  219. val = data["nozzle_diameter"]
  220. if isinstance(val, list) and val:
  221. self.metadata["nozzle_diameter"] = float(val[0])
  222. elif isinstance(val, (int, float, str)):
  223. self.metadata["nozzle_diameter"] = float(val)
  224. # Bed temperature - first layer or regular
  225. for key in ["bed_temperature_initial_layer", "bed_temperature"]:
  226. if key in data:
  227. val = data[key]
  228. if isinstance(val, list) and val:
  229. self.metadata["bed_temperature"] = int(float(val[0]))
  230. elif isinstance(val, (int, float, str)):
  231. self.metadata["bed_temperature"] = int(float(val))
  232. break
  233. # Nozzle temperature
  234. for key in ["nozzle_temperature_initial_layer", "nozzle_temperature"]:
  235. if key in data:
  236. val = data[key]
  237. if isinstance(val, list) and val:
  238. self.metadata["nozzle_temperature"] = int(float(val[0]))
  239. elif isinstance(val, (int, float, str)):
  240. self.metadata["nozzle_temperature"] = int(float(val))
  241. break
  242. # Printer model (extract and normalize)
  243. if "printer_model" in data:
  244. from backend.app.utils.printer_models import normalize_printer_model
  245. self.metadata["sliced_for_model"] = normalize_printer_model(data["printer_model"])
  246. except Exception:
  247. pass
  248. def _extract_settings_from_content(self, content: str):
  249. """Extract print settings from config content."""
  250. settings_map = {
  251. "layer_height": ("layer_height", float),
  252. "nozzle_diameter": ("nozzle_diameter", float),
  253. "bed_temperature": ("bed_temperature", int),
  254. "nozzle_temperature": ("nozzle_temperature", int),
  255. }
  256. for key, (search_key, converter) in settings_map.items():
  257. if key not in self.metadata:
  258. try:
  259. # Try JSON format
  260. if f'"{search_key}"' in content:
  261. start = content.find(f'"{search_key}"')
  262. value_start = content.find(":", start) + 1
  263. value_end = content.find(",", value_start)
  264. if value_end == -1:
  265. value_end = content.find("}", value_start)
  266. value = content[value_start:value_end].strip().strip('"')
  267. self.metadata[key] = converter(value)
  268. except Exception:
  269. pass
  270. def _parse_3dmodel(self, zf: zipfile.ZipFile):
  271. """Parse 3D/3dmodel.model for MakerWorld metadata."""
  272. import re
  273. try:
  274. model_path = "3D/3dmodel.model"
  275. if model_path not in zf.namelist():
  276. return
  277. content = zf.read(model_path).decode("utf-8", errors="ignore")
  278. # Parse XML metadata elements
  279. # MakerWorld adds metadata like: <metadata name="Designer">username</metadata>
  280. metadata_pattern = r'<metadata\s+name="([^"]+)"[^>]*>([^<]*)</metadata>'
  281. matches = re.findall(metadata_pattern, content)
  282. makerworld_fields = {}
  283. for name, value in matches:
  284. makerworld_fields[name] = value.strip()
  285. # Check for direct MakerWorld URL in content
  286. url_pattern = r'https?://makerworld\.com/[^\s<>"\']+/models/(\d+)'
  287. url_match = re.search(url_pattern, content)
  288. if url_match:
  289. self.metadata["makerworld_url"] = url_match.group(0)
  290. self.metadata["makerworld_model_id"] = url_match.group(1)
  291. # Extract model ID from DSM reference in image URLs
  292. # Format: https://makerworld.bblmw.com/makerworld/model/DSM00000001275614/...
  293. # The numeric part (1275614) is the MakerWorld model ID
  294. if "makerworld_url" not in self.metadata:
  295. dsm_pattern = r"DSM0+(\d+)"
  296. dsm_match = re.search(dsm_pattern, content)
  297. if dsm_match:
  298. model_id = dsm_match.group(1)
  299. self.metadata["makerworld_url"] = f"https://makerworld.com/en/models/{model_id}"
  300. self.metadata["makerworld_model_id"] = model_id
  301. # Store designer info
  302. if "Designer" in makerworld_fields:
  303. self.metadata["designer"] = makerworld_fields["Designer"]
  304. if "Title" in makerworld_fields:
  305. self.metadata["print_name"] = makerworld_fields["Title"]
  306. except Exception:
  307. pass
  308. def _extract_thumbnail(self, zf: zipfile.ZipFile):
  309. """Extract thumbnail image from 3MF.
  310. If a plate_number was specified, try to use that plate's thumbnail first.
  311. """
  312. thumbnail_paths = []
  313. # If a specific plate was printed, try that thumbnail first
  314. if self.plate_number:
  315. thumbnail_paths.append(f"Metadata/plate_{self.plate_number}.png")
  316. # Fallback to default paths
  317. thumbnail_paths.extend(
  318. [
  319. "Metadata/plate_1.png",
  320. "Metadata/thumbnail.png",
  321. "Metadata/model_thumbnail.png",
  322. ]
  323. )
  324. for thumb_path in thumbnail_paths:
  325. if thumb_path in zf.namelist():
  326. self.metadata["_thumbnail_data"] = zf.read(thumb_path)
  327. self.metadata["_thumbnail_ext"] = ".png"
  328. break
  329. def extract_printable_objects_from_3mf(
  330. data: bytes, plate_number: int | None = None, include_positions: bool = False
  331. ) -> dict[int, str] | dict[int, dict] | tuple[dict[int, dict], list | None]:
  332. """Extract printable objects from 3MF file bytes.
  333. This is a lightweight function used during print start to get the list
  334. of objects that can be skipped.
  335. Args:
  336. data: Raw bytes of the 3MF file
  337. plate_number: Which plate was printed (1-based), or None for first plate
  338. include_positions: If True, return tuple of (objects dict, bbox_all)
  339. Returns:
  340. If include_positions=False: Dictionary mapping identify_id (int) to object name (str)
  341. If include_positions=True: Tuple of (dict mapping identify_id to {name, x, y}, bbox_all list or None)
  342. """
  343. import json
  344. from io import BytesIO
  345. printable_objects: dict = {}
  346. bbox_all: list | None = None
  347. try:
  348. with zipfile.ZipFile(BytesIO(data), "r") as zf:
  349. if "Metadata/slice_info.config" not in zf.namelist():
  350. return printable_objects
  351. content = zf.read("Metadata/slice_info.config").decode()
  352. root = ET.fromstring(content)
  353. # Find the correct plate
  354. if plate_number:
  355. plate = root.find(f".//plate[@plate_idx='{plate_number}']")
  356. if plate is None:
  357. plate = root.find(".//plate")
  358. else:
  359. plate = root.find(".//plate")
  360. if plate is None:
  361. return printable_objects
  362. # Get actual plate index from metadata (sliced files only have one plate)
  363. plate_idx = plate_number or 1
  364. for meta in plate.findall("metadata"):
  365. if meta.get("key") == "index":
  366. try:
  367. plate_idx = int(meta.get("value", "1"))
  368. except ValueError:
  369. pass
  370. break
  371. # Load position data from plate_N.json if we need positions
  372. # Build a lookup by name - use list to handle duplicate names
  373. bbox_by_name: dict[str, list[list]] = {}
  374. if include_positions:
  375. plate_json_path = f"Metadata/plate_{plate_idx}.json"
  376. if plate_json_path in zf.namelist():
  377. try:
  378. plate_json = json.loads(zf.read(plate_json_path).decode())
  379. # Get bbox_all - the bounding box of all objects (used for image bounds)
  380. bbox_all = plate_json.get("bbox_all")
  381. for bbox_obj in plate_json.get("bbox_objects", []):
  382. obj_name = bbox_obj.get("name")
  383. bbox = bbox_obj.get("bbox", [])
  384. if obj_name and len(bbox) >= 4:
  385. if obj_name not in bbox_by_name:
  386. bbox_by_name[obj_name] = []
  387. bbox_by_name[obj_name].append(bbox)
  388. except (json.JSONDecodeError, KeyError):
  389. pass
  390. # Extract objects from slice_info.config
  391. for obj in plate.findall("object"):
  392. identify_id = obj.get("identify_id")
  393. name = obj.get("name")
  394. skipped = obj.get("skipped", "false")
  395. if identify_id and name and skipped.lower() != "true":
  396. try:
  397. obj_id = int(identify_id)
  398. if include_positions:
  399. x, y = None, None
  400. # Match by name - pop first bbox to handle duplicates
  401. bboxes = bbox_by_name.get(name)
  402. if bboxes:
  403. bbox = bboxes.pop(0)
  404. # Calculate center from bbox [x_min, y_min, x_max, y_max]
  405. x = (bbox[0] + bbox[2]) / 2
  406. y = (bbox[1] + bbox[3]) / 2
  407. printable_objects[obj_id] = {"name": name, "x": x, "y": y}
  408. else:
  409. printable_objects[obj_id] = name
  410. except ValueError:
  411. pass
  412. except Exception:
  413. pass
  414. if include_positions:
  415. return printable_objects, bbox_all
  416. return printable_objects
  417. class ProjectPageParser:
  418. """Parser for extracting project page data from Bambu Lab 3MF files."""
  419. def __init__(self, file_path: Path):
  420. self.file_path = file_path
  421. def parse(self, archive_id: int) -> dict:
  422. """Extract project page metadata and images from 3MF file."""
  423. import html
  424. import re
  425. result = {
  426. "title": None,
  427. "description": None,
  428. "designer": None,
  429. "designer_user_id": None,
  430. "license": None,
  431. "copyright": None,
  432. "creation_date": None,
  433. "modification_date": None,
  434. "origin": None,
  435. "profile_title": None,
  436. "profile_description": None,
  437. "profile_cover": None,
  438. "profile_user_id": None,
  439. "profile_user_name": None,
  440. "design_model_id": None,
  441. "design_profile_id": None,
  442. "design_region": None,
  443. "model_pictures": [],
  444. "profile_pictures": [],
  445. "thumbnails": [],
  446. }
  447. try:
  448. with zipfile.ZipFile(self.file_path, "r") as zf:
  449. # Parse 3D/3dmodel.model for metadata
  450. model_path = "3D/3dmodel.model"
  451. if model_path in zf.namelist():
  452. content = zf.read(model_path).decode("utf-8", errors="ignore")
  453. # Extract metadata elements using regex
  454. # Format: <metadata name="Key">Value</metadata> or <metadata name="Key" />
  455. metadata_pattern = r'<metadata\s+name="([^"]+)"[^>]*>([^<]*)</metadata>'
  456. matches = re.findall(metadata_pattern, content)
  457. field_mapping = {
  458. "Title": "title",
  459. "Description": "description",
  460. "Designer": "designer",
  461. "DesignerUserId": "designer_user_id",
  462. "License": "license",
  463. "Copyright": "copyright",
  464. "CreationDate": "creation_date",
  465. "ModificationDate": "modification_date",
  466. "Origin": "origin",
  467. "ProfileTitle": "profile_title",
  468. "ProfileDescription": "profile_description",
  469. "ProfileCover": "profile_cover",
  470. "ProfileUserId": "profile_user_id",
  471. "ProfileUserName": "profile_user_name",
  472. "DesignModelId": "design_model_id",
  473. "DesignProfileId": "design_profile_id",
  474. "DesignRegion": "design_region",
  475. }
  476. for name, value in matches:
  477. if name in field_mapping:
  478. # Decode HTML entities multiple times (content is often triple-encoded)
  479. decoded = value.strip()
  480. prev = None
  481. while prev != decoded:
  482. prev = decoded
  483. decoded = html.unescape(decoded)
  484. # Normalize non-breaking spaces to regular spaces
  485. decoded = decoded.replace("\xa0", " ")
  486. result[field_mapping[name]] = decoded if decoded else None
  487. # List images in Auxiliaries folder
  488. from urllib.parse import quote
  489. for name in zf.namelist():
  490. if name.startswith("Auxiliaries/Model Pictures/"):
  491. filename = name.split("/")[-1]
  492. if filename:
  493. result["model_pictures"].append(
  494. {
  495. "name": filename,
  496. "path": name,
  497. "url": f"/api/v1/archives/{archive_id}/project-image/{quote(name, safe='')}",
  498. }
  499. )
  500. elif name.startswith("Auxiliaries/Profile Pictures/"):
  501. filename = name.split("/")[-1]
  502. if filename:
  503. result["profile_pictures"].append(
  504. {
  505. "name": filename,
  506. "path": name,
  507. "url": f"/api/v1/archives/{archive_id}/project-image/{quote(name, safe='')}",
  508. }
  509. )
  510. elif name.startswith("Auxiliaries/.thumbnails/"):
  511. filename = name.split("/")[-1]
  512. if filename:
  513. result["thumbnails"].append(
  514. {
  515. "name": filename,
  516. "path": name,
  517. "url": f"/api/v1/archives/{archive_id}/project-image/{quote(name, safe='')}",
  518. }
  519. )
  520. except Exception as e:
  521. result["_error"] = str(e)
  522. return result
  523. def get_image(self, image_path: str) -> tuple[bytes, str] | None:
  524. """Extract an image from the 3MF file.
  525. Returns tuple of (image_data, content_type) or None if not found.
  526. """
  527. try:
  528. with zipfile.ZipFile(self.file_path, "r") as zf:
  529. if image_path in zf.namelist():
  530. data = zf.read(image_path)
  531. # Determine content type from extension
  532. ext = image_path.lower().split(".")[-1]
  533. content_types = {
  534. "png": "image/png",
  535. "jpg": "image/jpeg",
  536. "jpeg": "image/jpeg",
  537. "webp": "image/webp",
  538. "gif": "image/gif",
  539. }
  540. content_type = content_types.get(ext, "application/octet-stream")
  541. return (data, content_type)
  542. except Exception:
  543. pass
  544. return None
  545. def update_metadata(self, updates: dict) -> bool:
  546. """Update project page metadata in the 3MF file.
  547. Args:
  548. updates: Dict with fields to update (title, description, designer, etc.)
  549. Returns:
  550. True if successful, False otherwise.
  551. """
  552. import html
  553. import re
  554. import tempfile
  555. try:
  556. # Read the 3MF file
  557. with zipfile.ZipFile(self.file_path, "r") as zf_read:
  558. # Find and read the 3dmodel.model file
  559. model_path = "3D/3dmodel.model"
  560. if model_path not in zf_read.namelist():
  561. return False
  562. content = zf_read.read(model_path).decode("utf-8")
  563. # Update metadata fields
  564. field_mapping = {
  565. "title": "Title",
  566. "description": "Description",
  567. "designer": "Designer",
  568. "license": "License",
  569. "copyright": "Copyright",
  570. "profile_title": "ProfileTitle",
  571. "profile_description": "ProfileDescription",
  572. }
  573. for field, xml_name in field_mapping.items():
  574. if field in updates and updates[field] is not None:
  575. new_value = html.escape(updates[field])
  576. # Replace existing metadata or we'd need to add it
  577. pattern = rf'(<metadata\s+name="{xml_name}"[^>]*>)[^<]*(</metadata>)'
  578. replacement = rf"\g<1>{new_value}\g<2>"
  579. content = re.sub(pattern, replacement, content)
  580. # Write to a temporary file first
  581. with tempfile.NamedTemporaryFile(delete=False, suffix=".3mf") as tmp:
  582. tmp_path = Path(tmp.name)
  583. # Create new zip with updated content
  584. with zipfile.ZipFile(tmp_path, "w", zipfile.ZIP_DEFLATED) as zf_write:
  585. for item in zf_read.namelist():
  586. if item == model_path:
  587. zf_write.writestr(item, content.encode("utf-8"))
  588. else:
  589. zf_write.writestr(item, zf_read.read(item))
  590. # Replace original file with updated one
  591. shutil.move(tmp_path, self.file_path)
  592. return True
  593. except Exception:
  594. # Clean up temp file if it exists
  595. if "tmp_path" in locals() and tmp_path.exists():
  596. tmp_path.unlink()
  597. return False
  598. class ArchiveService:
  599. """Service for archiving print jobs."""
  600. def __init__(self, db: AsyncSession):
  601. self.db = db
  602. @staticmethod
  603. def compute_file_hash(file_path: Path) -> str:
  604. """Compute SHA256 hash of a file for duplicate detection."""
  605. sha256 = hashlib.sha256()
  606. with open(file_path, "rb") as f:
  607. # Read in chunks to handle large files
  608. for chunk in iter(lambda: f.read(8192), b""):
  609. sha256.update(chunk)
  610. return sha256.hexdigest()
  611. async def get_duplicate_hashes(self) -> set[str]:
  612. """Get all content hashes that appear more than once.
  613. Returns a set of hashes that have duplicates.
  614. """
  615. from sqlalchemy import func
  616. result = await self.db.execute(
  617. select(PrintArchive.content_hash)
  618. .where(PrintArchive.content_hash.isnot(None))
  619. .group_by(PrintArchive.content_hash)
  620. .having(func.count(PrintArchive.id) > 1)
  621. )
  622. return {row[0] for row in result.all()}
  623. async def find_duplicates(
  624. self,
  625. archive_id: int,
  626. content_hash: str | None = None,
  627. print_name: str | None = None,
  628. makerworld_model_id: str | None = None,
  629. ) -> list[dict]:
  630. """Find duplicate archives based on hash or name matching.
  631. Returns list of dicts with id, print_name, created_at, match_type.
  632. """
  633. duplicates = []
  634. # First, find exact matches by content hash
  635. if content_hash:
  636. result = await self.db.execute(
  637. select(PrintArchive)
  638. .where(
  639. and_(
  640. PrintArchive.content_hash == content_hash,
  641. PrintArchive.id != archive_id,
  642. )
  643. )
  644. .order_by(PrintArchive.created_at.desc())
  645. .limit(10)
  646. )
  647. for archive in result.scalars().all():
  648. duplicates.append(
  649. {
  650. "id": archive.id,
  651. "print_name": archive.print_name,
  652. "created_at": archive.created_at,
  653. "match_type": "exact",
  654. }
  655. )
  656. # Then, find similar matches by print name or MakerWorld ID
  657. if print_name or makerworld_model_id:
  658. conditions = [PrintArchive.id != archive_id]
  659. name_conditions = []
  660. if print_name:
  661. # Match if print names are similar (ignoring case)
  662. name_conditions.append(PrintArchive.print_name.ilike(print_name))
  663. if makerworld_model_id:
  664. # Match by MakerWorld model ID stored in extra_data
  665. # Use json_extract for SQLite compatibility (astext is PostgreSQL-only)
  666. from sqlalchemy import func
  667. name_conditions.append(
  668. func.json_extract(PrintArchive.extra_data, "$.makerworld_model_id") == str(makerworld_model_id)
  669. )
  670. if name_conditions:
  671. conditions.append(or_(*name_conditions))
  672. result = await self.db.execute(
  673. select(PrintArchive).where(and_(*conditions)).order_by(PrintArchive.created_at.desc()).limit(10)
  674. )
  675. for archive in result.scalars().all():
  676. # Don't add if already in duplicates (exact match)
  677. if not any(d["id"] == archive.id for d in duplicates):
  678. duplicates.append(
  679. {
  680. "id": archive.id,
  681. "print_name": archive.print_name,
  682. "created_at": archive.created_at,
  683. "match_type": "similar",
  684. }
  685. )
  686. return duplicates
  687. async def archive_print(
  688. self,
  689. printer_id: int | None,
  690. source_file: Path,
  691. print_data: dict | None = None,
  692. created_by_id: int | None = None,
  693. ) -> PrintArchive | None:
  694. """Archive a 3MF file with metadata.
  695. Args:
  696. printer_id: ID of the printer (optional)
  697. source_file: Path to the 3MF file
  698. print_data: Print data from MQTT (optional)
  699. created_by_id: User ID who created this archive (optional, for user tracking)
  700. """
  701. # Verify printer exists if specified
  702. if printer_id is not None:
  703. result = await self.db.execute(select(Printer).where(Printer.id == printer_id))
  704. printer = result.scalar_one_or_none()
  705. if not printer:
  706. return None
  707. # Create archive directory structure
  708. timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
  709. archive_name = f"{timestamp}_{source_file.stem}"
  710. # Use "unassigned" folder for archives without a printer
  711. printer_folder = str(printer_id) if printer_id is not None else "unassigned"
  712. archive_dir = settings.archive_dir / printer_folder / archive_name
  713. archive_dir.mkdir(parents=True, exist_ok=True)
  714. # Copy 3MF file
  715. dest_file = archive_dir / source_file.name
  716. shutil.copy2(source_file, dest_file)
  717. # Compute content hash for duplicate detection
  718. content_hash = self.compute_file_hash(dest_file)
  719. # Extract plate number from filename (e.g., "plate_5" from "/data/Metadata/plate_5.gcode")
  720. plate_number = None
  721. if print_data:
  722. filename = print_data.get("filename", "")
  723. match = re.search(r"plate_(\d+)", filename)
  724. if match:
  725. plate_number = int(match.group(1))
  726. # Parse 3MF metadata
  727. parser = ThreeMFParser(dest_file, plate_number=plate_number)
  728. metadata = parser.parse()
  729. # Save thumbnail if present
  730. thumbnail_path = None
  731. if "_thumbnail_data" in metadata:
  732. thumb_file = archive_dir / f"thumbnail{metadata['_thumbnail_ext']}"
  733. thumb_file.write_bytes(metadata["_thumbnail_data"])
  734. thumbnail_path = str(thumb_file.relative_to(settings.base_dir))
  735. del metadata["_thumbnail_data"]
  736. del metadata["_thumbnail_ext"]
  737. # Merge with print data from MQTT
  738. if print_data:
  739. metadata["_print_data"] = print_data
  740. # Determine status and timestamps
  741. status = print_data.get("status", "completed") if print_data else "archived"
  742. started_at = datetime.now() if status == "printing" else None
  743. completed_at = datetime.now() if status in ("completed", "failed", "archived") else None
  744. # Calculate cost based on filament usage and type
  745. cost = None
  746. filament_grams = metadata.get("filament_used_grams")
  747. filament_type = metadata.get("filament_type")
  748. if filament_grams and filament_type:
  749. # For multi-material prints, use the first filament type for cost calculation
  750. primary_type = filament_type.split(",")[0].strip()
  751. # Look up filament cost_per_kg from database
  752. filament_result = await self.db.execute(select(Filament).where(Filament.type == primary_type).limit(1))
  753. filament = filament_result.scalar_one_or_none()
  754. if filament:
  755. cost = round((filament_grams / 1000) * filament.cost_per_kg, 2)
  756. else:
  757. # Use default filament cost from settings
  758. from backend.app.api.routes.settings import get_setting
  759. default_cost_setting = await get_setting(self.db, "default_filament_cost")
  760. default_cost_per_kg = float(default_cost_setting) if default_cost_setting else 25.0
  761. cost = round((filament_grams / 1000) * default_cost_per_kg, 2)
  762. # Calculate quantity from printable objects count
  763. # printable_objects is a dict of {identify_id: name} for non-skipped objects
  764. quantity = 1 # Default to 1
  765. printable_objects = metadata.get("printable_objects")
  766. if printable_objects and isinstance(printable_objects, dict):
  767. quantity = len(printable_objects)
  768. logger.debug(f"Auto-detected {quantity} parts from 3MF printable objects")
  769. # Create archive record
  770. archive = PrintArchive(
  771. printer_id=printer_id,
  772. filename=source_file.name,
  773. file_path=str(dest_file.relative_to(settings.base_dir)),
  774. file_size=dest_file.stat().st_size,
  775. content_hash=content_hash,
  776. thumbnail_path=thumbnail_path,
  777. print_name=metadata.get("print_name") or source_file.stem,
  778. print_time_seconds=metadata.get("print_time_seconds"),
  779. filament_used_grams=metadata.get("filament_used_grams"),
  780. filament_type=metadata.get("filament_type"),
  781. filament_color=metadata.get("filament_color"),
  782. layer_height=metadata.get("layer_height"),
  783. total_layers=metadata.get("total_layers"),
  784. nozzle_diameter=metadata.get("nozzle_diameter"),
  785. bed_temperature=metadata.get("bed_temperature"),
  786. nozzle_temperature=metadata.get("nozzle_temperature"),
  787. sliced_for_model=metadata.get("sliced_for_model"),
  788. makerworld_url=metadata.get("makerworld_url"),
  789. designer=metadata.get("designer"),
  790. status=status,
  791. started_at=started_at,
  792. completed_at=completed_at,
  793. cost=cost,
  794. quantity=quantity,
  795. extra_data=metadata,
  796. created_by_id=created_by_id,
  797. )
  798. self.db.add(archive)
  799. await self.db.commit()
  800. await self.db.refresh(archive)
  801. return archive
  802. async def get_archive(self, archive_id: int) -> PrintArchive | None:
  803. """Get an archive by ID with creator loaded."""
  804. from sqlalchemy.orm import selectinload
  805. result = await self.db.execute(
  806. select(PrintArchive).options(selectinload(PrintArchive.created_by)).where(PrintArchive.id == archive_id)
  807. )
  808. return result.scalar_one_or_none()
  809. async def update_archive_status(
  810. self,
  811. archive_id: int,
  812. status: str,
  813. completed_at: datetime | None = None,
  814. failure_reason: str | None = None,
  815. ) -> bool:
  816. """Update the status of an archive."""
  817. archive = await self.get_archive(archive_id)
  818. if not archive:
  819. return False
  820. archive.status = status
  821. if completed_at:
  822. archive.completed_at = completed_at
  823. if failure_reason:
  824. archive.failure_reason = failure_reason
  825. await self.db.commit()
  826. return True
  827. async def add_reprint_cost(self, archive_id: int) -> bool:
  828. """Add cost for a reprint to the existing archive cost."""
  829. archive = await self.get_archive(archive_id)
  830. if not archive:
  831. return False
  832. if not archive.filament_used_grams or not archive.filament_type:
  833. return False
  834. # Calculate cost based on filament type or default
  835. from backend.app.api.routes.settings import get_setting
  836. primary_type = archive.filament_type.split(",")[0].strip()
  837. # Look up filament cost_per_kg from database
  838. filament_result = await self.db.execute(select(Filament).where(Filament.type == primary_type).limit(1))
  839. filament = filament_result.scalar_one_or_none()
  840. if filament:
  841. cost_per_kg = filament.cost_per_kg
  842. else:
  843. # Use default filament cost from settings
  844. default_cost_setting = await get_setting(self.db, "default_filament_cost")
  845. cost_per_kg = float(default_cost_setting) if default_cost_setting else 25.0
  846. additional_cost = round((archive.filament_used_grams / 1000) * cost_per_kg, 2)
  847. # Add to existing cost (or set if None)
  848. if archive.cost is None:
  849. archive.cost = additional_cost
  850. else:
  851. archive.cost = round(archive.cost + additional_cost, 2)
  852. await self.db.commit()
  853. logger.info(f"Added reprint cost {additional_cost} to archive {archive_id}, new total: {archive.cost}")
  854. return True
  855. async def list_archives(
  856. self,
  857. printer_id: int | None = None,
  858. project_id: int | None = None,
  859. limit: int = 50,
  860. offset: int = 0,
  861. ) -> list[PrintArchive]:
  862. """List archives with optional filtering."""
  863. from sqlalchemy.orm import selectinload
  864. query = (
  865. select(PrintArchive)
  866. .options(selectinload(PrintArchive.project), selectinload(PrintArchive.created_by))
  867. .order_by(PrintArchive.created_at.desc())
  868. )
  869. if printer_id:
  870. query = query.where(PrintArchive.printer_id == printer_id)
  871. if project_id:
  872. query = query.where(PrintArchive.project_id == project_id)
  873. query = query.limit(limit).offset(offset)
  874. result = await self.db.execute(query)
  875. return list(result.scalars().all())
  876. async def delete_archive(self, archive_id: int) -> bool:
  877. """Delete an archive and its files."""
  878. archive = await self.get_archive(archive_id)
  879. if not archive:
  880. return False
  881. # Delete files - with CRITICAL safety checks to prevent accidental deletion
  882. # of parent directories (e.g., /opt) if file_path is empty/malformed
  883. if archive.file_path and archive.file_path.strip():
  884. file_path = settings.base_dir / archive.file_path
  885. if file_path.exists():
  886. archive_dir = file_path.parent
  887. # Safety check 1: archive_dir must be inside archive_dir
  888. try:
  889. archive_dir.resolve().relative_to(settings.archive_dir.resolve())
  890. except ValueError:
  891. logger.error(
  892. f"SECURITY: Refusing to delete archive {archive_id} - "
  893. f"path {archive_dir} is outside archive directory {settings.archive_dir}"
  894. )
  895. # Still delete the database record, just not the files
  896. await self.db.delete(archive)
  897. await self.db.commit()
  898. return True
  899. # Safety check 2: archive_dir must be at least 1 level deep inside archive_dir
  900. # (should be archive_dir/uuid/file.3mf, so parent should be archive_dir/uuid)
  901. try:
  902. relative_path = archive_dir.resolve().relative_to(settings.archive_dir.resolve())
  903. if len(relative_path.parts) < 1:
  904. logger.error(
  905. f"SECURITY: Refusing to delete archive {archive_id} - "
  906. f"path {archive_dir} is not deep enough inside archive directory"
  907. )
  908. await self.db.delete(archive)
  909. await self.db.commit()
  910. return True
  911. except ValueError:
  912. pass # Already handled above
  913. shutil.rmtree(archive_dir, ignore_errors=True)
  914. else:
  915. logger.error(
  916. f"SECURITY: Refusing to delete files for archive {archive_id} - "
  917. f"file_path is empty or invalid: '{archive.file_path}'"
  918. )
  919. # Delete database record
  920. await self.db.delete(archive)
  921. await self.db.commit()
  922. return True
  923. async def attach_timelapse(
  924. self,
  925. archive_id: int,
  926. timelapse_data: bytes,
  927. filename: str = "timelapse.mp4",
  928. ) -> bool:
  929. """Attach a timelapse video to an archive."""
  930. import asyncio
  931. archive = await self.get_archive(archive_id)
  932. if not archive:
  933. return False
  934. # Get archive directory
  935. file_path = settings.base_dir / archive.file_path
  936. archive_dir = file_path.parent
  937. # Save timelapse - use thread pool to avoid blocking event loop
  938. # (timelapse files can be 100MB+, sync write blocks for seconds)
  939. timelapse_file = archive_dir / filename
  940. await asyncio.to_thread(timelapse_file.write_bytes, timelapse_data)
  941. # Update archive record
  942. archive.timelapse_path = str(timelapse_file.relative_to(settings.base_dir))
  943. await self.db.commit()
  944. return True