bambu/
file.rs

1//! A module for interacting with BambuLab file server.
2pub(crate) mod ftp;
3
4use std::io;
5
6use crate::tls::NoVerifier;
7use ftp::{metadata::FileMetadata, FtpClient};
8
9/// An async FTPS file client, similar to the Python version using curl.
10/// It can list files in a directory and download specific files.
11pub struct FileClient {
12    hostname: String,
13    access_code: String,
14}
15
16impl FileClient {
17    /// Create a new FileClient.
18    ///
19    /// * `hostname`: The FTP(S) server hostname.
20    /// * `access_code`: The password to use with user "bblp".
21    /// * `serial`: Some identifier (unused in this snippet, but kept for parity).
22    /// * `insecure`: If `true`, client will skip certificate validation.
23    pub fn new(hostname: impl Into<String>, access_code: impl Into<String>) -> Self {
24        Self {
25            hostname: hostname.into(),
26            access_code: access_code.into(),
27        }
28    }
29
30    /// List files in the given `directory`, filtering by `extension`.
31    /// This is roughly equivalent to running:
32    /// `curl --ftp-pasv --insecure ftps://HOSTNAME/DIRECTORY --user bblp:ACCESS_CODE`.
33    pub async fn get_files(&self, directory: &str) -> io::Result<Vec<FileMetadata>> {
34        // Connect to the server
35        // let mut ftp_stream = self.connect_and_login().await?;
36        let mut client = FtpClient::connect(
37            self.hostname.clone(),
38            "bblp".to_string(),
39            self.access_code.clone(),
40        )
41        .await
42        .unwrap();
43        let _message = client.authenticate().await?;
44        let files = client.list_files(directory).await?;
45        client.quit().await?;
46        Ok(files)
47    }
48}