Kaynağa Gözat

Add expiry time uncertainty flag, assign default expiry to unkown files

timvisee 7 yıl önce
ebeveyn
işleme
4b4060ab34
2 değiştirilmiş dosya ile 32 ekleme ve 3 silme
  1. 24 2
      api/src/file/remote_file.rs
  2. 8 1
      cli/src/action/history.rs

+ 24 - 2
api/src/file/remote_file.rs

@@ -38,6 +38,9 @@ pub struct RemoteFile {
     /// The time the file will expire at, if known.
     expire_at: Option<DateTime<Utc>>,
 
+    /// Define whether the expiry time is uncertain.
+    expire_uncertain: bool,
+
     /// The host the file was uploaded to.
     #[serde(with = "url_serde")]
     host: Url,
@@ -58,16 +61,24 @@ impl RemoteFile {
     pub fn new(
         id: String,
         upload_at: Option<DateTime<Utc>>,
-        expire_at: Option<DateTime<Utc>>,
+        mut expire_at: Option<DateTime<Utc>>,
         host: Url,
         url: Url,
         secret: Vec<u8>,
         owner_token: Option<String>,
     ) -> 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));
+        }
+
+        // Build the object
         Self {
             id,
             upload_at,
             expire_at,
+            expire_uncertain,
             host,
             url,
             secret,
@@ -196,6 +207,16 @@ impl RemoteFile {
         }
     }
 
+    /// Check whehter the set expiry time is uncertain.
+    /// If the expiry time of a file is unknown,
+    /// the default time is assigned from the first time
+    /// the file was used. Such time will be uncertain as it probably isn't
+    /// correct.
+    /// This time may be used however to check for expiry.
+    pub fn expire_uncertain(&self) -> bool {
+        self.expire_uncertain
+    }
+
     /// Get the file URL, provided by the server.
     pub fn url(&self) -> &Url {
         &self.url
@@ -265,8 +286,9 @@ impl RemoteFile {
         }
 
         // Set the expire time
-        if other.expire_at.is_some() && (self.expire_at.is_none() || overwrite) {
+        if other.expire_at.is_some() && !other.expire_uncertain() && (self.expire_at.is_none() || overwrite) {
             self.expire_at = other.expire_at.clone();
+            self.expire_uncertain = false;
             changed = true;
         }
 

+ 8 - 1
cli/src/action/history.rs

@@ -69,7 +69,14 @@ impl<'a> History<'a> {
         for (i, file) in history.files().iter().enumerate() {
             // Build the expiry time string
             let expiry = match file.expire_duration() {
-                Some(ref expire) => format_duration(expire),
+                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(),
             };