diff --git a/cli/src/action/delete.rs b/cli/src/action/delete.rs index 52b093e..fc0538b 100644 --- a/cli/src/action/delete.rs +++ b/cli/src/action/delete.rs @@ -1,4 +1,5 @@ use clap::ArgMatches; +use failure::Fail; use ffsend_api::action::delete::{ Error as DeleteError, Delete as ApiDelete, @@ -15,7 +16,8 @@ use cmd::matcher::{ main::MainMatcher, }; use error::ActionError; -use util::{ensure_owner_token, print_success}; +use history::History; +use util::{ensure_owner_token, print_error, print_success}; /// A file delete action. pub struct Delete<'a> { @@ -52,6 +54,17 @@ impl<'a> Delete<'a> { // Send the file deletion request ApiDelete::new(&file, None).invoke(&client)?; + // Remove the file from the history manager + // TODO: also remove if it was expired + if let Err(err) = History::load_remove_save( + matcher_main.history(), + &file, + ) { + print_error(err.context( + "Failed to remove file from local history, ignoring", + )); + } + // Print a success message print_success("File deleted"); diff --git a/cli/src/action/exists.rs b/cli/src/action/exists.rs index 8422817..854a035 100644 --- a/cli/src/action/exists.rs +++ b/cli/src/action/exists.rs @@ -1,4 +1,5 @@ use clap::ArgMatches; +use failure::Fail; use ffsend_api::action::exists::{ Error as ExistsError, Exists as ApiExists, @@ -12,8 +13,11 @@ use ffsend_api::reqwest::Client; use cmd::matcher::{ Matcher, exists::ExistsMatcher, + main::MainMatcher, }; use error::ActionError; +use history::History; +use util::print_error; /// A file exists action. pub struct Exists<'a> { @@ -33,6 +37,7 @@ impl<'a> Exists<'a> { pub fn invoke(&self) -> Result<(), ActionError> { // Create the command matchers let matcher_exists = ExistsMatcher::with(self.cmd_matches).unwrap(); + let matcher_main = MainMatcher::with(self.cmd_matches).unwrap(); // Get the share URL let url = matcher_exists.url(); @@ -54,6 +59,19 @@ impl<'a> Exists<'a> { println!("Password: {:?}", exists_response.has_password()); } + // Remove the file from the history manager if it doesn't exist + // TODO: add if it does exist + if !exists { + if let Err(err) = History::load_remove_save( + matcher_main.history(), + &file, + ) { + print_error(err.context( + "Failed to remove file from local history, ignoring", + )); + } + } + Ok(()) } } diff --git a/cli/src/action/info.rs b/cli/src/action/info.rs index a6df14f..e0de85d 100644 --- a/cli/src/action/info.rs +++ b/cli/src/action/info.rs @@ -20,6 +20,7 @@ use cmd::matcher::{ info::InfoMatcher, main::MainMatcher, }; +use history::History; use util::{ensure_owner_token, ensure_password, print_error}; /// A file info action. @@ -58,6 +59,16 @@ impl<'a> Info<'a> { // Check whether the file exists let exists = ApiExists::new(&file).invoke(&client)?; if !exists.exists() { + // Remove the file from the history manager if it doesn't exist + if let Err(err) = History::load_remove_save( + matcher_main.history(), + &file, + ) { + print_error(err.context( + "Failed to remove file from local history, ignoring", + )); + } + return Err(Error::Expired); } diff --git a/cli/src/action/upload.rs b/cli/src/action/upload.rs index 62dfd14..f4bf6fc 100644 --- a/cli/src/action/upload.rs +++ b/cli/src/action/upload.rs @@ -130,7 +130,9 @@ impl<'a> Upload<'a> { matcher_main.history(), file.clone(), ) { - print_error(err.context("Failed to add file to history, ignoring")); + print_error(err.context( + "Failed to add file to local history, ignoring", + )); } // Open the URL in the browser diff --git a/cli/src/history.rs b/cli/src/history.rs index cf852e6..51c009d 100644 --- a/cli/src/history.rs +++ b/cli/src/history.rs @@ -111,14 +111,23 @@ impl History { /// Load the history from the given path, add the given file, and save it /// again. - /// If there is not history file at the given path, a new empty one will - /// be created. + /// If there is no file at the given path, new history will be created. pub fn load_add_save(path: PathBuf, file: RemoteFile) -> Result<(), Error> { let mut history = Self::load_or_new(path)?; history.add(file); history.save().map_err(|err| err.into()) } + /// Load the history from the given path, remove the given file by it's + /// ID, and save it again. + /// True is returned if any file was removed. + pub fn load_remove_save(path: PathBuf, file: &RemoteFile) -> Result { + let mut history = Self::load_or_new(path)?; + let removed = history.remove(file); + history.save()?; + Ok(removed) + } + /// Add the given remote file to the history. pub fn add(&mut self, file: RemoteFile) { self.files.push(file);