bambu_ftp.py 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. import asyncio
  2. import ftplib # nosec B402
  3. import logging
  4. import os
  5. import socket
  6. import ssl
  7. import time
  8. from collections.abc import Awaitable, Callable
  9. from ftplib import FTP, FTP_TLS # nosec B402
  10. from io import BytesIO
  11. from pathlib import Path
  12. from typing import TypeVar
  13. logger = logging.getLogger(__name__)
  14. T = TypeVar("T")
  15. class FileNotOnPrinterError(Exception):
  16. """Raised when a remote FTP path returns 550 (file not found).
  17. 550 means the file does not exist at that path — retrying the same path
  18. will never succeed. Callers use this sentinel with with_ftp_retry's
  19. non_retry_exceptions to immediately move on to the next candidate path
  20. instead of burning the full retry budget (up to 11 × 30s per path) on
  21. a lookup that cannot recover.
  22. """
  23. class ImplicitFTP_TLS(FTP_TLS):
  24. """FTP_TLS subclass for implicit FTPS (port 990) with model-specific SSL handling.
  25. X1C/P1S printers (vsFTPd) require SSL with session reuse on the data channel.
  26. A1/A1 Mini printers have issues with SSL on the data channel entirely and
  27. timeout waiting for transfer completion. Set skip_session_reuse=True for A1
  28. printers to skip SSL on the data channel (control channel remains encrypted).
  29. """
  30. def __init__(self, *args, skip_session_reuse: bool = False, **kwargs):
  31. super().__init__(*args, **kwargs)
  32. self._sock = None
  33. self.skip_session_reuse = skip_session_reuse
  34. self.ssl_context = ssl.create_default_context()
  35. self.ssl_context.check_hostname = False
  36. self.ssl_context.verify_mode = ssl.CERT_NONE
  37. def connect(self, host="", port=990, timeout=-999, source_address=None):
  38. """Connect to host, wrapping socket in TLS immediately (implicit FTPS)."""
  39. if host:
  40. self.host = host
  41. if port > 0:
  42. self.port = port
  43. if timeout != -999:
  44. self.timeout = timeout
  45. if source_address:
  46. self.source_address = source_address
  47. # Create and wrap socket immediately (implicit TLS)
  48. self.sock = socket.create_connection((self.host, self.port), self.timeout, source_address=self.source_address)
  49. self.sock = self.ssl_context.wrap_socket(self.sock, server_hostname=self.host)
  50. self.af = self.sock.family
  51. self.file = self.sock.makefile("r", encoding=self.encoding)
  52. self.welcome = self.getresp()
  53. return self.welcome
  54. def ntransfercmd(self, cmd, rest=None):
  55. """Override to wrap data connection in SSL for X1C/P1S only.
  56. X1C/P1S printers (vsFTPd) require SSL session reuse on the data channel.
  57. A1/A1 Mini printers have issues with SSL on the data channel entirely -
  58. they timeout waiting for the transfer completion response. For A1, we
  59. skip SSL wrapping on the data channel (control channel remains encrypted).
  60. """
  61. conn, size = FTP.ntransfercmd(self, cmd, rest)
  62. if self._prot_p and not self.skip_session_reuse:
  63. # X1C/P1S: Wrap data channel with SSL session reuse (required by vsFTPd)
  64. conn = self.ssl_context.wrap_socket(
  65. conn,
  66. server_hostname=self.host,
  67. session=self.sock.session,
  68. )
  69. # A1/A1 Mini (skip_session_reuse=True): Don't wrap data channel in SSL
  70. # The control channel remains encrypted via implicit FTPS
  71. return conn, size
  72. class BambuFTPClient:
  73. """FTP client for retrieving files from Bambu Lab printers."""
  74. FTP_PORT = 990
  75. # Default timeout in seconds (increased for A1 printers)
  76. DEFAULT_TIMEOUT = 30
  77. # Models that may need SSL mode fallback (try prot_p first, fall back to prot_c)
  78. # These models have varying FTP SSL behavior depending on firmware version
  79. A1_MODELS = ("A1", "A1 Mini")
  80. # Chunk size for manual upload transfer (64KB)
  81. # Smaller chunks provide smoother progress reporting — at typical printer FTP
  82. # speeds (~50-100KB/s) this gives a progress update roughly every second.
  83. CHUNK_SIZE = 64 * 1024
  84. # Cache for working FTP modes per printer IP
  85. # Maps IP -> "prot_p" or "prot_c"
  86. _mode_cache: dict[str, str] = {}
  87. def __init__(
  88. self,
  89. ip_address: str,
  90. access_code: str,
  91. timeout: float | None = None,
  92. printer_model: str | None = None,
  93. force_prot_c: bool = False,
  94. ):
  95. self.ip_address = ip_address
  96. self.access_code = access_code
  97. self.timeout = timeout if timeout is not None else self.DEFAULT_TIMEOUT
  98. self.printer_model = printer_model
  99. self.force_prot_c = force_prot_c
  100. self._ftp: ImplicitFTP_TLS | None = None
  101. def _is_a1_model(self) -> bool:
  102. """Check if this is an A1 series printer."""
  103. if not self.printer_model:
  104. return False
  105. return self.printer_model in self.A1_MODELS
  106. def _get_cached_mode(self) -> str | None:
  107. """Get cached FTP mode for this printer."""
  108. return self._mode_cache.get(self.ip_address)
  109. @classmethod
  110. def cache_mode(cls, ip_address: str, mode: str):
  111. """Cache the working FTP mode for a printer."""
  112. cls._mode_cache[ip_address] = mode
  113. logger.info("FTP mode cached for %s: %s", ip_address, mode)
  114. def _should_use_prot_c(self) -> bool:
  115. """Determine if we should use prot_c (clear) mode."""
  116. # If explicitly forced, use prot_c
  117. if self.force_prot_c:
  118. return True
  119. # Check cache first
  120. cached = self._get_cached_mode()
  121. if cached:
  122. return cached == "prot_c"
  123. # Default: try prot_p first (will fall back if needed)
  124. return False
  125. def connect(self) -> bool:
  126. """Connect to the printer FTP server (implicit FTPS on port 990)."""
  127. try:
  128. use_prot_c = self._should_use_prot_c()
  129. logger.debug(
  130. f"FTP connecting to {self.ip_address}:{self.FTP_PORT} "
  131. f"(timeout={self.timeout}s, model={self.printer_model}, prot_c={use_prot_c})"
  132. )
  133. self._ftp = ImplicitFTP_TLS(skip_session_reuse=use_prot_c)
  134. self._ftp.connect(self.ip_address, self.FTP_PORT, timeout=self.timeout)
  135. logger.debug("FTP connected, logging in as bblp")
  136. self._ftp.login("bblp", self.access_code)
  137. if use_prot_c:
  138. # Use clear (unencrypted) data channel
  139. logger.debug("FTP logged in, setting prot_c (clear) and passive mode")
  140. self._ftp.prot_c()
  141. else:
  142. # Use protected (encrypted) data channel with session reuse
  143. logger.debug("FTP logged in, setting prot_p (protected) and passive mode")
  144. self._ftp.prot_p()
  145. self._ftp.set_pasv(True)
  146. # Log welcome message for debugging
  147. if hasattr(self._ftp, "welcome") and self._ftp.welcome:
  148. logger.debug("FTP server welcome: %s", self._ftp.welcome)
  149. logger.info(
  150. f"FTP connected successfully to {self.ip_address} (model={self.printer_model}, prot_c={use_prot_c})"
  151. )
  152. return True
  153. except ftplib.error_perm as e:
  154. logger.warning("FTP connection permission error to %s: %s", self.ip_address, e)
  155. self._ftp = None
  156. return False
  157. except TimeoutError as e:
  158. logger.warning("FTP connection timed out to %s: %s", self.ip_address, e)
  159. self._ftp = None
  160. return False
  161. except ssl.SSLError as e:
  162. logger.warning("FTP SSL error connecting to %s: %s", self.ip_address, e)
  163. self._ftp = None
  164. return False
  165. except (OSError, ftplib.Error) as e:
  166. logger.warning("FTP connection failed to %s: %s (type: %s)", self.ip_address, e, type(e).__name__)
  167. self._ftp = None
  168. return False
  169. def disconnect(self):
  170. """Disconnect from the FTP server."""
  171. if self._ftp:
  172. try:
  173. self._ftp.quit()
  174. except (OSError, ftplib.Error, EOFError):
  175. pass # Best-effort FTP cleanup; connection may already be closed
  176. self._ftp = None
  177. def list_files(self, path: str = "/") -> list[dict]:
  178. """List files in a directory."""
  179. if not self._ftp:
  180. return []
  181. files = []
  182. try:
  183. self._ftp.cwd(path)
  184. items = []
  185. self._ftp.retrlines("LIST", items.append)
  186. for item in items:
  187. parts = item.split()
  188. if len(parts) >= 9:
  189. name = " ".join(parts[8:])
  190. is_dir = item.startswith("d")
  191. size = int(parts[4]) if not is_dir else 0
  192. # Parse modification time from FTP listing
  193. # Format: "Nov 30 10:15" or "Nov 30 2024"
  194. mtime = None
  195. try:
  196. from datetime import datetime
  197. month = parts[5]
  198. day = parts[6]
  199. time_or_year = parts[7]
  200. # Determine if it's time (HH:MM) or year
  201. if ":" in time_or_year:
  202. # Recent file: "Nov 30 10:15" - assume current year
  203. year = datetime.now().year
  204. time_str = f"{month} {day} {year} {time_or_year}"
  205. mtime = datetime.strptime(time_str, "%b %d %Y %H:%M")
  206. # If parsed date is in the future, use last year
  207. if mtime > datetime.now():
  208. mtime = mtime.replace(year=year - 1)
  209. else:
  210. # Older file: "Nov 30 2024" - no time, just date
  211. time_str = f"{month} {day} {time_or_year}"
  212. mtime = datetime.strptime(time_str, "%b %d %Y")
  213. except (ValueError, IndexError):
  214. pass # Non-critical: mtime parsing is best-effort; file entry works without it
  215. file_entry = {
  216. "name": name,
  217. "is_directory": is_dir,
  218. "size": size,
  219. "path": f"{path.rstrip('/')}/{name}",
  220. }
  221. if mtime:
  222. file_entry["mtime"] = mtime
  223. files.append(file_entry)
  224. logger.debug("Listed %s files in %s", len(files), path)
  225. except (OSError, ftplib.Error) as e:
  226. logger.info("FTP list_files failed for %s: %s", path, e)
  227. return files
  228. def download_file(self, remote_path: str) -> bytes | None:
  229. """Download a file from the printer."""
  230. if not self._ftp:
  231. return None
  232. try:
  233. buffer = BytesIO()
  234. self._ftp.retrbinary(f"RETR {remote_path}", buffer.write)
  235. return buffer.getvalue()
  236. except (OSError, ftplib.Error):
  237. return None
  238. def download_to_file(self, remote_path: str, local_path: Path) -> bool:
  239. """Download a file from the printer to local filesystem."""
  240. if not self._ftp:
  241. logger.warning("download_to_file called but FTP not connected")
  242. return False
  243. try:
  244. local_path.parent.mkdir(parents=True, exist_ok=True)
  245. with open(local_path, "wb") as f:
  246. self._ftp.retrbinary(f"RETR {remote_path}", f.write)
  247. f.flush()
  248. os.fsync(f.fileno())
  249. file_size = local_path.stat().st_size if local_path.exists() else 0
  250. if file_size == 0:
  251. logger.warning("FTP download returned 0 bytes for %s", remote_path)
  252. if local_path.exists():
  253. local_path.unlink()
  254. return False
  255. logger.info("Successfully downloaded %s to %s (%s bytes)", remote_path, local_path, file_size)
  256. return True
  257. except (OSError, ftplib.Error) as e:
  258. # Clean up partial file if it exists
  259. if local_path.exists():
  260. try:
  261. local_path.unlink()
  262. except OSError:
  263. pass # Best-effort partial file cleanup; not critical if removal fails
  264. # 550 means the file is not at this path. Surface as a sentinel so
  265. # with_ftp_retry can abandon this path immediately and the caller
  266. # can advance to the next candidate instead of retrying 11× at
  267. # 30s intervals (the pattern that cost #972's reporter ~48min).
  268. if isinstance(e, ftplib.error_perm) and str(e).startswith("550"):
  269. logger.info("FTP download failed for %s: %s (not on printer)", remote_path, e)
  270. raise FileNotOnPrinterError(f"{remote_path}: {e}") from e
  271. # Log at INFO level so we can see failures in normal logs
  272. logger.info("FTP download failed for %s: %s", remote_path, e)
  273. return False
  274. def diagnose_storage(self) -> dict:
  275. """Run storage diagnostics and return results. For debugging upload issues."""
  276. results = {
  277. "connected": self._ftp is not None,
  278. "can_list_root": False,
  279. "root_files": [],
  280. "can_list_cache": False,
  281. "storage_info": None,
  282. "pwd": None,
  283. "errors": [],
  284. }
  285. if not self._ftp:
  286. results["errors"].append("FTP not connected")
  287. return results
  288. # Try to get current directory
  289. try:
  290. results["pwd"] = self._ftp.pwd()
  291. logger.debug("FTP current directory: %s", results["pwd"])
  292. except (OSError, ftplib.Error) as e:
  293. results["errors"].append(f"PWD failed: {e}")
  294. logger.debug("FTP PWD failed: %s", e)
  295. # Try to list root directory
  296. try:
  297. self._ftp.cwd("/")
  298. items = []
  299. self._ftp.retrlines("LIST", items.append)
  300. results["can_list_root"] = True
  301. results["root_files"] = items[:10] # First 10 entries
  302. logger.debug("FTP root listing (%s items): %s", len(items), items[:5])
  303. except (OSError, ftplib.Error) as e:
  304. results["errors"].append(f"LIST / failed: {e}")
  305. logger.debug("FTP LIST / failed: %s", e)
  306. # Try to list /cache (should exist on all printers)
  307. try:
  308. self._ftp.cwd("/cache")
  309. items = []
  310. self._ftp.retrlines("LIST", items.append)
  311. results["can_list_cache"] = True
  312. logger.debug("FTP /cache listing: %s items", len(items))
  313. except (OSError, ftplib.Error) as e:
  314. results["errors"].append(f"LIST /cache failed: {e}")
  315. logger.debug("FTP LIST /cache failed: %s", e)
  316. # Try to get storage info
  317. try:
  318. results["storage_info"] = self.get_storage_info()
  319. logger.debug("FTP storage info: %s", results["storage_info"])
  320. except (OSError, ftplib.Error) as e:
  321. results["errors"].append(f"Storage info failed: {e}")
  322. return results
  323. def upload_file(
  324. self,
  325. local_path: Path,
  326. remote_path: str,
  327. progress_callback: Callable[[int, int], None] | None = None,
  328. ) -> bool:
  329. """Upload a file to the printer with optional progress callback."""
  330. if not self._ftp:
  331. logger.warning("upload_file: FTP not connected")
  332. return False
  333. try:
  334. file_size = local_path.stat().st_size if local_path.exists() else 0
  335. logger.info("FTP uploading %s (%s bytes) to %s", local_path, file_size, remote_path)
  336. uploaded = 0
  337. callback_exception: Exception | None = None
  338. # Use manual transfer instead of storbinary() for A1 compatibility
  339. # A1 printers have issues with storbinary's voidresp() hanging after transfer
  340. with open(local_path, "rb") as f:
  341. logger.debug("FTP STOR command starting for %s", remote_path)
  342. t0 = time.monotonic()
  343. conn = self._ftp.transfercmd(f"STOR {remote_path}")
  344. logger.info(
  345. "FTP data channel ready in %.1fs (PASV + TLS handshake)",
  346. time.monotonic() - t0,
  347. )
  348. # Set explicit socket options for reliable transfer
  349. conn.setblocking(True)
  350. conn.settimeout(self.timeout)
  351. try:
  352. while True:
  353. chunk = f.read(self.CHUNK_SIZE)
  354. if not chunk:
  355. logger.debug("FTP upload: final chunk reached")
  356. break
  357. conn.sendall(chunk)
  358. uploaded += len(chunk)
  359. logger.debug("FTP upload progress: %s/%s bytes", uploaded, file_size)
  360. if progress_callback:
  361. try:
  362. progress_callback(uploaded, file_size)
  363. except Exception as e:
  364. callback_exception = e
  365. logger.info(
  366. "FTP upload callback requested stop for %s at %s/%s bytes: %s",
  367. remote_path,
  368. uploaded,
  369. file_size,
  370. e,
  371. )
  372. break
  373. except OSError as e:
  374. logger.error("FTP connection lost during upload: %s", e)
  375. raise
  376. finally:
  377. try:
  378. conn.close()
  379. except OSError:
  380. pass
  381. # Wait for the server's 226 "Transfer complete" response to confirm
  382. # the file has been flushed to the SD card. Without this, the printer
  383. # may try to read an incomplete file when the print command is sent,
  384. # causing 0500-C010 "MicroSD Card read/write exception" errors.
  385. # See: https://bugs.python.org/issue25458 (ftplib response desync)
  386. try:
  387. old_timeout = self._ftp.sock.gettimeout()
  388. # Use a generous timeout — H2D printers can take 30+ seconds
  389. # to send the 226 after the data channel closes.
  390. self._ftp.sock.settimeout(max(self.timeout, 60))
  391. try:
  392. resp = self._ftp.voidresp()
  393. logger.info("FTP STOR confirmed for %s: %s", remote_path, resp.strip())
  394. finally:
  395. self._ftp.sock.settimeout(old_timeout)
  396. except Exception as e:
  397. # Timeout or error reading 226 — log but proceed, the data
  398. # was fully sent so the file is likely on the SD card.
  399. logger.warning(
  400. "FTP STOR confirmation not received for %s (proceeding): %s (%s)",
  401. remote_path,
  402. e,
  403. type(e).__name__,
  404. )
  405. if callback_exception is not None:
  406. cleanup_ok = False
  407. try:
  408. cleanup_ok = self.delete_file(remote_path)
  409. except Exception as cleanup_error:
  410. logger.warning("FTP cancel cleanup failed for %s: %s", remote_path, cleanup_error)
  411. if cleanup_ok:
  412. logger.info("FTP cancel cleanup succeeded for %s", remote_path)
  413. raise callback_exception
  414. raise RuntimeError(
  415. f"Upload cancelled but failed to remove partial file {remote_path} from printer"
  416. ) from callback_exception
  417. elapsed = time.monotonic() - t0
  418. speed_kbs = (file_size / 1024) / elapsed if elapsed > 0 else 0
  419. logger.info(
  420. "FTP upload complete: %s (%s bytes in %.1fs, %.0f KB/s)",
  421. remote_path,
  422. file_size,
  423. elapsed,
  424. speed_kbs,
  425. )
  426. return True
  427. except ftplib.error_perm as e:
  428. # Permanent FTP error (4xx/5xx response)
  429. error_code = str(e)[:3] if str(e) else "unknown"
  430. logger.error("FTP upload failed for %s: %s (error code: %s)", remote_path, e, error_code)
  431. if error_code == "553":
  432. logger.error(
  433. "FTP 553 error - Could not create file. Possible causes: "
  434. "1) No SD card inserted, 2) SD card full, 3) SD card not formatted correctly (needs FAT32/exFAT), "
  435. "4) Printer busy/not ready, 5) File path issue"
  436. )
  437. elif error_code == "550":
  438. logger.error("FTP 550 error - File/directory not found or permission denied")
  439. elif error_code == "552":
  440. logger.error("FTP 552 error - Storage quota exceeded (SD card full?)")
  441. return False
  442. except (OSError, ftplib.Error) as e:
  443. logger.error("FTP upload failed for %s: %s (type: %s)", remote_path, e, type(e).__name__)
  444. return False
  445. def upload_bytes(self, data: bytes, remote_path: str) -> bool:
  446. """Upload bytes to the printer."""
  447. if not self._ftp:
  448. return False
  449. try:
  450. # Use manual transfer instead of storbinary() for A1 compatibility
  451. conn = self._ftp.transfercmd(f"STOR {remote_path}")
  452. conn.setblocking(True)
  453. conn.settimeout(self.timeout)
  454. try:
  455. # Send data in chunks
  456. offset = 0
  457. while offset < len(data):
  458. chunk = data[offset : offset + self.CHUNK_SIZE]
  459. conn.sendall(chunk)
  460. offset += len(chunk)
  461. except OSError as e:
  462. logger.error("FTP connection lost during upload_bytes: %s", e)
  463. raise
  464. finally:
  465. try:
  466. conn.close()
  467. except OSError:
  468. pass
  469. # Wait for 226 confirmation (see upload_file for rationale)
  470. try:
  471. old_timeout = self._ftp.sock.gettimeout()
  472. self._ftp.sock.settimeout(max(self.timeout, 60))
  473. try:
  474. self._ftp.voidresp()
  475. finally:
  476. self._ftp.sock.settimeout(old_timeout)
  477. except Exception:
  478. pass # Best-effort — data was sent, proceed
  479. return True
  480. except (OSError, ftplib.Error):
  481. return False
  482. def delete_file(self, remote_path: str) -> bool:
  483. """Delete a file from the printer."""
  484. if not self._ftp:
  485. return False
  486. try:
  487. self._ftp.delete(remote_path)
  488. return True
  489. except (OSError, ftplib.Error) as e:
  490. logger.warning("Failed to delete %s: %s", remote_path, e)
  491. return False
  492. def get_file_size(self, remote_path: str) -> int | None:
  493. """Get the size of a file."""
  494. if not self._ftp:
  495. return None
  496. try:
  497. return self._ftp.size(remote_path)
  498. except (OSError, ftplib.Error):
  499. return None
  500. def get_storage_info(self) -> dict | None:
  501. """Get storage information from the printer."""
  502. if not self._ftp:
  503. return None
  504. result = {}
  505. # Try AVBL command (available space) - some FTP servers support this
  506. try:
  507. response = self._ftp.sendcmd("AVBL")
  508. logger.debug("AVBL response: %s", response)
  509. # Response format: "213 <bytes available>"
  510. if response.startswith("213"):
  511. parts = response.split()
  512. if len(parts) >= 2:
  513. result["free_bytes"] = int(parts[1])
  514. except (OSError, ftplib.Error) as e:
  515. logger.debug("AVBL command not supported: %s", e)
  516. # Try STAT command as fallback
  517. try:
  518. response = self._ftp.sendcmd("STAT")
  519. logger.debug("STAT response: %s", response)
  520. except (OSError, ftplib.Error):
  521. pass # Both AVBL and STAT unsupported; storage info will rely on directory scan
  522. # Calculate used space by listing root directories
  523. try:
  524. total_used = 0
  525. dirs_to_scan = ["/cache", "/timelapse", "/model", "/data", "/data/Metadata", "/"]
  526. for dir_path in dirs_to_scan:
  527. try:
  528. self._ftp.cwd(dir_path)
  529. items = []
  530. self._ftp.retrlines("LIST", items.append)
  531. for item in items:
  532. parts = item.split()
  533. if len(parts) >= 5 and not item.startswith("d"):
  534. try:
  535. total_used += int(parts[4])
  536. except ValueError:
  537. pass # Skip entries with non-numeric size fields
  538. except (OSError, ftplib.Error):
  539. pass # Directory may not exist on this printer model; skip it
  540. result["used_bytes"] = total_used
  541. except (OSError, ftplib.Error):
  542. pass # Storage scan failed; return whatever info was collected above
  543. return result if result else None
  544. # Shared 3MF download cache (#972).
  545. #
  546. # Both the cover thumbnail endpoint (api/routes/printers.py) and the archive
  547. # metadata flow (main.py) fetch the same 3MF file over FTP during a print.
  548. # On slow / contended links (A1 Wi-Fi, large files) the duplicate transfers
  549. # compete for the printer's single FTP socket and trigger 425 "can't open
  550. # data channel" errors, feeding back into cause-2's retry storm.
  551. #
  552. # This cache stores the local path of a successfully-downloaded 3MF keyed
  553. # by (printer_id, normalized_name). Whichever flow downloads first populates
  554. # the cache; the other flow reuses the file read-only. Evicted on print
  555. # completion so a later print with the same name re-downloads fresh bytes.
  556. _threemf_path_cache: dict[tuple[int, str], Path] = {}
  557. def normalize_3mf_name(name: str) -> str:
  558. """Collapse various 3MF filename variants to a cache key.
  559. Bambu tooling produces names as bare subtask ("Part"), with .3mf, with
  560. .gcode.3mf, or (Studio-normalized) with spaces → underscores. All of
  561. these refer to the same print job on the same printer, so they must
  562. hash to the same cache key.
  563. """
  564. # Lowercase first so .3MF / .GCODE.3MF variants strip cleanly — a
  565. # real-world case since Windows-side tooling sometimes uppercases
  566. # extensions.
  567. cleaned = name.strip().lower().replace(".gcode.3mf", "").replace(".gcode", "").replace(".3mf", "")
  568. return cleaned.replace(" ", "_")
  569. def cache_3mf_download(printer_id: int, name: str, local_path: Path) -> None:
  570. """Record a successfully-downloaded 3MF so a sibling flow can reuse it."""
  571. _threemf_path_cache[(printer_id, normalize_3mf_name(name))] = local_path
  572. def get_cached_3mf(printer_id: int, name: str) -> Path | None:
  573. """Return a cached 3MF path for this printer/name if the file still exists."""
  574. key = (printer_id, normalize_3mf_name(name))
  575. cached = _threemf_path_cache.get(key)
  576. if cached and cached.exists() and cached.stat().st_size > 0:
  577. return cached
  578. # Evict dead entry — the file was cleaned up (temp dir clean, manual
  579. # deletion, restart) so the cache value is no longer usable.
  580. if cached:
  581. _threemf_path_cache.pop(key, None)
  582. return None
  583. def clear_3mf_cache(printer_id: int | None = None, delete_files: bool = True) -> None:
  584. """Drop cache entries for one printer (or all with None).
  585. When ``delete_files`` is True (default) the on-disk 3MF is removed as well
  586. — called from on_print_complete so temp files don't accumulate across
  587. prints. Tests that want to inspect the cache contents disable this.
  588. """
  589. def _maybe_unlink(path: Path) -> None:
  590. if delete_files and path.exists():
  591. try:
  592. path.unlink()
  593. except OSError as exc:
  594. logger.debug("3MF cache cleanup skipped %s: %s", path, exc)
  595. if printer_id is None:
  596. for path in list(_threemf_path_cache.values()):
  597. _maybe_unlink(path)
  598. _threemf_path_cache.clear()
  599. return
  600. for key in [k for k in _threemf_path_cache if k[0] == printer_id]:
  601. _maybe_unlink(_threemf_path_cache[key])
  602. _threemf_path_cache.pop(key, None)
  603. async def download_file_async(
  604. ip_address: str,
  605. access_code: str,
  606. remote_path: str,
  607. local_path: Path,
  608. timeout: float = 60.0,
  609. socket_timeout: float | None = None,
  610. printer_model: str | None = None,
  611. ) -> bool:
  612. """Async wrapper for downloading a file with timeout.
  613. For A1/A1 Mini printers, automatically tries prot_p first, then falls back
  614. to prot_c if the download fails. The working mode is cached for future operations.
  615. Args:
  616. ip_address: Printer IP address
  617. access_code: Printer access code
  618. remote_path: Remote file path on printer
  619. local_path: Local path to save file
  620. timeout: Overall operation timeout (asyncio)
  621. socket_timeout: FTP socket timeout for slow connections (e.g., A1 printers)
  622. printer_model: Printer model for A1-specific workarounds
  623. """
  624. loop = asyncio.get_event_loop()
  625. is_a1 = printer_model in BambuFTPClient.A1_MODELS if printer_model else False
  626. # Per-attempt completion state: asyncio.wait_for cannot cancel
  627. # run_in_executor threads, so on timeout the executor may still complete
  628. # the download after we stop waiting. The thread flips `success` to True
  629. # ONLY after the file is fully written — a post-timeout check lets us
  630. # salvage the download without mistaking an in-progress partial write
  631. # for a completed one. Each attempt gets its own dict so a zombie from
  632. # an earlier attempt can't flip the flag for a later one.
  633. def _download(force_prot_c: bool, completion: dict) -> bool:
  634. mode_str = "prot_c" if force_prot_c else "prot_p"
  635. client = BambuFTPClient(
  636. ip_address, access_code, timeout=socket_timeout, printer_model=printer_model, force_prot_c=force_prot_c
  637. )
  638. if client.connect():
  639. try:
  640. result = client.download_to_file(remote_path, local_path)
  641. if result:
  642. BambuFTPClient.cache_mode(ip_address, mode_str)
  643. completion["success"] = True
  644. return result
  645. finally:
  646. client.disconnect()
  647. return False
  648. async def _run(force_prot_c: bool) -> bool:
  649. completion = {"success": False}
  650. try:
  651. return await asyncio.wait_for(
  652. loop.run_in_executor(None, lambda: _download(force_prot_c, completion)), timeout=timeout
  653. )
  654. except TimeoutError:
  655. # Give the zombie executor thread a brief moment to finish if it
  656. # was already close to done. Only salvage when the thread has
  657. # signalled genuine success — checking file size alone would
  658. # mistake an in-progress partial write for a completed download.
  659. await asyncio.sleep(0.5)
  660. if completion["success"] and local_path.exists() and local_path.stat().st_size > 0:
  661. logger.info(
  662. "FTP download wait_for timed out after %ss for %s, but thread completed (%s bytes) — salvaging",
  663. timeout,
  664. remote_path,
  665. local_path.stat().st_size,
  666. )
  667. return True
  668. logger.warning("FTP download timed out after %ss for %s", timeout, remote_path)
  669. return False
  670. # Check if we have a cached mode for this printer
  671. cached_mode = BambuFTPClient._mode_cache.get(ip_address)
  672. if cached_mode:
  673. force_prot_c = cached_mode == "prot_c"
  674. return await _run(force_prot_c)
  675. # No cached mode - try prot_p first
  676. if await _run(False):
  677. return True
  678. # Download failed - for A1 models, try prot_c fallback
  679. if is_a1:
  680. logger.info("FTP download failed with prot_p for A1 model, trying prot_c fallback...")
  681. return await _run(True)
  682. return False
  683. async def download_file_try_paths_async(
  684. ip_address: str,
  685. access_code: str,
  686. remote_paths: list[str],
  687. local_path: Path,
  688. socket_timeout: float | None = None,
  689. printer_model: str | None = None,
  690. ) -> bool:
  691. """Try downloading a file from multiple paths using a single connection.
  692. Args:
  693. socket_timeout: FTP socket timeout for slow connections (e.g., A1 printers)
  694. printer_model: Printer model for A1-specific workarounds
  695. """
  696. loop = asyncio.get_event_loop()
  697. def _download():
  698. client = BambuFTPClient(ip_address, access_code, timeout=socket_timeout, printer_model=printer_model)
  699. if not client.connect():
  700. return False
  701. try:
  702. # FileNotOnPrinterError signals "try the next path", not "give up" —
  703. # this function's whole purpose is to walk a list of candidates
  704. # over one connection. Only a real transport error should bubble.
  705. for remote_path in remote_paths:
  706. try:
  707. if client.download_to_file(remote_path, local_path):
  708. return True
  709. except FileNotOnPrinterError:
  710. continue
  711. return False
  712. finally:
  713. client.disconnect()
  714. return await loop.run_in_executor(None, _download)
  715. async def upload_file_async(
  716. ip_address: str,
  717. access_code: str,
  718. local_path: Path,
  719. remote_path: str,
  720. timeout: float = 600.0,
  721. progress_callback: Callable[[int, int], None] | None = None,
  722. socket_timeout: float | None = None,
  723. printer_model: str | None = None,
  724. ) -> bool:
  725. """Async wrapper for uploading a file with timeout and progress callback.
  726. For A1/A1 Mini printers, automatically tries prot_p first, then falls back
  727. to prot_c if the upload fails. The working mode is cached for future uploads.
  728. Args:
  729. ip_address: Printer IP address
  730. access_code: Printer access code
  731. local_path: Local file path to upload
  732. remote_path: Remote path on printer
  733. timeout: Overall operation timeout (asyncio)
  734. progress_callback: Optional callback for progress updates
  735. socket_timeout: FTP socket timeout for slow connections (e.g., A1 printers)
  736. printer_model: Printer model for A1-specific workarounds
  737. """
  738. loop = asyncio.get_event_loop()
  739. is_a1 = printer_model in BambuFTPClient.A1_MODELS if printer_model else False
  740. def _upload(force_prot_c: bool = False) -> bool:
  741. mode_str = "prot_c" if force_prot_c else "prot_p"
  742. logger.info(
  743. f"FTP connecting to {ip_address} for upload (model={printer_model}, "
  744. f"mode={mode_str}, socket_timeout={socket_timeout}s)..."
  745. )
  746. client = BambuFTPClient(
  747. ip_address, access_code, timeout=socket_timeout, printer_model=printer_model, force_prot_c=force_prot_c
  748. )
  749. if client.connect():
  750. logger.info("FTP connected to %s", ip_address)
  751. try:
  752. result = client.upload_file(local_path, remote_path, progress_callback)
  753. if result:
  754. # Cache the working mode
  755. BambuFTPClient.cache_mode(ip_address, mode_str)
  756. return result
  757. finally:
  758. client.disconnect()
  759. logger.warning("FTP connection failed to %s", ip_address)
  760. return False
  761. try:
  762. # Check if we have a cached mode for this printer
  763. cached_mode = BambuFTPClient._mode_cache.get(ip_address)
  764. if cached_mode:
  765. # Use cached mode
  766. force_prot_c = cached_mode == "prot_c"
  767. return await asyncio.wait_for(loop.run_in_executor(None, lambda: _upload(force_prot_c)), timeout=timeout)
  768. # No cached mode - try prot_p first
  769. result = await asyncio.wait_for(loop.run_in_executor(None, lambda: _upload(False)), timeout=timeout)
  770. if result:
  771. return True
  772. # Upload failed - for A1 models, try prot_c fallback
  773. if is_a1:
  774. logger.info("FTP upload failed with prot_p for A1 model, trying prot_c fallback...")
  775. result = await asyncio.wait_for(loop.run_in_executor(None, lambda: _upload(True)), timeout=timeout)
  776. return result
  777. return False
  778. except TimeoutError:
  779. logger.warning("FTP upload timed out after %ss for %s", timeout, remote_path)
  780. return False
  781. async def list_files_async(
  782. ip_address: str,
  783. access_code: str,
  784. path: str = "/",
  785. timeout: float = 30.0,
  786. socket_timeout: float | None = None,
  787. printer_model: str | None = None,
  788. ) -> list[dict]:
  789. """Async wrapper for listing files with timeout.
  790. Args:
  791. socket_timeout: FTP socket timeout for slow connections (e.g., A1 printers)
  792. printer_model: Printer model for A1-specific workarounds
  793. """
  794. loop = asyncio.get_event_loop()
  795. def _list():
  796. client = BambuFTPClient(ip_address, access_code, timeout=socket_timeout, printer_model=printer_model)
  797. if client.connect():
  798. try:
  799. return client.list_files(path)
  800. finally:
  801. client.disconnect()
  802. return []
  803. try:
  804. return await asyncio.wait_for(loop.run_in_executor(None, _list), timeout=timeout)
  805. except TimeoutError:
  806. logger.warning("FTP list_files timed out after %ss for %s", timeout, path)
  807. return []
  808. async def delete_file_async(
  809. ip_address: str,
  810. access_code: str,
  811. remote_path: str,
  812. socket_timeout: float | None = None,
  813. printer_model: str | None = None,
  814. ) -> bool:
  815. """Async wrapper for deleting a file.
  816. Args:
  817. socket_timeout: FTP socket timeout for slow connections (e.g., A1 printers)
  818. printer_model: Printer model for A1-specific workarounds
  819. """
  820. loop = asyncio.get_event_loop()
  821. def _delete():
  822. client = BambuFTPClient(ip_address, access_code, timeout=socket_timeout, printer_model=printer_model)
  823. if client.connect():
  824. try:
  825. return client.delete_file(remote_path)
  826. finally:
  827. client.disconnect()
  828. return False
  829. return await loop.run_in_executor(None, _delete)
  830. async def download_file_bytes_async(
  831. ip_address: str,
  832. access_code: str,
  833. remote_path: str,
  834. socket_timeout: float | None = None,
  835. printer_model: str | None = None,
  836. ) -> bytes | None:
  837. """Async wrapper for downloading file as bytes.
  838. Args:
  839. socket_timeout: FTP socket timeout for slow connections (e.g., A1 printers)
  840. printer_model: Printer model for A1-specific workarounds
  841. """
  842. loop = asyncio.get_event_loop()
  843. def _download():
  844. client = BambuFTPClient(ip_address, access_code, timeout=socket_timeout, printer_model=printer_model)
  845. if client.connect():
  846. try:
  847. return client.download_file(remote_path)
  848. finally:
  849. client.disconnect()
  850. return None
  851. return await loop.run_in_executor(None, _download)
  852. async def get_storage_info_async(
  853. ip_address: str,
  854. access_code: str,
  855. socket_timeout: float | None = None,
  856. printer_model: str | None = None,
  857. ) -> dict | None:
  858. """Async wrapper for getting storage info.
  859. Args:
  860. socket_timeout: FTP socket timeout for slow connections (e.g., A1 printers)
  861. printer_model: Printer model for A1-specific workarounds
  862. """
  863. loop = asyncio.get_event_loop()
  864. def _get_storage():
  865. client = BambuFTPClient(ip_address, access_code, timeout=socket_timeout, printer_model=printer_model)
  866. if client.connect():
  867. try:
  868. return client.get_storage_info()
  869. finally:
  870. client.disconnect()
  871. return None
  872. return await loop.run_in_executor(None, _get_storage)
  873. async def get_ftp_retry_settings() -> tuple[bool, int, float, float]:
  874. """Get FTP retry settings from database.
  875. Returns:
  876. Tuple of (retry_enabled, retry_count, retry_delay, timeout)
  877. """
  878. from backend.app.api.routes.settings import get_setting
  879. from backend.app.core.database import async_session
  880. async with async_session() as db:
  881. enabled = (await get_setting(db, "ftp_retry_enabled") or "true") == "true"
  882. count = int(await get_setting(db, "ftp_retry_count") or "3")
  883. delay = float(await get_setting(db, "ftp_retry_delay") or "2")
  884. timeout = float(await get_setting(db, "ftp_timeout") or "30")
  885. return enabled, count, delay, timeout
  886. async def with_ftp_retry(
  887. operation: Callable[..., Awaitable[T]],
  888. *args,
  889. max_retries: int = 3,
  890. retry_delay: float = 2.0,
  891. operation_name: str = "FTP operation",
  892. non_retry_exceptions: tuple[type[BaseException], ...] = (),
  893. **kwargs,
  894. ) -> T | None:
  895. """Execute FTP operation with retry logic.
  896. Args:
  897. operation: Async function to execute
  898. *args: Positional arguments for the operation
  899. max_retries: Number of retry attempts (default: 3)
  900. retry_delay: Seconds to wait between retries (default: 2.0)
  901. operation_name: Name for logging purposes
  902. non_retry_exceptions: Exception types that should immediately abort retries
  903. **kwargs: Keyword arguments for the operation
  904. Returns:
  905. Result of the operation, or None if all attempts fail
  906. """
  907. last_error = None
  908. for attempt in range(max_retries + 1):
  909. try:
  910. result = await operation(*args, **kwargs)
  911. # Check for "falsy" success indicators
  912. if result not in (False, None, []):
  913. if attempt > 0:
  914. logger.info("%s succeeded on attempt %s/%s", operation_name, attempt + 1, max_retries + 1)
  915. return result
  916. # Operation returned failure indicator
  917. if attempt > 0:
  918. logger.info("%s attempt %s/%s returned failure", operation_name, attempt + 1, max_retries + 1)
  919. except Exception as e:
  920. if non_retry_exceptions and isinstance(e, non_retry_exceptions):
  921. raise
  922. last_error = e
  923. logger.warning("%s attempt %s/%s failed: %s", operation_name, attempt + 1, max_retries + 1, e)
  924. # Don't wait after the last attempt
  925. if attempt < max_retries:
  926. logger.info("%s will retry in %ss...", operation_name, retry_delay)
  927. await asyncio.sleep(retry_delay)
  928. logger.error("%s failed after %s attempts", operation_name, max_retries + 1)
  929. if last_error:
  930. logger.debug("Last error: %s", last_error)
  931. return None