2020-12-01 14:59:13 +01:00
|
|
|
//! ## FileTransferActivity
|
|
|
|
//!
|
|
|
|
//! `filetransfer_activiy` is the module which implements the Filetransfer activity, which is the main activity afterall
|
|
|
|
|
2021-03-26 22:25:10 +01:00
|
|
|
/**
|
|
|
|
* MIT License
|
|
|
|
*
|
|
|
|
* termscp - Copyright (c) 2021 Christian Visintin
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
2020-12-01 14:59:13 +01:00
|
|
|
// This module is split into files, cause it's just too big
|
2021-03-20 21:06:12 +01:00
|
|
|
mod actions;
|
2020-12-01 14:59:13 +01:00
|
|
|
mod misc;
|
|
|
|
mod session;
|
2021-03-20 21:06:12 +01:00
|
|
|
mod update;
|
|
|
|
mod view;
|
2020-12-01 14:59:13 +01:00
|
|
|
|
|
|
|
// Dependencies
|
|
|
|
extern crate chrono;
|
|
|
|
extern crate crossterm;
|
|
|
|
extern crate textwrap;
|
|
|
|
extern crate tui;
|
|
|
|
|
|
|
|
// locals
|
2021-03-21 17:16:52 +01:00
|
|
|
use super::{Activity, Context, ExitReason};
|
2020-12-01 16:43:03 +01:00
|
|
|
use crate::filetransfer::ftp_transfer::FtpFileTransfer;
|
2020-12-05 15:21:01 +01:00
|
|
|
use crate::filetransfer::scp_transfer::ScpFileTransfer;
|
2020-12-01 14:59:13 +01:00
|
|
|
use crate::filetransfer::sftp_transfer::SftpFileTransfer;
|
2020-12-24 17:27:57 +01:00
|
|
|
use crate::filetransfer::{FileTransfer, FileTransferProtocol};
|
2020-12-26 17:29:12 +01:00
|
|
|
use crate::fs::explorer::FileExplorer;
|
2020-12-01 14:59:13 +01:00
|
|
|
use crate::fs::FsEntry;
|
2020-12-24 17:27:57 +01:00
|
|
|
use crate::system::config_client::ConfigClient;
|
2021-03-20 21:06:12 +01:00
|
|
|
use crate::ui::layout::view::View;
|
2020-12-01 14:59:13 +01:00
|
|
|
|
|
|
|
// Includes
|
|
|
|
use chrono::{DateTime, Local};
|
|
|
|
use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
|
|
|
|
use std::collections::VecDeque;
|
2020-12-26 15:50:57 +01:00
|
|
|
use std::path::PathBuf;
|
2020-12-01 14:59:13 +01:00
|
|
|
use std::time::Instant;
|
|
|
|
|
2021-03-20 21:06:12 +01:00
|
|
|
// -- Storage keys
|
|
|
|
|
|
|
|
const STORAGE_EXPLORER_WIDTH: &str = "FILETRANSFER_EXPLORER_WIDTH";
|
|
|
|
const STORAGE_LOGBOX_WIDTH: &str = "LOGBOX_WIDTH";
|
|
|
|
|
|
|
|
// -- components
|
|
|
|
|
|
|
|
const COMPONENT_EXPLORER_LOCAL: &str = "EXPLORER_LOCAL";
|
|
|
|
const COMPONENT_EXPLORER_REMOTE: &str = "EXPLORER_REMOTE";
|
2021-03-25 22:59:42 +01:00
|
|
|
const COMPONENT_EXPLORER_FIND: &str = "EXPLORER_FIND";
|
2021-03-20 21:06:12 +01:00
|
|
|
const COMPONENT_LOG_BOX: &str = "LOG_BOX";
|
|
|
|
const COMPONENT_PROGRESS_BAR: &str = "PROGRESS_BAR";
|
|
|
|
const COMPONENT_TEXT_ERROR: &str = "TEXT_ERROR";
|
|
|
|
const COMPONENT_TEXT_FATAL: &str = "TEXT_FATAL";
|
2021-03-25 22:59:42 +01:00
|
|
|
const COMPONENT_TEXT_HELP: &str = "TEXT_HELP";
|
|
|
|
const COMPONENT_TEXT_WAIT: &str = "TEXT_WAIT";
|
2021-03-20 21:06:12 +01:00
|
|
|
const COMPONENT_INPUT_COPY: &str = "INPUT_COPY";
|
2021-03-21 22:57:05 +01:00
|
|
|
const COMPONENT_INPUT_EXEC: &str = "INPUT_EXEC";
|
2021-03-25 22:59:42 +01:00
|
|
|
const COMPONENT_INPUT_FIND: &str = "INPUT_FIND";
|
2021-03-20 21:06:12 +01:00
|
|
|
const COMPONENT_INPUT_GOTO: &str = "INPUT_GOTO";
|
2021-03-25 22:59:42 +01:00
|
|
|
const COMPONENT_INPUT_MKDIR: &str = "INPUT_MKDIR";
|
2021-03-20 21:06:12 +01:00
|
|
|
const COMPONENT_INPUT_NEWFILE: &str = "INPUT_NEWFILE";
|
|
|
|
const COMPONENT_INPUT_RENAME: &str = "INPUT_RENAME";
|
2021-03-25 22:59:42 +01:00
|
|
|
const COMPONENT_INPUT_SAVEAS: &str = "INPUT_SAVEAS";
|
|
|
|
const COMPONENT_RADIO_DELETE: &str = "RADIO_DELETE";
|
2021-03-20 21:06:12 +01:00
|
|
|
const COMPONENT_RADIO_DISCONNECT: &str = "RADIO_DISCONNECT";
|
2021-03-25 22:59:42 +01:00
|
|
|
const COMPONENT_RADIO_QUIT: &str = "RADIO_QUIT";
|
2021-03-20 21:06:12 +01:00
|
|
|
const COMPONENT_RADIO_SORTING: &str = "RADIO_SORTING";
|
|
|
|
const COMPONENT_LIST_FILEINFO: &str = "LIST_FILEINFO";
|
2020-12-01 14:59:13 +01:00
|
|
|
|
|
|
|
/// ## FileExplorerTab
|
|
|
|
///
|
|
|
|
/// File explorer tab
|
|
|
|
enum FileExplorerTab {
|
|
|
|
Local,
|
|
|
|
Remote,
|
2021-03-25 22:59:42 +01:00
|
|
|
FindLocal, // Find result tab
|
|
|
|
FindRemote, // Find result tab
|
2020-12-01 14:59:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ## LogLevel
|
|
|
|
///
|
|
|
|
/// Log level type
|
|
|
|
enum LogLevel {
|
|
|
|
Error,
|
|
|
|
Warn,
|
|
|
|
Info,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ## LogRecord
|
|
|
|
///
|
|
|
|
/// Log record entry
|
|
|
|
struct LogRecord {
|
|
|
|
pub time: DateTime<Local>,
|
|
|
|
pub level: LogLevel,
|
|
|
|
pub msg: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LogRecord {
|
|
|
|
/// ### new
|
|
|
|
///
|
|
|
|
/// Instantiates a new LogRecord
|
|
|
|
pub fn new(level: LogLevel, msg: &str) -> LogRecord {
|
|
|
|
LogRecord {
|
|
|
|
time: Local::now(),
|
2020-12-12 12:14:51 +01:00
|
|
|
level,
|
2020-12-01 14:59:13 +01:00
|
|
|
msg: String::from(msg),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 21:32:11 +01:00
|
|
|
/// ### TransferStates
|
|
|
|
///
|
|
|
|
/// TransferStates contains the states related to the transfer process
|
|
|
|
struct TransferStates {
|
2020-12-18 17:06:17 +01:00
|
|
|
pub progress: f64, // Current read/write progress (percentage)
|
|
|
|
pub started: Instant, // Instant the transfer process started
|
|
|
|
pub aborted: bool, // Describes whether the transfer process has been aborted
|
|
|
|
pub bytes_written: usize, // Bytes written during transfer
|
|
|
|
pub bytes_total: usize, // Total bytes to write
|
2020-12-12 21:32:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TransferStates {
|
|
|
|
/// ### new
|
|
|
|
///
|
|
|
|
/// Instantiates a new transfer states
|
|
|
|
pub fn new() -> TransferStates {
|
|
|
|
TransferStates {
|
|
|
|
progress: 0.0,
|
|
|
|
started: Instant::now(),
|
|
|
|
aborted: false,
|
2020-12-18 17:06:17 +01:00
|
|
|
bytes_written: 0,
|
|
|
|
bytes_total: 0,
|
2020-12-12 21:32:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ### reset
|
|
|
|
///
|
|
|
|
/// Re-intiialize transfer states
|
|
|
|
pub fn reset(&mut self) {
|
|
|
|
self.progress = 0.0;
|
|
|
|
self.started = Instant::now();
|
|
|
|
self.aborted = false;
|
2020-12-18 17:06:17 +01:00
|
|
|
self.bytes_written = 0;
|
|
|
|
self.bytes_total = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ### set_progress
|
|
|
|
///
|
|
|
|
/// Calculate progress percentage based on current progress
|
|
|
|
pub fn set_progress(&mut self, w: usize, sz: usize) {
|
|
|
|
self.bytes_written = w;
|
|
|
|
self.bytes_total = sz;
|
|
|
|
let mut prog: f64 = ((self.bytes_written as f64) * 100.0) / (self.bytes_total as f64);
|
|
|
|
// Check value
|
|
|
|
if prog > 100.0 {
|
|
|
|
prog = 100.0;
|
|
|
|
} else if prog < 0.0 {
|
|
|
|
prog = 0.0;
|
|
|
|
}
|
|
|
|
self.progress = prog;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ### byte_per_second
|
|
|
|
///
|
|
|
|
/// Calculate bytes per second
|
|
|
|
pub fn bytes_per_second(&self) -> u64 {
|
|
|
|
// bytes_written : elapsed_secs = x : 1
|
|
|
|
let elapsed_secs: u64 = self.started.elapsed().as_secs();
|
|
|
|
match elapsed_secs {
|
2020-12-28 22:54:28 +01:00
|
|
|
0 => match self.bytes_written == self.bytes_total {
|
|
|
|
// NOTE: would divide by 0 :D
|
|
|
|
true => self.bytes_total as u64, // Download completed in less than 1 second
|
|
|
|
false => 0, // 0 B/S
|
|
|
|
},
|
2020-12-18 17:06:17 +01:00
|
|
|
_ => self.bytes_written as u64 / elapsed_secs,
|
|
|
|
}
|
2020-12-12 21:32:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for TransferStates {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 14:59:13 +01:00
|
|
|
/// ## FileTransferActivity
|
|
|
|
///
|
|
|
|
/// FileTransferActivity is the data holder for the file transfer activity
|
|
|
|
pub struct FileTransferActivity {
|
2021-03-21 17:16:52 +01:00
|
|
|
exit_reason: Option<ExitReason>, // Exit reason
|
2020-12-01 14:59:13 +01:00
|
|
|
context: Option<Context>, // Context holder
|
2021-03-20 21:06:12 +01:00
|
|
|
view: View, // View
|
2020-12-01 14:59:13 +01:00
|
|
|
client: Box<dyn FileTransfer>, // File transfer client
|
|
|
|
local: FileExplorer, // Local File explorer state
|
|
|
|
remote: FileExplorer, // Remote File explorer state
|
2021-03-25 22:59:42 +01:00
|
|
|
found: Option<FileExplorer>, // File explorer for find result
|
2020-12-01 14:59:13 +01:00
|
|
|
tab: FileExplorerTab, // Current selected tab
|
|
|
|
log_index: usize, // Current log index entry selected
|
|
|
|
log_records: VecDeque<LogRecord>, // Log records
|
|
|
|
log_size: usize, // Log records size (max)
|
2020-12-12 21:32:11 +01:00
|
|
|
transfer: TransferStates, // Transfer states
|
2020-12-01 14:59:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FileTransferActivity {
|
|
|
|
/// ### new
|
|
|
|
///
|
|
|
|
/// Instantiates a new FileTransferActivity
|
2021-03-21 16:38:11 +01:00
|
|
|
pub fn new(protocol: FileTransferProtocol) -> FileTransferActivity {
|
2020-12-24 19:01:42 +01:00
|
|
|
// Get config client
|
|
|
|
let config_client: Option<ConfigClient> = Self::init_config_client();
|
2020-12-01 14:59:13 +01:00
|
|
|
FileTransferActivity {
|
2021-03-21 17:16:52 +01:00
|
|
|
exit_reason: None,
|
2020-12-01 14:59:13 +01:00
|
|
|
context: None,
|
2021-03-20 21:06:12 +01:00
|
|
|
view: View::init(),
|
2020-12-01 14:59:13 +01:00
|
|
|
client: match protocol {
|
2020-12-26 15:50:57 +01:00
|
|
|
FileTransferProtocol::Sftp => Box::new(SftpFileTransfer::new(
|
|
|
|
Self::make_ssh_storage(config_client.as_ref()),
|
|
|
|
)),
|
2020-12-05 15:21:01 +01:00
|
|
|
FileTransferProtocol::Ftp(ftps) => Box::new(FtpFileTransfer::new(ftps)),
|
2020-12-26 15:50:57 +01:00
|
|
|
FileTransferProtocol::Scp => Box::new(ScpFileTransfer::new(
|
|
|
|
Self::make_ssh_storage(config_client.as_ref()),
|
|
|
|
)),
|
2020-12-01 14:59:13 +01:00
|
|
|
},
|
2020-12-26 17:29:12 +01:00
|
|
|
local: Self::build_explorer(config_client.as_ref()),
|
|
|
|
remote: Self::build_explorer(config_client.as_ref()),
|
2021-03-25 22:59:42 +01:00
|
|
|
found: None,
|
2020-12-01 14:59:13 +01:00
|
|
|
tab: FileExplorerTab::Local,
|
|
|
|
log_index: 0,
|
|
|
|
log_records: VecDeque::with_capacity(256), // 256 events is enough I guess
|
|
|
|
log_size: 256, // Must match with capacity
|
2020-12-12 21:32:11 +01:00
|
|
|
transfer: TransferStates::default(),
|
2020-12-01 14:59:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Activity Trait
|
|
|
|
* Keep it clean :)
|
|
|
|
* Use methods instead!
|
|
|
|
*/
|
|
|
|
|
|
|
|
impl Activity for FileTransferActivity {
|
|
|
|
/// ### on_create
|
|
|
|
///
|
|
|
|
/// `on_create` is the function which must be called to initialize the activity.
|
|
|
|
/// `on_create` must initialize all the data structures used by the activity
|
|
|
|
fn on_create(&mut self, context: Context) {
|
|
|
|
// Set context
|
|
|
|
self.context = Some(context);
|
|
|
|
// Clear terminal
|
2020-12-19 21:47:03 +01:00
|
|
|
self.context.as_mut().unwrap().clear_screen();
|
2020-12-01 14:59:13 +01:00
|
|
|
// Put raw mode on enabled
|
|
|
|
let _ = enable_raw_mode();
|
2020-12-17 08:45:50 +01:00
|
|
|
// Set working directory
|
|
|
|
let pwd: PathBuf = self.context.as_ref().unwrap().local.pwd();
|
2020-12-01 14:59:13 +01:00
|
|
|
// Get files at current wd
|
2020-12-17 08:45:50 +01:00
|
|
|
self.local_scan(pwd.as_path());
|
|
|
|
self.local.wrkdir = pwd;
|
2020-12-24 19:01:42 +01:00
|
|
|
// Configure text editor
|
|
|
|
self.setup_text_editor();
|
2021-03-20 21:06:12 +01:00
|
|
|
// init view
|
|
|
|
self.init();
|
2021-03-08 12:01:40 +01:00
|
|
|
// Verify error state from context
|
|
|
|
if let Some(err) = self.context.as_mut().unwrap().get_error() {
|
2021-03-20 21:06:12 +01:00
|
|
|
self.mount_fatal(&err);
|
2021-03-08 12:01:40 +01:00
|
|
|
}
|
2020-12-01 14:59:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ### on_draw
|
|
|
|
///
|
|
|
|
/// `on_draw` is the function which draws the graphical interface.
|
|
|
|
/// This function must be called at each tick to refresh the interface
|
|
|
|
fn on_draw(&mut self) {
|
2020-12-17 08:45:50 +01:00
|
|
|
// Should ui actually be redrawned?
|
|
|
|
let mut redraw: bool = false;
|
|
|
|
// Context must be something
|
2020-12-01 14:59:13 +01:00
|
|
|
if self.context.is_none() {
|
|
|
|
return;
|
|
|
|
}
|
2020-12-27 10:59:12 +01:00
|
|
|
// Check if connected (popup must be None, otherwise would try reconnecting in loop in case of error)
|
2021-03-20 21:06:12 +01:00
|
|
|
if !self.client.is_connected() && self.view.get_props(COMPONENT_TEXT_FATAL).is_none() {
|
2021-03-21 16:38:11 +01:00
|
|
|
let params = self.context.as_ref().unwrap().ft_params.as_ref().unwrap();
|
|
|
|
let msg: String = format!("Connecting to {}:{}...", params.address, params.port);
|
2020-12-01 14:59:13 +01:00
|
|
|
// Set init state to connecting popup
|
2021-03-21 16:38:11 +01:00
|
|
|
self.mount_wait(msg.as_str());
|
2020-12-01 14:59:13 +01:00
|
|
|
// Force ui draw
|
2021-03-20 21:06:12 +01:00
|
|
|
self.view();
|
2020-12-01 14:59:13 +01:00
|
|
|
// Connect to remote
|
|
|
|
self.connect();
|
|
|
|
// Redraw
|
|
|
|
redraw = true;
|
|
|
|
}
|
2020-12-12 22:00:12 +01:00
|
|
|
// Handle input events (if false, becomes true; otherwise remains true)
|
|
|
|
redraw |= self.read_input_event();
|
2020-12-01 14:59:13 +01:00
|
|
|
// @! draw interface
|
|
|
|
if redraw {
|
2021-03-20 21:06:12 +01:00
|
|
|
self.view();
|
2020-12-01 14:59:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 17:16:52 +01:00
|
|
|
/// ### will_umount
|
|
|
|
///
|
|
|
|
/// `will_umount` is the method which must be able to report to the activity manager, whether
|
|
|
|
/// the activity should be terminated or not.
|
|
|
|
/// If not, the call will return `None`, otherwise return`Some(ExitReason)`
|
|
|
|
fn will_umount(&self) -> Option<&ExitReason> {
|
|
|
|
self.exit_reason.as_ref()
|
|
|
|
}
|
|
|
|
|
2020-12-01 14:59:13 +01:00
|
|
|
/// ### on_destroy
|
|
|
|
///
|
|
|
|
/// `on_destroy` is the function which cleans up runtime variables and data before terminating the activity.
|
|
|
|
/// This function must be called once before terminating the activity.
|
|
|
|
fn on_destroy(&mut self) -> Option<Context> {
|
|
|
|
// Disable raw mode
|
|
|
|
let _ = disable_raw_mode();
|
|
|
|
// Disconnect client
|
|
|
|
if self.client.is_connected() {
|
|
|
|
let _ = self.client.disconnect();
|
|
|
|
}
|
|
|
|
// Clear terminal and return
|
|
|
|
match self.context.take() {
|
|
|
|
Some(mut ctx) => {
|
2020-12-19 21:47:03 +01:00
|
|
|
ctx.clear_screen();
|
2020-12-01 14:59:13 +01:00
|
|
|
Some(ctx)
|
|
|
|
}
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|