2022-03-07 13:08:26 +03:00
|
|
|
/*
|
|
|
|
GoToSocial
|
|
|
|
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package media
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-12-12 14:22:19 +03:00
|
|
|
"errors"
|
|
|
|
"time"
|
2022-03-07 13:08:26 +03:00
|
|
|
|
2022-11-05 14:10:19 +03:00
|
|
|
"codeberg.org/gruf/go-store/v2/storage"
|
2022-03-07 13:08:26 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2022-07-19 11:47:55 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
2022-03-07 13:08:26 +03:00
|
|
|
)
|
|
|
|
|
2022-05-15 17:45:04 +03:00
|
|
|
func (m *manager) PruneAllRemote(ctx context.Context, olderThanDays int) (int, error) {
|
2022-03-07 13:08:26 +03:00
|
|
|
var totalPruned int
|
|
|
|
|
2022-12-12 14:22:19 +03:00
|
|
|
olderThan := time.Now().Add(-time.Hour * 24 * time.Duration(olderThanDays))
|
2022-07-19 11:47:55 +03:00
|
|
|
log.Infof("PruneAllRemote: pruning media older than %s", olderThan)
|
2022-03-07 13:08:26 +03:00
|
|
|
|
2022-12-12 14:22:19 +03:00
|
|
|
for {
|
|
|
|
// Select "selectPruneLimit" status attacchments at a time for pruning
|
|
|
|
attachments, err := m.db.GetRemoteOlderThan(ctx, olderThan, selectPruneLimit)
|
|
|
|
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
|
|
|
return totalPruned, err
|
|
|
|
} else if len(attachments) == 0 {
|
|
|
|
break
|
|
|
|
}
|
2022-03-07 13:08:26 +03:00
|
|
|
|
2022-12-12 14:22:19 +03:00
|
|
|
// use the age of the oldest attachment (last in slice) as the next 'olderThan' value
|
|
|
|
log.Tracef("PruneAllRemote: got %d status attachments older than %s", len(attachments), olderThan)
|
|
|
|
olderThan = attachments[len(attachments)-1].CreatedAt
|
2022-03-07 13:08:26 +03:00
|
|
|
|
2022-12-12 14:22:19 +03:00
|
|
|
// prune each status attachment
|
2022-03-07 13:08:26 +03:00
|
|
|
for _, attachment := range attachments {
|
2022-05-15 17:45:04 +03:00
|
|
|
if err := m.pruneOneRemote(ctx, attachment); err != nil {
|
2022-03-07 13:08:26 +03:00
|
|
|
return totalPruned, err
|
|
|
|
}
|
|
|
|
totalPruned++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 11:47:55 +03:00
|
|
|
log.Infof("PruneAllRemote: finished pruning remote media: pruned %d entries", totalPruned)
|
2022-03-07 13:08:26 +03:00
|
|
|
return totalPruned, nil
|
|
|
|
}
|
|
|
|
|
2022-05-15 17:45:04 +03:00
|
|
|
func (m *manager) pruneOneRemote(ctx context.Context, attachment *gtsmodel.MediaAttachment) error {
|
2022-08-15 13:35:05 +03:00
|
|
|
var changed bool
|
|
|
|
|
2022-03-07 13:08:26 +03:00
|
|
|
if attachment.File.Path != "" {
|
|
|
|
// delete the full size attachment from storage
|
2022-07-19 11:47:55 +03:00
|
|
|
log.Tracef("pruneOneRemote: deleting %s", attachment.File.Path)
|
2022-12-12 14:22:19 +03:00
|
|
|
if err := m.storage.Delete(ctx, attachment.File.Path); err != nil && !errors.Is(err, storage.ErrNotFound) {
|
2022-03-07 13:08:26 +03:00
|
|
|
return err
|
|
|
|
}
|
2022-08-15 13:35:05 +03:00
|
|
|
cached := false
|
|
|
|
attachment.Cached = &cached
|
|
|
|
changed = true
|
2022-03-07 13:08:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if attachment.Thumbnail.Path != "" {
|
|
|
|
// delete the thumbnail from storage
|
2022-07-19 11:47:55 +03:00
|
|
|
log.Tracef("pruneOneRemote: deleting %s", attachment.Thumbnail.Path)
|
2022-12-12 14:22:19 +03:00
|
|
|
if err := m.storage.Delete(ctx, attachment.Thumbnail.Path); err != nil && !errors.Is(err, storage.ErrNotFound) {
|
2022-03-07 13:08:26 +03:00
|
|
|
return err
|
|
|
|
}
|
2022-08-15 13:35:05 +03:00
|
|
|
cached := false
|
|
|
|
attachment.Cached = &cached
|
|
|
|
changed = true
|
2022-03-07 13:08:26 +03:00
|
|
|
}
|
|
|
|
|
2022-12-12 14:22:19 +03:00
|
|
|
if !changed {
|
|
|
|
return nil
|
2022-08-15 13:35:05 +03:00
|
|
|
}
|
|
|
|
|
2022-12-12 14:22:19 +03:00
|
|
|
// update the attachment to reflect that we no longer have it cached
|
|
|
|
return m.db.UpdateByID(ctx, attachment, attachment.ID, "updated_at", "cached")
|
2022-03-07 13:08:26 +03:00
|
|
|
}
|