mirror of
https://github.com/veeso/termscp.git
synced 2025-12-07 09:36:00 -08:00
Removed progress callback (not a priority atm)
This commit is contained in:
@@ -23,17 +23,14 @@
|
||||
*
|
||||
*/
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::fs::File;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::fs::FsEntry;
|
||||
|
||||
// Transfers
|
||||
pub mod sftp_transfer;
|
||||
|
||||
// Types
|
||||
pub type ProgressCallback = dyn Fn(usize, usize);
|
||||
|
||||
/// ## FileTransferProtocol
|
||||
///
|
||||
/// This enum defines the different transfer protocol available in TermSCP
|
||||
@@ -65,7 +62,9 @@ pub enum FileTransferError {
|
||||
impl std::fmt::Display for FileTransferError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
let err: String = match self {
|
||||
FileTransferError::AuthenticationFailed => String::from("Authentication failed: bad credentials"),
|
||||
FileTransferError::AuthenticationFailed => {
|
||||
String::from("Authentication failed: bad credentials")
|
||||
}
|
||||
FileTransferError::BadAddress => String::from("Bad address syntax"),
|
||||
FileTransferError::ConnectionError => String::from("Connection error"),
|
||||
FileTransferError::DirStatFailed => String::from("Could not stat directory"),
|
||||
@@ -86,12 +85,17 @@ impl std::fmt::Display for FileTransferError {
|
||||
/// File transfer trait must be implemented by all the file transfers and defines the method used by a generic file transfer
|
||||
|
||||
pub trait FileTransfer {
|
||||
|
||||
/// ### connect
|
||||
///
|
||||
/// Connect to the remote server
|
||||
|
||||
fn connect(&mut self, address: String, port: u16, username: Option<String>, password: Option<String>) -> Result<(), FileTransferError>;
|
||||
fn connect(
|
||||
&mut self,
|
||||
address: String,
|
||||
port: u16,
|
||||
username: Option<String>,
|
||||
password: Option<String>,
|
||||
) -> Result<(), FileTransferError>;
|
||||
|
||||
/// ### disconnect
|
||||
///
|
||||
@@ -142,11 +146,10 @@ pub trait FileTransfer {
|
||||
/// Send file to remote
|
||||
/// File name is referred to the name of the file as it will be saved
|
||||
/// Data contains the file data
|
||||
fn send_file(&self, file_name: &Path, file: &mut File, prog_cb: Option<Box<ProgressCallback>>) -> Result<(), FileTransferError>;
|
||||
fn send_file(&self, file_name: &Path, file: &mut File) -> Result<(), FileTransferError>;
|
||||
|
||||
/// ### recv_file
|
||||
///
|
||||
/// Receive file from remote with provided name
|
||||
fn recv_file(&self, file_name: &Path, dest_file: &mut File, prog_cb: Option<Box<ProgressCallback>>) -> Result<(), FileTransferError>;
|
||||
|
||||
fn recv_file(&self, file_name: &Path, dest_file: &mut File) -> Result<(), FileTransferError>;
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
extern crate ssh2;
|
||||
|
||||
// Locals
|
||||
use super::{FileTransfer, FileTransferError, ProgressCallback};
|
||||
use super::{FileTransfer, FileTransferError};
|
||||
use crate::fs::{FsDirectory, FsEntry, FsFile};
|
||||
|
||||
// Includes
|
||||
@@ -380,11 +380,11 @@ impl FileTransfer for SftpFileTransfer {
|
||||
// Get abs path of entry
|
||||
let abs_src: PathBuf = match file {
|
||||
FsEntry::Directory(dir) => dir.abs_path.clone(),
|
||||
FsEntry::File(file) => file.abs_path.clone()
|
||||
FsEntry::File(file) => file.abs_path.clone(),
|
||||
};
|
||||
match sftp.rename(abs_src.as_path(), abs_dst.as_path(), None) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(_) => Err(FileTransferError::FileCreateDenied)
|
||||
Err(_) => Err(FileTransferError::FileCreateDenied),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -395,18 +395,13 @@ impl FileTransfer for SftpFileTransfer {
|
||||
/// Send file to remote
|
||||
/// File name is referred to the name of the file as it will be saved
|
||||
/// Data contains the file data
|
||||
fn send_file(
|
||||
&self,
|
||||
file_name: &Path,
|
||||
file: &mut File,
|
||||
prog_cb: Option<Box<ProgressCallback>>,
|
||||
) -> Result<(), FileTransferError> {
|
||||
fn send_file(&self, file_name: &Path, file: &mut File) -> Result<(), FileTransferError> {
|
||||
match self.sftp.as_ref() {
|
||||
None => Err(FileTransferError::UninitializedSession),
|
||||
Some(sftp) => {
|
||||
let remote_path: PathBuf = self.get_abs_path(file_name);
|
||||
// Get file size
|
||||
let file_size: usize = file.seek(std::io::SeekFrom::End(0)).unwrap_or(0) as usize;
|
||||
//let file_size: usize = file.seek(std::io::SeekFrom::End(0)).unwrap_or(0) as usize;
|
||||
// rewind
|
||||
if let Err(err) = file.seek(std::io::SeekFrom::Start(0)) {
|
||||
return Err(FileTransferError::IoErr(err));
|
||||
@@ -414,13 +409,13 @@ impl FileTransfer for SftpFileTransfer {
|
||||
// Open remote file
|
||||
match sftp.create(remote_path.as_path()) {
|
||||
Ok(mut rhnd) => {
|
||||
let mut total_bytes_written: usize = 0;
|
||||
//let mut total_bytes_written: usize = 0;
|
||||
loop {
|
||||
// Read till you can
|
||||
let mut buffer: [u8; 8192] = [0; 8192];
|
||||
match file.read(&mut buffer) {
|
||||
Ok(bytes_read) => {
|
||||
total_bytes_written += bytes_read;
|
||||
//total_bytes_written += bytes_read;
|
||||
if bytes_read == 0 {
|
||||
break;
|
||||
} else {
|
||||
@@ -428,10 +423,6 @@ impl FileTransfer for SftpFileTransfer {
|
||||
if let Err(err) = rhnd.write(&buffer) {
|
||||
return Err(FileTransferError::IoErr(err));
|
||||
}
|
||||
// Call callback
|
||||
if let Some(ref cb) = prog_cb {
|
||||
cb(total_bytes_written, file_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => return Err(FileTransferError::IoErr(err)),
|
||||
@@ -448,12 +439,7 @@ impl FileTransfer for SftpFileTransfer {
|
||||
/// ### recv_file
|
||||
///
|
||||
/// Receive file from remote with provided name
|
||||
fn recv_file(
|
||||
&self,
|
||||
file_name: &Path,
|
||||
dest_file: &mut File,
|
||||
prog_cb: Option<Box<ProgressCallback>>,
|
||||
) -> Result<(), FileTransferError> {
|
||||
fn recv_file(&self, file_name: &Path, dest_file: &mut File) -> Result<(), FileTransferError> {
|
||||
match self.sftp.as_ref() {
|
||||
None => Err(FileTransferError::UninitializedSession),
|
||||
Some(sftp) => {
|
||||
@@ -465,20 +451,19 @@ impl FileTransfer for SftpFileTransfer {
|
||||
// Open remote file
|
||||
match sftp.open(remote_path.as_path()) {
|
||||
Ok(mut rhnd) => {
|
||||
let file_size: usize =
|
||||
rhnd.seek(std::io::SeekFrom::End(0)).unwrap_or(0) as usize;
|
||||
// let file_size: usize = rhnd.seek(std::io::SeekFrom::End(0)).unwrap_or(0) as usize;
|
||||
// rewind
|
||||
if let Err(err) = rhnd.seek(std::io::SeekFrom::Start(0)) {
|
||||
return Err(FileTransferError::IoErr(err));
|
||||
};
|
||||
// Write local file
|
||||
let mut total_bytes_written: usize = 0;
|
||||
// let mut total_bytes_written: usize = 0;
|
||||
loop {
|
||||
// Read till you can
|
||||
let mut buffer: [u8; 8192] = [0; 8192];
|
||||
match rhnd.read(&mut buffer) {
|
||||
Ok(bytes_read) => {
|
||||
total_bytes_written += bytes_read;
|
||||
// total_bytes_written += bytes_read;
|
||||
if bytes_read == 0 {
|
||||
break;
|
||||
} else {
|
||||
@@ -486,10 +471,6 @@ impl FileTransfer for SftpFileTransfer {
|
||||
if let Err(err) = dest_file.write(&buffer) {
|
||||
return Err(FileTransferError::IoErr(err));
|
||||
}
|
||||
// Call callback
|
||||
if let Some(ref cb) = prog_cb {
|
||||
cb(total_bytes_written, file_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => return Err(FileTransferError::IoErr(err)),
|
||||
@@ -696,11 +677,7 @@ mod tests {
|
||||
.unwrap();
|
||||
// Receive file
|
||||
assert!(client
|
||||
.recv_file(
|
||||
PathBuf::from("readme.txt").as_path(),
|
||||
&mut dst_file_hnd,
|
||||
Some(Box::new(progress_callback))
|
||||
)
|
||||
.recv_file(PathBuf::from("readme.txt").as_path(), &mut dst_file_hnd)
|
||||
.is_ok());
|
||||
// Disconnect
|
||||
assert!(client.disconnect().is_ok());
|
||||
@@ -730,11 +707,7 @@ mod tests {
|
||||
.unwrap();
|
||||
// Receive file
|
||||
assert!(client
|
||||
.recv_file(
|
||||
PathBuf::from("omar.txt").as_path(),
|
||||
&mut dst_file_hnd,
|
||||
Some(Box::new(progress_callback))
|
||||
)
|
||||
.recv_file(PathBuf::from("omar.txt").as_path(), &mut dst_file_hnd)
|
||||
.is_err());
|
||||
// Disconnect
|
||||
assert!(client.disconnect().is_ok());
|
||||
@@ -767,11 +740,7 @@ mod tests {
|
||||
let mut dst_file_hnd: File = OpenOptions::new().read(true).open(dst_file.path()).unwrap();
|
||||
// Receive file
|
||||
assert!(client
|
||||
.recv_file(
|
||||
PathBuf::from("readme.txt").as_path(),
|
||||
&mut dst_file_hnd,
|
||||
Some(Box::new(progress_callback))
|
||||
)
|
||||
.recv_file(PathBuf::from("readme.txt").as_path(), &mut dst_file_hnd)
|
||||
.is_err());
|
||||
// Disconnect
|
||||
assert!(client.disconnect().is_ok());
|
||||
@@ -794,8 +763,4 @@ mod tests {
|
||||
assert!(client.disconnect().is_ok());
|
||||
}
|
||||
*/
|
||||
|
||||
fn progress_callback(it: usize, max: usize) {
|
||||
assert!(it <= max);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ use crate::filetransfer::FileTransferProtocol;
|
||||
|
||||
// File transfer
|
||||
use crate::filetransfer::sftp_transfer::SftpFileTransfer;
|
||||
use crate::filetransfer::{FileTransfer, ProgressCallback};
|
||||
use crate::filetransfer::FileTransfer;
|
||||
use crate::fs::FsEntry;
|
||||
|
||||
// Includes
|
||||
@@ -55,9 +55,6 @@ use tui::{
|
||||
};
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
// Holder for current upload progress
|
||||
static mut UPLOAD_PROGRESS: f64 = 0.0; // VERY VERY BAD CODING
|
||||
|
||||
// Types
|
||||
type DialogCallback = fn(&mut FileTransferActivity);
|
||||
type OnInputSubmitCallback = fn(&mut FileTransferActivity, String);
|
||||
@@ -315,22 +312,15 @@ impl FileTransferActivity {
|
||||
FsEntry::Directory(dir) => dir.name.clone(),
|
||||
FsEntry::File(file) => file.name.clone(),
|
||||
};
|
||||
self.input_mode = InputMode::Popup(PopupType::Progress(format!(
|
||||
"Uploading \"{}\"...",
|
||||
file_name
|
||||
)));
|
||||
self.input_mode =
|
||||
InputMode::Popup(PopupType::Wait(format!("Uploading \"{}\"...", file_name)));
|
||||
// Draw
|
||||
self.draw();
|
||||
// Get remote path
|
||||
let remote_path: PathBuf = match dst_name {
|
||||
Some(s) => PathBuf::from(s.as_str()),
|
||||
None => PathBuf::from(file_name.as_str()),
|
||||
};
|
||||
let prog_cb: Box<ProgressCallback> = Box::new(|c, sz| {
|
||||
// Progress callback
|
||||
let percentage: f64 = ((c as f64) * 100.0) / (sz as f64);
|
||||
unsafe {
|
||||
UPLOAD_PROGRESS = percentage;
|
||||
}
|
||||
// FIXME: can't draw here...
|
||||
});
|
||||
// Match entry
|
||||
match entry {
|
||||
FsEntry::File(file) => {
|
||||
@@ -343,11 +333,7 @@ impl FileTransferActivity {
|
||||
.local
|
||||
.open_file_read(file.abs_path.as_path())
|
||||
{
|
||||
Ok(mut f) => {
|
||||
match self
|
||||
.client
|
||||
.send_file(remote_path.as_path(), &mut f, Some(prog_cb))
|
||||
{
|
||||
Ok(mut f) => match self.client.send_file(remote_path.as_path(), &mut f) {
|
||||
Ok(_) => self.log(
|
||||
LogLevel::Info,
|
||||
format!(
|
||||
@@ -366,8 +352,7 @@ impl FileTransferActivity {
|
||||
)
|
||||
.as_ref(),
|
||||
),
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(err) => {
|
||||
// Report error
|
||||
self.log(
|
||||
@@ -441,18 +426,10 @@ impl FileTransferActivity {
|
||||
FsEntry::Directory(dir) => dir.name.clone(),
|
||||
FsEntry::File(file) => file.name.clone(),
|
||||
};
|
||||
self.input_mode = InputMode::Popup(PopupType::Progress(format!(
|
||||
"Downloading \"{}\"...",
|
||||
file_name
|
||||
)));
|
||||
let prog_cb: Box<ProgressCallback> = Box::new(|c, sz| {
|
||||
// Progress callback
|
||||
let percentage: f64 = ((c as f64) * 100.0) / (sz as f64);
|
||||
unsafe {
|
||||
UPLOAD_PROGRESS = percentage;
|
||||
}
|
||||
// FIXME: can't draw here...
|
||||
});
|
||||
self.input_mode =
|
||||
InputMode::Popup(PopupType::Wait(format!("Downloading \"{}\"...", file_name)));
|
||||
// Draw
|
||||
self.draw();
|
||||
// Match entry
|
||||
match entry {
|
||||
FsEntry::File(file) => {
|
||||
@@ -473,11 +450,10 @@ impl FileTransferActivity {
|
||||
{
|
||||
Ok(mut local_file) => {
|
||||
// Download file from remote
|
||||
match self.client.recv_file(
|
||||
file.abs_path.as_path(),
|
||||
&mut local_file,
|
||||
Some(prog_cb),
|
||||
) {
|
||||
match self
|
||||
.client
|
||||
.recv_file(file.abs_path.as_path(), &mut local_file)
|
||||
{
|
||||
Ok(_) => self.log(
|
||||
LogLevel::Info,
|
||||
format!(
|
||||
|
||||
Reference in New Issue
Block a user