bambu_ftp.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. import asyncio
  2. import logging
  3. import os
  4. import socket
  5. import ssl
  6. from collections.abc import Awaitable, Callable
  7. from ftplib import FTP, FTP_TLS
  8. from io import BytesIO
  9. from pathlib import Path
  10. from typing import TypeVar
  11. logger = logging.getLogger(__name__)
  12. T = TypeVar("T")
  13. class ImplicitFTP_TLS(FTP_TLS):
  14. """FTP_TLS subclass for implicit FTPS (port 990) with model-specific SSL handling.
  15. X1C/P1S printers (vsFTPd) require SSL with session reuse on the data channel.
  16. A1/A1 Mini printers have issues with SSL on the data channel entirely and
  17. timeout waiting for transfer completion. Set skip_session_reuse=True for A1
  18. printers to skip SSL on the data channel (control channel remains encrypted).
  19. """
  20. def __init__(self, *args, skip_session_reuse: bool = False, **kwargs):
  21. super().__init__(*args, **kwargs)
  22. self._sock = None
  23. self.skip_session_reuse = skip_session_reuse
  24. self.ssl_context = ssl.create_default_context()
  25. self.ssl_context.check_hostname = False
  26. self.ssl_context.verify_mode = ssl.CERT_NONE
  27. def connect(self, host="", port=990, timeout=-999, source_address=None):
  28. """Connect to host, wrapping socket in TLS immediately (implicit FTPS)."""
  29. if host:
  30. self.host = host
  31. if port > 0:
  32. self.port = port
  33. if timeout != -999:
  34. self.timeout = timeout
  35. if source_address:
  36. self.source_address = source_address
  37. # Create and wrap socket immediately (implicit TLS)
  38. self.sock = socket.create_connection((self.host, self.port), self.timeout, source_address=self.source_address)
  39. self.sock = self.ssl_context.wrap_socket(self.sock, server_hostname=self.host)
  40. self.af = self.sock.family
  41. self.file = self.sock.makefile("r", encoding=self.encoding)
  42. self.welcome = self.getresp()
  43. return self.welcome
  44. def ntransfercmd(self, cmd, rest=None):
  45. """Override to wrap data connection in SSL for X1C/P1S only.
  46. X1C/P1S printers (vsFTPd) require SSL session reuse on the data channel.
  47. A1/A1 Mini printers have issues with SSL on the data channel entirely -
  48. they timeout waiting for the transfer completion response. For A1, we
  49. skip SSL wrapping on the data channel (control channel remains encrypted).
  50. """
  51. conn, size = FTP.ntransfercmd(self, cmd, rest)
  52. if self._prot_p and not self.skip_session_reuse:
  53. # X1C/P1S: Wrap data channel with SSL session reuse (required by vsFTPd)
  54. conn = self.ssl_context.wrap_socket(
  55. conn,
  56. server_hostname=self.host,
  57. session=self.sock.session,
  58. )
  59. # A1/A1 Mini (skip_session_reuse=True): Don't wrap data channel in SSL
  60. # The control channel remains encrypted via implicit FTPS
  61. return conn, size
  62. class BambuFTPClient:
  63. """FTP client for retrieving files from Bambu Lab printers."""
  64. FTP_PORT = 990
  65. DEFAULT_TIMEOUT = 30 # Default timeout in seconds (increased for A1 printers)
  66. # Models that need SSL session reuse disabled (A1 series has FTP issues with session reuse)
  67. SKIP_SESSION_REUSE_MODELS = ("A1", "A1 Mini")
  68. def __init__(
  69. self,
  70. ip_address: str,
  71. access_code: str,
  72. timeout: float | None = None,
  73. printer_model: str | None = None,
  74. ):
  75. self.ip_address = ip_address
  76. self.access_code = access_code
  77. self.timeout = timeout if timeout is not None else self.DEFAULT_TIMEOUT
  78. self.printer_model = printer_model
  79. self._ftp: ImplicitFTP_TLS | None = None
  80. def _should_skip_session_reuse(self) -> bool:
  81. """Check if this printer model needs SSL session reuse disabled."""
  82. if not self.printer_model:
  83. return False
  84. return self.printer_model in self.SKIP_SESSION_REUSE_MODELS
  85. def connect(self) -> bool:
  86. """Connect to the printer FTP server (implicit FTPS on port 990)."""
  87. try:
  88. skip_reuse = self._should_skip_session_reuse()
  89. logger.debug(
  90. f"FTP connecting to {self.ip_address}:{self.FTP_PORT} "
  91. f"(timeout={self.timeout}s, model={self.printer_model}, skip_session_reuse={skip_reuse})"
  92. )
  93. self._ftp = ImplicitFTP_TLS(skip_session_reuse=skip_reuse)
  94. self._ftp.connect(self.ip_address, self.FTP_PORT, timeout=self.timeout)
  95. logger.debug("FTP connected, logging in as bblp")
  96. self._ftp.login("bblp", self.access_code)
  97. logger.debug("FTP logged in, setting prot_p and passive mode")
  98. self._ftp.prot_p()
  99. self._ftp.set_pasv(True)
  100. logger.info(f"FTP connected successfully to {self.ip_address}")
  101. return True
  102. except Exception as e:
  103. logger.warning(f"FTP connection failed to {self.ip_address}: {e}")
  104. self._ftp = None
  105. return False
  106. def disconnect(self):
  107. """Disconnect from the FTP server."""
  108. if self._ftp:
  109. try:
  110. self._ftp.quit()
  111. except Exception:
  112. pass
  113. self._ftp = None
  114. def list_files(self, path: str = "/") -> list[dict]:
  115. """List files in a directory."""
  116. if not self._ftp:
  117. return []
  118. files = []
  119. try:
  120. self._ftp.cwd(path)
  121. items = []
  122. self._ftp.retrlines("LIST", items.append)
  123. for item in items:
  124. parts = item.split()
  125. if len(parts) >= 9:
  126. name = " ".join(parts[8:])
  127. is_dir = item.startswith("d")
  128. size = int(parts[4]) if not is_dir else 0
  129. # Parse modification time from FTP listing
  130. # Format: "Nov 30 10:15" or "Nov 30 2024"
  131. mtime = None
  132. try:
  133. from datetime import datetime
  134. month = parts[5]
  135. day = parts[6]
  136. time_or_year = parts[7]
  137. # Determine if it's time (HH:MM) or year
  138. if ":" in time_or_year:
  139. # Recent file: "Nov 30 10:15" - assume current year
  140. year = datetime.now().year
  141. time_str = f"{month} {day} {year} {time_or_year}"
  142. mtime = datetime.strptime(time_str, "%b %d %Y %H:%M")
  143. # If parsed date is in the future, use last year
  144. if mtime > datetime.now():
  145. mtime = mtime.replace(year=year - 1)
  146. else:
  147. # Older file: "Nov 30 2024" - no time, just date
  148. time_str = f"{month} {day} {time_or_year}"
  149. mtime = datetime.strptime(time_str, "%b %d %Y")
  150. except (ValueError, IndexError):
  151. pass
  152. file_entry = {
  153. "name": name,
  154. "is_directory": is_dir,
  155. "size": size,
  156. "path": f"{path.rstrip('/')}/{name}",
  157. }
  158. if mtime:
  159. file_entry["mtime"] = mtime
  160. files.append(file_entry)
  161. logger.debug(f"Listed {len(files)} files in {path}")
  162. except Exception as e:
  163. logger.info(f"FTP list_files failed for {path}: {e}")
  164. return files
  165. def download_file(self, remote_path: str) -> bytes | None:
  166. """Download a file from the printer."""
  167. if not self._ftp:
  168. return None
  169. try:
  170. buffer = BytesIO()
  171. self._ftp.retrbinary(f"RETR {remote_path}", buffer.write)
  172. return buffer.getvalue()
  173. except Exception:
  174. return None
  175. def download_to_file(self, remote_path: str, local_path: Path) -> bool:
  176. """Download a file from the printer to local filesystem."""
  177. if not self._ftp:
  178. logger.warning("download_to_file called but FTP not connected")
  179. return False
  180. try:
  181. local_path.parent.mkdir(parents=True, exist_ok=True)
  182. with open(local_path, "wb") as f:
  183. self._ftp.retrbinary(f"RETR {remote_path}", f.write)
  184. f.flush()
  185. os.fsync(f.fileno())
  186. file_size = local_path.stat().st_size if local_path.exists() else 0
  187. logger.info(f"Successfully downloaded {remote_path} to {local_path} ({file_size} bytes)")
  188. return True
  189. except Exception as e:
  190. # Log at INFO level so we can see failures in normal logs
  191. logger.info(f"FTP download failed for {remote_path}: {e}")
  192. # Clean up partial file if it exists
  193. if local_path.exists():
  194. try:
  195. local_path.unlink()
  196. except Exception:
  197. pass
  198. return False
  199. def upload_file(
  200. self,
  201. local_path: Path,
  202. remote_path: str,
  203. progress_callback: Callable[[int, int], None] | None = None,
  204. ) -> bool:
  205. """Upload a file to the printer with optional progress callback."""
  206. if not self._ftp:
  207. logger.warning("upload_file: FTP not connected")
  208. return False
  209. try:
  210. file_size = local_path.stat().st_size if local_path.exists() else 0
  211. logger.info(f"FTP uploading {local_path} ({file_size} bytes) to {remote_path}")
  212. uploaded = 0
  213. def on_block(block: bytes):
  214. nonlocal uploaded
  215. uploaded += len(block)
  216. if progress_callback:
  217. progress_callback(uploaded, file_size)
  218. with open(local_path, "rb") as f:
  219. self._ftp.storbinary(f"STOR {remote_path}", f, callback=on_block)
  220. logger.info(f"FTP upload complete: {remote_path}")
  221. return True
  222. except Exception as e:
  223. logger.error(f"FTP upload failed for {remote_path}: {e}")
  224. return False
  225. def upload_bytes(self, data: bytes, remote_path: str) -> bool:
  226. """Upload bytes to the printer."""
  227. if not self._ftp:
  228. return False
  229. try:
  230. buffer = BytesIO(data)
  231. self._ftp.storbinary(f"STOR {remote_path}", buffer)
  232. return True
  233. except Exception:
  234. return False
  235. def delete_file(self, remote_path: str) -> bool:
  236. """Delete a file from the printer."""
  237. if not self._ftp:
  238. return False
  239. try:
  240. self._ftp.delete(remote_path)
  241. return True
  242. except Exception as e:
  243. logger.warning(f"Failed to delete {remote_path}: {e}")
  244. return False
  245. def get_file_size(self, remote_path: str) -> int | None:
  246. """Get the size of a file."""
  247. if not self._ftp:
  248. return None
  249. try:
  250. return self._ftp.size(remote_path)
  251. except Exception:
  252. return None
  253. def get_storage_info(self) -> dict | None:
  254. """Get storage information from the printer."""
  255. if not self._ftp:
  256. return None
  257. result = {}
  258. # Try AVBL command (available space) - some FTP servers support this
  259. try:
  260. response = self._ftp.sendcmd("AVBL")
  261. logger.debug(f"AVBL response: {response}")
  262. # Response format: "213 <bytes available>"
  263. if response.startswith("213"):
  264. parts = response.split()
  265. if len(parts) >= 2:
  266. result["free_bytes"] = int(parts[1])
  267. except Exception as e:
  268. logger.debug(f"AVBL command not supported: {e}")
  269. # Try STAT command as fallback
  270. try:
  271. response = self._ftp.sendcmd("STAT")
  272. logger.debug(f"STAT response: {response}")
  273. except Exception:
  274. pass
  275. # Calculate used space by listing root directories
  276. try:
  277. total_used = 0
  278. dirs_to_scan = ["/cache", "/timelapse", "/model"]
  279. for dir_path in dirs_to_scan:
  280. try:
  281. self._ftp.cwd(dir_path)
  282. items = []
  283. self._ftp.retrlines("LIST", items.append)
  284. for item in items:
  285. parts = item.split()
  286. if len(parts) >= 5 and not item.startswith("d"):
  287. try:
  288. total_used += int(parts[4])
  289. except ValueError:
  290. pass
  291. except Exception:
  292. pass
  293. result["used_bytes"] = total_used
  294. except Exception:
  295. pass
  296. return result if result else None
  297. async def download_file_async(
  298. ip_address: str,
  299. access_code: str,
  300. remote_path: str,
  301. local_path: Path,
  302. timeout: float = 60.0,
  303. socket_timeout: float | None = None,
  304. printer_model: str | None = None,
  305. ) -> bool:
  306. """Async wrapper for downloading a file with timeout.
  307. Args:
  308. ip_address: Printer IP address
  309. access_code: Printer access code
  310. remote_path: Remote file path on printer
  311. local_path: Local path to save file
  312. timeout: Overall operation timeout (asyncio)
  313. socket_timeout: FTP socket timeout for slow connections (e.g., A1 printers)
  314. printer_model: Printer model for A1-specific workarounds
  315. """
  316. loop = asyncio.get_event_loop()
  317. def _download():
  318. client = BambuFTPClient(ip_address, access_code, timeout=socket_timeout, printer_model=printer_model)
  319. if client.connect():
  320. try:
  321. return client.download_to_file(remote_path, local_path)
  322. finally:
  323. client.disconnect()
  324. return False
  325. try:
  326. return await asyncio.wait_for(loop.run_in_executor(None, _download), timeout=timeout)
  327. except TimeoutError:
  328. logger.warning(f"FTP download timed out after {timeout}s for {remote_path}")
  329. return False
  330. async def download_file_try_paths_async(
  331. ip_address: str,
  332. access_code: str,
  333. remote_paths: list[str],
  334. local_path: Path,
  335. socket_timeout: float | None = None,
  336. printer_model: str | None = None,
  337. ) -> bool:
  338. """Try downloading a file from multiple paths using a single connection.
  339. Args:
  340. socket_timeout: FTP socket timeout for slow connections (e.g., A1 printers)
  341. printer_model: Printer model for A1-specific workarounds
  342. """
  343. loop = asyncio.get_event_loop()
  344. def _download():
  345. client = BambuFTPClient(ip_address, access_code, timeout=socket_timeout, printer_model=printer_model)
  346. if not client.connect():
  347. return False
  348. try:
  349. return any(client.download_to_file(remote_path, local_path) for remote_path in remote_paths)
  350. finally:
  351. client.disconnect()
  352. return await loop.run_in_executor(None, _download)
  353. async def upload_file_async(
  354. ip_address: str,
  355. access_code: str,
  356. local_path: Path,
  357. remote_path: str,
  358. timeout: float = 600.0,
  359. progress_callback: Callable[[int, int], None] | None = None,
  360. socket_timeout: float | None = None,
  361. printer_model: str | None = None,
  362. ) -> bool:
  363. """Async wrapper for uploading a file with timeout and progress callback.
  364. Args:
  365. ip_address: Printer IP address
  366. access_code: Printer access code
  367. local_path: Local file path to upload
  368. remote_path: Remote path on printer
  369. timeout: Overall operation timeout (asyncio)
  370. progress_callback: Optional callback for progress updates
  371. socket_timeout: FTP socket timeout for slow connections (e.g., A1 printers)
  372. printer_model: Printer model for A1-specific workarounds
  373. """
  374. loop = asyncio.get_event_loop()
  375. def _upload():
  376. logger.info(
  377. f"FTP connecting to {ip_address} for upload (model={printer_model}, socket_timeout={socket_timeout}s)..."
  378. )
  379. client = BambuFTPClient(ip_address, access_code, timeout=socket_timeout, printer_model=printer_model)
  380. if client.connect():
  381. logger.info(f"FTP connected to {ip_address}")
  382. try:
  383. return client.upload_file(local_path, remote_path, progress_callback)
  384. finally:
  385. client.disconnect()
  386. logger.warning(f"FTP connection failed to {ip_address}")
  387. return False
  388. try:
  389. return await asyncio.wait_for(loop.run_in_executor(None, _upload), timeout=timeout)
  390. except TimeoutError:
  391. logger.warning(f"FTP upload timed out after {timeout}s for {remote_path}")
  392. return False
  393. async def list_files_async(
  394. ip_address: str,
  395. access_code: str,
  396. path: str = "/",
  397. timeout: float = 30.0,
  398. socket_timeout: float | None = None,
  399. printer_model: str | None = None,
  400. ) -> list[dict]:
  401. """Async wrapper for listing files with timeout.
  402. Args:
  403. socket_timeout: FTP socket timeout for slow connections (e.g., A1 printers)
  404. printer_model: Printer model for A1-specific workarounds
  405. """
  406. loop = asyncio.get_event_loop()
  407. def _list():
  408. client = BambuFTPClient(ip_address, access_code, timeout=socket_timeout, printer_model=printer_model)
  409. if client.connect():
  410. try:
  411. return client.list_files(path)
  412. finally:
  413. client.disconnect()
  414. return []
  415. try:
  416. return await asyncio.wait_for(loop.run_in_executor(None, _list), timeout=timeout)
  417. except TimeoutError:
  418. logger.warning(f"FTP list_files timed out after {timeout}s for {path}")
  419. return []
  420. async def delete_file_async(
  421. ip_address: str,
  422. access_code: str,
  423. remote_path: str,
  424. socket_timeout: float | None = None,
  425. printer_model: str | None = None,
  426. ) -> bool:
  427. """Async wrapper for deleting a file.
  428. Args:
  429. socket_timeout: FTP socket timeout for slow connections (e.g., A1 printers)
  430. printer_model: Printer model for A1-specific workarounds
  431. """
  432. loop = asyncio.get_event_loop()
  433. def _delete():
  434. client = BambuFTPClient(ip_address, access_code, timeout=socket_timeout, printer_model=printer_model)
  435. if client.connect():
  436. try:
  437. return client.delete_file(remote_path)
  438. finally:
  439. client.disconnect()
  440. return False
  441. return await loop.run_in_executor(None, _delete)
  442. async def download_file_bytes_async(
  443. ip_address: str,
  444. access_code: str,
  445. remote_path: str,
  446. socket_timeout: float | None = None,
  447. printer_model: str | None = None,
  448. ) -> bytes | None:
  449. """Async wrapper for downloading file as bytes.
  450. Args:
  451. socket_timeout: FTP socket timeout for slow connections (e.g., A1 printers)
  452. printer_model: Printer model for A1-specific workarounds
  453. """
  454. loop = asyncio.get_event_loop()
  455. def _download():
  456. client = BambuFTPClient(ip_address, access_code, timeout=socket_timeout, printer_model=printer_model)
  457. if client.connect():
  458. try:
  459. return client.download_file(remote_path)
  460. finally:
  461. client.disconnect()
  462. return None
  463. return await loop.run_in_executor(None, _download)
  464. async def get_storage_info_async(
  465. ip_address: str,
  466. access_code: str,
  467. socket_timeout: float | None = None,
  468. printer_model: str | None = None,
  469. ) -> dict | None:
  470. """Async wrapper for getting storage info.
  471. Args:
  472. socket_timeout: FTP socket timeout for slow connections (e.g., A1 printers)
  473. printer_model: Printer model for A1-specific workarounds
  474. """
  475. loop = asyncio.get_event_loop()
  476. def _get_storage():
  477. client = BambuFTPClient(ip_address, access_code, timeout=socket_timeout, printer_model=printer_model)
  478. if client.connect():
  479. try:
  480. return client.get_storage_info()
  481. finally:
  482. client.disconnect()
  483. return None
  484. return await loop.run_in_executor(None, _get_storage)
  485. async def get_ftp_retry_settings() -> tuple[bool, int, float, float]:
  486. """Get FTP retry settings from database.
  487. Returns:
  488. Tuple of (retry_enabled, retry_count, retry_delay, timeout)
  489. """
  490. from backend.app.api.routes.settings import get_setting
  491. from backend.app.core.database import async_session
  492. async with async_session() as db:
  493. enabled = (await get_setting(db, "ftp_retry_enabled") or "true") == "true"
  494. count = int(await get_setting(db, "ftp_retry_count") or "3")
  495. delay = float(await get_setting(db, "ftp_retry_delay") or "2")
  496. timeout = float(await get_setting(db, "ftp_timeout") or "30")
  497. return enabled, count, delay, timeout
  498. async def with_ftp_retry(
  499. operation: Callable[..., Awaitable[T]],
  500. *args,
  501. max_retries: int = 3,
  502. retry_delay: float = 2.0,
  503. operation_name: str = "FTP operation",
  504. **kwargs,
  505. ) -> T | None:
  506. """Execute FTP operation with retry logic.
  507. Args:
  508. operation: Async function to execute
  509. *args: Positional arguments for the operation
  510. max_retries: Number of retry attempts (default: 3)
  511. retry_delay: Seconds to wait between retries (default: 2.0)
  512. operation_name: Name for logging purposes
  513. **kwargs: Keyword arguments for the operation
  514. Returns:
  515. Result of the operation, or None if all attempts fail
  516. """
  517. last_error = None
  518. for attempt in range(max_retries + 1):
  519. try:
  520. result = await operation(*args, **kwargs)
  521. # Check for "falsy" success indicators
  522. if result not in (False, None, []):
  523. if attempt > 0:
  524. logger.info(f"{operation_name} succeeded on attempt {attempt + 1}/{max_retries + 1}")
  525. return result
  526. # Operation returned failure indicator
  527. if attempt > 0:
  528. logger.info(f"{operation_name} attempt {attempt + 1}/{max_retries + 1} returned failure")
  529. except Exception as e:
  530. last_error = e
  531. logger.warning(f"{operation_name} attempt {attempt + 1}/{max_retries + 1} failed: {e}")
  532. # Don't wait after the last attempt
  533. if attempt < max_retries:
  534. logger.info(f"{operation_name} will retry in {retry_delay}s...")
  535. await asyncio.sleep(retry_delay)
  536. logger.error(f"{operation_name} failed after {max_retries + 1} attempts")
  537. if last_error:
  538. logger.debug(f"Last error: {last_error}")
  539. return None