mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-12-10 23:58:07 -06:00
[feature] Unused avatar and header cleanup (#574)
* rename + tidy up remote pruning * fix media attachment account join see https://bun.uptrace.dev/guide/golang-orm.html#table-relationships * update logging to new function name * add get avatars and headers to bun * add pruneallmeta function * don't set uncached since we're deleting anyway * fix totalPruned being in wrong place * test pruning meta * go fmt ./... * rename mediaprune * add meta pruning to routine mediaprune * tidy up cleanup job scheduling * rename adminmediaremoteprune * update mediacleanup to use renamed prune func * update swagger docs a little bit * reuse cancel + context
This commit is contained in:
parent
6e947ff266
commit
b143877995
17 changed files with 365 additions and 80 deletions
|
|
@ -29,10 +29,7 @@ import (
|
|||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
)
|
||||
|
||||
// amount of media attachments to select at a time from the db when pruning
|
||||
const selectPruneLimit = 20
|
||||
|
||||
func (m *manager) PruneRemote(ctx context.Context, olderThanDays int) (int, error) {
|
||||
func (m *manager) PruneAllRemote(ctx context.Context, olderThanDays int) (int, error) {
|
||||
var totalPruned int
|
||||
|
||||
// convert days into a duration string
|
||||
|
|
@ -40,23 +37,23 @@ func (m *manager) PruneRemote(ctx context.Context, olderThanDays int) (int, erro
|
|||
// parse the duration string into a duration
|
||||
olderThanHours, err := time.ParseDuration(olderThanHoursString)
|
||||
if err != nil {
|
||||
return totalPruned, fmt.Errorf("PruneRemote: %d", err)
|
||||
return totalPruned, fmt.Errorf("PruneAllRemote: %d", err)
|
||||
}
|
||||
// 'subtract' that from the time now to give our threshold
|
||||
olderThan := time.Now().Add(-olderThanHours)
|
||||
logrus.Infof("PruneRemote: pruning media older than %s", olderThan)
|
||||
logrus.Infof("PruneAllRemote: pruning media older than %s", olderThan)
|
||||
|
||||
// select 20 attachments at a time and prune them
|
||||
for attachments, err := m.db.GetRemoteOlderThan(ctx, olderThan, selectPruneLimit); err == nil && len(attachments) != 0; attachments, err = m.db.GetRemoteOlderThan(ctx, olderThan, selectPruneLimit) {
|
||||
|
||||
// use the age of the oldest attachment (the last one in the slice) as the next 'older than' value
|
||||
l := len(attachments)
|
||||
logrus.Tracef("PruneRemote: got %d attachments older than %s", l, olderThan)
|
||||
logrus.Tracef("PruneAllRemote: got %d attachments older than %s", l, olderThan)
|
||||
olderThan = attachments[l-1].CreatedAt
|
||||
|
||||
// prune each attachment
|
||||
for _, attachment := range attachments {
|
||||
if err := m.PruneOne(ctx, attachment); err != nil {
|
||||
if err := m.pruneOneRemote(ctx, attachment); err != nil {
|
||||
return totalPruned, err
|
||||
}
|
||||
totalPruned++
|
||||
|
|
@ -68,14 +65,14 @@ func (m *manager) PruneRemote(ctx context.Context, olderThanDays int) (int, erro
|
|||
return totalPruned, err
|
||||
}
|
||||
|
||||
logrus.Infof("PruneRemote: finished pruning remote media: pruned %d entries", totalPruned)
|
||||
logrus.Infof("PruneAllRemote: finished pruning remote media: pruned %d entries", totalPruned)
|
||||
return totalPruned, nil
|
||||
}
|
||||
|
||||
func (m *manager) PruneOne(ctx context.Context, attachment *gtsmodel.MediaAttachment) error {
|
||||
func (m *manager) pruneOneRemote(ctx context.Context, attachment *gtsmodel.MediaAttachment) error {
|
||||
if attachment.File.Path != "" {
|
||||
// delete the full size attachment from storage
|
||||
logrus.Tracef("PruneOne: deleting %s", attachment.File.Path)
|
||||
logrus.Tracef("pruneOneRemote: deleting %s", attachment.File.Path)
|
||||
if err := m.storage.Delete(attachment.File.Path); err != nil && err != storage.ErrNotFound {
|
||||
return err
|
||||
}
|
||||
|
|
@ -84,7 +81,7 @@ func (m *manager) PruneOne(ctx context.Context, attachment *gtsmodel.MediaAttach
|
|||
|
||||
if attachment.Thumbnail.Path != "" {
|
||||
// delete the thumbnail from storage
|
||||
logrus.Tracef("PruneOne: deleting %s", attachment.Thumbnail.Path)
|
||||
logrus.Tracef("pruneOneRemote: deleting %s", attachment.Thumbnail.Path)
|
||||
if err := m.storage.Delete(attachment.Thumbnail.Path); err != nil && err != storage.ErrNotFound {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue