Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: example function utilizing retr #85

Open
tadghh opened this issue May 27, 2024 · 2 comments
Open

Question: example function utilizing retr #85

tadghh opened this issue May 27, 2024 · 2 comments
Assignees
Labels
question Further information is requested

Comments

@tadghh
Copy link
Contributor

tadghh commented May 27, 2024

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.

@tadghh tadghh added the question Further information is requested label May 27, 2024
@jaydevelopsstuff
Copy link

Ever figure this out?

@tadghh
Copy link
Contributor Author

tadghh commented Aug 2, 2024

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(())
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants