From 176d95c2a8e55c31de479cf0857028cc3daf6927 Mon Sep 17 00:00:00 2001 From: Lux Aliaga Date: Fri, 7 Jun 2024 12:53:44 -0400 Subject: [PATCH] admin: media: Force flag on past media removal When enabled, if a file is deemed unremovable, it skips past it and continues deleting all other files that fit the criteria. Additionally, fix age comparison under the same command. Signed-off-by: Lux Aliaga --- src/admin/media/media_commands.rs | 6 ++++-- src/admin/media/mod.rs | 6 +++++- src/service/media/mod.rs | 12 +++++++++--- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/admin/media/media_commands.rs b/src/admin/media/media_commands.rs index e69fbfec..956f5743 100644 --- a/src/admin/media/media_commands.rs +++ b/src/admin/media/media_commands.rs @@ -164,10 +164,12 @@ pub(crate) async fn delete_list(body: Vec<&str>) -> Result, duration: String) -> Result { +pub(crate) async fn delete_past_remote_media( + _body: Vec<&str>, duration: String, force: bool, +) -> Result { let deleted_count = services() .media - .delete_all_remote_media_at_after_time(duration) + .delete_all_remote_media_at_after_time(duration, force) .await?; Ok(RoomMessageEventContent::text_plain(format!( diff --git a/src/admin/media/mod.rs b/src/admin/media/mod.rs index 4e21b750..b5957370 100644 --- a/src/admin/media/mod.rs +++ b/src/admin/media/mod.rs @@ -32,6 +32,9 @@ pub(crate) enum MediaCommand { /// - The duration (at or after), e.g. "5m" to delete all media in the /// past 5 minutes duration: String, + /// Continues deleting remote media if an undeletable object is found + #[arg(short, long)] + force: bool, }, } @@ -44,6 +47,7 @@ pub(crate) async fn process(command: MediaCommand, body: Vec<&str>) -> Result delete_list(body).await?, MediaCommand::DeletePastRemoteMedia { duration, - } => delete_past_remote_media(body, duration).await?, + force, + } => delete_past_remote_media(body, duration, force).await?, }) } diff --git a/src/service/media/mod.rs b/src/service/media/mod.rs index c7326e1a..d23e082c 100644 --- a/src/service/media/mod.rs +++ b/src/service/media/mod.rs @@ -182,7 +182,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) -> Result { + pub async fn delete_all_remote_media_at_after_time(&self, time: String, force: bool) -> Result { let all_keys = self.db.get_all_media_keys(); let user_duration: SystemTime = match cyborgtime::parse_duration(&time) { @@ -258,11 +258,17 @@ impl Service { debug!("btime is unsupported, using mtime instead"); file_metadata.modified()? }, - Err(err) => return Err(err.into()), + Err(err) => { + if force { + error!("Could not delete MXC path {:?}: {:?}. Skipping...", path, err); + continue; + } + return Err(err.into()); + }, }; debug!("File created at: {:?}", file_created_at); - if file_created_at >= user_duration { + if file_created_at <= user_duration { debug!("File is within user duration, pushing to list of file paths and keys to delete."); remote_mxcs.push(mxc.to_string()); }