allow deleting dangling thumbnails

Previously attempting to delete an MXC that is only associated with
dangling thumbnails would fail, because it assumes that every thumbnail
must have a corresponding original in the db, and errors out if it can't
find the original. This is incorrect because we create dangling
thumbnails when requesting a remote thumbnail over federation when we
don't have the original file.
This commit is contained in:
Olivia Lee 2024-12-01 00:40:55 -08:00
parent 916088a22f
commit 46e8a63489
No known key found for this signature in database
GPG key ID: 54D568A15B9CD1F9

View file

@ -117,16 +117,11 @@ impl Service {
/// Deletes a media object and all associated thumbnails. /// Deletes a media object and all associated thumbnails.
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub(crate) async fn delete(&self, mxc: OwnedMxcUri) -> Result<()> { pub(crate) async fn delete(&self, mxc: OwnedMxcUri) -> Result<()> {
let (_, key) = self let mut any_files = false;
.db
.search_file_metadata(mxc.clone(), 0, 0)
.inspect_err(
|error| warn!(%error, "Failed to find original media key"),
)?
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Media not found"))?;
let thumbnails = self.db.search_thumbnails_metadata(mxc)?; let thumbnails = self.db.search_thumbnails_metadata(mxc.clone())?;
for (_, thumbnail_key) in thumbnails { for (_, thumbnail_key) in thumbnails {
any_files = true;
self.delete_by_key(thumbnail_key.clone()).await.inspect_err( self.delete_by_key(thumbnail_key.clone()).await.inspect_err(
|error| { |error| {
warn!( warn!(
@ -140,11 +135,25 @@ impl Service {
)?; )?;
} }
if let Some((_, key)) =
self.db.search_file_metadata(mxc, 0, 0).inspect_err(
|error| warn!(%error, "Failed to find original media key"),
)?
{
any_files = true;
self.delete_by_key(key).await.inspect_err( self.delete_by_key(key).await.inspect_err(
|error| warn!(%error, "Failed to delete original media"), |error| warn!(%error, "Failed to delete original media"),
)?; )?;
}
if any_files {
Ok(()) Ok(())
} else {
let error =
Error::BadRequest(ErrorKind::NotFound, "Media not found");
warn!(%error, "Failed to delete media");
Err(error)
}
} }
/// Deletes a specific media key, which may or may not be a thumbnail. /// Deletes a specific media key, which may or may not be a thumbnail.