From 966a3e9d8fad6aec18446199225afa5d28b264c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Wed, 22 Jan 2025 10:54:33 +0100 Subject: [PATCH] feat(sdk): Add methods to Media to interact with MediaRetentionPolicy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Kévin Commaille --- crates/matrix-sdk/src/media.rs | 40 +++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/crates/matrix-sdk/src/media.rs b/crates/matrix-sdk/src/media.rs index b39098bd2f..5b90ce0041 100644 --- a/crates/matrix-sdk/src/media.rs +++ b/crates/matrix-sdk/src/media.rs @@ -24,7 +24,7 @@ use std::{fmt, fs::File, path::Path}; use eyeball::SharedObservable; use futures_util::future::try_join; use matrix_sdk_base::event_cache::store::media::IgnoreMediaRetentionPolicy; -pub use matrix_sdk_base::media::*; +pub use matrix_sdk_base::{event_cache::store::media::MediaRetentionPolicy, media::*}; use mime::Mime; use ruma::{ api::{ @@ -672,6 +672,44 @@ impl Media { Ok(()) } + /// Set the [`MediaRetentionPolicy`] to use for deciding whether to store or + /// keep media content. + /// + /// It is used: + /// + /// * When a media needs to be cached, to check that it does not exceed the + /// max file size. + /// + /// * When [`Media::clean_up_media_cache()`], to check that all media + /// content in the store fits those criterias. + /// + /// To apply the new policy to the media cache right away, + /// [`Media::clean_up_media_cache()`] should be called after this. + /// + /// By default, an empty `MediaRetentionPolicy` is used, which means that no + /// criterias are applied. + /// + /// # Arguments + /// + /// * `policy` - The `MediaRetentionPolicy` to use. + pub async fn set_media_retention_policy(&self, policy: MediaRetentionPolicy) -> Result<()> { + self.client.event_cache_store().lock().await?.set_media_retention_policy(policy).await?; + Ok(()) + } + + /// Get the current `MediaRetentionPolicy`. + pub async fn media_retention_policy(&self) -> Result { + Ok(self.client.event_cache_store().lock().await?.media_retention_policy()) + } + + /// Clean up the media cache with the current [`MediaRetentionPolicy`]. + /// + /// If there is already an ongoing cleanup, this is a noop. + pub async fn clean_up_media_cache(&self) -> Result<()> { + self.client.event_cache_store().lock().await?.clean_up_media_cache().await?; + Ok(()) + } + /// Upload the file bytes in `data` and return the source information. pub(crate) async fn upload_plain_media_and_thumbnail( &self,