[chore/performance] Update media prune logic, add extra CLI command (#1474)

* start updating media prune stuff a wee bit

* continue prune / uncache work

* more tidying + consistency stuff

* add prune CLI command

* docs

* arg
This commit is contained in:
tobi
2023-02-11 12:48:38 +01:00
committed by GitHub
parent 70739d32cc
commit 40bc03e717
31 changed files with 1113 additions and 1090 deletions

View File

@@ -23,7 +23,6 @@ import (
"fmt"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/log"
)
func (p *processor) MediaPrune(ctx context.Context, mediaRemoteCacheDays int) gtserror.WithCode {
@@ -32,32 +31,10 @@ func (p *processor) MediaPrune(ctx context.Context, mediaRemoteCacheDays int) gt
return gtserror.NewErrorBadRequest(err, err.Error())
}
go func() {
pruned, err := p.mediaManager.PruneAllRemote(context.Background(), mediaRemoteCacheDays)
if err != nil {
log.Errorf("MediaPrune: error pruning remote cache: %s", err)
} else {
log.Infof("MediaPrune: pruned %d remote cache entries", pruned)
}
}()
go func() {
pruned, err := p.mediaManager.PruneUnusedLocalAttachments(context.Background())
if err != nil {
log.Errorf("MediaPrune: error pruning unused local cache: %s", err)
} else {
log.Infof("MediaPrune: pruned %d unused local cache entries", pruned)
}
}()
go func() {
pruned, err := p.mediaManager.PruneAllMeta(context.Background())
if err != nil {
log.Errorf("MediaPrune: error pruning meta: %s", err)
} else {
log.Infof("MediaPrune: pruned %d meta entries", pruned)
}
}()
if err := p.mediaManager.PruneAll(ctx, mediaRemoteCacheDays, false); err != nil {
err = fmt.Errorf("MediaPrune: %w", err)
return gtserror.NewErrorInternalError(err)
}
return nil
}

View File

@@ -33,14 +33,42 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/uris"
)
// ParseMediaType converts s to a recognized MediaType, or returns an error if unrecognized
func parseMediaType(s string) (media.Type, error) {
switch s {
case string(media.TypeAttachment):
return media.TypeAttachment, nil
case string(media.TypeHeader):
return media.TypeHeader, nil
case string(media.TypeAvatar):
return media.TypeAvatar, nil
case string(media.TypeEmoji):
return media.TypeEmoji, nil
}
return "", fmt.Errorf("%s not a recognized media.Type", s)
}
// ParseMediaSize converts s to a recognized MediaSize, or returns an error if unrecognized
func parseMediaSize(s string) (media.Size, error) {
switch s {
case string(media.SizeSmall):
return media.SizeSmall, nil
case string(media.SizeOriginal):
return media.SizeOriginal, nil
case string(media.SizeStatic):
return media.SizeStatic, nil
}
return "", fmt.Errorf("%s not a recognized media.Size", s)
}
func (p *processor) GetFile(ctx context.Context, requestingAccount *gtsmodel.Account, form *apimodel.GetContentRequestForm) (*apimodel.Content, gtserror.WithCode) {
// parse the form fields
mediaSize, err := media.ParseMediaSize(form.MediaSize)
mediaSize, err := parseMediaSize(form.MediaSize)
if err != nil {
return nil, gtserror.NewErrorNotFound(fmt.Errorf("media size %s not valid", form.MediaSize))
}
mediaType, err := media.ParseMediaType(form.MediaType)
mediaType, err := parseMediaType(form.MediaType)
if err != nil {
return nil, gtserror.NewErrorNotFound(fmt.Errorf("media type %s not valid", form.MediaType))
}