FCLauncher/FCLauncher.old/src-tauri/src/ftp.rs

63 lines
2.1 KiB
Rust
Raw Normal View History

2024-06-22 17:52:25 -06:00
use std::io::Write;
use std::path::PathBuf;
use suppaftp::FtpError;
use suppaftp::NativeTlsFtpStream;
use suppaftp::NativeTlsConnector;
use suppaftp::native_tls::Certificate;
use suppaftp::native_tls::TlsConnector;
fn ftp_connection_anonymous() -> Result<NativeTlsFtpStream, FtpError>{
ftp_connection("anonymous", "anonymous@")
}
2024-07-12 21:11:52 -06:00
pub fn test_cred(username: &str, password: &str) -> bool{
return ftp_connection(username, password).is_ok();
}
2024-06-22 17:52:25 -06:00
fn ftp_connection(username: &str, password: &str) -> Result<NativeTlsFtpStream, FtpError>{
2024-07-12 21:49:00 -06:00
let ftp_stream = NativeTlsFtpStream::connect("gitea.piwalker.net:21")?;
2024-06-22 17:52:25 -06:00
let cert = include_bytes!("../res/vsftpd.crt");
let cert = Certificate::from_pem(cert).unwrap();
let mut ftp_stream = ftp_stream.into_secure(NativeTlsConnector::from(TlsConnector::builder().add_root_certificate(cert).build().unwrap()), "gitea.piwalker.net").unwrap();
2024-07-12 21:11:52 -06:00
let result = ftp_stream.login(username, password);
if result.is_ok() {
return Ok(ftp_stream);
}
Err(result.unwrap_err())
2024-06-22 17:52:25 -06:00
}
2024-06-23 15:07:25 -06:00
pub fn ftp_retr(window: Option<tauri::Window>, file: PathBuf , mut writer: impl Write, mut callback: impl FnMut(Option<tauri::Window>, usize, usize)) -> Result<bool, FtpError> {
2024-07-12 21:49:00 -06:00
let mut ftp_stream = ftp_connection_anonymous()?;
2024-06-22 17:52:25 -06:00
let file = file.to_str().unwrap().replace("\\", "/");
let size = ftp_stream.size(&file)?;
let mut total = 0;
ftp_stream.retr(file.as_str(), |stream| {
let mut tx_bytes = [0u8; 1500];
loop {
let bytes_read = stream.read(&mut tx_bytes).unwrap();
writer.write_all(&mut tx_bytes.split_at(bytes_read).0).unwrap();
total += bytes_read;
if total == size || bytes_read == 0 {
2024-06-23 15:07:25 -06:00
callback(window.clone(), 0, size);
2024-06-22 17:52:25 -06:00
break;
}else{
2024-06-23 15:07:25 -06:00
callback(window.clone(), bytes_read, size);
2024-06-22 17:52:25 -06:00
}
}
Ok(true)
})?;
Ok(true)
}
pub fn ftp_get_size(file: PathBuf) -> Result<usize, FtpError> {
let mut stream = ftp_connection_anonymous()?;
stream.size(file.to_str().unwrap().replace("\\", "/"))
}