diff options
Diffstat (limited to 'internal/processing/media')
| -rw-r--r-- | internal/processing/media/create.go | 3 | ||||
| -rw-r--r-- | internal/processing/media/delete.go | 3 | ||||
| -rw-r--r-- | internal/processing/media/getemoji.go | 4 | ||||
| -rw-r--r-- | internal/processing/media/getfile.go | 71 | ||||
| -rw-r--r-- | internal/processing/media/getmedia.go | 2 | ||||
| -rw-r--r-- | internal/processing/media/media.go | 25 | ||||
| -rw-r--r-- | internal/processing/media/unattach.go | 4 | ||||
| -rw-r--r-- | internal/processing/media/update.go | 3 | 
8 files changed, 52 insertions, 63 deletions
diff --git a/internal/processing/media/create.go b/internal/processing/media/create.go index 494434acb..dc26989f8 100644 --- a/internal/processing/media/create.go +++ b/internal/processing/media/create.go @@ -29,7 +29,8 @@ import (  	"github.com/superseriousbusiness/gotosocial/internal/media"  ) -func (p *processor) Create(ctx context.Context, account *gtsmodel.Account, form *apimodel.AttachmentRequest) (*apimodel.Attachment, gtserror.WithCode) { +// Create creates a new media attachment belonging to the given account, using the request form. +func (p *Processor) Create(ctx context.Context, account *gtsmodel.Account, form *apimodel.AttachmentRequest) (*apimodel.Attachment, gtserror.WithCode) {  	data := func(innerCtx context.Context) (io.ReadCloser, int64, error) {  		f, err := form.File.Open()  		return f, form.File.Size, err diff --git a/internal/processing/media/delete.go b/internal/processing/media/delete.go index 31e733bc7..6507fcae4 100644 --- a/internal/processing/media/delete.go +++ b/internal/processing/media/delete.go @@ -11,7 +11,8 @@ import (  	"github.com/superseriousbusiness/gotosocial/internal/gtserror"  ) -func (p *processor) Delete(ctx context.Context, mediaAttachmentID string) gtserror.WithCode { +// Delete deletes the media attachment with the given ID, including all files pertaining to that attachment. +func (p *Processor) Delete(ctx context.Context, mediaAttachmentID string) gtserror.WithCode {  	attachment, err := p.db.GetAttachmentByID(ctx, mediaAttachmentID)  	if err != nil {  		if err == db.ErrNoEntries { diff --git a/internal/processing/media/getemoji.go b/internal/processing/media/getemoji.go index 4b1e76772..4c0ce9930 100644 --- a/internal/processing/media/getemoji.go +++ b/internal/processing/media/getemoji.go @@ -28,7 +28,9 @@ import (  	"github.com/superseriousbusiness/gotosocial/internal/log"  ) -func (p *processor) GetCustomEmojis(ctx context.Context) ([]*apimodel.Emoji, gtserror.WithCode) { +// GetCustomEmojis returns a list of all useable local custom emojis stored on this instance. +// 'useable' in this context means visible and picker, and not disabled. +func (p *Processor) GetCustomEmojis(ctx context.Context) ([]*apimodel.Emoji, gtserror.WithCode) {  	emojis, err := p.db.GetUseableEmojis(ctx)  	if err != nil {  		if err != db.ErrNoEntries { diff --git a/internal/processing/media/getfile.go b/internal/processing/media/getfile.go index 41250b3f5..2a4ef2097 100644 --- a/internal/processing/media/getfile.go +++ b/internal/processing/media/getfile.go @@ -33,42 +33,15 @@ 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) { +// GetFile retrieves a file from storage and streams it back to the caller via an io.reader embedded in *apimodel.Content. +func (p *Processor) GetFile(ctx context.Context, requestingAccount *gtsmodel.Account, form *apimodel.GetContentRequestForm) (*apimodel.Content, gtserror.WithCode) {  	// parse the form fields -	mediaSize, err := parseMediaSize(form.MediaSize) +	mediaSize, err := parseSize(form.MediaSize)  	if err != nil {  		return nil, gtserror.NewErrorNotFound(fmt.Errorf("media size %s not valid", form.MediaSize))  	} -	mediaType, err := parseMediaType(form.MediaType) +	mediaType, err := parseType(form.MediaType)  	if err != nil {  		return nil, gtserror.NewErrorNotFound(fmt.Errorf("media type %s not valid", form.MediaType))  	} @@ -112,7 +85,37 @@ func (p *processor) GetFile(ctx context.Context, requestingAccount *gtsmodel.Acc  	}  } -func (p *processor) getAttachmentContent(ctx context.Context, requestingAccount *gtsmodel.Account, wantedMediaID string, owningAccountID string, mediaSize media.Size) (*apimodel.Content, gtserror.WithCode) { +/* +	UTIL FUNCTIONS +*/ + +func parseType(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) +} + +func parseSize(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) getAttachmentContent(ctx context.Context, requestingAccount *gtsmodel.Account, wantedMediaID string, owningAccountID string, mediaSize media.Size) (*apimodel.Content, gtserror.WithCode) {  	// retrieve attachment from the database and do basic checks on it  	a, err := p.db.GetAttachmentByID(ctx, wantedMediaID)  	if err != nil { @@ -196,7 +199,7 @@ func (p *processor) getAttachmentContent(ctx context.Context, requestingAccount  	return p.retrieveFromStorage(ctx, storagePath, attachmentContent)  } -func (p *processor) getEmojiContent(ctx context.Context, fileName string, owningAccountID string, emojiSize media.Size) (*apimodel.Content, gtserror.WithCode) { +func (p *Processor) getEmojiContent(ctx context.Context, fileName string, owningAccountID string, emojiSize media.Size) (*apimodel.Content, gtserror.WithCode) {  	emojiContent := &apimodel.Content{}  	var storagePath string @@ -231,7 +234,7 @@ func (p *processor) getEmojiContent(ctx context.Context, fileName string, owning  	return p.retrieveFromStorage(ctx, storagePath, emojiContent)  } -func (p *processor) retrieveFromStorage(ctx context.Context, storagePath string, content *apimodel.Content) (*apimodel.Content, gtserror.WithCode) { +func (p *Processor) retrieveFromStorage(ctx context.Context, storagePath string, content *apimodel.Content) (*apimodel.Content, gtserror.WithCode) {  	// If running on S3 storage with proxying disabled then  	// just fetch a pre-signed URL instead of serving the content.  	if url := p.storage.URL(ctx, storagePath); url != nil { diff --git a/internal/processing/media/getmedia.go b/internal/processing/media/getmedia.go index 1ae3770f6..03d5ba770 100644 --- a/internal/processing/media/getmedia.go +++ b/internal/processing/media/getmedia.go @@ -29,7 +29,7 @@ import (  	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"  ) -func (p *processor) GetMedia(ctx context.Context, account *gtsmodel.Account, mediaAttachmentID string) (*apimodel.Attachment, gtserror.WithCode) { +func (p *Processor) Get(ctx context.Context, account *gtsmodel.Account, mediaAttachmentID string) (*apimodel.Attachment, gtserror.WithCode) {  	attachment, err := p.db.GetAttachmentByID(ctx, mediaAttachmentID)  	if err != nil {  		if err == db.ErrNoEntries { diff --git a/internal/processing/media/media.go b/internal/processing/media/media.go index 2456ebcfa..ca95e276f 100644 --- a/internal/processing/media/media.go +++ b/internal/processing/media/media.go @@ -19,35 +19,14 @@  package media  import ( -	"context" - -	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"  	"github.com/superseriousbusiness/gotosocial/internal/db" -	"github.com/superseriousbusiness/gotosocial/internal/gtserror" -	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"  	"github.com/superseriousbusiness/gotosocial/internal/media"  	"github.com/superseriousbusiness/gotosocial/internal/storage"  	"github.com/superseriousbusiness/gotosocial/internal/transport"  	"github.com/superseriousbusiness/gotosocial/internal/typeutils"  ) -// Processor wraps a bunch of functions for processing media actions. -type Processor interface { -	// Create creates a new media attachment belonging to the given account, using the request form. -	Create(ctx context.Context, account *gtsmodel.Account, form *apimodel.AttachmentRequest) (*apimodel.Attachment, gtserror.WithCode) -	// Delete deletes the media attachment with the given ID, including all files pertaining to that attachment. -	Delete(ctx context.Context, mediaAttachmentID string) gtserror.WithCode -	// Unattach unattaches the media attachment with the given ID from any statuses it was attached to, making it available -	// for reattachment again. -	Unattach(ctx context.Context, account *gtsmodel.Account, mediaAttachmentID string) (*apimodel.Attachment, gtserror.WithCode) -	// GetFile retrieves a file from storage and streams it back to the caller via an io.reader embedded in *apimodel.Content. -	GetFile(ctx context.Context, account *gtsmodel.Account, form *apimodel.GetContentRequestForm) (*apimodel.Content, gtserror.WithCode) -	GetCustomEmojis(ctx context.Context) ([]*apimodel.Emoji, gtserror.WithCode) -	GetMedia(ctx context.Context, account *gtsmodel.Account, mediaAttachmentID string) (*apimodel.Attachment, gtserror.WithCode) -	Update(ctx context.Context, account *gtsmodel.Account, mediaAttachmentID string, form *apimodel.AttachmentUpdateRequest) (*apimodel.Attachment, gtserror.WithCode) -} - -type processor struct { +type Processor struct {  	tc                  typeutils.TypeConverter  	mediaManager        media.Manager  	transportController transport.Controller @@ -57,7 +36,7 @@ type processor struct {  // New returns a new media processor.  func New(db db.DB, tc typeutils.TypeConverter, mediaManager media.Manager, transportController transport.Controller, storage *storage.Driver) Processor { -	return &processor{ +	return Processor{  		tc:                  tc,  		mediaManager:        mediaManager,  		transportController: transportController, diff --git a/internal/processing/media/unattach.go b/internal/processing/media/unattach.go index 421b64b2f..816b5134e 100644 --- a/internal/processing/media/unattach.go +++ b/internal/processing/media/unattach.go @@ -30,7 +30,9 @@ import (  	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"  ) -func (p *processor) Unattach(ctx context.Context, account *gtsmodel.Account, mediaAttachmentID string) (*apimodel.Attachment, gtserror.WithCode) { +// Unattach unattaches the media attachment with the given ID from any statuses it was attached to, making it available +// for reattachment again. +func (p *Processor) Unattach(ctx context.Context, account *gtsmodel.Account, mediaAttachmentID string) (*apimodel.Attachment, gtserror.WithCode) {  	attachment, err := p.db.GetAttachmentByID(ctx, mediaAttachmentID)  	if err != nil {  		if err == db.ErrNoEntries { diff --git a/internal/processing/media/update.go b/internal/processing/media/update.go index 17b86aa11..c03df705b 100644 --- a/internal/processing/media/update.go +++ b/internal/processing/media/update.go @@ -30,7 +30,8 @@ import (  	"github.com/superseriousbusiness/gotosocial/internal/text"  ) -func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, mediaAttachmentID string, form *apimodel.AttachmentUpdateRequest) (*apimodel.Attachment, gtserror.WithCode) { +// Update updates a media attachment with the given id, using the provided form parameters. +func (p *Processor) Update(ctx context.Context, account *gtsmodel.Account, mediaAttachmentID string, form *apimodel.AttachmentUpdateRequest) (*apimodel.Attachment, gtserror.WithCode) {  	attachment, err := p.db.GetAttachmentByID(ctx, mediaAttachmentID)  	if err != nil {  		if err == db.ErrNoEntries {  | 
