mirror of
https://github.com/veeso/termscp.git
synced 2026-09-28 06:51:20 -07:00
Working on open
This commit is contained in:
@@ -29,54 +29,90 @@
|
||||
extern crate open;
|
||||
// locals
|
||||
use super::{FileTransferActivity, FsEntry, LogLevel, SelectedEntry};
|
||||
// ext
|
||||
use std::path::PathBuf;
|
||||
|
||||
impl FileTransferActivity {
|
||||
/// ### action_open_local
|
||||
///
|
||||
/// Open local file
|
||||
pub(crate) fn action_open_local(&mut self, entry: &FsEntry, open_with: Option<&str>) {
|
||||
let real_entry: FsEntry = entry.get_realfile();
|
||||
if let FsEntry::File(file) = real_entry {
|
||||
// Open file
|
||||
let result = match open_with {
|
||||
None => open::that(file.abs_path.as_path()),
|
||||
Some(with) => open::with(file.abs_path.as_path(), with),
|
||||
};
|
||||
// Log result
|
||||
match result {
|
||||
Ok(_) => self.log(
|
||||
LogLevel::Info,
|
||||
format!("Opened file `{}`", entry.get_abs_path().display(),),
|
||||
pub(crate) fn action_open_local(&mut self) {
|
||||
let entries: Vec<FsEntry> = match self.get_local_selected_entries() {
|
||||
SelectedEntry::One(entry) => vec![entry],
|
||||
SelectedEntry::Many(entries) => entries,
|
||||
SelectedEntry::None => vec![],
|
||||
};
|
||||
entries
|
||||
.iter()
|
||||
.for_each(|x| self.action_open_local_file(x, None));
|
||||
}
|
||||
|
||||
/// ### action_open_remote
|
||||
///
|
||||
/// Open local file
|
||||
pub(crate) fn action_open_remote(&mut self) {
|
||||
let entries: Vec<FsEntry> = match self.get_remote_selected_entries() {
|
||||
SelectedEntry::One(entry) => vec![entry],
|
||||
SelectedEntry::Many(entries) => entries,
|
||||
SelectedEntry::None => vec![],
|
||||
};
|
||||
entries
|
||||
.iter()
|
||||
.for_each(|x| self.action_open_remote_file(x, None));
|
||||
}
|
||||
|
||||
/// ### action_open_local_file
|
||||
///
|
||||
/// Perform open lopcal file
|
||||
pub(crate) fn action_open_local_file(&mut self, entry: &FsEntry, open_with: Option<&str>) {
|
||||
let entry: FsEntry = entry.get_realfile();
|
||||
// Open file
|
||||
let result = match open_with {
|
||||
None => open::that(entry.get_abs_path().as_path()),
|
||||
Some(with) => open::with(entry.get_abs_path().as_path(), with),
|
||||
};
|
||||
// Log result
|
||||
match result {
|
||||
Ok(_) => self.log(
|
||||
LogLevel::Info,
|
||||
format!("Opened file `{}`", entry.get_abs_path().display(),),
|
||||
),
|
||||
Err(err) => self.log(
|
||||
LogLevel::Error,
|
||||
format!(
|
||||
"Failed to open filoe `{}`: {}",
|
||||
entry.get_abs_path().display(),
|
||||
err
|
||||
),
|
||||
Err(err) => self.log(
|
||||
LogLevel::Error,
|
||||
format!(
|
||||
"Failed to open filoe `{}`: {}",
|
||||
entry.get_abs_path().display(),
|
||||
err
|
||||
),
|
||||
),
|
||||
}
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// ### action_open_local
|
||||
///
|
||||
/// Open remote file. The file is first downloaded to a temporary directory on localhost
|
||||
pub(crate) fn action_open_remote(&mut self, entry: &FsEntry, open_with: Option<&str>) {
|
||||
let real_entry: FsEntry = entry.get_realfile();
|
||||
if let FsEntry::File(file) = real_entry {
|
||||
// Download file
|
||||
let tmp = match self.download_file_as_temp(&file) {
|
||||
Ok(f) => f,
|
||||
Err(err) => {
|
||||
self.log(
|
||||
LogLevel::Error,
|
||||
format!("Could not open `{}`: {}", file.abs_path.display(), err),
|
||||
);
|
||||
return;
|
||||
}
|
||||
};
|
||||
pub(crate) fn action_open_remote_file(&mut self, entry: &FsEntry, open_with: Option<&str>) {
|
||||
let entry: FsEntry = entry.get_realfile();
|
||||
// Download file
|
||||
let tmpfile: String = match self.get_cache_tmp_name(entry.get_name()) {
|
||||
None => {
|
||||
self.log(LogLevel::Error, String::from("Could not create tempdir"));
|
||||
return;
|
||||
}
|
||||
Some(p) => p,
|
||||
};
|
||||
let cache: PathBuf = match self.cache.as_ref() {
|
||||
None => {
|
||||
self.log(LogLevel::Error, String::from("Could not create tempdir"));
|
||||
return;
|
||||
}
|
||||
Some(p) => p.path().to_path_buf(),
|
||||
};
|
||||
self.filetransfer_recv(entry, cache.as_path(), Some(tmpfile.clone()));
|
||||
// Make file and open if file exists
|
||||
let mut tmp: PathBuf = cache;
|
||||
tmp.push(tmpfile.as_str());
|
||||
if tmp.exists() {
|
||||
// Open file
|
||||
let result = match open_with {
|
||||
None => open::that(tmp.as_path()),
|
||||
@@ -110,9 +146,9 @@ impl FileTransferActivity {
|
||||
SelectedEntry::None => vec![],
|
||||
};
|
||||
// Open all entries
|
||||
for entry in entries.iter() {
|
||||
self.action_open_local(entry, Some(with));
|
||||
}
|
||||
entries
|
||||
.iter()
|
||||
.for_each(|x| self.action_open_local_file(x, Some(with)));
|
||||
}
|
||||
|
||||
/// ### action_remote_open_with
|
||||
@@ -125,8 +161,8 @@ impl FileTransferActivity {
|
||||
SelectedEntry::None => vec![],
|
||||
};
|
||||
// Open all entries
|
||||
for entry in entries.iter() {
|
||||
self.action_open_remote(entry, Some(with));
|
||||
}
|
||||
entries
|
||||
.iter()
|
||||
.for_each(|x| self.action_open_remote_file(x, Some(with)));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user