bambu_ftp.py 47 KB

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