archive.py 56 KB

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