From f7ac22a65f539f36c2f807e893bada358d1c3178 Mon Sep 17 00:00:00 2001 From: Travis Ralston <travpc@gmail.com> Date: Tue, 3 Sep 2019 19:48:18 -0600 Subject: [PATCH] Don't delete media unless it is the last one Fixes https://github.com/turt2live/matrix-media-repo/issues/199 --- .../maintainance_controller.go | 20 +++++++++++++++++-- docs/admin.md | 2 ++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/controllers/maintenance_controller/maintainance_controller.go b/controllers/maintenance_controller/maintainance_controller.go index 34cc6409..c9b701e3 100644 --- a/controllers/maintenance_controller/maintainance_controller.go +++ b/controllers/maintenance_controller/maintainance_controller.go @@ -283,10 +283,27 @@ func doPurge(media *types.Media, ctx context.Context, log *logrus.Entry) error { return err } - err = ds.DeleteObject(media.Location) + mediaDb := storage.GetDatabase().GetMediaStore(ctx, log) + similarMedia, err := mediaDb.GetByHash(media.Sha256Hash) if err != nil { return err } + hasSimilar := false + for _, m := range similarMedia { + if m.Origin != media.Origin && m.MediaId != media.MediaId { + hasSimilar = true + break + } + } + + if !hasSimilar || media.Quarantined { + err = ds.DeleteObject(media.Location) + if err != nil { + return err + } + } else { + log.Warnf("Not deleting media from datastore: media is shared over %d objects", len(similarMedia)) + } metadataDb := storage.GetDatabase().GetMetadataStore(ctx, log) @@ -308,7 +325,6 @@ func doPurge(media *types.Media, ctx context.Context, log *logrus.Entry) error { return nil } - mediaDb := storage.GetDatabase().GetMediaStore(ctx, log) err = mediaDb.Delete(media.Origin, media.MediaId) if err != nil { return err diff --git a/docs/admin.md b/docs/admin.md index 945e48bc..466a1d6d 100644 --- a/docs/admin.md +++ b/docs/admin.md @@ -6,6 +6,8 @@ All the API calls here require your user ID to be listed in the configuration as Sometimes you just want your disk space back - purging media is the best way to do that. **Be careful about what you're purging.** The media repo will happily purge a local media object, making it highly unlikely to ever exist in Matrix again. When the media repo deletes remote media, it is only deleting its copy of it - it cannot delete media on the remote server itself. Thumbnails will also be deleted for the media. +If the file is duplicated over many media records, it will not be physically deleted (however the media record that was purged will be counted as deleted). The exception to this is quarantined media: when the record being purged is also quarantined, the media is deleted from the datastore even if it is duplicated in multiple records. + #### Purge remote media URL: `POST /_matrix/media/unstable/admin/purge/remote?before_ts=1234567890&access_token=your_access_token` (`before_ts` is in milliseconds) -- GitLab