diff --git a/ROADMAP.md b/ROADMAP.md index 432c522..cc06856 100644 --- a/ROADMAP.md +++ b/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 diff --git a/api/src/file/remote_file.rs b/api/src/file/remote_file.rs index 009c7c5..56bdb04 100644 --- a/api/src/file/remote_file.rs +++ b/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>, - /// The time the file will expire at, if known. - expire_at: Option>, + /// The time the file will expire at. + expire_at: DateTime, /// Define whether the expiry time is uncertain. expire_uncertain: bool, @@ -61,7 +61,7 @@ impl RemoteFile { pub fn new( id: String, upload_at: Option>, - mut expire_at: Option>, + expire_at: Option>, host: Url, url: Url, secret: Vec, @@ -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 { - self.expire_at.as_ref().map(|time| { - // Get the current time - let now = Utc::now(); + pub fn expire_duration(&self) -> Duration { + // 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() - } - }) + // 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>) { - 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; } diff --git a/cli/src/action/history.rs b/cli/src/action/history.rs index 9607c04..40bbc08 100644 --- a/cli/src/action/history.rs +++ b/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() { diff --git a/cli/src/history.rs b/cli/src/history.rs index 625d511..73111c1 100644 --- a/cli/src/history.rs +++ b/cli/src/history.rs @@ -208,7 +208,7 @@ impl History { // Get a list of expired files let expired: Vec = self.files .iter() - .filter(|f| f.has_expired(false)) + .filter(|f| f.has_expired()) .cloned() .collect();