bambu_ftp.py 22 KB

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