archive.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. import hashlib
  2. import json
  3. import re
  4. import shutil
  5. import zipfile
  6. from datetime import datetime
  7. from pathlib import Path
  8. from xml.etree import ElementTree as ET
  9. from sqlalchemy import and_, or_, select
  10. from sqlalchemy.ext.asyncio import AsyncSession
  11. from backend.app.core.config import settings
  12. from backend.app.models.archive import PrintArchive
  13. from backend.app.models.filament import Filament
  14. from backend.app.models.printer import Printer
  15. class ThreeMFParser:
  16. """Parser for Bambu Lab 3MF files."""
  17. def __init__(self, file_path: Path, plate_number: int | None = None):
  18. self.file_path = file_path
  19. self.plate_number = plate_number # Which plate was printed (1, 2, 3, etc.)
  20. self.metadata: dict = {}
  21. def parse(self) -> dict:
  22. """Extract metadata from 3MF file."""
  23. try:
  24. with zipfile.ZipFile(self.file_path, "r") as zf:
  25. self._parse_slice_info(zf)
  26. self._parse_project_settings(zf)
  27. self._parse_gcode_header(zf)
  28. self._parse_3dmodel(zf)
  29. self._extract_thumbnail(zf)
  30. # ALWAYS prefer slice_info values - they contain ONLY filaments actually used in print
  31. # project_settings contains ALL configured filaments (AMS slots), not just used ones
  32. if self.metadata.get("_slice_filament_type"):
  33. self.metadata["filament_type"] = self.metadata["_slice_filament_type"]
  34. if self.metadata.get("_slice_filament_color"):
  35. self.metadata["filament_color"] = self.metadata["_slice_filament_color"]
  36. # Clean up internal keys
  37. self.metadata.pop("_slice_filament_type", None)
  38. self.metadata.pop("_slice_filament_color", None)
  39. except Exception:
  40. pass
  41. return self.metadata
  42. def _parse_slice_info(self, zf: zipfile.ZipFile):
  43. """Parse slice_info.config for print settings."""
  44. try:
  45. if "Metadata/slice_info.config" in zf.namelist():
  46. content = zf.read("Metadata/slice_info.config").decode()
  47. root = ET.fromstring(content)
  48. # Get first plate's metadata
  49. plate = root.find(".//plate")
  50. if plate is not None:
  51. # Get prediction and weight from metadata elements
  52. for meta in plate.findall("metadata"):
  53. key = meta.get("key")
  54. value = meta.get("value")
  55. if key == "prediction" and value:
  56. self.metadata["print_time_seconds"] = int(value)
  57. elif key == "weight" and value:
  58. self.metadata["filament_used_grams"] = float(value)
  59. # Get filament info from filaments ACTUALLY USED in the print
  60. # slice_info has <filament id="1" type="PLA" color="#FFFFFF" used_g="100" />
  61. # Only include filaments where used_g > 0
  62. filaments = root.findall(".//filament")
  63. if filaments:
  64. # Collect unique filament types and colors for filaments that are actually used
  65. types = []
  66. colors = []
  67. for f in filaments:
  68. # Check if this filament is actually used in the print
  69. used_g = f.get("used_g", "0")
  70. try:
  71. used_amount = float(used_g)
  72. except (ValueError, TypeError):
  73. used_amount = 0
  74. # Only include if used_g > 0 (filament is actually consumed)
  75. if used_amount > 0:
  76. ftype = f.get("type")
  77. fcolor = f.get("color")
  78. if ftype and ftype not in types:
  79. types.append(ftype)
  80. if fcolor and fcolor not in colors:
  81. colors.append(fcolor)
  82. if types:
  83. self.metadata["_slice_filament_type"] = ", ".join(types)
  84. if colors:
  85. self.metadata["_slice_filament_color"] = ",".join(colors)
  86. except Exception:
  87. pass
  88. def _parse_project_settings(self, zf: zipfile.ZipFile):
  89. """Parse project settings for print configuration."""
  90. try:
  91. if "Metadata/project_settings.config" in zf.namelist():
  92. content = zf.read("Metadata/project_settings.config").decode()
  93. try:
  94. data = json.loads(content)
  95. self._extract_filament_info(data)
  96. self._extract_print_settings(data)
  97. except json.JSONDecodeError:
  98. pass
  99. except Exception:
  100. pass
  101. def _parse_gcode_header(self, zf: zipfile.ZipFile):
  102. """Parse G-code file header for total layer count."""
  103. import re
  104. try:
  105. # Look for plate_1.gcode or similar
  106. gcode_files = [f for f in zf.namelist() if f.endswith(".gcode")]
  107. if not gcode_files:
  108. return
  109. # Read first 2KB of G-code (header contains the layer count)
  110. gcode_path = gcode_files[0]
  111. with zf.open(gcode_path) as f:
  112. header = f.read(2048).decode("utf-8", errors="ignore")
  113. # Look for "; total layer number: XX" pattern
  114. match = re.search(r";\s*total\s+layer\s+number[:\s]+(\d+)", header, re.IGNORECASE)
  115. if match:
  116. self.metadata["total_layers"] = int(match.group(1))
  117. except Exception:
  118. pass
  119. def _extract_filament_info(self, data: dict):
  120. """Extract filament info, preferring non-support filaments."""
  121. try:
  122. filament_types = data.get("filament_type", [])
  123. filament_colors = data.get("filament_colour", [])
  124. filament_is_support = data.get("filament_is_support", [])
  125. if not filament_types:
  126. return
  127. # Collect all non-support filaments
  128. non_support_types = []
  129. non_support_colors = []
  130. for i, ftype in enumerate(filament_types):
  131. is_support = filament_is_support[i] if i < len(filament_is_support) else "0"
  132. if is_support == "0":
  133. if ftype and ftype not in non_support_types:
  134. non_support_types.append(ftype)
  135. if i < len(filament_colors) and filament_colors[i]:
  136. color = filament_colors[i]
  137. if color not in non_support_colors:
  138. non_support_colors.append(color)
  139. # Fallback to first filament if all are support
  140. if not non_support_types and filament_types:
  141. non_support_types = [filament_types[0]]
  142. if not non_support_colors and filament_colors:
  143. non_support_colors = [filament_colors[0]]
  144. # Store filament type(s)
  145. if non_support_types:
  146. self.metadata["filament_type"] = ", ".join(non_support_types)
  147. # Store all colors as comma-separated (for multi-color display)
  148. if non_support_colors:
  149. self.metadata["filament_color"] = ",".join(non_support_colors)
  150. except Exception:
  151. pass
  152. def _extract_print_settings(self, data: dict):
  153. """Extract print settings from JSON config."""
  154. try:
  155. # Layer height - usually an array, get first value
  156. if "layer_height" in data:
  157. val = data["layer_height"]
  158. if isinstance(val, list) and val:
  159. self.metadata["layer_height"] = float(val[0])
  160. elif isinstance(val, (int, float, str)):
  161. self.metadata["layer_height"] = float(val)
  162. # Nozzle diameter
  163. if "nozzle_diameter" in data:
  164. val = data["nozzle_diameter"]
  165. if isinstance(val, list) and val:
  166. self.metadata["nozzle_diameter"] = float(val[0])
  167. elif isinstance(val, (int, float, str)):
  168. self.metadata["nozzle_diameter"] = float(val)
  169. # Bed temperature - first layer or regular
  170. for key in ["bed_temperature_initial_layer", "bed_temperature"]:
  171. if key in data:
  172. val = data[key]
  173. if isinstance(val, list) and val:
  174. self.metadata["bed_temperature"] = int(float(val[0]))
  175. elif isinstance(val, (int, float, str)):
  176. self.metadata["bed_temperature"] = int(float(val))
  177. break
  178. # Nozzle temperature
  179. for key in ["nozzle_temperature_initial_layer", "nozzle_temperature"]:
  180. if key in data:
  181. val = data[key]
  182. if isinstance(val, list) and val:
  183. self.metadata["nozzle_temperature"] = int(float(val[0]))
  184. elif isinstance(val, (int, float, str)):
  185. self.metadata["nozzle_temperature"] = int(float(val))
  186. break
  187. except Exception:
  188. pass
  189. def _extract_settings_from_content(self, content: str):
  190. """Extract print settings from config content."""
  191. settings_map = {
  192. "layer_height": ("layer_height", float),
  193. "nozzle_diameter": ("nozzle_diameter", float),
  194. "bed_temperature": ("bed_temperature", int),
  195. "nozzle_temperature": ("nozzle_temperature", int),
  196. }
  197. for key, (search_key, converter) in settings_map.items():
  198. if key not in self.metadata:
  199. try:
  200. # Try JSON format
  201. if f'"{search_key}"' in content:
  202. start = content.find(f'"{search_key}"')
  203. value_start = content.find(":", start) + 1
  204. value_end = content.find(",", value_start)
  205. if value_end == -1:
  206. value_end = content.find("}", value_start)
  207. value = content[value_start:value_end].strip().strip('"')
  208. self.metadata[key] = converter(value)
  209. except Exception:
  210. pass
  211. def _parse_3dmodel(self, zf: zipfile.ZipFile):
  212. """Parse 3D/3dmodel.model for MakerWorld metadata."""
  213. import re
  214. try:
  215. model_path = "3D/3dmodel.model"
  216. if model_path not in zf.namelist():
  217. return
  218. content = zf.read(model_path).decode("utf-8", errors="ignore")
  219. # Parse XML metadata elements
  220. # MakerWorld adds metadata like: <metadata name="Designer">username</metadata>
  221. metadata_pattern = r'<metadata\s+name="([^"]+)"[^>]*>([^<]*)</metadata>'
  222. matches = re.findall(metadata_pattern, content)
  223. makerworld_fields = {}
  224. for name, value in matches:
  225. makerworld_fields[name] = value.strip()
  226. # Check for direct MakerWorld URL in content
  227. url_pattern = r'https?://makerworld\.com/[^\s<>"\']+/models/(\d+)'
  228. url_match = re.search(url_pattern, content)
  229. if url_match:
  230. self.metadata["makerworld_url"] = url_match.group(0)
  231. self.metadata["makerworld_model_id"] = url_match.group(1)
  232. # Extract model ID from DSM reference in image URLs
  233. # Format: https://makerworld.bblmw.com/makerworld/model/DSM00000001275614/...
  234. # The numeric part (1275614) is the MakerWorld model ID
  235. if "makerworld_url" not in self.metadata:
  236. dsm_pattern = r"DSM0+(\d+)"
  237. dsm_match = re.search(dsm_pattern, content)
  238. if dsm_match:
  239. model_id = dsm_match.group(1)
  240. self.metadata["makerworld_url"] = f"https://makerworld.com/en/models/{model_id}"
  241. self.metadata["makerworld_model_id"] = model_id
  242. # Store designer info
  243. if "Designer" in makerworld_fields:
  244. self.metadata["designer"] = makerworld_fields["Designer"]
  245. if "Title" in makerworld_fields:
  246. self.metadata["print_name"] = makerworld_fields["Title"]
  247. except Exception:
  248. pass
  249. def _extract_thumbnail(self, zf: zipfile.ZipFile):
  250. """Extract thumbnail image from 3MF.
  251. If a plate_number was specified, try to use that plate's thumbnail first.
  252. """
  253. thumbnail_paths = []
  254. # If a specific plate was printed, try that thumbnail first
  255. if self.plate_number:
  256. thumbnail_paths.append(f"Metadata/plate_{self.plate_number}.png")
  257. # Fallback to default paths
  258. thumbnail_paths.extend(
  259. [
  260. "Metadata/plate_1.png",
  261. "Metadata/thumbnail.png",
  262. "Metadata/model_thumbnail.png",
  263. ]
  264. )
  265. for thumb_path in thumbnail_paths:
  266. if thumb_path in zf.namelist():
  267. self.metadata["_thumbnail_data"] = zf.read(thumb_path)
  268. self.metadata["_thumbnail_ext"] = ".png"
  269. break
  270. class ProjectPageParser:
  271. """Parser for extracting project page data from Bambu Lab 3MF files."""
  272. def __init__(self, file_path: Path):
  273. self.file_path = file_path
  274. def parse(self, archive_id: int) -> dict:
  275. """Extract project page metadata and images from 3MF file."""
  276. import html
  277. import re
  278. result = {
  279. "title": None,
  280. "description": None,
  281. "designer": None,
  282. "designer_user_id": None,
  283. "license": None,
  284. "copyright": None,
  285. "creation_date": None,
  286. "modification_date": None,
  287. "origin": None,
  288. "profile_title": None,
  289. "profile_description": None,
  290. "profile_cover": None,
  291. "profile_user_id": None,
  292. "profile_user_name": None,
  293. "design_model_id": None,
  294. "design_profile_id": None,
  295. "design_region": None,
  296. "model_pictures": [],
  297. "profile_pictures": [],
  298. "thumbnails": [],
  299. }
  300. try:
  301. with zipfile.ZipFile(self.file_path, "r") as zf:
  302. # Parse 3D/3dmodel.model for metadata
  303. model_path = "3D/3dmodel.model"
  304. if model_path in zf.namelist():
  305. content = zf.read(model_path).decode("utf-8", errors="ignore")
  306. # Extract metadata elements using regex
  307. # Format: <metadata name="Key">Value</metadata> or <metadata name="Key" />
  308. metadata_pattern = r'<metadata\s+name="([^"]+)"[^>]*>([^<]*)</metadata>'
  309. matches = re.findall(metadata_pattern, content)
  310. field_mapping = {
  311. "Title": "title",
  312. "Description": "description",
  313. "Designer": "designer",
  314. "DesignerUserId": "designer_user_id",
  315. "License": "license",
  316. "Copyright": "copyright",
  317. "CreationDate": "creation_date",
  318. "ModificationDate": "modification_date",
  319. "Origin": "origin",
  320. "ProfileTitle": "profile_title",
  321. "ProfileDescription": "profile_description",
  322. "ProfileCover": "profile_cover",
  323. "ProfileUserId": "profile_user_id",
  324. "ProfileUserName": "profile_user_name",
  325. "DesignModelId": "design_model_id",
  326. "DesignProfileId": "design_profile_id",
  327. "DesignRegion": "design_region",
  328. }
  329. for name, value in matches:
  330. if name in field_mapping:
  331. # Decode HTML entities multiple times (content is often triple-encoded)
  332. decoded = value.strip()
  333. prev = None
  334. while prev != decoded:
  335. prev = decoded
  336. decoded = html.unescape(decoded)
  337. # Normalize non-breaking spaces to regular spaces
  338. decoded = decoded.replace("\xa0", " ")
  339. result[field_mapping[name]] = decoded if decoded else None
  340. # List images in Auxiliaries folder
  341. from urllib.parse import quote
  342. for name in zf.namelist():
  343. if name.startswith("Auxiliaries/Model Pictures/"):
  344. filename = name.split("/")[-1]
  345. if filename:
  346. result["model_pictures"].append(
  347. {
  348. "name": filename,
  349. "path": name,
  350. "url": f"/api/v1/archives/{archive_id}/project-image/{quote(name, safe='')}",
  351. }
  352. )
  353. elif name.startswith("Auxiliaries/Profile Pictures/"):
  354. filename = name.split("/")[-1]
  355. if filename:
  356. result["profile_pictures"].append(
  357. {
  358. "name": filename,
  359. "path": name,
  360. "url": f"/api/v1/archives/{archive_id}/project-image/{quote(name, safe='')}",
  361. }
  362. )
  363. elif name.startswith("Auxiliaries/.thumbnails/"):
  364. filename = name.split("/")[-1]
  365. if filename:
  366. result["thumbnails"].append(
  367. {
  368. "name": filename,
  369. "path": name,
  370. "url": f"/api/v1/archives/{archive_id}/project-image/{quote(name, safe='')}",
  371. }
  372. )
  373. except Exception as e:
  374. result["_error"] = str(e)
  375. return result
  376. def get_image(self, image_path: str) -> tuple[bytes, str] | None:
  377. """Extract an image from the 3MF file.
  378. Returns tuple of (image_data, content_type) or None if not found.
  379. """
  380. try:
  381. with zipfile.ZipFile(self.file_path, "r") as zf:
  382. if image_path in zf.namelist():
  383. data = zf.read(image_path)
  384. # Determine content type from extension
  385. ext = image_path.lower().split(".")[-1]
  386. content_types = {
  387. "png": "image/png",
  388. "jpg": "image/jpeg",
  389. "jpeg": "image/jpeg",
  390. "webp": "image/webp",
  391. "gif": "image/gif",
  392. }
  393. content_type = content_types.get(ext, "application/octet-stream")
  394. return (data, content_type)
  395. except Exception:
  396. pass
  397. return None
  398. def update_metadata(self, updates: dict) -> bool:
  399. """Update project page metadata in the 3MF file.
  400. Args:
  401. updates: Dict with fields to update (title, description, designer, etc.)
  402. Returns:
  403. True if successful, False otherwise.
  404. """
  405. import html
  406. import re
  407. import tempfile
  408. try:
  409. # Read the 3MF file
  410. with zipfile.ZipFile(self.file_path, "r") as zf_read:
  411. # Find and read the 3dmodel.model file
  412. model_path = "3D/3dmodel.model"
  413. if model_path not in zf_read.namelist():
  414. return False
  415. content = zf_read.read(model_path).decode("utf-8")
  416. # Update metadata fields
  417. field_mapping = {
  418. "title": "Title",
  419. "description": "Description",
  420. "designer": "Designer",
  421. "license": "License",
  422. "copyright": "Copyright",
  423. "profile_title": "ProfileTitle",
  424. "profile_description": "ProfileDescription",
  425. }
  426. for field, xml_name in field_mapping.items():
  427. if field in updates and updates[field] is not None:
  428. new_value = html.escape(updates[field])
  429. # Replace existing metadata or we'd need to add it
  430. pattern = rf'(<metadata\s+name="{xml_name}"[^>]*>)[^<]*(</metadata>)'
  431. replacement = rf"\g<1>{new_value}\g<2>"
  432. content = re.sub(pattern, replacement, content)
  433. # Write to a temporary file first
  434. with tempfile.NamedTemporaryFile(delete=False, suffix=".3mf") as tmp:
  435. tmp_path = Path(tmp.name)
  436. # Create new zip with updated content
  437. with zipfile.ZipFile(tmp_path, "w", zipfile.ZIP_DEFLATED) as zf_write:
  438. for item in zf_read.namelist():
  439. if item == model_path:
  440. zf_write.writestr(item, content.encode("utf-8"))
  441. else:
  442. zf_write.writestr(item, zf_read.read(item))
  443. # Replace original file with updated one
  444. shutil.move(tmp_path, self.file_path)
  445. return True
  446. except Exception:
  447. # Clean up temp file if it exists
  448. if "tmp_path" in locals() and tmp_path.exists():
  449. tmp_path.unlink()
  450. return False
  451. class ArchiveService:
  452. """Service for archiving print jobs."""
  453. def __init__(self, db: AsyncSession):
  454. self.db = db
  455. @staticmethod
  456. def compute_file_hash(file_path: Path) -> str:
  457. """Compute SHA256 hash of a file for duplicate detection."""
  458. sha256 = hashlib.sha256()
  459. with open(file_path, "rb") as f:
  460. # Read in chunks to handle large files
  461. for chunk in iter(lambda: f.read(8192), b""):
  462. sha256.update(chunk)
  463. return sha256.hexdigest()
  464. async def get_duplicate_hashes(self) -> set[str]:
  465. """Get all content hashes that appear more than once.
  466. Returns a set of hashes that have duplicates.
  467. """
  468. from sqlalchemy import func
  469. result = await self.db.execute(
  470. select(PrintArchive.content_hash)
  471. .where(PrintArchive.content_hash.isnot(None))
  472. .group_by(PrintArchive.content_hash)
  473. .having(func.count(PrintArchive.id) > 1)
  474. )
  475. return {row[0] for row in result.all()}
  476. async def find_duplicates(
  477. self,
  478. archive_id: int,
  479. content_hash: str | None = None,
  480. print_name: str | None = None,
  481. makerworld_model_id: str | None = None,
  482. ) -> list[dict]:
  483. """Find duplicate archives based on hash or name matching.
  484. Returns list of dicts with id, print_name, created_at, match_type.
  485. """
  486. duplicates = []
  487. # First, find exact matches by content hash
  488. if content_hash:
  489. result = await self.db.execute(
  490. select(PrintArchive)
  491. .where(
  492. and_(
  493. PrintArchive.content_hash == content_hash,
  494. PrintArchive.id != archive_id,
  495. )
  496. )
  497. .order_by(PrintArchive.created_at.desc())
  498. .limit(10)
  499. )
  500. for archive in result.scalars().all():
  501. duplicates.append(
  502. {
  503. "id": archive.id,
  504. "print_name": archive.print_name,
  505. "created_at": archive.created_at,
  506. "match_type": "exact",
  507. }
  508. )
  509. # Then, find similar matches by print name or MakerWorld ID
  510. if print_name or makerworld_model_id:
  511. conditions = [PrintArchive.id != archive_id]
  512. name_conditions = []
  513. if print_name:
  514. # Match if print names are similar (ignoring case)
  515. name_conditions.append(PrintArchive.print_name.ilike(print_name))
  516. if makerworld_model_id:
  517. # Match by MakerWorld model ID stored in extra_data
  518. # Use json_extract for SQLite compatibility (astext is PostgreSQL-only)
  519. from sqlalchemy import func
  520. name_conditions.append(
  521. func.json_extract(PrintArchive.extra_data, "$.makerworld_model_id") == str(makerworld_model_id)
  522. )
  523. if name_conditions:
  524. conditions.append(or_(*name_conditions))
  525. result = await self.db.execute(
  526. select(PrintArchive).where(and_(*conditions)).order_by(PrintArchive.created_at.desc()).limit(10)
  527. )
  528. for archive in result.scalars().all():
  529. # Don't add if already in duplicates (exact match)
  530. if not any(d["id"] == archive.id for d in duplicates):
  531. duplicates.append(
  532. {
  533. "id": archive.id,
  534. "print_name": archive.print_name,
  535. "created_at": archive.created_at,
  536. "match_type": "similar",
  537. }
  538. )
  539. return duplicates
  540. async def archive_print(
  541. self,
  542. printer_id: int | None,
  543. source_file: Path,
  544. print_data: dict | None = None,
  545. ) -> PrintArchive | None:
  546. """Archive a 3MF file with metadata."""
  547. # Verify printer exists if specified
  548. if printer_id is not None:
  549. result = await self.db.execute(select(Printer).where(Printer.id == printer_id))
  550. printer = result.scalar_one_or_none()
  551. if not printer:
  552. return None
  553. # Create archive directory structure
  554. timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
  555. archive_name = f"{timestamp}_{source_file.stem}"
  556. # Use "unassigned" folder for archives without a printer
  557. printer_folder = str(printer_id) if printer_id is not None else "unassigned"
  558. archive_dir = settings.archive_dir / printer_folder / archive_name
  559. archive_dir.mkdir(parents=True, exist_ok=True)
  560. # Copy 3MF file
  561. dest_file = archive_dir / source_file.name
  562. shutil.copy2(source_file, dest_file)
  563. # Compute content hash for duplicate detection
  564. content_hash = self.compute_file_hash(dest_file)
  565. # Extract plate number from filename (e.g., "plate_5" from "/data/Metadata/plate_5.gcode")
  566. plate_number = None
  567. if print_data:
  568. filename = print_data.get("filename", "")
  569. match = re.search(r"plate_(\d+)", filename)
  570. if match:
  571. plate_number = int(match.group(1))
  572. # Parse 3MF metadata
  573. parser = ThreeMFParser(dest_file, plate_number=plate_number)
  574. metadata = parser.parse()
  575. # Save thumbnail if present
  576. thumbnail_path = None
  577. if "_thumbnail_data" in metadata:
  578. thumb_file = archive_dir / f"thumbnail{metadata['_thumbnail_ext']}"
  579. thumb_file.write_bytes(metadata["_thumbnail_data"])
  580. thumbnail_path = str(thumb_file.relative_to(settings.base_dir))
  581. del metadata["_thumbnail_data"]
  582. del metadata["_thumbnail_ext"]
  583. # Merge with print data from MQTT
  584. if print_data:
  585. metadata["_print_data"] = print_data
  586. # Determine status and timestamps
  587. status = print_data.get("status", "completed") if print_data else "archived"
  588. started_at = datetime.now() if status == "printing" else None
  589. completed_at = datetime.now() if status in ("completed", "failed", "archived") else None
  590. # Calculate cost based on filament usage and type
  591. cost = None
  592. filament_grams = metadata.get("filament_used_grams")
  593. filament_type = metadata.get("filament_type")
  594. if filament_grams and filament_type:
  595. # For multi-material prints, use the first filament type for cost calculation
  596. primary_type = filament_type.split(",")[0].strip()
  597. # Look up filament cost_per_kg from database
  598. filament_result = await self.db.execute(select(Filament).where(Filament.type == primary_type).limit(1))
  599. filament = filament_result.scalar_one_or_none()
  600. if filament:
  601. cost = round((filament_grams / 1000) * filament.cost_per_kg, 2)
  602. else:
  603. # Default cost_per_kg if filament type not found
  604. default_cost_per_kg = 25.0
  605. cost = round((filament_grams / 1000) * default_cost_per_kg, 2)
  606. # Create archive record
  607. archive = PrintArchive(
  608. printer_id=printer_id,
  609. filename=source_file.name,
  610. file_path=str(dest_file.relative_to(settings.base_dir)),
  611. file_size=dest_file.stat().st_size,
  612. content_hash=content_hash,
  613. thumbnail_path=thumbnail_path,
  614. print_name=metadata.get("print_name") or source_file.stem,
  615. print_time_seconds=metadata.get("print_time_seconds"),
  616. filament_used_grams=metadata.get("filament_used_grams"),
  617. filament_type=metadata.get("filament_type"),
  618. filament_color=metadata.get("filament_color"),
  619. layer_height=metadata.get("layer_height"),
  620. total_layers=metadata.get("total_layers"),
  621. nozzle_diameter=metadata.get("nozzle_diameter"),
  622. bed_temperature=metadata.get("bed_temperature"),
  623. nozzle_temperature=metadata.get("nozzle_temperature"),
  624. makerworld_url=metadata.get("makerworld_url"),
  625. designer=metadata.get("designer"),
  626. status=status,
  627. started_at=started_at,
  628. completed_at=completed_at,
  629. cost=cost,
  630. extra_data=metadata,
  631. )
  632. self.db.add(archive)
  633. await self.db.commit()
  634. await self.db.refresh(archive)
  635. return archive
  636. async def get_archive(self, archive_id: int) -> PrintArchive | None:
  637. """Get an archive by ID."""
  638. result = await self.db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  639. return result.scalar_one_or_none()
  640. async def update_archive_status(
  641. self,
  642. archive_id: int,
  643. status: str,
  644. completed_at: datetime | None = None,
  645. failure_reason: str | None = None,
  646. ) -> bool:
  647. """Update the status of an archive."""
  648. archive = await self.get_archive(archive_id)
  649. if not archive:
  650. return False
  651. archive.status = status
  652. if completed_at:
  653. archive.completed_at = completed_at
  654. if failure_reason:
  655. archive.failure_reason = failure_reason
  656. await self.db.commit()
  657. return True
  658. async def list_archives(
  659. self,
  660. printer_id: int | None = None,
  661. project_id: int | None = None,
  662. limit: int = 50,
  663. offset: int = 0,
  664. ) -> list[PrintArchive]:
  665. """List archives with optional filtering."""
  666. from sqlalchemy.orm import selectinload
  667. query = (
  668. select(PrintArchive).options(selectinload(PrintArchive.project)).order_by(PrintArchive.created_at.desc())
  669. )
  670. if printer_id:
  671. query = query.where(PrintArchive.printer_id == printer_id)
  672. if project_id:
  673. query = query.where(PrintArchive.project_id == project_id)
  674. query = query.limit(limit).offset(offset)
  675. result = await self.db.execute(query)
  676. return list(result.scalars().all())
  677. async def delete_archive(self, archive_id: int) -> bool:
  678. """Delete an archive and its files."""
  679. archive = await self.get_archive(archive_id)
  680. if not archive:
  681. return False
  682. # Delete files
  683. file_path = settings.base_dir / archive.file_path
  684. if file_path.exists():
  685. archive_dir = file_path.parent
  686. shutil.rmtree(archive_dir, ignore_errors=True)
  687. # Delete database record
  688. await self.db.delete(archive)
  689. await self.db.commit()
  690. return True
  691. async def attach_timelapse(
  692. self,
  693. archive_id: int,
  694. timelapse_data: bytes,
  695. filename: str = "timelapse.mp4",
  696. ) -> bool:
  697. """Attach a timelapse video to an archive."""
  698. import asyncio
  699. archive = await self.get_archive(archive_id)
  700. if not archive:
  701. return False
  702. # Get archive directory
  703. file_path = settings.base_dir / archive.file_path
  704. archive_dir = file_path.parent
  705. # Save timelapse - use thread pool to avoid blocking event loop
  706. # (timelapse files can be 100MB+, sync write blocks for seconds)
  707. timelapse_file = archive_dir / filename
  708. await asyncio.to_thread(timelapse_file.write_bytes, timelapse_data)
  709. # Update archive record
  710. archive.timelapse_path = str(timelapse_file.relative_to(settings.base_dir))
  711. await self.db.commit()
  712. return True