veeso / suppaftp

a super FTP/FTPS client library for Rust with support for both passive and active mode
Apache License 2.0
121 stars 31 forks source link

Question: example function utilizing retr #85

Open tadghh opened 5 months ago

tadghh commented 5 months ago

I'm trying to download a single file using the retr function, but I can't seem to grasp the reader: F part. I attempted to get ChatGPT to explain but that resulted in "dyn async_std::io::Read cannot be unpinned".

Here is what I have so far, it contains the issue mentioned


pub async fn download_file2(file_name: String) -> Result<(), suppaftp::FtpError> {
    //NOTE need to include port
    let mut ftp_stream = AsyncFtpStream::connect("127.0.0.1:21").await?;

    ftp_stream.login("user", "pass").await.expect("no login");

    //TODO env for this path, it also needs to prefix with /public
    ftp_stream.cwd("/path/to/images").await?;

    match ftp_stream
        .retr(file_name, |stream| {
            let mut buffer = Vec::new();
            // Cannot be unpinned
            stream.read_vectored(&mut buffer);
            Ok(buffer)
        })
        .await
    {
        Ok(data) => {
            // Write the file to the local file system
            eprintln!("shush");
        }
        Err(e) => {
            eprintln!("Failed to retrieve file: {}", e);
        }
    }
    // Logout and quit the session
    ftp_stream.quit().await?;
    Ok(())
}

I'm still learning so perhaps im missing something silly. Thanks in advance.

jaydevelopsstuff commented 3 months ago

Ever figure this out?

tadghh commented 3 months ago

Ever figure this out?

yeah decided to not use async


pub async fn download_file(file_name: String, ftp_path: String) -> Result<(), suppaftp::FtpError> {
    let ftp_server = env!("BLOG_FTP_ADDRESS");

    let mut ftp_stream = FtpStream::connect(ftp_server).unwrap();

    ftp_stream
        .login(env!("BLOG_FTP_USERNAME"), env!("BLOG_FTP_PASSWORD"))
        .expect("FTP Login failed");

    ftp_stream
        .cwd(ftp_path)
        .expect("FTP failed to change working directory");

    let mut ftp_download_buffer = Vec::new();

    ftp_stream
        .retr(&file_name, |stream| {
            stream
                .read_to_end(&mut ftp_download_buffer)
                .map_err(|e| FtpError::ConnectionError(e))
        })
        .expect(&format!(
            "Failed to write stream{}",
            ftp_stream.pwd().unwrap()
        ));

    ftp_stream.quit().expect("FTP Failed to quit");

    let file_path = get_cache_dir().join(file_name);

    write(file_path, ftp_download_buffer)
        .await
        .expect("Tauri failed to write to cache");
    Ok(())
}