use failure::Fail;
use ffsend_api::file::remote_file::RemoteFile;
use crate::cmd::matcher::MainMatcher;
use crate::history::{Error as HistoryError, History};
use crate::util::print_error;
fn add_error(
matcher_main: &MainMatcher,
file: RemoteFile,
overwrite: bool,
) -> Result<(), HistoryError> {
if matcher_main.incognito() {
return Ok(());
}
let mut history = History::load_or_new(matcher_main.history())?;
history.add(file, overwrite);
history.save().map_err(|err| err.into())
}
pub fn add(matcher_main: &MainMatcher, file: RemoteFile, overwrite: bool) {
if let Err(err) = add_error(matcher_main, file, overwrite) {
print_error(err.context("failed to add file to local history, ignoring"));
}
}
fn remove_error(matcher_main: &MainMatcher, file: &RemoteFile) -> Result<bool, HistoryError> {
if matcher_main.incognito() {
return Ok(false);
}
let mut history = History::load_or_new(matcher_main.history())?;
let removed = history.remove(file.id());
history.save()?;
Ok(removed)
}
pub fn remove(matcher_main: &MainMatcher, file: &RemoteFile) -> bool {
let result = remove_error(matcher_main, file);
let ok = result.is_ok();
if let Err(err) = result {
print_error(err.context("failed to remove file from local history, ignoring"));
}
ok
}
pub fn derive_file_properties(matcher_main: &MainMatcher, file: &mut RemoteFile) -> bool {
if file.has_secret() && file.has_owner_token() {
return false;
}
let history = match History::load_or_new(matcher_main.history()) {
Ok(history) => history,
Err(err) => {
print_error(err.context("failed to derive file properties from history, ignoring"));
return false;
}
};
match history.get_file(file) {
Some(f) => {
if f.has_secret() {
file.set_secret(f.secret_raw().clone());
}
if f.has_owner_token() {
file.set_owner_token(f.owner_token().cloned());
}
f.has_secret() || f.has_owner_token()
}
None => false,
}
}