bambu_ftp.py 18 KB

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