archive.py 31 KB

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