Make remote file expiry time required, better implement uncertainty flag

This commit is contained in:
timvisee 2018-05-02 18:52:41 +02:00
parent 4b4060ab34
commit d71ac28458
No known key found for this signature in database
GPG key ID: 109CBA0BF74036C2
4 changed files with 33 additions and 44 deletions

View file

@ -1,9 +1,7 @@
# Release 0.1 # 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 - Create history command
- History compiler flag - History compiler flag
- Lowercase error messages
- Automatically get owner token, from file history when setting password - Automatically get owner token, from file history when setting password
- Allow file/directory archiving on upload - Allow file/directory archiving on upload
- Allow unarchiving on download - Allow unarchiving on download

View file

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

View file

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

View file

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