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
- 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

View file

@ -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| {
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
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>>) {
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;
}

View file

@ -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);
let mut expiry = format_duration(&file.expire_duration());
if file.expire_uncertain() {
expiry.insert(0, '~');
}
expiry
},
None => "?".into(),
};
// Get the owner token
let owner_token: String = match file.owner_token() {

View file

@ -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();