archive.py 53 KB

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