Browse Source

Make remote file expiry time required, better implement uncertainty flag

timvisee 7 years ago
parent
commit
d71ac28458
4 changed files with 34 additions and 45 deletions
  1. 1 3
      ROADMAP.md
  2. 28 30
      api/src/file/remote_file.rs
  3. 4 11
      cli/src/action/history.rs
  4. 1 1
      cli/src/history.rs

+ 1 - 3
ROADMAP.md

@@ -1,9 +1,7 @@
 # Release 0.1
-- Remote file expiry time:
-  - Add uncertainty flag
-  - Give all files the default expiry flag if not set (with uncertainty flag)
 - Create history command
 - History compiler flag
+- Lowercase error messages
 - Automatically get owner token, from file history when setting password
 - Allow file/directory archiving on upload
 - Allow unarchiving on download 

+ 28 - 30
api/src/file/remote_file.rs

@@ -35,8 +35,8 @@ pub struct RemoteFile {
     /// The time the file was uploaded at, if known.
     upload_at: Option<DateTime<Utc>>,
 
-    /// The time the file will expire at, if known.
-    expire_at: Option<DateTime<Utc>>,
+    /// The time the file will expire at.
+    expire_at: DateTime<Utc>,
 
     /// Define whether the expiry time is uncertain.
     expire_uncertain: bool,
@@ -61,7 +61,7 @@ impl RemoteFile {
     pub fn new(
         id: String,
         upload_at: Option<DateTime<Utc>>,
-        mut expire_at: Option<DateTime<Utc>>,
+        expire_at: Option<DateTime<Utc>>,
         host: Url,
         url: Url,
         secret: Vec<u8>,
@@ -69,9 +69,9 @@ impl RemoteFile {
     ) -> Self {
         // Assign the default expiry time if uncetain
         let expire_uncertain = expire_at.is_none();
-        if expire_uncertain {
-            expire_at = Some(Utc::now() + Duration::seconds(SEND_DEFAULT_EXPIRE_TIME));
-        }
+        let expire_at = expire_at.unwrap_or(
+            Utc::now() + Duration::seconds(SEND_DEFAULT_EXPIRE_TIME)
+        );
 
         // Build the object
         Self {
@@ -170,24 +170,28 @@ impl RemoteFile {
     /// Get the duration the file will expire after,
     /// if an expiry time is known.
     /// Otherwise `None` is returned.
-    pub fn expire_duration(&self) -> Option<Duration> {
-        self.expire_at.as_ref().map(|time| {
-            // Get the current time
-            let now = Utc::now();
-
-            // Return the duration if not expired, otherwise return zero
-            if time > &now {
-                *time - now
-            } else {
-                Duration::zero()
-            }
-        })
+    pub fn expire_duration(&self) -> Duration {
+        // Get the current time
+        let now = Utc::now();
+
+        // Return the duration if not expired, otherwise return zero
+        if self.expire_at > now {
+            self.expire_at - now
+        } else {
+            Duration::zero()
+        }
     }
 
     /// Set the time this file will expire at.
-    /// None may be given if the expire time is unknown.
+    /// None may be given to assign the default expiry time with the
+    /// uncertainty flag set.
     pub fn set_expire_at(&mut self, expire_at: Option<DateTime<Utc>>) {
-        self.expire_at = expire_at;
+        if let Some(expire_at) = expire_at {
+            self.expire_at = expire_at;
+        } else {
+            self.expire_at = Utc::now() + Duration::seconds(SEND_DEFAULT_EXPIRE_TIME);
+            self.expire_uncertain = true;
+        }
     }
 
     /// Set the time this file will expire at,
@@ -197,14 +201,8 @@ impl RemoteFile {
     }
 
     /// Check whether this file has expired, based on it's expiry property.
-    ///
-    /// If no expiry time is set (known) for this file,
-    /// the `def` value is returned instead.
-    pub fn has_expired(&self, def: bool) -> bool {
-        match self.expire_at {
-            Some(time) => time < Utc::now(),
-            None => def,
-        }
+    pub fn has_expired(&self) -> bool {
+        self.expire_at < Utc::now()
     }
 
     /// Check whehter the set expiry time is uncertain.
@@ -286,9 +284,9 @@ impl RemoteFile {
         }
 
         // Set the expire time
-        if other.expire_at.is_some() && !other.expire_uncertain() && (self.expire_at.is_none() || overwrite) {
+        if !other.expire_uncertain() && (self.expire_uncertain() || overwrite) {
             self.expire_at = other.expire_at.clone();
-            self.expire_uncertain = false;
+            self.expire_uncertain = other.expire_uncertain();
             changed = true;
         }
 

+ 4 - 11
cli/src/action/history.rs

@@ -68,17 +68,10 @@ impl<'a> History<'a> {
         // Add an entry for each file
         for (i, file) in history.files().iter().enumerate() {
             // Build the expiry time string
-            let expiry = match file.expire_duration() {
-                Some(ref expire) => {
-                    // Format the expiry date, add uncertainty question mark if relevant
-                    let mut expiry = format_duration(expire);
-                    if file.expire_uncertain() {
-                        expiry.insert(0, '~');
-                    }
-                    expiry
-                },
-                None => "?".into(),
-            };
+            let mut expiry = format_duration(&file.expire_duration());
+            if file.expire_uncertain() {
+                expiry.insert(0, '~');
+            }
 
             // Get the owner token
             let owner_token: String = match file.owner_token() {

+ 1 - 1
cli/src/history.rs

@@ -208,7 +208,7 @@ impl History {
         // Get a list of expired files
         let expired: Vec<RemoteFile> = self.files
             .iter()
-            .filter(|f| f.has_expired(false))
+            .filter(|f| f.has_expired())
             .cloned()
             .collect();