bambu_ftp.py 48 KB

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