From 916088a22fee4cca76de67d72224d7fab045cc1a Mon Sep 17 00:00:00 2001 From: Olivia Lee Date: Fri, 29 Nov 2024 11:33:13 -0800 Subject: [PATCH] include mxcs from dangling thumbnails in service::media::iter_all When requesting remote thumbnails over federation, we can end up with a thumbnail in the media db without an associated original file. Because of this, skipping thumbnails is insufficient to get a list of all MXCs. --- src/database/key_value/media.rs | 5 ----- src/service/media.rs | 20 ++++++++++++++++++-- src/service/media/data.rs | 4 +--- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/database/key_value/media.rs b/src/database/key_value/media.rs index a7272042..53e2146e 100644 --- a/src/database/key_value/media.rs +++ b/src/database/key_value/media.rs @@ -208,11 +208,6 @@ impl service::media::Data for KeyValueDatabase { let key = MediaFileKey::new(key); let parts = MediaFileKeyParts::try_from(&key)?; - if parts.width != 0 || parts.height != 0 { - // Skip thumbnails - return Ok(None); - }; - Ok(Some((parts.mxc, parts.meta, key))) }) .filter_map(Result::transpose), diff --git a/src/service/media.rs b/src/service/media.rs index b9ccddae..836e8ef9 100644 --- a/src/service/media.rs +++ b/src/service/media.rs @@ -169,11 +169,27 @@ impl Service { /// List all media stored in the database. /// - /// Each MXC is list once. Thumbnails are not included separately from the + /// Each MXC is listed once. Thumbnails are not included separately from the /// original media. #[tracing::instrument(skip(self))] pub(crate) fn iter_all(&self) -> impl Iterator> { - self.db.all_file_metadata().map(|media| media.map(|(mxc, ..)| mxc)) + let mut prev_mxc = None; + self.db + .all_file_metadata() + .map(|media| media.map(|(mxc, ..)| mxc)) + .filter(move |mxc| { + if let Ok(mxc) = mxc { + // Skip mxcs that we have already seen. All files associated + // with a given mxc should appear consecutively in the db + // iterator, so we only need to check against the previous + // value. + if prev_mxc.as_ref() == Some(mxc) { + return false; + } + prev_mxc = Some(mxc.clone()); + } + true + }) } /// Returns width, height of the thumbnail and whether it should be cropped. diff --git a/src/service/media/data.rs b/src/service/media/data.rs index 01b2ceb1..c3cff8cb 100644 --- a/src/service/media/data.rs +++ b/src/service/media/data.rs @@ -30,9 +30,7 @@ pub(crate) trait Data: Send + Sync { mxc: OwnedMxcUri, ) -> Result>; - /// Returns an iterator over metadata for all media. - /// - /// Thumbnails are not included. + /// Returns an iterator over metadata for all media, including thumbnails. fn all_file_metadata( &self, ) -> Box<