浏览代码

Update history file properties from info action response, add history files in exists action

timvisee 7 年之前
父节点
当前提交
c0dafe41e6

+ 1 - 0
Cargo.lock

@@ -361,6 +361,7 @@ dependencies = [
  "rpassword 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "serde 1.0.43 (registry+https://github.com/rust-lang/crates.io-index)",
  "serde_derive 1.0.43 (registry+https://github.com/rust-lang/crates.io-index)",
+ "time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
  "toml 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 

+ 1 - 2
ROADMAP.md

@@ -1,6 +1,4 @@
 # Release 0.1
-- Define minimum arguments for options such as `--history`, do not allow empty
-- Update history file TTL and other properties on info fetch
 - Remote file expiry time:
   - Add uncertainty flag
   - Give all files the default expiry flag if not set (with uncertainty flag)
@@ -11,6 +9,7 @@
 - Allow unarchiving on download 
 - Show a simplified command list when calling `ffsend` without arguments
 - Use clipboard through `xclip` on Linux if available for persistence
+- Allow environment variable settings using `Arg.env(NAME)`
 - Automated releases through CI
 - Release binaries on GitHub
 - Ubuntu PPA package

+ 17 - 5
api/src/file/remote_file.rs

@@ -157,6 +157,18 @@ impl RemoteFile {
         &self.id
     }
 
+    /// Set the time this file will expire at.
+    /// None may be given if the expire time is unknown.
+    pub fn set_expire_at(&mut self, expire_at: Option<DateTime<Utc>>) {
+        self.expire_at = expire_at;
+    }
+
+    /// Set the time this file will expire at,
+    /// based on the given duration from now.
+    pub fn set_expire_duration(&mut self, duration: Duration) {
+        self.set_expire_at(Some(Utc::now() + duration));
+    }
+
     /// Check whether this file has expired, based on it's expiry property.
     ///
     /// If no expiry time is set (known) for this file,
@@ -221,30 +233,30 @@ impl RemoteFile {
     /// This is ofcourse only done for properties that may be empty.
     ///
     /// The file IDs are not asserted for equality.
-    pub fn merge(&mut self, other: &RemoteFile) -> bool {
+    pub fn merge(&mut self, other: &RemoteFile, overwrite: bool) -> bool {
         // Remember whether anything was changed
         let mut changed = false;
 
         // Set the upload time
-        if self.upload_at.is_none() && other.upload_at.is_some() {
+        if other.upload_at.is_some() && (self.upload_at.is_none() || overwrite) {
             self.upload_at = other.upload_at.clone();
             changed = true;
         }
 
         // Set the expire time
-        if self.expire_at.is_none() && other.expire_at.is_some() {
+        if other.expire_at.is_some() && (self.expire_at.is_none() || overwrite) {
             self.expire_at = other.expire_at.clone();
             changed = true;
         }
 
         // Set the secret
-        if !self.has_secret() && other.has_secret() {
+        if other.has_secret() && (!self.has_secret() || overwrite) {
             self.secret = other.secret_raw().clone();
             changed = true;
         }
 
         // Set the owner token
-        if self.owner_token.is_none() && other.owner_token.is_some() {
+        if other.owner_token.is_some() && (self.owner_token.is_none() || overwrite) {
             self.owner_token = other.owner_token.clone();
             changed = true;
         }

+ 1 - 0
cli/Cargo.toml

@@ -30,4 +30,5 @@ pbr = "1"
 rpassword = "2.0"
 serde = "1.0"
 serde_derive = "1.0"
+time = "0.1"
 toml = "0.4"

+ 1 - 1
cli/src/action/download.rs

@@ -108,7 +108,7 @@ impl<'a> Download<'a> {
         ).invoke(&client, bar)?;
 
         // Add the file to the history
-        history_tool::add(&matcher_main, file);
+        history_tool::add(&matcher_main, file, true);
 
         // TODO: open the file, or it's location
         // TODO: copy the file location

+ 4 - 3
cli/src/action/exists.rs

@@ -57,9 +57,10 @@ 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 {
+        // Add or remove the file from the history
+        if exists {
+            history_tool::add(&matcher_main, file, false);
+        } else {
             history_tool::remove(&matcher_main, &file);
         }
 

+ 7 - 1
cli/src/action/info.rs

@@ -14,6 +14,7 @@ use ffsend_api::file::remote_file::{
     RemoteFile,
 };
 use ffsend_api::reqwest::Client;
+use time::Duration;
 
 use cmd::matcher::{
     Matcher,
@@ -77,8 +78,13 @@ impl<'a> Info<'a> {
             )))
             .ok();
 
+        // Update file properties
+        file.set_expire_duration(
+            Duration::milliseconds(info.ttl_millis() as i64),
+        );
+
         // Add the file to the history
-        history_tool::add(&matcher_main, file.clone());
+        history_tool::add(&matcher_main, file.clone(), true);
 
         // Print the result
         println!("ID: {}", file.id());

+ 1 - 1
cli/src/action/params.rs

@@ -65,7 +65,7 @@ impl<'a> Params<'a> {
         result?;
 
         // Update the history
-        history_tool::add(&matcher_main, file);
+        history_tool::add(&matcher_main, file, true);
 
         // Print a success message
         print_success("Parameters set");

+ 1 - 1
cli/src/action/password.rs

@@ -60,7 +60,7 @@ impl<'a> Password<'a> {
         result?;
 
         // Add the file to the history
-        history_tool::add(&matcher_main, file);
+        history_tool::add(&matcher_main, file, true);
 
         // Print a success message
         print_success("Password set");

+ 1 - 1
cli/src/action/upload.rs

@@ -126,7 +126,7 @@ impl<'a> Upload<'a> {
         println!("Owner token: {}", file.owner_token().unwrap());
 
         // Add the file to the history manager
-        history_tool::add(&matcher_main, file.clone());
+        history_tool::add(&matcher_main, file.clone(), false);
 
         // Open the URL in the browser
         if matcher_upload.open() {

+ 1 - 1
cli/src/cmd/handler.rs

@@ -71,7 +71,7 @@ impl<'a: 'b, 'b> Handler<'a> {
             .arg(Arg::with_name("yes")
                 .long("yes")
                 .short("y")
-                .visible_alias("assume-yes")
+                .alias("assume-yes")
                 .global(true)
                 .help("Assume yes for prompts"))
             .arg(Arg::with_name("history")

+ 5 - 2
cli/src/history.rs

@@ -112,13 +112,16 @@ impl History {
     /// Add the given remote file to the history.
     /// If a file with the same ID as the given file exists,
     /// the files are merged, see `RemoteFile::merge()`.
-    pub fn add(&mut self, file: RemoteFile) {
+    ///
+    /// If `overwrite` is set to true, the given file will overwrite
+    /// properties on the existing file.
+    pub fn add(&mut self, file: RemoteFile, overwrite: bool) {
         // Merge any existing file with the same ID
         {
             // Find anything to merge
             let merge_info: Vec<bool> = self.files.iter_mut()
                 .filter(|f| f.id() == file.id())
-                .map(|ref mut f| f.merge(&file))
+                .map(|ref mut f| f.merge(&file, overwrite))
                 .collect();
             let merged = !merge_info.is_empty();
             let changed = merge_info.iter().any(|i| *i);

+ 14 - 4
cli/src/history_tool.rs

@@ -10,8 +10,13 @@ use util::print_error;
 
 /// Load the history from the given path, add the given file, and save it
 /// again.
+///
+/// When a file with the same ID already exists, the existing file is
+/// merged with this one. If `overwrite` is set to true, this file will
+/// overwrite properties in the already existing file when merging.
+///
 /// If there is no file at the given path, new history will be created.
-fn add_error(matcher_main: &MainMatcher, file: RemoteFile)
+fn add_error(matcher_main: &MainMatcher, file: RemoteFile, overwrite: bool)
     -> Result<(), HistoryError>
 {
     // Ignore if incognito
@@ -21,16 +26,21 @@ fn add_error(matcher_main: &MainMatcher, file: RemoteFile)
 
     // Load the history, add the file, and save
     let mut history = History::load_or_new(matcher_main.history())?;
-    history.add(file);
+    history.add(file, overwrite);
     history.save().map_err(|err| err.into())
 }
 
 /// Load the history from the given path, add the given file, and save it
 /// again.
 /// If there is no file at the given path, new history will be created.
+///
+/// When a file with the same ID already exists, the existing file is
+/// merged with this one. If `overwrite` is set to true, this file will
+/// overwrite properties in the already existing file when merging.
+///
 /// If an error occurred, the error is printed and ignored.
-pub fn add(matcher_main: &MainMatcher, file: RemoteFile) {
-    if let Err(err) = add_error(matcher_main, file) {
+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",
         ));

+ 1 - 0
cli/src/main.rs

@@ -12,6 +12,7 @@ extern crate rpassword;
 extern crate serde;
 #[macro_use]
 extern crate serde_derive;
+extern crate time;
 
 mod action;
 mod cmd;