mirror of
https://github.com/veeso/termscp.git
synced 2025-12-07 09:36:00 -08:00
Option: prompt user when about to replace an existing file caused by a file transfer
This commit is contained in:
@@ -27,7 +27,9 @@
|
||||
*/
|
||||
// locals
|
||||
use super::super::browser::FileExplorerTab;
|
||||
use super::{FileTransferActivity, FsEntry, LogLevel, SelectedEntry, TransferPayload};
|
||||
use super::{
|
||||
FileTransferActivity, FsEntry, LogLevel, SelectedEntry, TransferOpts, TransferPayload,
|
||||
};
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
@@ -69,7 +71,7 @@ impl FileTransferActivity {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn action_find_transfer(&mut self, save_as: Option<String>) {
|
||||
pub(crate) fn action_find_transfer(&mut self, opts: TransferOpts) {
|
||||
let wrkdir: PathBuf = match self.browser.tab() {
|
||||
FileExplorerTab::FindLocal | FileExplorerTab::Local => self.remote().wrkdir.clone(),
|
||||
FileExplorerTab::FindRemote | FileExplorerTab::Remote => self.local().wrkdir.clone(),
|
||||
@@ -77,10 +79,19 @@ impl FileTransferActivity {
|
||||
match self.get_found_selected_entries() {
|
||||
SelectedEntry::One(entry) => match self.browser.tab() {
|
||||
FileExplorerTab::FindLocal | FileExplorerTab::Local => {
|
||||
if let Err(err) = self.filetransfer_send(
|
||||
let file_to_check = Self::file_to_check(&entry, opts.save_as.as_ref());
|
||||
if opts.check_replace
|
||||
&& self.config().get_prompt_on_file_replace()
|
||||
&& self.remote_file_exists(file_to_check.as_path())
|
||||
{
|
||||
// Save pending transfer
|
||||
self.set_pending_transfer(
|
||||
opts.save_as.as_deref().unwrap_or_else(|| entry.get_name()),
|
||||
);
|
||||
} else if let Err(err) = self.filetransfer_send(
|
||||
TransferPayload::Any(entry.get_realfile()),
|
||||
wrkdir.as_path(),
|
||||
save_as,
|
||||
opts.save_as,
|
||||
) {
|
||||
self.log_and_alert(
|
||||
LogLevel::Error,
|
||||
@@ -90,10 +101,19 @@ impl FileTransferActivity {
|
||||
}
|
||||
}
|
||||
FileExplorerTab::FindRemote | FileExplorerTab::Remote => {
|
||||
if let Err(err) = self.filetransfer_recv(
|
||||
let file_to_check = Self::file_to_check(&entry, opts.save_as.as_ref());
|
||||
if opts.check_replace
|
||||
&& self.config().get_prompt_on_file_replace()
|
||||
&& self.local_file_exists(file_to_check.as_path())
|
||||
{
|
||||
// Save pending transfer
|
||||
self.set_pending_transfer(
|
||||
opts.save_as.as_deref().unwrap_or_else(|| entry.get_name()),
|
||||
);
|
||||
} else if let Err(err) = self.filetransfer_recv(
|
||||
TransferPayload::Any(entry.get_realfile()),
|
||||
wrkdir.as_path(),
|
||||
save_as,
|
||||
opts.save_as,
|
||||
) {
|
||||
self.log_and_alert(
|
||||
LogLevel::Error,
|
||||
@@ -106,7 +126,7 @@ impl FileTransferActivity {
|
||||
SelectedEntry::Many(entries) => {
|
||||
// In case of selection: save multiple files in wrkdir/input
|
||||
let mut dest_path: PathBuf = wrkdir;
|
||||
if let Some(save_as) = save_as {
|
||||
if let Some(save_as) = opts.save_as {
|
||||
dest_path.push(save_as);
|
||||
}
|
||||
// Iter files
|
||||
|
||||
@@ -25,7 +25,10 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
pub(self) use super::{FileTransferActivity, FsEntry, LogLevel, TransferPayload};
|
||||
pub(self) use super::{
|
||||
browser::FileExplorerTab, FileTransferActivity, FsEntry, LogLevel, TransferOpts,
|
||||
TransferPayload,
|
||||
};
|
||||
use tuirealm::{Payload, Value};
|
||||
|
||||
// actions
|
||||
|
||||
@@ -26,34 +26,85 @@
|
||||
* SOFTWARE.
|
||||
*/
|
||||
// locals
|
||||
use super::{FileTransferActivity, LogLevel, SelectedEntry, TransferPayload};
|
||||
use super::{
|
||||
super::STORAGE_PENDING_TRANSFER, FileExplorerTab, FileTransferActivity, FsEntry, LogLevel,
|
||||
SelectedEntry, TransferOpts, TransferPayload,
|
||||
};
|
||||
use std::path::PathBuf;
|
||||
|
||||
impl FileTransferActivity {
|
||||
pub(crate) fn action_local_saveas(&mut self, input: String) {
|
||||
self.action_local_send_file(Some(input));
|
||||
self.local_send_file(TransferOpts::default().save_as(input));
|
||||
}
|
||||
|
||||
pub(crate) fn action_remote_saveas(&mut self, input: String) {
|
||||
self.action_remote_recv_file(Some(input));
|
||||
self.remote_recv_file(TransferOpts::default().save_as(input));
|
||||
}
|
||||
|
||||
pub(crate) fn action_local_send(&mut self) {
|
||||
self.action_local_send_file(None);
|
||||
self.local_send_file(TransferOpts::default());
|
||||
}
|
||||
|
||||
pub(crate) fn action_remote_recv(&mut self) {
|
||||
self.action_remote_recv_file(None);
|
||||
self.remote_recv_file(TransferOpts::default());
|
||||
}
|
||||
|
||||
fn action_local_send_file(&mut self, save_as: Option<String>) {
|
||||
/// ### action_finalize_pending_transfer
|
||||
///
|
||||
/// Finalize "pending" transfer.
|
||||
/// The pending transfer is created after a transfer which required a user action to be completed first.
|
||||
/// The name of the file to transfer, is contained in the storage at `STORAGE_PENDING_TRANSFER`.
|
||||
/// NOTE: Panics if `STORAGE_PENDING_TRANSFER` is undefined
|
||||
pub(crate) fn action_finalize_pending_transfer(&mut self) {
|
||||
// Retrieve pending transfer
|
||||
let file_name: String = self
|
||||
.context_mut()
|
||||
.store_mut()
|
||||
.take_string(STORAGE_PENDING_TRANSFER)
|
||||
.unwrap();
|
||||
// Send file
|
||||
match self.browser.tab() {
|
||||
FileExplorerTab::Local => self.local_send_file(
|
||||
TransferOpts::default()
|
||||
.save_as(file_name)
|
||||
.check_replace(false),
|
||||
),
|
||||
FileExplorerTab::Remote => self.remote_recv_file(
|
||||
TransferOpts::default()
|
||||
.save_as(file_name)
|
||||
.check_replace(false),
|
||||
),
|
||||
FileExplorerTab::FindLocal | FileExplorerTab::FindRemote => self.action_find_transfer(
|
||||
TransferOpts::default()
|
||||
.save_as(file_name)
|
||||
.check_replace(false),
|
||||
),
|
||||
}
|
||||
// Reload browsers
|
||||
match self.browser.tab() {
|
||||
FileExplorerTab::Local => self.reload_remote_dir(),
|
||||
FileExplorerTab::Remote => self.reload_local_dir(),
|
||||
FileExplorerTab::FindLocal | FileExplorerTab::FindRemote => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn local_send_file(&mut self, opts: TransferOpts) {
|
||||
let wrkdir: PathBuf = self.remote().wrkdir.clone();
|
||||
match self.get_local_selected_entries() {
|
||||
SelectedEntry::One(entry) => {
|
||||
if let Err(err) = self.filetransfer_send(
|
||||
let file_to_check = Self::file_to_check(&entry, opts.save_as.as_ref());
|
||||
if opts.check_replace
|
||||
&& self.config().get_prompt_on_file_replace()
|
||||
&& self.remote_file_exists(file_to_check.as_path())
|
||||
{
|
||||
// Save pending transfer
|
||||
self.set_pending_transfer(
|
||||
opts.save_as.as_deref().unwrap_or_else(|| entry.get_name()),
|
||||
);
|
||||
} else if let Err(err) = self.filetransfer_send(
|
||||
TransferPayload::Any(entry.get_realfile()),
|
||||
wrkdir.as_path(),
|
||||
save_as,
|
||||
opts.save_as,
|
||||
) {
|
||||
{
|
||||
self.log_and_alert(
|
||||
@@ -67,7 +118,7 @@ impl FileTransferActivity {
|
||||
SelectedEntry::Many(entries) => {
|
||||
// In case of selection: save multiple files in wrkdir/input
|
||||
let mut dest_path: PathBuf = wrkdir;
|
||||
if let Some(save_as) = save_as {
|
||||
if let Some(save_as) = opts.save_as {
|
||||
dest_path.push(save_as);
|
||||
}
|
||||
// Iter files
|
||||
@@ -90,14 +141,23 @@ impl FileTransferActivity {
|
||||
}
|
||||
}
|
||||
|
||||
fn action_remote_recv_file(&mut self, save_as: Option<String>) {
|
||||
fn remote_recv_file(&mut self, opts: TransferOpts) {
|
||||
let wrkdir: PathBuf = self.local().wrkdir.clone();
|
||||
match self.get_remote_selected_entries() {
|
||||
SelectedEntry::One(entry) => {
|
||||
if let Err(err) = self.filetransfer_recv(
|
||||
let file_to_check = Self::file_to_check(&entry, opts.save_as.as_ref());
|
||||
if opts.check_replace
|
||||
&& self.config().get_prompt_on_file_replace()
|
||||
&& self.local_file_exists(file_to_check.as_path())
|
||||
{
|
||||
// Save pending transfer
|
||||
self.set_pending_transfer(
|
||||
opts.save_as.as_deref().unwrap_or_else(|| entry.get_name()),
|
||||
);
|
||||
} else if let Err(err) = self.filetransfer_recv(
|
||||
TransferPayload::Any(entry.get_realfile()),
|
||||
wrkdir.as_path(),
|
||||
save_as,
|
||||
opts.save_as,
|
||||
) {
|
||||
{
|
||||
self.log_and_alert(
|
||||
@@ -111,7 +171,7 @@ impl FileTransferActivity {
|
||||
SelectedEntry::Many(entries) => {
|
||||
// In case of selection: save multiple files in wrkdir/input
|
||||
let mut dest_path: PathBuf = wrkdir;
|
||||
if let Some(save_as) = save_as {
|
||||
if let Some(save_as) = opts.save_as {
|
||||
dest_path.push(save_as);
|
||||
}
|
||||
// Iter files
|
||||
@@ -133,4 +193,25 @@ impl FileTransferActivity {
|
||||
SelectedEntry::None => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// ### set_pending_transfer
|
||||
///
|
||||
/// Set pending transfer into storage
|
||||
pub(crate) fn set_pending_transfer(&mut self, file_name: &str) {
|
||||
self.mount_radio_replace(file_name);
|
||||
// Put pending transfer in store
|
||||
self.context_mut()
|
||||
.store_mut()
|
||||
.set_string(STORAGE_PENDING_TRANSFER, file_name.to_string());
|
||||
}
|
||||
|
||||
/// ### file_to_check
|
||||
///
|
||||
/// Get file to check for path
|
||||
pub(crate) fn file_to_check(e: &FsEntry, alt: Option<&String>) -> PathBuf {
|
||||
match alt {
|
||||
Some(s) => PathBuf::from(s),
|
||||
None => PathBuf::from(e.get_name()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@ use bytesize::ByteSize;
|
||||
use std::fmt;
|
||||
use std::time::Instant;
|
||||
|
||||
// -- States and progress
|
||||
|
||||
/// ### TransferStates
|
||||
///
|
||||
/// TransferStates contains the states related to the transfer process
|
||||
@@ -195,6 +197,45 @@ impl ProgressStates {
|
||||
}
|
||||
}
|
||||
|
||||
// -- Options
|
||||
|
||||
/// ## TransferOpts
|
||||
///
|
||||
/// Defines the transfer options for transfer actions
|
||||
pub struct TransferOpts {
|
||||
/// Save file as
|
||||
pub save_as: Option<String>,
|
||||
/// Whether to check if file is being replaced
|
||||
pub check_replace: bool,
|
||||
}
|
||||
|
||||
impl Default for TransferOpts {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
save_as: None,
|
||||
check_replace: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TransferOpts {
|
||||
/// ### save_as
|
||||
///
|
||||
/// Define the name of the file to be saved
|
||||
pub fn save_as<S: AsRef<str>>(mut self, n: S) -> Self {
|
||||
self.save_as = Some(n.as_ref().to_string());
|
||||
self
|
||||
}
|
||||
|
||||
/// ### check_replace
|
||||
///
|
||||
/// Set whether to check if the file being transferred will "replace" an existing one
|
||||
pub fn check_replace(mut self, opt: bool) -> Self {
|
||||
self.check_replace = opt;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
|
||||
@@ -265,4 +306,16 @@ mod test {
|
||||
states.reset();
|
||||
assert_eq!(states.aborted(), false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transfer_opts() {
|
||||
let opts = TransferOpts::default();
|
||||
assert_eq!(opts.check_replace, true);
|
||||
assert!(opts.save_as.is_none());
|
||||
let opts = TransferOpts::default()
|
||||
.check_replace(false)
|
||||
.save_as("omar.txt");
|
||||
assert_eq!(opts.save_as.as_deref().unwrap(), "omar.txt");
|
||||
assert_eq!(opts.check_replace, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ use crate::host::Localhost;
|
||||
use crate::system::config_client::ConfigClient;
|
||||
pub(self) use lib::browser;
|
||||
use lib::browser::Browser;
|
||||
use lib::transfer::TransferStates;
|
||||
use lib::transfer::{TransferOpts, TransferStates};
|
||||
pub(self) use session::TransferPayload;
|
||||
|
||||
// Includes
|
||||
@@ -57,6 +57,7 @@ use tuirealm::View;
|
||||
// -- Storage keys
|
||||
|
||||
const STORAGE_EXPLORER_WIDTH: &str = "FILETRANSFER_EXPLORER_WIDTH";
|
||||
const STORAGE_PENDING_TRANSFER: &str = "FILETRANSFER_PENDING_TRANSFER";
|
||||
|
||||
// -- components
|
||||
|
||||
@@ -80,6 +81,7 @@ const COMPONENT_INPUT_OPEN_WITH: &str = "INPUT_OPEN_WITH";
|
||||
const COMPONENT_INPUT_RENAME: &str = "INPUT_RENAME";
|
||||
const COMPONENT_INPUT_SAVEAS: &str = "INPUT_SAVEAS";
|
||||
const COMPONENT_RADIO_DELETE: &str = "RADIO_DELETE";
|
||||
const COMPONENT_RADIO_REPLACE: &str = "RADIO_REPLACE"; // NOTE: used for file transfers, to choose whether to replace files
|
||||
const COMPONENT_RADIO_DISCONNECT: &str = "RADIO_DISCONNECT";
|
||||
const COMPONENT_RADIO_QUIT: &str = "RADIO_QUIT";
|
||||
const COMPONENT_RADIO_SORTING: &str = "RADIO_SORTING";
|
||||
|
||||
@@ -1187,4 +1187,14 @@ impl FileTransferActivity {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// -- file exist
|
||||
|
||||
pub(crate) fn local_file_exists(&mut self, p: &Path) -> bool {
|
||||
self.host.file_exists(p)
|
||||
}
|
||||
|
||||
pub(crate) fn remote_file_exists(&mut self, p: &Path) -> bool {
|
||||
self.client.stat(p).is_ok()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,14 +27,14 @@
|
||||
*/
|
||||
// locals
|
||||
use super::{
|
||||
actions::SelectedEntry, browser::FileExplorerTab, FileTransferActivity, LogLevel,
|
||||
actions::SelectedEntry, browser::FileExplorerTab, FileTransferActivity, LogLevel, TransferOpts,
|
||||
COMPONENT_EXPLORER_FIND, COMPONENT_EXPLORER_LOCAL, COMPONENT_EXPLORER_REMOTE,
|
||||
COMPONENT_INPUT_COPY, COMPONENT_INPUT_EXEC, COMPONENT_INPUT_FIND, COMPONENT_INPUT_GOTO,
|
||||
COMPONENT_INPUT_MKDIR, COMPONENT_INPUT_NEWFILE, COMPONENT_INPUT_OPEN_WITH,
|
||||
COMPONENT_INPUT_RENAME, COMPONENT_INPUT_SAVEAS, COMPONENT_LIST_FILEINFO, COMPONENT_LOG_BOX,
|
||||
COMPONENT_PROGRESS_BAR_FULL, COMPONENT_PROGRESS_BAR_PARTIAL, COMPONENT_RADIO_DELETE,
|
||||
COMPONENT_RADIO_DISCONNECT, COMPONENT_RADIO_QUIT, COMPONENT_RADIO_SORTING,
|
||||
COMPONENT_TEXT_ERROR, COMPONENT_TEXT_FATAL, COMPONENT_TEXT_HELP,
|
||||
COMPONENT_RADIO_DISCONNECT, COMPONENT_RADIO_QUIT, COMPONENT_RADIO_REPLACE,
|
||||
COMPONENT_RADIO_SORTING, COMPONENT_TEXT_ERROR, COMPONENT_TEXT_FATAL, COMPONENT_TEXT_HELP,
|
||||
};
|
||||
use crate::fs::explorer::FileSorting;
|
||||
use crate::fs::FsEntry;
|
||||
@@ -358,7 +358,7 @@ impl Update for FileTransferActivity {
|
||||
}
|
||||
(COMPONENT_EXPLORER_FIND, key) if key == &MSG_KEY_SPACE => {
|
||||
// Get entry
|
||||
self.action_find_transfer(None);
|
||||
self.action_find_transfer(TransferOpts::default());
|
||||
// Reload files
|
||||
match self.browser.tab() {
|
||||
// NOTE: swapped by purpose
|
||||
@@ -583,7 +583,7 @@ impl Update for FileTransferActivity {
|
||||
FileExplorerTab::Remote => self.action_remote_saveas(input.to_string()),
|
||||
FileExplorerTab::FindLocal | FileExplorerTab::FindRemote => {
|
||||
// Get entry
|
||||
self.action_find_transfer(Some(input.to_string()));
|
||||
self.action_find_transfer(TransferOpts::default().save_as(input));
|
||||
}
|
||||
}
|
||||
self.umount_saveas();
|
||||
@@ -653,6 +653,21 @@ impl Update for FileTransferActivity {
|
||||
}
|
||||
}
|
||||
(COMPONENT_RADIO_DELETE, _) => None,
|
||||
// -- replace
|
||||
(COMPONENT_RADIO_REPLACE, key)
|
||||
if key == &MSG_KEY_ESC
|
||||
|| key == &Msg::OnSubmit(Payload::One(Value::Usize(1))) =>
|
||||
{
|
||||
self.umount_radio_replace();
|
||||
None
|
||||
}
|
||||
(COMPONENT_RADIO_REPLACE, Msg::OnSubmit(Payload::One(Value::Usize(0)))) => {
|
||||
// Choice is 'YES'
|
||||
self.umount_radio_replace();
|
||||
self.action_finalize_pending_transfer();
|
||||
None
|
||||
}
|
||||
(COMPONENT_RADIO_REPLACE, _) => None,
|
||||
// -- disconnect
|
||||
(COMPONENT_RADIO_DISCONNECT, key)
|
||||
if key == &MSG_KEY_ESC
|
||||
|
||||
@@ -308,6 +308,14 @@ impl FileTransferActivity {
|
||||
self.view.render(super::COMPONENT_RADIO_DELETE, f, popup);
|
||||
}
|
||||
}
|
||||
if let Some(props) = self.view.get_props(super::COMPONENT_RADIO_REPLACE) {
|
||||
if props.visible {
|
||||
let popup = draw_area_in(f.size(), 50, 10);
|
||||
f.render_widget(Clear, popup);
|
||||
// make popup
|
||||
self.view.render(super::COMPONENT_RADIO_REPLACE, f, popup);
|
||||
}
|
||||
}
|
||||
if let Some(props) = self.view.get_props(super::COMPONENT_RADIO_DISCONNECT) {
|
||||
if props.visible {
|
||||
let popup = draw_area_in(f.size(), 30, 10);
|
||||
@@ -698,6 +706,32 @@ impl FileTransferActivity {
|
||||
self.view.umount(super::COMPONENT_RADIO_DELETE);
|
||||
}
|
||||
|
||||
pub(super) fn mount_radio_replace(&mut self, file_name: &str) {
|
||||
let warn_color = self.theme().misc_warn_dialog;
|
||||
self.view.mount(
|
||||
super::COMPONENT_RADIO_REPLACE,
|
||||
Box::new(Radio::new(
|
||||
RadioPropsBuilder::default()
|
||||
.with_color(warn_color)
|
||||
.with_inverted_color(Color::Black)
|
||||
.with_borders(Borders::ALL, BorderType::Plain, warn_color)
|
||||
.with_title(
|
||||
format!("File '{}' already exists. Overwrite file?", file_name),
|
||||
Alignment::Center,
|
||||
)
|
||||
.with_options(&[String::from("Yes"), String::from("No")])
|
||||
.with_value(0)
|
||||
.rewind(true)
|
||||
.build(),
|
||||
)),
|
||||
);
|
||||
self.view.active(super::COMPONENT_RADIO_REPLACE);
|
||||
}
|
||||
|
||||
pub(super) fn umount_radio_replace(&mut self) {
|
||||
self.view.umount(super::COMPONENT_RADIO_REPLACE);
|
||||
}
|
||||
|
||||
pub(super) fn mount_file_info(&mut self, file: &FsEntry) {
|
||||
let mut texts: TableBuilder = TableBuilder::default();
|
||||
// Abs path
|
||||
|
||||
Reference in New Issue
Block a user