lint, fmt

This commit is contained in:
tsmethurst 2022-01-15 17:41:18 +01:00
parent 6bf39d0fc1
commit 723bfe8944
2 changed files with 10 additions and 30 deletions

View File

@ -260,31 +260,28 @@ func (p *ProcessingMedia) fetchRawData(ctx context.Context) error {
return fmt.Errorf("fetchRawData: error parsing content type: %s", err)
}
if !supportedImage(contentType) {
return fmt.Errorf("fetchRawData: media type %s not (yet) supported", contentType)
}
split := strings.Split(contentType, "/")
if len(split) != 2 {
return fmt.Errorf("fetchRawData: content type %s was not valid", contentType)
}
mainType := split[0] // something like 'image'
extension := split[1] // something like 'jpeg'
// set some additional fields on the attachment now that
// we know more about what the underlying media actually is
if extension == mimeGif {
p.attachment.Type = gtsmodel.FileTypeGif
} else {
p.attachment.Type = gtsmodel.FileTypeImage
}
p.attachment.URL = uris.GenerateURIForAttachment(p.attachment.AccountID, string(TypeAttachment), string(SizeOriginal), p.attachment.ID, extension)
p.attachment.File.Path = fmt.Sprintf("%s/%s/%s/%s.%s", p.attachment.AccountID, TypeAttachment, SizeOriginal, p.attachment.ID, extension)
p.attachment.File.ContentType = contentType
switch mainType {
case mimeImage:
if extension == mimeGif {
p.attachment.Type = gtsmodel.FileTypeGif
} else {
p.attachment.Type = gtsmodel.FileTypeImage
}
default:
return fmt.Errorf("fetchRawData: cannot process mime type %s (yet)", mainType)
}
return nil
}

View File

@ -20,12 +20,10 @@ package media
import (
"bytes"
"context"
"errors"
"fmt"
"github.com/h2non/filetype"
"github.com/superseriousbusiness/gotosocial/internal/db"
)
// parseContentType parses the MIME content type from a file, returning it as a string in the form (eg., "image/jpeg").
@ -65,7 +63,7 @@ func supportedImage(mimeType string) bool {
return false
}
// supportedEmoji checks that the content type is image/png -- the only type supported for emoji.
// supportedEmoji checks that the content type is image/png or image/gif -- the only types supported for emoji.
func supportedEmoji(mimeType string) bool {
acceptedEmojiTypes := []string{
mimeImageGif,
@ -106,18 +104,3 @@ func ParseMediaSize(s string) (Size, error) {
}
return "", fmt.Errorf("%s not a recognized MediaSize", s)
}
// putOrUpdate is just a convenience function for first trying to PUT the attachment or emoji in the database,
// and then if that doesn't work because the attachment/emoji already exists, updating it instead.
func putOrUpdate(ctx context.Context, database db.DB, i interface{}) error {
if err := database.Put(ctx, i); err != nil {
if err != db.ErrAlreadyExists {
return fmt.Errorf("putOrUpdate: proper error while putting: %s", err)
}
if err := database.UpdateByPrimaryKey(ctx, i); err != nil {
return fmt.Errorf("putOrUpdate: error while updating: %s", err)
}
}
return nil
}