summaryrefslogtreecommitdiff
path: root/internal/media/manager.go
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2022-06-30 12:22:10 +0200
committerLibravatar GitHub <noreply@github.com>2022-06-30 12:22:10 +0200
commit9e7d022a06779a03e3eaaadad6cc33423f46892b (patch)
tree7c13c5af98fba382fc75c1dc195bdf0cc9cdd905 /internal/media/manager.go
parent[feature] Use default instance thumbnail if instance account header not set (... (diff)
downloadgotosocial-9e7d022a06779a03e3eaaadad6cc33423f46892b.tar.xz
[feature] Cleanup unattached local media (#680)
* add localUnattached db function * add parseOlderThan util function * add pruneunusedlocalattachments to media manager * add unusedlocal pruning to schedule + admin call * set number of days to keep as a const * fix test
Diffstat (limited to 'internal/media/manager.go')
-rw-r--r--internal/media/manager.go26
1 files changed, 24 insertions, 2 deletions
diff --git a/internal/media/manager.go b/internal/media/manager.go
index 663f74123..aacf607cc 100644
--- a/internal/media/manager.go
+++ b/internal/media/manager.go
@@ -34,6 +34,10 @@ import (
// selectPruneLimit is the amount of media entries to select at a time from the db when pruning
const selectPruneLimit = 20
+// UnusedLocalAttachmentCacheDays is the amount of days to keep local media in storage if it
+// is not attached to a status, or was never attached to a status.
+const UnusedLocalAttachmentCacheDays = 3
+
// Manager provides an interface for managing media: parsing, storing, and retrieving media objects like photos, videos, and gifs.
type Manager interface {
// ProcessMedia begins the process of decoding and storing the given data as an attachment.
@@ -75,11 +79,16 @@ type Manager interface {
//
// The returned int is the amount of media that was pruned by this function.
PruneAllRemote(ctx context.Context, olderThanDays int) (int, error)
- // PruneAllMeta prunes unused meta media -- currently, this means unused avatars + headers, but can also be extended
- // to include things like attachments that were uploaded on this server but left unused, etc.
+ // PruneAllMeta prunes unused/out of date headers and avatars cached on this instance.
//
// The returned int is the amount of media that was pruned by this function.
PruneAllMeta(ctx context.Context) (int, error)
+ // PruneUnusedLocalAttachments prunes unused media attachments that were uploaded by
+ // a user on this instance, but never actually attached to a status, or attached but
+ // later detached.
+ //
+ // The returned int is the amount of media that was pruned by this function.
+ PruneUnusedLocalAttachments(ctx context.Context) (int, error)
// Stop stops the underlying worker pool of the manager. It should be called
// when closing GoToSocial in order to cleanly finish any in-progress jobs.
@@ -210,6 +219,19 @@ func scheduleCleanupJobs(m *manager) error {
return fmt.Errorf("error starting media manager meta cleanup job: %s", err)
}
+ if _, err := c.AddFunc("@midnight", func() {
+ begin := time.Now()
+ pruned, err := m.PruneUnusedLocalAttachments(pruneCtx)
+ if err != nil {
+ logrus.Errorf("media manager: error pruning unused local attachments: %s", err)
+ return
+ }
+ logrus.Infof("media manager: pruned %d unused local attachments in %s", pruned, time.Since(begin))
+ }); err != nil {
+ pruneCancel()
+ return fmt.Errorf("error starting media manager unused local attachments cleanup job: %s", err)
+ }
+
// start remote cache cleanup cronjob if configured
if mediaRemoteCacheDays := config.GetMediaRemoteCacheDays(); mediaRemoteCacheDays > 0 {
if _, err := c.AddFunc("@midnight", func() {