bambu_ftp.py 22 KB

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