archive.py 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329
  1. import asyncio
  2. import hashlib
  3. import html
  4. import json
  5. import logging
  6. import os
  7. import re
  8. import shutil
  9. import zipfile
  10. from collections.abc import Sequence
  11. from datetime import date, datetime, timezone, tzinfo
  12. from pathlib import Path
  13. from typing import Any
  14. from defusedxml import ElementTree as ET
  15. from sqlalchemy import and_, or_, select, text
  16. from sqlalchemy.exc import IntegrityError
  17. from sqlalchemy.ext.asyncio import AsyncSession
  18. from backend.app.core.config import settings
  19. from backend.app.core.printer_scope import PrinterScope
  20. from backend.app.core.tasks import spawn_background_task
  21. from backend.app.models.archive import PrintArchive
  22. from backend.app.models.filament import Filament
  23. from backend.app.models.printer import Printer
  24. from backend.app.utils.archive_paths import archive_dir as resolve_archive_dir
  25. from backend.app.utils.ffmpeg_output import NO_FFMPEG_OUTPUT, summarize_ffmpeg_stderr
  26. from backend.app.utils.filename import clean_display_name
  27. from backend.app.utils.local_time import local_date_end, local_date_start
  28. from backend.app.utils.safe_path import PathTraversalError, assert_under, safe_join_under
  29. from backend.app.utils.threemf_tools import bed_temperature_from_config, used_filament_indexes
  30. logger = logging.getLogger(__name__)
  31. def _copy_and_fsync(src: Path, dst: Path, chunk_size: int = 1024 * 1024) -> None:
  32. """Copy src to dst with an explicit chunked read/write and fsync the dst.
  33. Replacement for shutil.copy2 in the archive pipeline. shutil.copy2 uses
  34. Linux sendfile(), which on some kernels/filesystems has returned a short
  35. count on the first call and truncated the destination for larger 3MF
  36. uploads (#1032, observed on Raspberry Pi OS bookworm / armv7l). An
  37. explicit loop with fsync avoids that path and guarantees the dest bytes
  38. are on disk before the caller inspects them as a ZIP.
  39. """
  40. with src.open("rb") as rf, dst.open("wb") as wf:
  41. while True:
  42. buf = rf.read(chunk_size)
  43. if not buf:
  44. break
  45. wf.write(buf)
  46. wf.flush()
  47. os.fsync(wf.fileno())
  48. shutil.copystat(src, dst)
  49. def resolve_display_stem(filename: str) -> str:
  50. """Return a clean human-readable stem from a 3MF/gcode filename.
  51. Bambu Studio's "Send to printer" dialog typically writes files like
  52. ``Plate_1.gcode.3mf`` (a sliced gcode payload wrapped in a 3MF container).
  53. The naive ``Path(filename).stem`` only drops the last suffix, leaving
  54. ``Plate_1.gcode`` — which then surfaces in the archive UI as a confusing
  55. ``Plate_1.gcode`` rather than ``Plate_1`` (#1152 follow-up).
  56. Strip the recognised print-format suffixes in order:
  57. - ``.gcode.3mf`` → bare stem (Bambu Studio FTP send)
  58. - ``.3mf`` → bare stem
  59. - ``.gcode`` → bare stem (rare standalone gcode upload)
  60. Anything else passes through unchanged.
  61. """
  62. name = Path(filename).name # drop any path components
  63. lower = name.lower()
  64. for suffix in (".gcode.3mf", ".3mf", ".gcode"):
  65. if lower.endswith(suffix):
  66. return name[: -len(suffix)]
  67. return Path(name).stem
  68. def _read_plate_index(plate) -> int | None:
  69. """Return the 1-based index of a ``slice_info.config`` ``<plate>`` element, or None.
  70. Bambu Studio and OrcaSlicer record it as a ``<metadata key="index"
  71. value="N"/>`` child — there is no ``plate_idx`` attribute on ``<plate>``
  72. itself, so an XPath predicate on one never matches (#2522).
  73. """
  74. for meta in plate.findall("metadata"):
  75. if meta.get("key") == "index":
  76. value = meta.get("value")
  77. if not value:
  78. return None
  79. try:
  80. return int(value)
  81. except ValueError:
  82. return None
  83. return None
  84. def plate_indexes_in_3mf(file_path: Path) -> list[int | None]:
  85. """Return one entry per ``<plate>`` a Bambu 3MF declares, in file order.
  86. Reads only ``Metadata/slice_info.config``. An entry is None when that plate
  87. carries no readable index, and the list is empty for a file that could not
  88. be read at all — callers must not confuse either with "this file has plates
  89. and yours is not among them". An unreadable 3MF is a parse this code does
  90. not understand, not evidence about which plate it holds (#2957).
  91. """
  92. try:
  93. with zipfile.ZipFile(file_path, "r") as zf:
  94. if "Metadata/slice_info.config" not in zf.namelist():
  95. return []
  96. root = ET.fromstring(zf.read("Metadata/slice_info.config").decode())
  97. return [_read_plate_index(plate) for plate in root.findall(".//plate")]
  98. except Exception:
  99. return []
  100. def peek_plate_index_in_3mf(file_path: Path) -> int | None:
  101. """Return the plate index a single-plate Bambu 3MF represents, or None.
  102. Reads only ``Metadata/slice_info.config`` to keep this cheap — used by
  103. the print-start callback to verify that the 3MF we just downloaded over
  104. FTP actually matches the plate the printer is running (#1204). The full
  105. ThreeMFParser does much more work and runs later inside ArchiveService.
  106. An all-plates export carries every plate, so "which plate is this file"
  107. has no answer; returning None there keeps the #1204 guard from reading
  108. plate 1 out of such a file, declaring a mismatch against the plate that
  109. is really running, and discarding a perfectly good 3MF (#2522).
  110. """
  111. plates = plate_indexes_in_3mf(file_path)
  112. return plates[0] if len(plates) == 1 else None
  113. _PLATE_SUFFIX_RE = re.compile(r"^(.*?)(\s*-\s*Plate\s+|_plate_)(\d+)$", re.IGNORECASE)
  114. def swap_plate_suffix(name: str | None, target_plate: int) -> str | None:
  115. """Return ``name`` with its trailing plate number replaced, or None.
  116. Bambu Studio names multi-plate uploads ``"<Project> - Plate <N>"`` (and
  117. a lowercase ``"_plate_<N>"`` variant exists too — see
  118. test_print_start_expected_promotion). When MQTT subtask_name lags
  119. across consecutive plates of the same model (#1204) the suffix points
  120. at the previous plate; swapping it gives us the correct upload to
  121. re-fetch from FTP. Returns None if no recognised suffix is present.
  122. """
  123. if not name:
  124. return None
  125. m = _PLATE_SUFFIX_RE.match(name)
  126. if not m:
  127. return None
  128. base, separator, _ = m.groups()
  129. return f"{base}{separator}{target_plate}"
  130. _MESH_OPEN = b"<mesh"
  131. _MESH_CLOSE = b"</mesh>"
  132. _MODEL_READ_CHUNK = 1024 * 1024
  133. def read_3dmodel_without_meshes(zf: zipfile.ZipFile, model_path: str = "3D/3dmodel.model") -> str:
  134. """Return the model XML with every ``<mesh>...</mesh>`` body left out.
  135. The metadata scans only want ``<metadata>`` elements and the URLs inside
  136. them, but a plain 3MF keeps its geometry inline in this same entry: a
  137. combined plate's is hundreds of MB of vertex and triangle rows (#3162).
  138. Streaming past the meshes keeps memory to the non-geometry part. The
  139. entry can't simply be cut at ``<resources>``: BambuStudio writes its
  140. MakerWorld metadata after ``</build>``. Mesh bodies hold only numbers, and
  141. a ``<`` inside a metadata value is escaped, so nothing a scan looks for is
  142. dropped.
  143. """
  144. out = bytearray()
  145. pending = b""
  146. in_mesh = False
  147. with zf.open(model_path) as fh:
  148. while chunk := fh.read(_MODEL_READ_CHUNK):
  149. data = pending + chunk
  150. pos = 0
  151. while True:
  152. if in_mesh:
  153. end = data.find(_MESH_CLOSE, pos)
  154. if end == -1:
  155. # Keep enough of the tail to match a tag split across chunks.
  156. pending = data[max(pos, len(data) - len(_MESH_CLOSE) + 1) :]
  157. break
  158. pos = end + len(_MESH_CLOSE)
  159. in_mesh = False
  160. else:
  161. start = data.find(_MESH_OPEN, pos)
  162. if start == -1:
  163. keep = max(pos, len(data) - len(_MESH_OPEN) + 1)
  164. out += data[pos:keep]
  165. pending = data[keep:]
  166. break
  167. out += data[pos:start]
  168. pos = start + len(_MESH_OPEN)
  169. in_mesh = True
  170. if not in_mesh:
  171. out += pending
  172. return out.decode("utf-8", errors="ignore")
  173. # How much of a plate's G-code to scan for header/config values. The header
  174. # block ends in the first kilobyte; the CONFIG_BLOCK that follows it carries
  175. # layer_height 14-25KB in (measured across the sliced 3MFs on hand, Bambu
  176. # Studio and OrcaSlicer alike), so 4KB — what this used to read — could only
  177. # ever see the header.
  178. _GCODE_SCAN_BYTES = 64 * 1024
  179. async def estimate_filament_cost(db: AsyncSession, grams: float | None, filament_type: str | None) -> float | None:
  180. """An archive's filament cost from its grams and type: the first type's
  181. cost per kg on record, else the default filament cost (25 when unset).
  182. ``None`` without grams or a type."""
  183. if not grams or not filament_type:
  184. return None
  185. # For multi-material prints, the first filament type prices the whole print
  186. primary_type = filament_type.split(",")[0].strip()
  187. filament = (await db.execute(select(Filament).where(Filament.type == primary_type).limit(1))).scalar_one_or_none()
  188. if filament:
  189. return round((grams / 1000) * filament.cost_per_kg, 2)
  190. from backend.app.api.routes.settings import get_setting
  191. default_cost_setting = await get_setting(db, "default_filament_cost")
  192. default_cost_per_kg = float(default_cost_setting) if default_cost_setting else 25.0
  193. return round((grams / 1000) * default_cost_per_kg, 2)
  194. class ThreeMFParser:
  195. """Parser for Bambu Lab 3MF files."""
  196. def __init__(self, file_path: Path, plate_number: int | None = None):
  197. self.file_path = file_path
  198. self.plate_number = plate_number # Which plate was printed (1, 2, 3, etc.)
  199. self.metadata: dict = {}
  200. def parse(self) -> dict:
  201. """Extract metadata from 3MF file."""
  202. try:
  203. with zipfile.ZipFile(self.file_path, "r") as zf:
  204. self._parse_slice_info(zf) # Now sets self.plate_number from slice_info
  205. self._parse_project_settings(zf)
  206. self._parse_gcode_header(zf)
  207. self._parse_3dmodel(zf)
  208. self._extract_thumbnail(zf) # Uses correct plate_number for thumbnail
  209. # Enhance print_name with plate info if this is a multi-plate export
  210. plate_index = self.metadata.get("_plate_index")
  211. if plate_index and plate_index > 1:
  212. # Append plate number to distinguish from other plates
  213. existing_name = self.metadata.get("print_name", "")
  214. if existing_name and f"Plate {plate_index}" not in existing_name:
  215. self.metadata["print_name"] = f"{existing_name} - Plate {plate_index}"
  216. # ALWAYS prefer slice_info values - they contain ONLY filaments actually used in print
  217. # project_settings contains ALL configured filaments (AMS slots), not just used ones
  218. if self.metadata.get("_slice_filament_type"):
  219. self.metadata["filament_type"] = self.metadata["_slice_filament_type"]
  220. if self.metadata.get("_slice_filament_color"):
  221. self.metadata["filament_color"] = self.metadata["_slice_filament_color"]
  222. # Clean up internal keys
  223. self.metadata.pop("_slice_filament_type", None)
  224. self.metadata.pop("_slice_filament_color", None)
  225. self.metadata.pop("_plate_index", None)
  226. except Exception as e:
  227. # Return whatever metadata was extracted before the error, but
  228. # surface the failure so corrupted / truncated 3MF archives are
  229. # visible in support bundles (#1032).
  230. logger.warning(
  231. "ThreeMFParser: failed to parse %s: %s(%s) — returning partial metadata",
  232. self.file_path,
  233. type(e).__name__,
  234. e,
  235. )
  236. return self.metadata
  237. def _parse_slice_info(self, zf: zipfile.ZipFile):
  238. """Parse slice_info.config for print settings and printable objects."""
  239. try:
  240. if "Metadata/slice_info.config" in zf.namelist():
  241. content = zf.read("Metadata/slice_info.config").decode()
  242. root = ET.fromstring(content)
  243. # Extract printer_model_id from plate metadata
  244. # Format: <plate><metadata key="printer_model_id" value="C11" /></plate>
  245. for meta in root.findall(".//metadata"):
  246. key = meta.get("key")
  247. value = meta.get("value")
  248. if key == "printer_model_id" and value:
  249. from backend.app.utils.printer_models import normalize_printer_model_id
  250. normalized = normalize_printer_model_id(value)
  251. if normalized:
  252. self.metadata["sliced_for_model"] = normalized
  253. break
  254. # Loop every <plate> so multi-plate exports get summed file-level
  255. # totals. Pre-fix, this used `root.find(".//plate")` which
  256. # returned only the first plate — file-level `print_time_seconds`
  257. # / `filament_used_grams` reflected plate 1 alone, and the
  258. # archive card / project rollup under-reported by the number
  259. # of plates (#1593). Per-plate breakdown is still served by
  260. # the dedicated `/plates` endpoint.
  261. plates = root.findall(".//plate")
  262. summed_time = 0
  263. summed_grams = 0.0
  264. any_time_seen = False
  265. any_grams_seen = False
  266. for plate in plates:
  267. # Plate-level fields that only make sense at the file
  268. # level when there's exactly one plate. ``plate_number``
  269. # / ``_plate_index`` describe which plate the export
  270. # represents — meaningless for an all-plates 3MF, so we
  271. # only record them in the single-plate case. ``bed_type``
  272. # is also single-valued; we take the first plate's value
  273. # as a best-effort default for the archive metadata.
  274. plate_index_value: int | None = None
  275. for meta in plate.findall("metadata"):
  276. key = meta.get("key")
  277. value = meta.get("value")
  278. if key == "index" and value:
  279. try:
  280. plate_index_value = int(value)
  281. except ValueError:
  282. pass # Skip non-numeric plate index
  283. elif key == "prediction" and value:
  284. try:
  285. summed_time += int(value)
  286. any_time_seen = True
  287. except ValueError:
  288. pass
  289. elif key == "weight" and value:
  290. try:
  291. summed_grams += float(value)
  292. any_grams_seen = True
  293. except ValueError:
  294. pass
  295. elif key == "curr_bed_type" and value and "bed_type" not in self.metadata:
  296. self.metadata["bed_type"] = value
  297. # Per-plate object lists are only kept at the file level
  298. # when there's one plate — the skip-object affordance
  299. # operates on the plate being printed, which is the
  300. # `/plates` endpoint's job for multi-plate exports.
  301. if len(plates) == 1:
  302. if plate_index_value is not None:
  303. if not self.plate_number:
  304. self.plate_number = plate_index_value
  305. self.metadata["_plate_index"] = plate_index_value
  306. printable_objects: dict[int, str] = {}
  307. for obj in plate.findall("object"):
  308. identify_id = obj.get("identify_id")
  309. name = obj.get("name")
  310. skipped = obj.get("skipped", "false")
  311. if identify_id and name and skipped.lower() != "true":
  312. try:
  313. printable_objects[int(identify_id)] = name
  314. except ValueError:
  315. pass # Skip objects with non-numeric identify_id
  316. if printable_objects:
  317. self.metadata["printable_objects"] = printable_objects
  318. self._plate_filament_indexes = used_filament_indexes(plates, self.plate_number)
  319. if any_time_seen:
  320. self.metadata["print_time_seconds"] = summed_time
  321. if any_grams_seen:
  322. self.metadata["filament_used_grams"] = round(summed_grams, 2)
  323. # Get filament info from filaments ACTUALLY USED in the print
  324. # slice_info has <filament id="1" type="PLA" color="#FFFFFF" used_g="100" />
  325. # Only include filaments where used_g > 0
  326. filaments = root.findall(".//filament")
  327. if filaments:
  328. # Collect unique filament types and colors for filaments that are actually used
  329. types = []
  330. colors = []
  331. for f in filaments:
  332. # Check if this filament is actually used in the print
  333. used_g = f.get("used_g", "0")
  334. try:
  335. used_amount = float(used_g)
  336. except (ValueError, TypeError):
  337. used_amount = 0
  338. # Only include if used_g > 0 (filament is actually consumed)
  339. if used_amount > 0:
  340. ftype = f.get("type")
  341. fcolor = f.get("color")
  342. if ftype and ftype not in types:
  343. types.append(ftype)
  344. if fcolor and fcolor not in colors:
  345. colors.append(fcolor)
  346. if types:
  347. self.metadata["_slice_filament_type"] = ", ".join(types)
  348. if colors:
  349. self.metadata["_slice_filament_color"] = ",".join(colors)
  350. # Collect per-slot filament usage for tracking & notifications
  351. filament_slots = []
  352. for f in filaments:
  353. slot_id = f.get("id")
  354. used_g_str = f.get("used_g", "0")
  355. try:
  356. used_g = float(used_g_str)
  357. except (ValueError, TypeError):
  358. used_g = 0
  359. if used_g > 0 and slot_id:
  360. filament_slots.append(
  361. {
  362. "slot_id": int(slot_id),
  363. "used_g": round(used_g, 2),
  364. "type": f.get("type", ""),
  365. "color": f.get("color", ""),
  366. }
  367. )
  368. if filament_slots:
  369. self.metadata["filament_slots"] = filament_slots
  370. except Exception:
  371. pass # Skip unparseable slice_info metadata
  372. def _parse_project_settings(self, zf: zipfile.ZipFile):
  373. """Parse project settings for print configuration."""
  374. try:
  375. if "Metadata/project_settings.config" in zf.namelist():
  376. content = zf.read("Metadata/project_settings.config").decode()
  377. try:
  378. data = json.loads(content)
  379. self._extract_filament_info(data)
  380. self._extract_print_settings(data)
  381. except json.JSONDecodeError:
  382. pass # Skip malformed project_settings JSON
  383. except Exception:
  384. pass # Skip unreadable project settings file
  385. def _printed_plate_gcode(self, gcode_files: list[str]) -> str:
  386. """Return the G-code entry for the plate this archive is about.
  387. ``plate_number`` is known for a plate-specific export (slice_info sets
  388. it) and picking blindly is wrong there: a project sliced with plate 2
  389. at 0.08 and plate 1 at 0.2 would otherwise report plate 1's numbers.
  390. Falls back to the lowest plate index, then to zip order, so a file
  391. whose entries are named some other way still parses as it did before.
  392. """
  393. if self.plate_number:
  394. wanted = f"Metadata/plate_{self.plate_number}.gcode"
  395. if wanted in gcode_files:
  396. return wanted
  397. def plate_index(name: str) -> int:
  398. match = re.search(r"plate_(\d+)\.gcode$", name)
  399. return int(match.group(1)) if match else 10**6
  400. return min(gcode_files, key=lambda name: (plate_index(name), gcode_files.index(name)))
  401. def _parse_gcode_header(self, zf: zipfile.ZipFile):
  402. """Parse the printed plate's G-code for what only it can settle.
  403. The plate's own G-code is the file the printer executes, so where it
  404. disagrees with ``project_settings.config`` — the *project's* record,
  405. which a multi-plate or per-plate-modified export can leave describing
  406. a different plate entirely — the G-code wins.
  407. """
  408. try:
  409. gcode_files = [f for f in zf.namelist() if f.endswith(".gcode")]
  410. if not gcode_files:
  411. return
  412. gcode_path = self._printed_plate_gcode(gcode_files)
  413. # 64KB, not 4KB: the header block ends within the first kilobyte,
  414. # but the CONFIG_BLOCK that carries layer_height starts right after
  415. # it and the keys are alphabetical, so layer_height lands 14-25KB
  416. # in on real files. The read is decompress-on-demand, so the cost
  417. # of the wider window is a few tens of KB per archived file.
  418. with zf.open(gcode_path) as f:
  419. header = f.read(_GCODE_SCAN_BYTES).decode("utf-8", errors="ignore")
  420. # Look for "; total layer number: XX" pattern
  421. match = re.search(r";\s*total\s+layer\s+number[:\s]+(\d+)", header, re.IGNORECASE)
  422. if match:
  423. self.metadata["total_layers"] = int(match.group(1))
  424. # Layer height, overriding project_settings.config when both are
  425. # present. The project config records the project's settings and can
  426. # describe a plate other than this one; the plate's G-code is what
  427. # the printer executes, so it decides. Anchored to the line start so keys ending in
  428. # "layer_height" (independent_support_layer_height) can't match.
  429. match = re.search(r"^;\s*layer_height\s*=\s*([\d.]+)\s*$", header, re.IGNORECASE | re.MULTILINE)
  430. if match:
  431. try:
  432. self.metadata["layer_height"] = float(match.group(1))
  433. except ValueError:
  434. pass # Malformed value: keep whatever project_settings gave us
  435. # Total filament usage. The slicer writes the print's totals into
  436. # the G-code header ("; total filament weight [g] : 126.26"). Only
  437. # a fallback — slice_info.config is more authoritative when present
  438. # — but it covers sliced outputs whose slice_info lacks per-filament
  439. # used_g, and it's the slicer's own figure regardless.
  440. if "filament_used_grams" not in self.metadata:
  441. match = re.search(r";\s*total\s+filament\s+weight\s*\[g\]\s*:\s*([\d.]+)", header, re.IGNORECASE)
  442. if match:
  443. self.metadata["filament_used_grams"] = float(match.group(1))
  444. if "filament_used_mm" not in self.metadata:
  445. match = re.search(r";\s*total\s+filament\s+length\s*\[mm\]\s*:\s*([\d.]+)", header, re.IGNORECASE)
  446. if match:
  447. self.metadata["filament_used_mm"] = float(match.group(1))
  448. # Look for printer_model in gcode header (fallback if not found in slice_info)
  449. # Format: "; printer_model = Bambu Lab X1 Carbon" or "; printer_model = X1C"
  450. if "sliced_for_model" not in self.metadata:
  451. match = re.search(r";\s*printer_model\s*=\s*(.+)", header, re.IGNORECASE)
  452. if match:
  453. from backend.app.utils.printer_models import normalize_printer_model
  454. raw_model = match.group(1).strip()
  455. self.metadata["sliced_for_model"] = normalize_printer_model(raw_model)
  456. except Exception:
  457. pass # G-code header parsing is best-effort; metadata may come from other sources
  458. def _extract_filament_info(self, data: dict):
  459. """Extract filament info from project settings — includes support
  460. materials so a PLA-model / PVA-support project shows both on the
  461. archive card badge (#1881).
  462. Earlier code filtered by ``filament_is_support``; that hid PVA
  463. (and any other soluble/breakaway support material) from the card
  464. even when the user had explicitly configured it, and made source
  465. 3MFs look single-material until the print completed. slice_info
  466. (parsed separately) is still preferred when present — it lists
  467. only filaments the print actually consumes, this fallback only
  468. runs on unsliced source 3MFs.
  469. """
  470. try:
  471. filament_types = data.get("filament_type", [])
  472. filament_colors = data.get("filament_colour", [])
  473. if not filament_types:
  474. return
  475. unique_types: list[str] = []
  476. for ftype in filament_types:
  477. if ftype and ftype not in unique_types:
  478. unique_types.append(ftype)
  479. unique_colors: list[str] = []
  480. for color in filament_colors:
  481. if color and color not in unique_colors:
  482. unique_colors.append(color)
  483. if unique_types:
  484. self.metadata["filament_type"] = ", ".join(unique_types)
  485. if unique_colors:
  486. self.metadata["filament_color"] = ",".join(unique_colors)
  487. except Exception:
  488. pass # Filament info is optional; fall back to slice_info values
  489. def _extract_print_settings(self, data: dict):
  490. """Extract print settings from JSON config."""
  491. try:
  492. # Layer height - usually an array, get first value
  493. if "layer_height" in data:
  494. val = data["layer_height"]
  495. if isinstance(val, list) and val:
  496. self.metadata["layer_height"] = float(val[0])
  497. elif isinstance(val, (int, float, str)):
  498. self.metadata["layer_height"] = float(val)
  499. # Nozzle diameter
  500. if "nozzle_diameter" in data:
  501. val = data["nozzle_diameter"]
  502. if isinstance(val, list) and val:
  503. self.metadata["nozzle_diameter"] = float(val[0])
  504. elif isinstance(val, (int, float, str)):
  505. self.metadata["nozzle_diameter"] = float(val)
  506. # Bed temperature, for the plate this project is sliced for. This
  507. # used to look for `bed_temperature` alone, a key BambuStudio does
  508. # not write -- so every archive from a Bambu slice stored NULL, and
  509. # preheat fell back to a configured bed temperature on every job
  510. # (#2989). Orca-exported 3MFs keep working through the generic keys.
  511. bed_temperature = bed_temperature_from_config(data)
  512. if bed_temperature is not None:
  513. self.metadata["bed_temperature"] = bed_temperature
  514. # Nozzle temperature: one per filament, so the printed plate's own
  515. # (nozzle diameter above is one per nozzle, not per filament).
  516. for key in ["nozzle_temperature_initial_layer", "nozzle_temperature"]:
  517. if key in data:
  518. val = data[key]
  519. if isinstance(val, list) and val:
  520. index = (getattr(self, "_plate_filament_indexes", None) or [0])[0]
  521. self.metadata["nozzle_temperature"] = int(float(val[index if index < len(val) else 0]))
  522. elif isinstance(val, (int, float, str)):
  523. self.metadata["nozzle_temperature"] = int(float(val))
  524. break
  525. # Printer model (extract and normalize)
  526. if "printer_model" in data:
  527. from backend.app.utils.printer_models import normalize_printer_model
  528. self.metadata["sliced_for_model"] = normalize_printer_model(data["printer_model"])
  529. # Build plate type — only set from project_settings if slice_info didn't already
  530. # provide it (slice_info is more authoritative as it reflects the exported plate).
  531. if "bed_type" not in self.metadata and "curr_bed_type" in data:
  532. val = data["curr_bed_type"]
  533. if isinstance(val, str) and val.strip():
  534. self.metadata["bed_type"] = val.strip()
  535. except Exception:
  536. pass # Print settings are optional; missing values are left unset
  537. def _parse_3dmodel(self, zf: zipfile.ZipFile):
  538. """Parse 3D/3dmodel.model for MakerWorld metadata."""
  539. try:
  540. model_path = "3D/3dmodel.model"
  541. if model_path not in zf.namelist():
  542. return
  543. content = read_3dmodel_without_meshes(zf, model_path)
  544. # Parse XML metadata elements
  545. # MakerWorld adds metadata like: <metadata name="Designer">username</metadata>
  546. metadata_pattern = r'<metadata\s+name="([^"]+)"[^>]*>([^<]*)</metadata>'
  547. matches = re.findall(metadata_pattern, content)
  548. # 3MF metadata values are XML-encoded — `&` becomes `&amp;`, etc.
  549. # ProjectPageParser learned this the hard way: BambuStudio sometimes
  550. # writes triple-encoded payloads (`&amp;amp;amp;`), so we unescape
  551. # in a loop until the string stabilises. Without this, a Title like
  552. # "Foo & Bar" lands in the DB as raw "Foo &amp; Bar" and React then
  553. # double-escapes it on render to "Foo &amp;amp; Bar" (#1658).
  554. makerworld_fields = {}
  555. for name, value in matches:
  556. decoded = value.strip()
  557. prev = None
  558. while prev != decoded:
  559. prev = decoded
  560. decoded = html.unescape(decoded)
  561. makerworld_fields[name] = decoded
  562. # Check for direct MakerWorld URL in content
  563. url_pattern = r'https?://makerworld\.com/[^\s<>"\']+/models/(\d+)'
  564. url_match = re.search(url_pattern, content)
  565. if url_match:
  566. self.metadata["makerworld_url"] = url_match.group(0)
  567. self.metadata["makerworld_model_id"] = url_match.group(1)
  568. # Extract model ID from DSM reference in image URLs
  569. # Format: https://makerworld.bblmw.com/makerworld/model/DSM00000001275614/...
  570. # The numeric part (1275614) is the MakerWorld model ID
  571. if "makerworld_url" not in self.metadata:
  572. dsm_pattern = r"DSM0+(\d+)"
  573. dsm_match = re.search(dsm_pattern, content)
  574. if dsm_match:
  575. model_id = dsm_match.group(1)
  576. self.metadata["makerworld_url"] = f"https://makerworld.com/en/models/{model_id}"
  577. self.metadata["makerworld_model_id"] = model_id
  578. # Store designer info
  579. if "Designer" in makerworld_fields:
  580. self.metadata["designer"] = makerworld_fields["Designer"]
  581. if "Title" in makerworld_fields:
  582. self.metadata["print_name"] = makerworld_fields["Title"]
  583. except Exception:
  584. pass # MakerWorld/3dmodel metadata is optional
  585. def _extract_thumbnail(self, zf: zipfile.ZipFile):
  586. """Extract thumbnail image from 3MF.
  587. If a plate_number was specified, try to use that plate's thumbnail first.
  588. """
  589. thumbnail_paths = []
  590. # If a specific plate was printed, try that thumbnail first
  591. if self.plate_number:
  592. thumbnail_paths.append(f"Metadata/plate_{self.plate_number}.png")
  593. # Fallback to default paths
  594. thumbnail_paths.extend(
  595. [
  596. "Metadata/plate_1.png",
  597. "Metadata/thumbnail.png",
  598. "Metadata/model_thumbnail.png",
  599. # Project-wide thumbnail BambuStudio embeds at upload time. We
  600. # only reach this when BS hasn't written a per-plate
  601. # ``Metadata/plate_N.png`` — most notably the #1493 cross-class
  602. # re-slice path where ``--arrange`` rearranges objects but the
  603. # CLI then doesn't emit a fresh per-plate preview. The
  604. # ``_middle`` size is the editor-quality variant (~500 KB);
  605. # ``_small`` and ``_3mf`` are smaller alternates if it's not
  606. # present. Without this fallback the re-sliced archive cards
  607. # render without a cover image.
  608. "Auxiliaries/.thumbnails/thumbnail_middle.png",
  609. "Auxiliaries/.thumbnails/thumbnail_small.png",
  610. "Auxiliaries/.thumbnails/thumbnail_3mf.png",
  611. ]
  612. )
  613. for thumb_path in thumbnail_paths:
  614. if thumb_path in zf.namelist():
  615. self.metadata["_thumbnail_data"] = zf.read(thumb_path)
  616. self.metadata["_thumbnail_ext"] = ".png"
  617. break
  618. def extract_printable_objects_from_archive(
  619. file_path: Path, plate_number: int | None = None
  620. ) -> tuple[dict[int, dict], list | None]:
  621. """Objects and plate bbox for an archived print, read off local disk.
  622. The archive of a running print usually holds the very 3MF the printer is
  623. executing, so the object list can be rebuilt without asking the printer for
  624. a file we already have -- 15 MB over FTPS from a machine that is mid-print,
  625. in the case this was written for. Returns empty when the archive has
  626. no readable 3MF, which is the caller's signal to fall back to the printer.
  627. """
  628. if not file_path.is_file() or not str(file_path).endswith(".3mf"):
  629. return {}, None
  630. try:
  631. data = file_path.read_bytes()
  632. except OSError:
  633. return {}, None
  634. return extract_printable_objects_from_3mf(data, plate_number=plate_number, include_positions=True)
  635. def extract_printable_objects_from_3mf(
  636. data: bytes, plate_number: int | None = None, include_positions: bool = False
  637. ) -> dict[int, str] | dict[int, dict] | tuple[dict[int, dict], list | None]:
  638. """Extract printable objects from 3MF file bytes.
  639. This is a lightweight function used during print start to get the list
  640. of objects that can be skipped.
  641. Args:
  642. data: Raw bytes of the 3MF file
  643. plate_number: Which plate was printed (1-based), or None for first plate
  644. include_positions: If True, return tuple of (objects dict, bbox_all)
  645. Returns:
  646. If include_positions=False: Dictionary mapping identify_id (int) to object name (str)
  647. If include_positions=True: Tuple of (dict mapping identify_id to {name, x, y}, bbox_all list or None)
  648. """
  649. from io import BytesIO
  650. printable_objects: dict = {}
  651. bbox_all: list | None = None
  652. try:
  653. with zipfile.ZipFile(BytesIO(data), "r") as zf:
  654. if "Metadata/slice_info.config" not in zf.namelist():
  655. return printable_objects
  656. content = zf.read("Metadata/slice_info.config").decode()
  657. root = ET.fromstring(content)
  658. plates = root.findall(".//plate")
  659. if not plates:
  660. return printable_objects
  661. # Pick the plate that is actually printing. An all-plates export
  662. # lists every plate, so without this we offered the objects (and
  663. # the marker positions) of plate 1 whatever the printer was
  664. # running (#2522). Falling back to the first plate keeps the
  665. # single-plate export — the common case — working when the caller
  666. # has no plate to give us.
  667. plate = None
  668. if plate_number is not None:
  669. plate = next((p for p in plates if _read_plate_index(p) == plate_number), None)
  670. if plate is None:
  671. plate = plates[0]
  672. # Derive plate_idx from the plate we settled on, never from the
  673. # requested one: on a fallback they differ, and plate_idx also
  674. # selects the plate_N.json the positions come from.
  675. plate_idx = _read_plate_index(plate) or 1
  676. # Load position data from plate_N.json if we need positions
  677. # Build a lookup by name - use list to handle duplicate names
  678. bbox_by_name: dict[str, list[list]] = {}
  679. if include_positions:
  680. plate_json_path = f"Metadata/plate_{plate_idx}.json"
  681. if plate_json_path in zf.namelist():
  682. try:
  683. plate_json = json.loads(zf.read(plate_json_path).decode())
  684. # Get bbox_all - the bounding box of all objects (used for image bounds)
  685. bbox_all = plate_json.get("bbox_all")
  686. for bbox_obj in plate_json.get("bbox_objects", []):
  687. obj_name = bbox_obj.get("name")
  688. bbox = bbox_obj.get("bbox", [])
  689. if obj_name and len(bbox) >= 4:
  690. if obj_name not in bbox_by_name:
  691. bbox_by_name[obj_name] = []
  692. bbox_by_name[obj_name].append(bbox)
  693. except (json.JSONDecodeError, KeyError):
  694. pass # Position data is optional; objects will lack x/y coordinates
  695. # Extract objects from slice_info.config
  696. for obj in plate.findall("object"):
  697. identify_id = obj.get("identify_id")
  698. name = obj.get("name")
  699. skipped = obj.get("skipped", "false")
  700. if identify_id and name and skipped.lower() != "true":
  701. try:
  702. obj_id = int(identify_id)
  703. if include_positions:
  704. x, y = None, None
  705. # Match by name - pop first bbox to handle duplicates
  706. bboxes = bbox_by_name.get(name)
  707. if bboxes:
  708. bbox = bboxes.pop(0)
  709. # Calculate center from bbox [x_min, y_min, x_max, y_max]
  710. x = (bbox[0] + bbox[2]) / 2
  711. y = (bbox[1] + bbox[3]) / 2
  712. printable_objects[obj_id] = {"name": name, "x": x, "y": y}
  713. else:
  714. printable_objects[obj_id] = name
  715. except ValueError:
  716. pass # Skip objects with non-numeric identify_id
  717. except Exception:
  718. pass # Return empty dict if 3MF is corrupt or unreadable
  719. if include_positions:
  720. return printable_objects, bbox_all
  721. return printable_objects
  722. class ProjectPageParser:
  723. """Parser for extracting project page data from Bambu Lab 3MF files."""
  724. def __init__(self, file_path: Path):
  725. self.file_path = file_path
  726. def parse(self, archive_id: int) -> dict:
  727. """Extract project page metadata and images from 3MF file."""
  728. import html
  729. result = {
  730. "title": None,
  731. "description": None,
  732. "designer": None,
  733. "designer_user_id": None,
  734. "license": None,
  735. "copyright": None,
  736. "creation_date": None,
  737. "modification_date": None,
  738. "origin": None,
  739. "profile_title": None,
  740. "profile_description": None,
  741. "profile_cover": None,
  742. "profile_user_id": None,
  743. "profile_user_name": None,
  744. "design_model_id": None,
  745. "design_profile_id": None,
  746. "design_region": None,
  747. "model_pictures": [],
  748. "profile_pictures": [],
  749. "thumbnails": [],
  750. }
  751. try:
  752. with zipfile.ZipFile(self.file_path, "r") as zf:
  753. # Parse 3D/3dmodel.model for metadata
  754. model_path = "3D/3dmodel.model"
  755. if model_path in zf.namelist():
  756. content = read_3dmodel_without_meshes(zf, model_path)
  757. # Extract metadata elements using regex
  758. # Format: <metadata name="Key">Value</metadata> or <metadata name="Key" />
  759. metadata_pattern = r'<metadata\s+name="([^"]+)"[^>]*>([^<]*)</metadata>'
  760. matches = re.findall(metadata_pattern, content)
  761. field_mapping = {
  762. "Title": "title",
  763. "Description": "description",
  764. "Designer": "designer",
  765. "DesignerUserId": "designer_user_id",
  766. "License": "license",
  767. "Copyright": "copyright",
  768. "CreationDate": "creation_date",
  769. "ModificationDate": "modification_date",
  770. "Origin": "origin",
  771. "ProfileTitle": "profile_title",
  772. "ProfileDescription": "profile_description",
  773. "ProfileCover": "profile_cover",
  774. "ProfileUserId": "profile_user_id",
  775. "ProfileUserName": "profile_user_name",
  776. "DesignModelId": "design_model_id",
  777. "DesignProfileId": "design_profile_id",
  778. "DesignRegion": "design_region",
  779. }
  780. for name, value in matches:
  781. if name in field_mapping:
  782. # Decode HTML entities multiple times (content is often triple-encoded)
  783. decoded = value.strip()
  784. prev = None
  785. while prev != decoded:
  786. prev = decoded
  787. decoded = html.unescape(decoded)
  788. # Normalize non-breaking spaces to regular spaces
  789. decoded = decoded.replace("\xa0", " ")
  790. result[field_mapping[name]] = decoded if decoded else None
  791. # List images in Auxiliaries folder
  792. from urllib.parse import quote
  793. for name in zf.namelist():
  794. if name.startswith("Auxiliaries/Model Pictures/"):
  795. filename = name.split("/")[-1]
  796. if filename:
  797. result["model_pictures"].append(
  798. {
  799. "name": filename,
  800. "path": name,
  801. "url": f"/api/v1/archives/{archive_id}/project-image/{quote(name, safe='')}",
  802. }
  803. )
  804. elif name.startswith("Auxiliaries/Profile Pictures/"):
  805. filename = name.split("/")[-1]
  806. if filename:
  807. result["profile_pictures"].append(
  808. {
  809. "name": filename,
  810. "path": name,
  811. "url": f"/api/v1/archives/{archive_id}/project-image/{quote(name, safe='')}",
  812. }
  813. )
  814. elif name.startswith("Auxiliaries/.thumbnails/"):
  815. filename = name.split("/")[-1]
  816. if filename:
  817. result["thumbnails"].append(
  818. {
  819. "name": filename,
  820. "path": name,
  821. "url": f"/api/v1/archives/{archive_id}/project-image/{quote(name, safe='')}",
  822. }
  823. )
  824. except Exception as e:
  825. result["_error"] = str(e)
  826. return result
  827. def get_image(self, image_path: str) -> tuple[bytes, str] | None:
  828. """Extract an image from the 3MF file.
  829. Returns tuple of (image_data, content_type) or None if not found.
  830. """
  831. try:
  832. with zipfile.ZipFile(self.file_path, "r") as zf:
  833. if image_path in zf.namelist():
  834. data = zf.read(image_path)
  835. # Determine content type from extension
  836. ext = image_path.lower().split(".")[-1]
  837. content_types = {
  838. "png": "image/png",
  839. "jpg": "image/jpeg",
  840. "jpeg": "image/jpeg",
  841. "webp": "image/webp",
  842. "gif": "image/gif",
  843. }
  844. content_type = content_types.get(ext, "application/octet-stream")
  845. return (data, content_type)
  846. except Exception:
  847. pass # Return None if image cannot be extracted from 3MF
  848. return None
  849. def update_metadata(self, updates: dict) -> bool:
  850. """Update project page metadata in the 3MF file.
  851. Args:
  852. updates: Dict with fields to update (title, description, designer, etc.)
  853. Returns:
  854. True if successful, False otherwise.
  855. """
  856. import html
  857. import tempfile
  858. try:
  859. # Read the 3MF file
  860. with zipfile.ZipFile(self.file_path, "r") as zf_read:
  861. # Find and read the 3dmodel.model file
  862. model_path = "3D/3dmodel.model"
  863. if model_path not in zf_read.namelist():
  864. return False
  865. content = zf_read.read(model_path).decode("utf-8")
  866. # Update metadata fields
  867. field_mapping = {
  868. "title": "Title",
  869. "description": "Description",
  870. "designer": "Designer",
  871. "license": "License",
  872. "copyright": "Copyright",
  873. "profile_title": "ProfileTitle",
  874. "profile_description": "ProfileDescription",
  875. }
  876. for field, xml_name in field_mapping.items():
  877. if field in updates and updates[field] is not None:
  878. new_value = html.escape(updates[field])
  879. # Replace existing metadata or we'd need to add it
  880. pattern = rf'(<metadata\s+name="{xml_name}"[^>]*>)[^<]*(</metadata>)'
  881. replacement = rf"\g<1>{new_value}\g<2>"
  882. content = re.sub(pattern, replacement, content)
  883. # Write to a temporary file first
  884. with tempfile.NamedTemporaryFile(delete=False, suffix=".3mf") as tmp:
  885. tmp_path = Path(tmp.name)
  886. # Create new zip with updated content
  887. with zipfile.ZipFile(tmp_path, "w", zipfile.ZIP_DEFLATED) as zf_write:
  888. for item in zf_read.namelist():
  889. if item == model_path:
  890. zf_write.writestr(item, content.encode("utf-8"))
  891. else:
  892. zf_write.writestr(item, zf_read.read(item))
  893. # Replace original file with updated one
  894. shutil.move(tmp_path, self.file_path)
  895. return True
  896. except Exception:
  897. # Clean up temp file if it exists
  898. if "tmp_path" in locals() and tmp_path.exists():
  899. tmp_path.unlink()
  900. return False
  901. async def _null_print_log_thumbnail_paths(db: AsyncSession, archive_id: int) -> None:
  902. """NULL thumbnail_path on PrintLogEntry rows linked to *archive_id*.
  903. Called from both soft- and hard-delete paths before the archive's files
  904. leave disk. The FK on PrintLogEntry.archive_id is ON DELETE SET NULL so
  905. log rows survive the archive — without this clear, their cached
  906. thumbnail_path would still point at a deleted file and the print-log
  907. view would 404-storm on every render (#1348 follow-up). Lazy-NULL on
  908. the GET route self-heals stragglers (e.g. failed prints that never had
  909. a thumbnail written), but eager clear here avoids the one-time storm.
  910. """
  911. from sqlalchemy import update as sa_update
  912. from backend.app.models.print_log import PrintLogEntry
  913. await db.execute(
  914. sa_update(PrintLogEntry)
  915. .where(PrintLogEntry.archive_id == archive_id)
  916. .values(thumbnail_path=None)
  917. .execution_options(include_open_runs=True)
  918. )
  919. async def _delete_related_queue_items(db: AsyncSession, archive_id: int) -> int:
  920. """Delete every queue item pointing at *archive_id* (#1734).
  921. Called from ``soft_delete_archive``. Hard-delete is covered by the
  922. ``ON DELETE CASCADE`` on ``print_queue.archive_id`` — same end state
  923. via the FK. Pre-#1734 this helper merely flipped pending rows to
  924. ``status='cancelled'`` while leaving every other status alone and
  925. leaving the rows in the DB, which surprised users who expected the
  926. queue lines to disappear when their backing archive went away. Worse,
  927. a Send-All archive backed N queue items (one per plate, #1733) — soft-
  928. deleting that archive left N "cancelled" rows behind, none of which
  929. could ever dispatch.
  930. Now we delete unconditionally regardless of status. ``printing`` rows
  931. are blocked one layer up at the route (``delete_archive`` returns 409
  932. when a related row is mid-print) so we never delete an actively-
  933. running queue row out from under the dispatcher. Completed / failed
  934. / cancelled rows go too — they're queue history, not print history.
  935. PrintLogEntry rows are the authoritative print history and are
  936. untouched (FK ``ON DELETE SET NULL``).
  937. Returns the number of rows removed so the caller can report it.
  938. """
  939. from sqlalchemy import delete as sa_delete
  940. from backend.app.models.print_queue import PrintQueueItem
  941. result = await db.execute(sa_delete(PrintQueueItem).where(PrintQueueItem.archive_id == archive_id))
  942. return result.rowcount or 0
  943. async def _count_related_queue_items(db: AsyncSession, archive_id: int) -> tuple[int, int]:
  944. """Return ``(total, printing)`` queue items linked to *archive_id*.
  945. Used by the archive GET response so the frontend delete-confirm modal
  946. can surface how much the deletion will wipe out, and by the delete
  947. route so it can 409 when a related row is currently printing (#1734).
  948. """
  949. from sqlalchemy import func as sa_func, select as sa_select
  950. from backend.app.models.print_queue import PrintQueueItem
  951. total = (
  952. await db.execute(
  953. sa_select(sa_func.count()).select_from(PrintQueueItem).where(PrintQueueItem.archive_id == archive_id)
  954. )
  955. ).scalar_one()
  956. printing = (
  957. await db.execute(
  958. sa_select(sa_func.count())
  959. .select_from(PrintQueueItem)
  960. .where(
  961. PrintQueueItem.archive_id == archive_id,
  962. PrintQueueItem.status == "printing",
  963. )
  964. )
  965. ).scalar_one()
  966. return int(total or 0), int(printing or 0)
  967. def printed_on_clause(printer_id: int):
  968. """Archives printed on ``printer_id``, as a WHERE clause: the archive's
  969. own printer (its latest print's) or any print's, a print running now
  970. included (archive redesign P4)."""
  971. from backend.app.models.print_log import PrintLogEntry
  972. # The table, not the entity: the ORM hides open runs from every read.
  973. runs = PrintLogEntry.__table__
  974. printed_on = select(runs.c.archive_id).where(runs.c.archive_id.is_not(None), runs.c.printer_id == printer_id)
  975. return or_(PrintArchive.printer_id == printer_id, PrintArchive.id.in_(printed_on))
  976. class ArchiveService:
  977. """Service for archiving print jobs."""
  978. def __init__(self, db: AsyncSession):
  979. self.db = db
  980. @staticmethod
  981. def compute_file_hash(file_path: Path) -> str:
  982. """Compute SHA256 hash of a file for duplicate detection."""
  983. sha256 = hashlib.sha256()
  984. with open(file_path, "rb") as f:
  985. # Read in chunks to handle large files
  986. for chunk in iter(lambda: f.read(8192), b""):
  987. sha256.update(chunk)
  988. return sha256.hexdigest()
  989. async def get_duplicate_hashes_and_names(
  990. self, visible: Sequence[Any] = ()
  991. ) -> tuple[set[str], set[tuple[str, str]]]:
  992. """Get all content hashes and (print name, hash) pairs that appear more than once.
  993. ``visible`` limits the count to archives the caller may see.
  994. For hashes: returns all hashes with > 1 archive (true duplicates).
  995. For name/hash pairs: returns only pairs that have > 1 archive
  996. (i.e., same file archived multiple times, not different files with same name).
  997. Returns a tuple of (duplicate_hashes, duplicate_name_hash_pairs).
  998. """
  999. from sqlalchemy import func
  1000. # Soft-deleted archives don't appear in the listing (#1343), so they
  1001. # mustn't influence the duplicate-group counts either — otherwise a
  1002. # group with 1 live + 4 soft-deleted would still be flagged as a
  1003. # duplicate even though the user only sees one row.
  1004. result = await self.db.execute(
  1005. select(PrintArchive.content_hash)
  1006. .where(PrintArchive.content_hash.isnot(None), PrintArchive.deleted_at.is_(None), *visible)
  1007. .group_by(PrintArchive.content_hash)
  1008. .having(func.count(PrintArchive.id) > 1)
  1009. )
  1010. duplicate_hashes = {row[0] for row in result.all()}
  1011. # Find print names that have multiple archives with the SAME hash
  1012. # This avoids marking different files with the same name as duplicates
  1013. result = await self.db.execute(
  1014. select(func.lower(PrintArchive.print_name), PrintArchive.content_hash)
  1015. .where(
  1016. PrintArchive.print_name.isnot(None),
  1017. PrintArchive.content_hash.isnot(None),
  1018. PrintArchive.deleted_at.is_(None),
  1019. *visible,
  1020. )
  1021. .group_by(func.lower(PrintArchive.print_name), PrintArchive.content_hash)
  1022. .having(func.count(PrintArchive.id) > 1)
  1023. )
  1024. duplicate_name_hash_pairs = {(row[0], row[1]) for row in result.all()}
  1025. return duplicate_hashes, duplicate_name_hash_pairs
  1026. async def find_duplicates(
  1027. self,
  1028. archive_id: int,
  1029. content_hash: str | None = None,
  1030. print_name: str | None = None,
  1031. makerworld_model_id: str | None = None,
  1032. visible: Sequence[Any] = (),
  1033. ) -> list[dict]:
  1034. """Find duplicate archives based on hash or name matching.
  1035. ``visible`` holds WHERE clauses limiting matches to archives the caller may see.
  1036. Returns list of dicts with id, print_name, created_at, match_type.
  1037. """
  1038. duplicates = []
  1039. # First, find exact matches by content hash
  1040. if content_hash:
  1041. result = await self.db.execute(
  1042. select(PrintArchive)
  1043. .where(
  1044. and_(
  1045. PrintArchive.content_hash == content_hash,
  1046. PrintArchive.id != archive_id,
  1047. PrintArchive.deleted_at.is_(None),
  1048. *visible,
  1049. )
  1050. )
  1051. .order_by(PrintArchive.created_at.desc())
  1052. .limit(10)
  1053. )
  1054. for archive in result.scalars().all():
  1055. duplicates.append(
  1056. {
  1057. "id": archive.id,
  1058. "print_name": archive.print_name,
  1059. "created_at": archive.created_at,
  1060. "match_type": "exact",
  1061. }
  1062. )
  1063. # Then, find similar matches by print name or MakerWorld ID
  1064. # Prefer strict name+hash matching when hash exists; fallback to name-only for legacy/manual
  1065. # archives that may not have a content_hash.
  1066. if print_name or makerworld_model_id:
  1067. conditions = [PrintArchive.id != archive_id, PrintArchive.deleted_at.is_(None), *visible]
  1068. name_conditions = []
  1069. if print_name:
  1070. if content_hash:
  1071. # Match if print names are similar AND have the same hash (same file)
  1072. name_conditions.append(
  1073. and_(PrintArchive.print_name.ilike(print_name), PrintArchive.content_hash == content_hash)
  1074. )
  1075. else:
  1076. # Fallback for archives without hash data: match by print name only.
  1077. name_conditions.append(PrintArchive.print_name.ilike(print_name))
  1078. if makerworld_model_id:
  1079. # Match by MakerWorld model ID stored in extra_data
  1080. from backend.app.core.db_dialect import is_sqlite
  1081. if is_sqlite():
  1082. from sqlalchemy import func
  1083. name_conditions.append(
  1084. func.json_extract(PrintArchive.extra_data, "$.makerworld_model_id") == str(makerworld_model_id)
  1085. )
  1086. else:
  1087. name_conditions.append(
  1088. text("(extra_data::jsonb->>'makerworld_model_id') = :mw_id").bindparams(
  1089. mw_id=str(makerworld_model_id)
  1090. )
  1091. )
  1092. if name_conditions:
  1093. conditions.append(or_(*name_conditions))
  1094. result = await self.db.execute(
  1095. select(PrintArchive).where(and_(*conditions)).order_by(PrintArchive.created_at.desc()).limit(10)
  1096. )
  1097. for archive in result.scalars().all():
  1098. # Don't add if already in duplicates (exact match)
  1099. if not any(d["id"] == archive.id for d in duplicates):
  1100. duplicates.append(
  1101. {
  1102. "id": archive.id,
  1103. "print_name": archive.print_name,
  1104. "created_at": archive.created_at,
  1105. "match_type": "similar",
  1106. }
  1107. )
  1108. return duplicates
  1109. @staticmethod
  1110. def _has_sliced_plate(file_path: Path, plate_number: int) -> bool:
  1111. try:
  1112. with zipfile.ZipFile(file_path) as zf:
  1113. return f"Metadata/plate_{plate_number}.gcode" in zf.namelist()
  1114. except (OSError, zipfile.BadZipFile):
  1115. return False
  1116. async def archive_print(
  1117. self,
  1118. printer_id: int | None,
  1119. source_file: Path,
  1120. print_data: dict | None = None,
  1121. created_by_id: int | None = None,
  1122. original_filename: str | None = None,
  1123. project_id: int | None = None,
  1124. cost_center_id: int | None = None,
  1125. subtask_id: str | None = None,
  1126. prefer_filename_for_name: bool = False,
  1127. plate_id: int | None = None,
  1128. library_file_id: int | None = None,
  1129. slicer_ams_mapping: list[int] | None = None,
  1130. slicer_ams_mapping_printer_id: int | None = None,
  1131. update_archive_id: int | None = None,
  1132. started_at: datetime | None = None,
  1133. replace_archive_id: int | None = None,
  1134. ) -> PrintArchive | None:
  1135. """Archive a 3MF file with metadata.
  1136. Args:
  1137. printer_id: ID of the printer (optional)
  1138. source_file: Path to the 3MF file
  1139. print_data: Print data from MQTT (optional)
  1140. created_by_id: User ID who created this archive (optional, for user tracking)
  1141. original_filename: Original human-readable filename (optional, for library files
  1142. stored with UUID names)
  1143. project_id: Project to associate this archive with (optional, set when triggered
  1144. from the project view)
  1145. library_file_id: Library file this run was dispatched from (optional,
  1146. set by the queue scheduler — powers per-file project progress, #1897)
  1147. subtask_id: MQTT-provided task identifier (optional). Used to match an
  1148. existing archive across a backend restart mid-print so the
  1149. original row can be resumed instead of cancelled (#972).
  1150. prefer_filename_for_name: When True, use the uploaded filename stem as the
  1151. archive's display name even if the 3MF embeds a `print_name` in its
  1152. metadata. Used by virtual-printer flows so users who rename a job in
  1153. BambuStudio's "send to printer" dialog see that name instead of the
  1154. creator-baked title (#1152).
  1155. slicer_ams_mapping: The slicer's own live-resolved AMS-slot pick, to persist
  1156. onto `extra_data.slicer_ams_mapping` for a later reprint to reuse. Deliberately
  1157. a distinct parameter, not read off `print_data["ams_mapping"]` — that key is
  1158. populated on every MQTT print-start callback regardless of source (bambu_mqtt's
  1159. request-topic interception captures it for slicer-direct LAN prints too), so
  1160. promoting it unconditionally would stamp every archive on installs with no
  1161. virtual printer at all. Callers that gate this behind an opt-in (the VP-queue
  1162. "Save AMS mapping" toggle) pass it explicitly; everyone else leaves it unset.
  1163. slicer_ams_mapping_printer_id: The printer `slicer_ams_mapping`'s tray IDs were
  1164. resolved against. Required alongside `slicer_ams_mapping` — a global tray ID
  1165. only means something relative to one printer's specific AMS layout, so a
  1166. mapping saved without knowing which printer it came from can't be safely
  1167. reused later on any printer, including the same one (there'd be no way to
  1168. tell). A model-based VP with no fixed target printer has no valid value to
  1169. pass here and must leave both params unset.
  1170. update_archive_id: Fill in an existing archive row instead of adding one.
  1171. Used to upgrade a no-3MF fallback archive once the file finally arrives
  1172. (#2957). Everything above the row itself — the copy, the parse, the
  1173. thumbnail, the cost — is exactly what a fresh archive does; only the
  1174. destination differs. The row must keep its id: the energy-start reading,
  1175. the timelapse session, ``_active_prints``, the start notification and any
  1176. queue link were all written against it while the print was running, and a
  1177. second row would orphan every one of them. Fields the fallback path
  1178. already established from MQTT (``started_at``, ``subtask_id``,
  1179. ``created_by_id``, ``project_id``) are left alone; the 3MF has nothing
  1180. better to say about them.
  1181. started_at: When the print started, for a "printing" archive whose 3MF
  1182. was downloaded after the start (#3327). Defaults to now.
  1183. replace_archive_id: Put this file in place of an existing archive's: the
  1184. same slice sent again from the slicer (#1634, same G-code in a new
  1185. 3MF). The file goes into the archive's own folder, where its photos
  1186. and timelapses are, and the old one is deleted. Everything read from
  1187. the file is the new one's; what belongs to the archive (owner,
  1188. project, notes, tags, photos, runs, a saved AMS mapping) stays, and so
  1189. does a name set by hand. Status and start are the caller's: the run
  1190. is opened as for any print of an existing archive.
  1191. """
  1192. # Verify printer exists if specified
  1193. if printer_id is not None:
  1194. result = await self.db.execute(select(Printer).where(Printer.id == printer_id))
  1195. printer = result.scalar_one_or_none()
  1196. if not printer:
  1197. return None
  1198. # Create archive directory structure
  1199. timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
  1200. display_stem = resolve_display_stem(original_filename if original_filename else source_file.name)
  1201. archive_name = f"{timestamp}_{display_stem}"
  1202. # Use "unassigned" folder for archives without a printer
  1203. printer_folder = str(printer_id) if printer_id is not None else "unassigned"
  1204. archive_dir = (
  1205. settings.archive_dir / printer_folder / archive_name
  1206. ) # SEC-PATH-OK: printer_folder = str(int|None) → digits or "unassigned"; archive_name = f"{timestamp}_{display_stem}" where resolve_display_stem strips path components via Path(filename).name
  1207. # A slicer resend replacing an archive's file (#1634): into that
  1208. # archive's folder, which holds its photos and timelapses and which
  1209. # deleting it removes. One without a file (restored from a backup, a
  1210. # fallback) gets a new folder, as a fallback given its 3MF does.
  1211. replacing: PrintArchive | None = None
  1212. if replace_archive_id is not None:
  1213. replacing = await self.db.get(PrintArchive, replace_archive_id)
  1214. if replacing is None:
  1215. logger.warning("archive_print: archive %s to replace the file of no longer exists", replace_archive_id)
  1216. return None
  1217. if replacing.file_path:
  1218. archive_dir = resolve_archive_dir(replacing)
  1219. # Removed again if the row is not written (below); only a folder this
  1220. # call created, never another archive's of the same name.
  1221. created_archive_dir = not archive_dir.exists()
  1222. archive_dir.mkdir(parents=True, exist_ok=True)
  1223. # Copy 3MF file with an explicit fsync'd loop (avoids a sendfile
  1224. # short-read quirk that silently truncated 3MF archives on some
  1225. # platforms — see _copy_and_fsync and #1032).
  1226. dest_file = archive_dir / source_file.name
  1227. # Over the replaced archive's file of the same name only once the copy
  1228. # is whole and checked, so a failed copy leaves the archive its file.
  1229. copy_target = dest_file
  1230. if replacing is not None and dest_file.exists():
  1231. copy_target = archive_dir / f".replacing-{source_file.name}"
  1232. _copy_and_fsync(source_file, copy_target)
  1233. # If we just archived a 3MF, verify the dest is a valid ZIP before
  1234. # going any further. Staying quiet here is how #1032 escaped review —
  1235. # the archive row was written but every later zipfile.ZipFile() call
  1236. # on the dest failed with "File is not a zip file".
  1237. if (
  1238. source_file.suffix.lower() == ".3mf"
  1239. and zipfile.is_zipfile(source_file)
  1240. and not zipfile.is_zipfile(copy_target)
  1241. ):
  1242. try:
  1243. src_size = source_file.stat().st_size
  1244. dst_size = copy_target.stat().st_size
  1245. except OSError:
  1246. src_size = dst_size = -1
  1247. logger.error(
  1248. "Archive copy corrupted 3MF: src=%s (%s bytes, valid ZIP) -> dst=%s (%s bytes, NOT a ZIP). Refusing to create archive row.",
  1249. source_file,
  1250. src_size,
  1251. copy_target,
  1252. dst_size,
  1253. )
  1254. # Narrow cleanup: remove only the truncated file and the archive
  1255. # directory if it's now empty. archive_dir was created with
  1256. # exist_ok=True so it could in theory pre-date this call (e.g.
  1257. # same-second same-filename collision); rmtree would be too broad.
  1258. try:
  1259. copy_target.unlink()
  1260. except OSError:
  1261. pass
  1262. try:
  1263. archive_dir.rmdir()
  1264. except OSError:
  1265. pass # directory not empty — leave untouched
  1266. return None
  1267. if copy_target != dest_file:
  1268. os.replace(copy_target, dest_file)
  1269. # Compute content hash for duplicate detection
  1270. content_hash = self.compute_file_hash(dest_file)
  1271. # And of its G-code alone, so the same slice sent again in a new 3MF
  1272. # adds a run to this archive (#1634). Off the event loop: it unzips.
  1273. from backend.app.services.archive_gcode_hash import NO_GCODE, gcode_fingerprint
  1274. gcode_hash = await asyncio.to_thread(gcode_fingerprint, dest_file) or NO_GCODE
  1275. # Extract plate number from filename (e.g., "plate_5" from "/data/Metadata/plate_5.gcode")
  1276. plate_number = None
  1277. if print_data:
  1278. filename = print_data.get("filename", "")
  1279. match = re.search(r"plate_(\d+)", filename)
  1280. if match:
  1281. plate_number = int(match.group(1))
  1282. if plate_id is None:
  1283. # A print Bambuddy did not dispatch only names its plate in the
  1284. # gcode path. Kept as the archive's plate when the file really has
  1285. # it: some firmware reports the .3mf name there instead, and a user
  1286. # name like "x_plate_2" must not label a single-plate file.
  1287. if plate_number is not None and self._has_sliced_plate(dest_file, plate_number):
  1288. plate_id = plate_number
  1289. else:
  1290. # The dispatcher knows the plate; the path may name none, or not
  1291. # the plate sent (#1166). Without it the parser describes another.
  1292. plate_number = plate_id
  1293. # Parse 3MF metadata
  1294. parser = ThreeMFParser(dest_file, plate_number=plate_number)
  1295. metadata = parser.parse()
  1296. # Save thumbnail if present
  1297. thumbnail_path = None
  1298. if "_thumbnail_data" in metadata:
  1299. thumb_file = archive_dir / f"thumbnail{metadata['_thumbnail_ext']}"
  1300. thumb_file.write_bytes(metadata["_thumbnail_data"])
  1301. thumbnail_path = str(thumb_file.relative_to(settings.base_dir))
  1302. del metadata["_thumbnail_data"]
  1303. del metadata["_thumbnail_ext"]
  1304. # Merge with print data from MQTT
  1305. if print_data:
  1306. metadata["_print_data"] = print_data
  1307. # Promote the slicer's own live-resolved AMS-slot pick, when the caller
  1308. # explicitly opted in (see the `slicer_ams_mapping` param docstring for
  1309. # why this is NOT read off `print_data["ams_mapping"]`), to a stable
  1310. # top-level extra_data key. Lets a later reprint reuse the exact tray
  1311. # the user picked/BambuStudio auto-matched at slice time instead of the
  1312. # scheduler re-deriving one from just the file's static type/color,
  1313. # which can land on the wrong physical spool when that match isn't
  1314. # unique. Top-level (not nested under the `_print_data` diagnostic bag)
  1315. # so API consumers have a single stable path:
  1316. # `archive.extra_data.slicer_ams_mapping`. Stored together with the
  1317. # printer it was resolved against — see `slicer_ams_mapping_printer_id`
  1318. # param docstring — so a later reprint can tell whether it's even
  1319. # applicable before trying to reuse it.
  1320. if slicer_ams_mapping and slicer_ams_mapping_printer_id is not None:
  1321. metadata["slicer_ams_mapping"] = {
  1322. "mapping": slicer_ams_mapping,
  1323. "printer_id": slicer_ams_mapping_printer_id,
  1324. }
  1325. # Determine status and timestamps
  1326. status = print_data.get("status", "completed") if print_data else "archived"
  1327. started_at = (started_at or datetime.now(timezone.utc)) if status == "printing" else None
  1328. completed_at = datetime.now(timezone.utc) if status in ("completed", "failed", "archived") else None
  1329. cost = await estimate_filament_cost(self.db, metadata.get("filament_used_grams"), metadata.get("filament_type"))
  1330. # Calculate quantity from printable objects count
  1331. # printable_objects is a dict of {identify_id: name} for non-skipped objects
  1332. quantity = 1 # Default to 1
  1333. printable_objects = metadata.get("printable_objects")
  1334. if printable_objects and isinstance(printable_objects, dict):
  1335. quantity = len(printable_objects)
  1336. logger.debug("Auto-detected %s parts from 3MF printable objects", quantity)
  1337. # Recovery of an existing fallback row: assign the freshly-parsed values
  1338. # onto it rather than adding a second archive for the same print (#2957).
  1339. if replacing is not None:
  1340. return await self._take_replacement_file(
  1341. replacing,
  1342. dest_file=dest_file,
  1343. filename=original_filename or source_file.name,
  1344. print_name=(
  1345. clean_display_name(display_stem)
  1346. if prefer_filename_for_name
  1347. else (clean_display_name(metadata.get("print_name")) or clean_display_name(display_stem))
  1348. ),
  1349. content_hash=content_hash,
  1350. gcode_hash=gcode_hash,
  1351. thumbnail_path=thumbnail_path,
  1352. metadata=metadata,
  1353. cost=cost,
  1354. quantity=quantity,
  1355. plate_id=plate_id,
  1356. )
  1357. if update_archive_id is not None:
  1358. existing = await self.db.get(PrintArchive, update_archive_id)
  1359. if existing is None:
  1360. logger.warning("archive_print: archive %s to update no longer exists", update_archive_id)
  1361. return None
  1362. # `metadata` is freshly parsed from the 3MF, so assigning it drops
  1363. # the row's `no_3mf_available` / `no_3mf_reason` markers as a side
  1364. # effect — which is correct, the archive is no longer a fallback,
  1365. # and it is what stops the Archives banner counting it.
  1366. # `_print_data` is diagnostic history rather than something the 3MF
  1367. # knows about: keep the row's copy for a caller that passed no
  1368. # print_data of its own.
  1369. merged = dict(metadata)
  1370. preserved = (existing.extra_data or {}).get("_print_data")
  1371. if preserved is not None and "_print_data" not in merged:
  1372. merged["_print_data"] = preserved
  1373. # A record that this row started life without a 3MF, which the
  1374. # dropped markers no longer say.
  1375. merged["recovered_no_3mf"] = True
  1376. existing.filename = original_filename or source_file.name
  1377. existing.file_path = str(dest_file.relative_to(settings.base_dir))
  1378. existing.file_size = dest_file.stat().st_size
  1379. existing.content_hash = content_hash
  1380. existing.gcode_hash = gcode_hash
  1381. existing.thumbnail_path = thumbnail_path
  1382. existing.print_name = (
  1383. clean_display_name(display_stem)
  1384. if prefer_filename_for_name
  1385. else (clean_display_name(metadata.get("print_name")) or clean_display_name(display_stem))
  1386. )
  1387. # Only overwrite what the 3MF actually knows. A fallback archive
  1388. # recovered mid-print has a real print_time_seconds from MQTT and a
  1389. # filament type/colour from the AMS; a 3MF that omits a field must
  1390. # not blank them back out.
  1391. for field in (
  1392. "print_time_seconds",
  1393. "filament_used_grams",
  1394. "filament_type",
  1395. "filament_color",
  1396. "layer_height",
  1397. "total_layers",
  1398. "nozzle_diameter",
  1399. "bed_temperature",
  1400. "bed_type",
  1401. "nozzle_temperature",
  1402. "sliced_for_model",
  1403. "makerworld_url",
  1404. "designer",
  1405. ):
  1406. value = metadata.get(field)
  1407. if value is not None:
  1408. setattr(existing, field, value)
  1409. if cost is not None:
  1410. existing.cost = cost
  1411. existing.quantity = quantity
  1412. existing.extra_data = merged
  1413. if plate_id is not None:
  1414. existing.plate_id = plate_id
  1415. if library_file_id is not None:
  1416. existing.library_file_id = library_file_id
  1417. from backend.app.services.print_cost_estimate import refresh_plate_estimates
  1418. refresh_plate_estimates(existing)
  1419. await self.db.commit()
  1420. await self.db.refresh(existing)
  1421. return existing
  1422. # Create archive record
  1423. archive = PrintArchive(
  1424. printer_id=printer_id,
  1425. filename=original_filename or source_file.name,
  1426. file_path=str(dest_file.relative_to(settings.base_dir)),
  1427. file_size=dest_file.stat().st_size,
  1428. content_hash=content_hash,
  1429. gcode_hash=gcode_hash,
  1430. thumbnail_path=thumbnail_path,
  1431. # clean_display_name because the 3MF's own metadata reaches this
  1432. # verbatim, and a control character in it renders nowhere and
  1433. # truncates somewhere (#2832). The schema does the same for names
  1434. # arriving over the API. Cleaned before the fallback rather than
  1435. # after it, so an embedded name that is only whitespace still falls
  1436. # through to the filename instead of leaving the archive nameless.
  1437. print_name=(
  1438. clean_display_name(display_stem)
  1439. if prefer_filename_for_name
  1440. else (clean_display_name(metadata.get("print_name")) or clean_display_name(display_stem))
  1441. ),
  1442. print_time_seconds=metadata.get("print_time_seconds"),
  1443. filament_used_grams=metadata.get("filament_used_grams"),
  1444. filament_type=metadata.get("filament_type"),
  1445. filament_color=metadata.get("filament_color"),
  1446. layer_height=metadata.get("layer_height"),
  1447. total_layers=metadata.get("total_layers"),
  1448. nozzle_diameter=metadata.get("nozzle_diameter"),
  1449. bed_temperature=metadata.get("bed_temperature"),
  1450. bed_type=metadata.get("bed_type"),
  1451. nozzle_temperature=metadata.get("nozzle_temperature"),
  1452. sliced_for_model=metadata.get("sliced_for_model"),
  1453. makerworld_url=metadata.get("makerworld_url"),
  1454. designer=metadata.get("designer"),
  1455. status=status,
  1456. started_at=started_at,
  1457. completed_at=completed_at,
  1458. cost=cost,
  1459. quantity=quantity,
  1460. extra_data=metadata,
  1461. created_by_id=created_by_id,
  1462. project_id=project_id,
  1463. library_file_id=library_file_id,
  1464. cost_center_id=cost_center_id,
  1465. subtask_id=subtask_id,
  1466. plate_id=plate_id,
  1467. )
  1468. from backend.app.services.print_cost_estimate import refresh_plate_estimates
  1469. refresh_plate_estimates(archive)
  1470. self.db.add(archive)
  1471. try:
  1472. await self.db.commit()
  1473. except IntegrityError:
  1474. # Refused, so the row was not written (a printer deleted meanwhile,
  1475. # by its foreign key): the folder its files went into would be
  1476. # orphaned. Not on other errors: a connection lost during COMMIT may
  1477. # still have written the row, whose files these are.
  1478. if created_archive_dir:
  1479. shutil.rmtree(archive_dir, ignore_errors=True)
  1480. raise
  1481. await self.db.refresh(archive)
  1482. return archive
  1483. async def _take_replacement_file(
  1484. self,
  1485. archive: PrintArchive,
  1486. *,
  1487. dest_file: Path,
  1488. filename: str,
  1489. print_name: str | None,
  1490. content_hash: str,
  1491. gcode_hash: str,
  1492. thumbnail_path: str | None,
  1493. metadata: dict,
  1494. cost: float | None,
  1495. quantity: int,
  1496. plate_id: int | None,
  1497. ) -> PrintArchive:
  1498. """Give ``archive`` the file just placed at ``dest_file`` in place of its
  1499. own (``archive_print(replace_archive_id=...)``, #1634), and delete the
  1500. old one once that is committed."""
  1501. old_file = (
  1502. settings.base_dir / archive.file_path if archive.file_path else None
  1503. ) # SEC-PATH-OK: archive.file_path is DB-stored, internally generated
  1504. old_extra = archive.extra_data or {}
  1505. # A name the old file gave follows the file; one set by hand stays.
  1506. # Both ways archive_print names a file: its 3MF title, or its file name.
  1507. generated = {
  1508. clean_display_name(old_extra.get("print_name")),
  1509. clean_display_name(resolve_display_stem(archive.filename)) if archive.filename else None,
  1510. } - {None}
  1511. if print_name and (not archive.print_name or archive.print_name in generated):
  1512. archive.print_name = print_name
  1513. archive.filename = filename
  1514. archive.file_path = str(dest_file.relative_to(settings.base_dir))
  1515. archive.file_size = dest_file.stat().st_size
  1516. archive.content_hash = content_hash
  1517. archive.gcode_hash = gcode_hash
  1518. # Earlier runs point at the old thumbnail by path: one of another name
  1519. # stays in the folder for them, one of the same name now shows the new
  1520. # file's (the same slice).
  1521. if thumbnail_path is not None:
  1522. archive.thumbnail_path = thumbnail_path
  1523. # The same G-code, so what the new 3MF leaves out the old one said.
  1524. for field in (
  1525. "print_time_seconds",
  1526. "filament_used_grams",
  1527. "filament_type",
  1528. "filament_color",
  1529. "layer_height",
  1530. "total_layers",
  1531. "nozzle_diameter",
  1532. "bed_temperature",
  1533. "bed_type",
  1534. "nozzle_temperature",
  1535. "sliced_for_model",
  1536. "makerworld_url",
  1537. "designer",
  1538. ):
  1539. value = metadata.get(field)
  1540. if value is not None:
  1541. setattr(archive, field, value)
  1542. if cost is not None:
  1543. archive.cost = cost
  1544. archive.quantity = quantity
  1545. # The new file's description. Of what is not the file's, only the saved
  1546. # AMS pick is the archive's (a pause warning is its last run's).
  1547. extra = dict(metadata)
  1548. if "slicer_ams_mapping" in old_extra:
  1549. extra["slicer_ams_mapping"] = old_extra["slicer_ams_mapping"]
  1550. archive.extra_data = extra
  1551. if plate_id is not None:
  1552. archive.plate_id = plate_id
  1553. from backend.app.services.print_cost_estimate import refresh_plate_estimates
  1554. refresh_plate_estimates(archive)
  1555. await self.db.commit()
  1556. await self.db.refresh(archive)
  1557. if old_file is not None and old_file != dest_file:
  1558. try:
  1559. old_file.unlink(missing_ok=True)
  1560. except OSError as exc:
  1561. logger.warning("Could not delete the replaced file %s of archive %s: %s", old_file, archive.id, exc)
  1562. return archive
  1563. async def get_archive(self, archive_id: int) -> PrintArchive | None:
  1564. """Get an archive by ID with relationships loaded."""
  1565. from sqlalchemy.orm import selectinload
  1566. result = await self.db.execute(
  1567. select(PrintArchive)
  1568. .options(selectinload(PrintArchive.created_by), selectinload(PrintArchive.project))
  1569. .where(PrintArchive.id == archive_id)
  1570. )
  1571. return result.scalar_one_or_none()
  1572. async def update_archive_status(
  1573. self,
  1574. archive_id: int,
  1575. status: str,
  1576. completed_at: datetime | None = None,
  1577. failure_reason: str | None = None,
  1578. ) -> bool:
  1579. """Update the status of an archive."""
  1580. archive = await self.get_archive(archive_id)
  1581. if not archive:
  1582. return False
  1583. archive.status = status
  1584. if completed_at:
  1585. archive.completed_at = completed_at
  1586. # This run's reason, or none: a reprint that completes, or a print
  1587. # that finished after all once it was stopped offline, keeps no reason
  1588. # of an earlier outcome.
  1589. archive.failure_reason = failure_reason
  1590. await self.db.commit()
  1591. return True
  1592. async def list_archives(
  1593. self,
  1594. printer_id: int | None = None,
  1595. project_id: int | None = None,
  1596. date_from: date | None = None,
  1597. date_to: date | None = None,
  1598. limit: int = 50,
  1599. offset: int = 0,
  1600. visible_to_user_id: int | None = None,
  1601. printer_scope: PrinterScope | None = None,
  1602. day_zone: tzinfo | None = None,
  1603. ) -> list[PrintArchive]:
  1604. """List archives with optional filtering.
  1605. ``visible_to_user_id`` scopes results to archives that user owns. Used
  1606. when the caller has ARCHIVES_READ_OWN but not ARCHIVES_READ_ALL — pass
  1607. ``None`` to skip the filter (caller has read-all or auth is disabled).
  1608. """
  1609. from sqlalchemy.orm import selectinload
  1610. query = (
  1611. select(PrintArchive)
  1612. .options(selectinload(PrintArchive.project), selectinload(PrintArchive.created_by))
  1613. # Hide soft-deleted rows from the listings (#1343). The stats
  1614. # endpoint deliberately does NOT add this filter so deleted
  1615. # archives keep contributing to Quick Stats.
  1616. .where(PrintArchive.deleted_at.is_(None))
  1617. .order_by(PrintArchive.created_at.desc())
  1618. )
  1619. if printer_id:
  1620. # Printed on it: the archive's own printer (its latest print's) or
  1621. # any print's, a print running now included (archive redesign). A
  1622. # printer outside the caller's scope finds nothing: its prints are
  1623. # not theirs to see.
  1624. if printer_scope is not None and not printer_scope.allows(printer_id):
  1625. return []
  1626. query = query.where(printed_on_clause(printer_id))
  1627. if project_id:
  1628. query = query.where(PrintArchive.project_id == project_id)
  1629. if date_from:
  1630. dt_from = local_date_start(date_from, day_zone)
  1631. query = query.where(PrintArchive.created_at >= dt_from)
  1632. if date_to:
  1633. dt_to = local_date_end(date_to, day_zone)
  1634. query = query.where(PrintArchive.created_at <= dt_to)
  1635. if visible_to_user_id is not None:
  1636. query = query.where(PrintArchive.created_by_id == visible_to_user_id)
  1637. # Only archives from printers the caller may see (#1727)
  1638. from backend.app.core.row_visibility import archive_scope_clause
  1639. if printer_scope is not None and (clause := archive_scope_clause(printer_scope)) is not None:
  1640. query = query.where(clause)
  1641. query = query.limit(limit).offset(offset)
  1642. result = await self.db.execute(query)
  1643. return list(result.scalars().all())
  1644. async def soft_delete_archive(self, archive_id: int) -> bool:
  1645. """Soft-delete an archive (#1343).
  1646. Removes the archive's files from disk (it disappears from the listings
  1647. and frees the storage) but flips the row's ``deleted_at`` so the stats
  1648. endpoint keeps counting its filament / energy / time / cost. The user
  1649. can opt into a hard delete via the "Also remove from statistics"
  1650. checkbox in the delete dialog — that path calls ``delete_archive``
  1651. instead and removes the row entirely.
  1652. """
  1653. archive = await self.get_archive(archive_id)
  1654. if not archive:
  1655. return False
  1656. if archive.deleted_at is not None:
  1657. # Already soft-deleted; nothing to do. The files were purged on
  1658. # the first soft-delete pass so there is nothing left on disk.
  1659. return True
  1660. dirs_to_delete = self._resolve_archive_dirs_for_delete(archive)
  1661. recorded_paths = (archive.timelapse_path, archive.thumbnail_path)
  1662. await _null_print_log_thumbnail_paths(self.db, archive_id)
  1663. await _delete_related_queue_items(self.db, archive_id)
  1664. archive.deleted_at = datetime.now(timezone.utc)
  1665. await self.db.commit()
  1666. for directory in dirs_to_delete:
  1667. shutil.rmtree(directory, ignore_errors=True)
  1668. self._purge_id_named_dir(archive_id, recorded_paths)
  1669. return True
  1670. def _resolve_archive_dirs_for_delete(self, archive: PrintArchive) -> list[Path]:
  1671. """Directories belonging to *archive* alone, safe to remove whole.
  1672. Shared by soft-delete and hard-delete so the two cannot drift apart
  1673. again — the previous helper said it was extracted for that reason, and
  1674. ``delete_archive`` was still doing its own copy of the same rules.
  1675. An archive with a 3MF owns the directory its ``file_path`` sits in,
  1676. ``<archive_dir>/<printer_id>/<timestamp>_<name>/``. Any archive may also
  1677. own ``archive/no_source/<id>/``, where a source 3MF uploaded onto a
  1678. no-3MF archive lands (#1531); that one was never removed, so deleting
  1679. such an archive freed the row and left the upload behind.
  1680. Two directories are deliberately absent. ``<base_dir>/photos`` is the
  1681. legacy location *every* no-3MF archive wrote into at once, so removing
  1682. it on one delete would take the others' photos with it. And
  1683. ``<archive_dir>/<id>`` — the directory :func:`resolve_archive_dir` gives
  1684. an archive with no ``file_path`` — is handled by
  1685. :meth:`_purge_id_named_dir` instead, for the reason given there.
  1686. """
  1687. candidates: list[Path] = []
  1688. # Only when there is a path to derive it from. Without one,
  1689. # ``resolve_archive_dir`` returns the id-named directory, which must not
  1690. # be removed wholesale -- see _purge_id_named_dir.
  1691. if archive.file_path and archive.file_path.strip():
  1692. candidates.append(resolve_archive_dir(archive))
  1693. candidates.append(settings.archive_dir / "no_source" / str(archive.id))
  1694. resolved: list[Path] = []
  1695. for candidate in candidates:
  1696. if candidate in resolved or not candidate.is_dir():
  1697. continue
  1698. try:
  1699. relative_path = candidate.resolve().relative_to(settings.archive_dir.resolve())
  1700. except ValueError:
  1701. # A genuine guard trip, unlike the empty ``file_path`` this used
  1702. # to shout about: the row points somewhere outside the archive
  1703. # tree, which only a corrupted import or hand-edited SQL can do.
  1704. logger.error(
  1705. f"SECURITY: Refusing to delete archive {archive.id} - "
  1706. f"path {candidate} is outside archive directory {settings.archive_dir}"
  1707. )
  1708. continue
  1709. # Two deep, not one. An archive directory has been
  1710. # ``<archive_dir>/<printer_id>/<timestamp>_<name>/`` since the first
  1711. # commit, so nothing legitimate sits one level down -- but the
  1712. # per-printer folder does, and it holds every print that printer
  1713. # ever made. Under the old ``< 1`` a row whose file_path had lost a
  1714. # path component took the whole folder with it.
  1715. if len(relative_path.parts) < 2:
  1716. logger.error(
  1717. f"SECURITY: Refusing to delete archive {archive.id} - "
  1718. f"path {candidate} is not deep enough inside archive directory"
  1719. )
  1720. continue
  1721. resolved.append(candidate)
  1722. return resolved
  1723. def _purge_id_named_dir(self, archive_id: int, recorded_paths: tuple[str | None, ...]) -> None:
  1724. """Remove one archive's own files from ``<archive_dir>/<id>``, carefully.
  1725. Takes the recorded paths rather than the row because ``delete_archive``
  1726. removes the row before it touches the disk, deliberately: a failed
  1727. commit must leave the files alone. Reading ``archive.timelapse_path``
  1728. off a deleted instance afterwards would raise or silently refresh.
  1729. That directory is where an archive with no 3MF keeps its timelapse and
  1730. its finish photos (:func:`resolve_archive_dir`). It is emphatically NOT
  1731. an ``rmtree`` target, because it shares a namespace with the per-printer
  1732. folders: a normal archive lives at
  1733. ``<archive_dir>/<printer_id>/<timestamp>_<name>/``, so ``archive/1`` is
  1734. printer 1's folder *and* the directory the helper hands archive id 1.
  1735. Archive ids and printer ids are both small integers from unrelated
  1736. sequences, so on any install the first few archives collide with the
  1737. printers. Removing the directory would take every print that printer
  1738. ever made — measured on a scratch tree before this guard existed.
  1739. So nothing is removed that has not been identified as this archive's.
  1740. ``photos`` is a fixed subdirectory name and an archive directory is
  1741. always ``<timestamp>_<name>``, so the two cannot be confused; the video
  1742. is removed by the path the row itself records. The directory then goes
  1743. only if that left it empty, which a printer folder holding prints never
  1744. will. Anything unrecognised keeps it alive and is leaked rather than
  1745. guessed at — the safe direction for a recursive delete.
  1746. """
  1747. directory = settings.archive_dir / str(archive_id)
  1748. if not directory.is_dir():
  1749. return
  1750. try:
  1751. relative_path = directory.resolve().relative_to(settings.archive_dir.resolve())
  1752. except ValueError:
  1753. return
  1754. if len(relative_path.parts) != 1:
  1755. return
  1756. shutil.rmtree(directory / "photos", ignore_errors=True) # SEC-PATH-OK: constant subdirectory
  1757. for recorded in recorded_paths:
  1758. if not recorded:
  1759. continue
  1760. try:
  1761. # Two checks, not one. safe_join_under rejects the absolute and
  1762. # ``..`` shapes and proves the result is inside the data
  1763. # directory; assert_under then narrows it to *this* archive's
  1764. # own directory, because a row whose timelapse_path names
  1765. # another archive's file must not take it with this delete.
  1766. # The column is written by Bambuddy from a filename the printer
  1767. # supplied over FTP, so it is not a trusted constant.
  1768. candidate = safe_join_under(settings.base_dir, recorded, http=False)
  1769. assert_under(directory, candidate, http=False)
  1770. except PathTraversalError:
  1771. continue
  1772. if candidate.is_file():
  1773. candidate.unlink(missing_ok=True)
  1774. try:
  1775. directory.rmdir()
  1776. except OSError:
  1777. # Not empty (a printer folder, or a file this archive did not
  1778. # record) or already gone. Both are fine: the point of rmdir over
  1779. # rmtree is that it cannot take anything with it.
  1780. pass
  1781. async def delete_archive(self, archive_id: int) -> bool:
  1782. """Delete an archive and its files."""
  1783. archive = await self.get_archive(archive_id)
  1784. if not archive:
  1785. return False
  1786. # Resolved BEFORE committing the DB change, since the row is what says
  1787. # where the files are. Shared with soft-delete rather than repeated
  1788. # here: this was a second copy of the same checks and it had already
  1789. # diverged from the one it was extracted from.
  1790. dirs_to_delete = self._resolve_archive_dirs_for_delete(archive)
  1791. recorded_paths = (archive.timelapse_path, archive.thumbnail_path)
  1792. # NULL stale thumbnail_path on linked PrintLogEntries before the FK
  1793. # SET-NULL cascade fires. The on-disk file is about to be removed by
  1794. # the rmtree below, so the path on any surviving log entry (archive_id
  1795. # gets SET NULL by the FK) would otherwise point at a missing file
  1796. # and produce 404 storms in the print-log view (#1348-followup).
  1797. await _null_print_log_thumbnail_paths(self.db, archive_id)
  1798. # Delete database record FIRST — if the commit fails (e.g. database locked
  1799. # during concurrent bulk deletes), the files stay on disk and nothing is lost.
  1800. await self.db.delete(archive)
  1801. await self.db.commit()
  1802. # Only delete files AFTER the DB commit succeeds to avoid orphaned records
  1803. for directory in dirs_to_delete:
  1804. shutil.rmtree(directory, ignore_errors=True)
  1805. self._purge_id_named_dir(archive_id, recorded_paths)
  1806. return True
  1807. async def attach_timelapse(
  1808. self,
  1809. archive_id: int,
  1810. timelapse_data: bytes,
  1811. filename: str = "timelapse.mp4",
  1812. *,
  1813. plate_id: int | None = None,
  1814. run_id: int | None = None,
  1815. ) -> bool:
  1816. """Attach a timelapse video to an archive.
  1817. Non-MP4 videos (e.g. AVI from P1S) are saved as-is and a background
  1818. task converts them to MP4 for browser compatibility.
  1819. ``plate_id`` is the plate whose run produced the video, when the caller
  1820. knows it; print start uses it to tell a reprint from the next plate of
  1821. a shared archive (#3275).
  1822. ``run_id`` is the print whose video it is (archive redesign): the
  1823. video is that run's, and the archive's only while that run is the
  1824. one it shows, its newest. Without one (attached by hand) it is the
  1825. archive's and its newest run's.
  1826. """
  1827. import asyncio
  1828. from backend.app.services.print_log import drop_earlier_plate_videos, is_newest_run, newest_run, run_of
  1829. archive = await self.get_archive(archive_id)
  1830. if not archive:
  1831. return False
  1832. # Where this archive's files live. Deliberately the shared helper: an
  1833. # archive created without a 3MF has ``file_path == ""``, and deriving the
  1834. # directory here as ``(base_dir / "").parent`` resolved to the parent of
  1835. # base_dir — outside the data directory entirely. In Docker that is /app,
  1836. # so the write failed with EACCES and the timelapse was retried and
  1837. # discarded 25 times; where the parent happens to be writable it
  1838. # succeeded, dropped a stray video next to the install, and then failed
  1839. # anyway on the relative_to() below. Every H2-series and P2S print sent
  1840. # from the slicer takes that path, because the file goes to internal
  1841. # storage and no 3MF can be fetched.
  1842. archive_dir = resolve_archive_dir(archive)
  1843. # Save timelapse - use thread pool to avoid blocking event loop
  1844. # (timelapse files can be 100MB+, sync write blocks for seconds).
  1845. # `filename` ultimately comes from a printer's FTP listing (compromised-
  1846. # printer threat model) or a query param on /archives/{id}/timelapse/select;
  1847. # the safe-join helper rejects ``..`` segments and absolute paths so a
  1848. # crafted name can't escape the archive directory. Use http=False so a
  1849. # service-layer reject surfaces as a return False (matching the existing
  1850. # not-found contract) rather than a 400 raised from inside a background
  1851. # task.
  1852. try:
  1853. timelapse_file = safe_join_under(archive_dir, filename, http=False)
  1854. except PathTraversalError:
  1855. logger.warning(
  1856. "Refusing to attach timelapse with unsafe filename %r to archive %s",
  1857. filename,
  1858. archive_id,
  1859. )
  1860. return False
  1861. run = await (run_of(self.db, archive_id, run_id) if run_id is not None else newest_run(self.db, archive_id))
  1862. if run_id is not None and run is None:
  1863. logger.info("Run %s of archive %s is gone; not attaching its timelapse", run_id, archive_id)
  1864. return False
  1865. # Created only once the name has been vetted, so a rejected filename
  1866. # leaves nothing behind. A no-3MF archive has never had a directory of
  1867. # its own, and the timelapse can be the first thing to want one.
  1868. await asyncio.to_thread(lambda: timelapse_file.parent.mkdir(parents=True, exist_ok=True))
  1869. # A file of this name that the archive no longer points at is an earlier
  1870. # run's video, kept because another plate of a shared archive started
  1871. # (#3275). Writing over it would destroy the copy that was kept: the
  1872. # external-camera stitch always names its output layer_timelapse.mp4.
  1873. # Re-attaching the file the archive does point at still replaces it.
  1874. # A run's video is replaced only by that run's.
  1875. current = run.timelapse_path if run_id is not None and run is not None else archive.timelapse_path
  1876. timelapse_file = await asyncio.to_thread(_free_timelapse_name, timelapse_file, current, settings.base_dir)
  1877. await asyncio.to_thread(timelapse_file.write_bytes, timelapse_data)
  1878. relpath = str(timelapse_file.relative_to(settings.base_dir))
  1879. if run is not None:
  1880. run.timelapse_path = relpath
  1881. # A plate keeps its latest print's video: earlier prints of it that
  1882. # got theirs meanwhile (printing on other printers at once) lose them.
  1883. replaced = await drop_earlier_plate_videos(self.db, run) if run_id is not None and run is not None else []
  1884. if run_id is None or (run is not None and await is_newest_run(self.db, run)):
  1885. archive.timelapse_path = relpath
  1886. archive.timelapse_plate_id = plate_id
  1887. await self.db.commit()
  1888. for old_relpath in replaced:
  1889. try:
  1890. old_file = safe_join_under(settings.base_dir, old_relpath, http=False)
  1891. except PathTraversalError:
  1892. continue
  1893. await asyncio.to_thread(old_file.unlink, missing_ok=True)
  1894. logger.info("Replaced plate %s's earlier timelapse %s on archive %s", plate_id, old_relpath, archive_id)
  1895. await _announce_timelapse(archive_id, attached=True)
  1896. # For non-MP4 videos (e.g. AVI from P1S), kick off background conversion
  1897. if not filename.lower().endswith(".mp4"):
  1898. from backend.app.services import archive_work
  1899. # Held from the spawn, not from when the task first runs: a merge
  1900. # in between would move the video out from under it.
  1901. spawn_background_task(
  1902. archive_work.held_while(archive_id, _convert_held_timelapse(archive_id, timelapse_file)),
  1903. name=f"timelapse-convert-{archive_id}",
  1904. )
  1905. return True
  1906. def _free_timelapse_name(target: Path, current_relpath: str | None, base_dir: Path) -> Path:
  1907. """``target``, or ``<stem>_<n><suffix>`` beside it when ``target`` would
  1908. replace a video the archive does not point at (see ``attach_timelapse``).
  1909. A non-MP4 is converted to ``<stem>.mp4`` afterwards, so that name counts as
  1910. taken too.
  1911. """
  1912. def outputs(path: Path) -> set[Path]:
  1913. return {path.resolve(), path.with_suffix(".mp4").resolve()}
  1914. # Only compared against ``target``, which attach_timelapse has already
  1915. # safe-joined; nothing is read or written through this path.
  1916. current = (base_dir / current_relpath).resolve() if current_relpath else None # SEC-PATH-OK: comparison only
  1917. taken = {p for p in outputs(target) if p.exists()}
  1918. if not taken or current in taken:
  1919. return target
  1920. n = 2
  1921. while True:
  1922. candidate = target.with_name(f"{target.stem}_{n}{target.suffix}")
  1923. if not any(p.exists() for p in outputs(candidate)):
  1924. return candidate
  1925. n += 1
  1926. async def _announce_timelapse(archive_id: int, *, attached: bool) -> None:
  1927. """Tell open Archives pages that this archive's timelapse changed."""
  1928. from backend.app.core.websocket import ws_manager
  1929. try:
  1930. await ws_manager.send_archive_updated({"id": archive_id, "timelapse_attached": attached})
  1931. except Exception: # noqa: BLE001 -- a refresh hint must not fail the change it reports
  1932. logger.debug("Could not announce the timelapse of archive %s", archive_id, exc_info=True)
  1933. async def _convert_timelapse_to_mp4(archive_id: int, source_path: Path) -> None:
  1934. """Background task: convert non-MP4 timelapse (e.g. AVI from P1S) to MP4.
  1935. Runs with low CPU priority (-threads 1, nice) so it doesn't starve
  1936. other processes on resource-constrained devices like Raspberry Pi. Holds
  1937. the archive meanwhile (archive_work): a merge would move the video out
  1938. from under it.
  1939. """
  1940. from backend.app.services import archive_work
  1941. await archive_work.held_while(archive_id, _convert_held_timelapse(archive_id, source_path))
  1942. async def _convert_held_timelapse(archive_id: int, source_path: Path) -> None:
  1943. """``_convert_timelapse_to_mp4``'s conversion."""
  1944. import asyncio
  1945. from backend.app.core.database import async_session
  1946. from backend.app.services.camera import get_ffmpeg_path
  1947. logger = logging.getLogger(__name__)
  1948. ffmpeg = get_ffmpeg_path()
  1949. if not ffmpeg:
  1950. logger.info(
  1951. "FFmpeg not available, skipping timelapse conversion for archive %s (file saved as %s)",
  1952. archive_id,
  1953. source_path.suffix,
  1954. )
  1955. return
  1956. mp4_path = source_path.with_suffix(".mp4")
  1957. try:
  1958. cmd = [
  1959. ffmpeg,
  1960. "-y",
  1961. "-i",
  1962. str(source_path),
  1963. "-c:v",
  1964. "libx264",
  1965. "-preset",
  1966. "fast",
  1967. "-crf",
  1968. "23",
  1969. "-threads",
  1970. "1",
  1971. "-movflags",
  1972. "+faststart",
  1973. str(mp4_path),
  1974. ]
  1975. # Try with nice for lower CPU priority (standard on Linux/macOS)
  1976. try:
  1977. process = await asyncio.create_subprocess_exec(
  1978. "nice",
  1979. "-n",
  1980. "19",
  1981. *cmd,
  1982. stdout=asyncio.subprocess.PIPE,
  1983. stderr=asyncio.subprocess.PIPE,
  1984. )
  1985. except FileNotFoundError:
  1986. # nice not available (e.g. Windows), run without
  1987. process = await asyncio.create_subprocess_exec(
  1988. *cmd,
  1989. stdout=asyncio.subprocess.PIPE,
  1990. stderr=asyncio.subprocess.PIPE,
  1991. )
  1992. _, stderr = await process.communicate()
  1993. if process.returncode != 0:
  1994. logger.warning(
  1995. "Timelapse conversion failed for archive %s: %s",
  1996. archive_id,
  1997. summarize_ffmpeg_stderr(stderr) or NO_FFMPEG_OUTPUT,
  1998. )
  1999. if mp4_path.exists():
  2000. mp4_path.unlink()
  2001. return
  2002. # Update DB path to the new MP4 file
  2003. async with async_session() as db:
  2004. from backend.app.models.archive import PrintArchive
  2005. from backend.app.services.print_log import runs_with_timelapse
  2006. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  2007. archive = result.scalar_one_or_none()
  2008. source_rel = str(source_path.relative_to(settings.base_dir))
  2009. runs = await runs_with_timelapse(db, archive_id, source_rel) if archive is not None else []
  2010. # Only if the archive or one of its runs still shows this video:
  2011. # one deleted while it converted, or replaced by a reprint's, must
  2012. # not come back.
  2013. if archive is None or (archive.timelapse_path != source_rel and not runs):
  2014. logger.info(
  2015. "Archive %s no longer shows %s; discarding its MP4 conversion", archive_id, source_path.name
  2016. )
  2017. mp4_path.unlink(missing_ok=True)
  2018. return
  2019. mp4_rel = str(mp4_path.relative_to(settings.base_dir))
  2020. if archive.timelapse_path == source_rel:
  2021. archive.timelapse_path = mp4_rel
  2022. for run in runs:
  2023. run.timelapse_path = mp4_rel
  2024. await db.commit()
  2025. await _announce_timelapse(archive_id, attached=True)
  2026. # Remove original non-MP4 file
  2027. if source_path.exists():
  2028. source_path.unlink()
  2029. logger.info(
  2030. "Converted timelapse to MP4 for archive %s (%s → %s)",
  2031. archive_id,
  2032. source_path.name,
  2033. mp4_path.name,
  2034. )
  2035. except Exception as e:
  2036. logger.warning("Timelapse conversion error for archive %s: %s", archive_id, e)
  2037. if mp4_path.exists():
  2038. mp4_path.unlink()