From dea5fee6a38c5400d85d565fee1771633af3b180 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Tue, 27 Aug 2024 01:23:44 +0000 Subject: [PATCH] abstract duration parsing into utils Signed-off-by: Jason Volk --- Cargo.lock | 2 +- src/admin/media/commands.rs | 1 + src/core/Cargo.toml | 1 + src/core/utils/time.rs | 18 ++++++++++++++++++ src/service/Cargo.toml | 1 - src/service/media/mod.rs | 13 +++---------- 6 files changed, 24 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fabb9b09..a2a70cda 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -692,6 +692,7 @@ dependencies = [ "conduit_macros", "const-str", "ctor", + "cyborgtime", "either", "figment", "hardened_malloc-rs", @@ -790,7 +791,6 @@ dependencies = [ "conduit_core", "conduit_database", "const-str", - "cyborgtime", "futures-util", "hickory-resolver", "http", diff --git a/src/admin/media/commands.rs b/src/admin/media/commands.rs index 3894d5df..385419eb 100644 --- a/src/admin/media/commands.rs +++ b/src/admin/media/commands.rs @@ -176,6 +176,7 @@ pub(super) async fn delete_list(&self) -> Result { #[admin_command] pub(super) async fn delete_past_remote_media(&self, duration: String, force: bool) -> Result { + let duration = parse_timepoint_ago(&duration)?; let deleted_count = self .services .media diff --git a/src/core/Cargo.toml b/src/core/Cargo.toml index 2e2b0581..cc5865c8 100644 --- a/src/core/Cargo.toml +++ b/src/core/Cargo.toml @@ -68,6 +68,7 @@ clap.workspace = true conduit-macros.workspace = true const-str.workspace = true ctor.workspace = true +cyborgtime.workspace = true either.workspace = true figment.workspace = true http-body-util.workspace = true diff --git a/src/core/utils/time.rs b/src/core/utils/time.rs index 9a31632e..04f47ac3 100644 --- a/src/core/utils/time.rs +++ b/src/core/utils/time.rs @@ -1,5 +1,7 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH}; +use crate::{err, Result}; + #[inline] #[must_use] #[allow(clippy::as_conversions, clippy::cast_possible_truncation)] @@ -10,6 +12,22 @@ pub fn now_millis() -> u64 { .as_millis() as u64 } +#[inline] +pub fn parse_timepoint_ago(ago: &str) -> Result { timepoint_ago(parse_duration(ago)?) } + +#[inline] +pub fn timepoint_ago(duration: Duration) -> Result { + SystemTime::now() + .checked_sub(duration) + .ok_or_else(|| err!(Arithmetic("Duration {duration:?} is too large"))) +} + +#[inline] +pub fn parse_duration(duration: &str) -> Result { + cyborgtime::parse_duration(duration) + .map_err(|error| err!("'{duration:?}' is not a valid duration string: {error:?}")) +} + #[must_use] pub fn rfc2822_from_seconds(epoch: i64) -> String { use chrono::{DateTime, Utc}; diff --git a/src/service/Cargo.toml b/src/service/Cargo.toml index 92563934..cfed5a0e 100644 --- a/src/service/Cargo.toml +++ b/src/service/Cargo.toml @@ -46,7 +46,6 @@ bytes.workspace = true conduit-core.workspace = true conduit-database.workspace = true const-str.workspace = true -cyborgtime.workspace = true futures-util.workspace = true hickory-resolver.workspace = true http.workspace = true diff --git a/src/service/media/mod.rs b/src/service/media/mod.rs index 5e3f82b3..db0b8459 100644 --- a/src/service/media/mod.rs +++ b/src/service/media/mod.rs @@ -196,14 +196,7 @@ impl Service { /// Deletes all remote only media files in the given at or after /// time/duration. Returns a u32 with the amount of media files deleted. - pub async fn delete_all_remote_media_at_after_time(&self, time: String, force: bool) -> Result { - let user_duration: SystemTime = match cyborgtime::parse_duration(&time) { - Err(e) => return Err!(Database(error!("Failed to parse specified time duration: {e}"))), - Ok(duration) => SystemTime::now() - .checked_sub(duration) - .ok_or(err!(Arithmetic("Duration {duration:?} is too large")))?, - }; - + pub async fn delete_all_remote_media_at_after_time(&self, time: SystemTime, force: bool) -> Result { let all_keys = self.db.get_all_media_keys(); let mut remote_mxcs = Vec::with_capacity(all_keys.len()); @@ -271,7 +264,7 @@ impl Service { }; debug!("File created at: {file_created_at:?}"); - if file_created_at <= user_duration { + if file_created_at <= time { debug!("File is within user duration, pushing to list of file paths and keys to delete."); remote_mxcs.push(mxc.to_string()); } @@ -284,7 +277,7 @@ impl Service { return Err!(Database("Did not found any eligible MXCs to delete.")); } - debug_info!("Deleting media now in the past {user_duration:?}."); + debug_info!("Deleting media now in the past {time:?}."); let mut deletion_count: usize = 0; for mxc in remote_mxcs { let mxc: Mxc<'_> = mxc.as_str().try_into()?;