diff options
author | 2021-12-20 15:19:53 +0100 | |
---|---|---|
committer | 2021-12-20 15:19:53 +0100 | |
commit | cb8688f4298a1a3ed5e28565004588be3c071df0 (patch) | |
tree | 038b196e914b949857bf8b7c00f22374408bc1ca /internal/media | |
parent | return first offer when no accept header set (#351) (diff) | |
download | gotosocial-cb8688f4298a1a3ed5e28565004588be3c071df0.tar.xz |
Remove unnecessary storage config variables (#344)
* rewire config to not use extraneous serve vars
* rename 'file' to 'local' for consistency
* use Type and Size again
Diffstat (limited to 'internal/media')
-rw-r--r-- | internal/media/handler.go | 68 | ||||
-rw-r--r-- | internal/media/processicon.go | 22 | ||||
-rw-r--r-- | internal/media/processimage.go | 17 | ||||
-rw-r--r-- | internal/media/util.go | 32 |
4 files changed, 53 insertions, 86 deletions
diff --git a/internal/media/handler.go b/internal/media/handler.go index 24963e404..98c65ff36 100644 --- a/internal/media/handler.go +++ b/internal/media/handler.go @@ -28,39 +28,31 @@ import ( "codeberg.org/gruf/go-store/kv" "github.com/sirupsen/logrus" - "github.com/spf13/viper" - "github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/id" "github.com/superseriousbusiness/gotosocial/internal/transport" + "github.com/superseriousbusiness/gotosocial/internal/uris" ) -// Size describes the *size* of a piece of media +// EmojiMaxBytes is the maximum permitted bytes of an emoji upload (50kb) +const EmojiMaxBytes = 51200 + type Size string -// Type describes the *type* of a piece of media +const ( + SizeSmall Size = "small" // SizeSmall is the key for small/thumbnail versions of media + SizeOriginal Size = "original" // SizeOriginal is the key for original/fullsize versions of media and emoji + SizeStatic Size = "static" // SizeStatic is the key for static (non-animated) versions of emoji +) + type Type string const ( - // Small is the key for small/thumbnail versions of media - Small Size = "small" - // Original is the key for original/fullsize versions of media and emoji - Original Size = "original" - // Static is the key for static (non-animated) versions of emoji - Static Size = "static" - - // Attachment is the key for media attachments - Attachment Type = "attachment" - // Header is the key for profile header requests - Header Type = "header" - // Avatar is the key for profile avatar requests - Avatar Type = "avatar" - // Emoji is the key for emoji type requests - Emoji Type = "emoji" - - // EmojiMaxBytes is the maximum permitted bytes of an emoji upload (50kb) - EmojiMaxBytes = 51200 + TypeAttachment Type = "attachment" // TypeAttachment is the key for media attachments + TypeHeader Type = "header" // TypeHeader is the key for profile header requests + TypeAvatar Type = "avatar" // TypeAvatar is the key for profile avatar requests + TypeEmoji Type = "emoji" // TypeEmoji is the key for emoji type requests ) // Handler provides an interface for parsing, storing, and retrieving media objects like photos, videos, and gifs. @@ -107,7 +99,7 @@ func New(database db.DB, storage *kv.KVStore) Handler { func (mh *mediaHandler) ProcessHeaderOrAvatar(ctx context.Context, attachment []byte, accountID string, mediaType Type, remoteURL string) (*gtsmodel.MediaAttachment, error) { l := logrus.WithField("func", "SetHeaderForAccountID") - if mediaType != Header && mediaType != Avatar { + if mediaType != TypeHeader && mediaType != TypeAvatar { return nil, errors.New("header or avatar not selected") } @@ -178,8 +170,6 @@ func (mh *mediaHandler) ProcessAttachment(ctx context.Context, attachmentBytes [ // *gts.Emoji for it, then returns it to the caller. It's the caller's responsibility to put the returned struct // in the database. func (mh *mediaHandler) ProcessLocalEmoji(ctx context.Context, emojiBytes []byte, shortcode string) (*gtsmodel.Emoji, error) { - keys := config.Keys - var clean []byte var err error var original *imageAndMeta @@ -234,31 +224,23 @@ func (mh *mediaHandler) ProcessLocalEmoji(ctx context.Context, emojiBytes []byte // the file extension (either png or gif) extension := strings.Split(contentType, "/")[1] - // create the urls and storage paths - serveProtocol := viper.GetString(keys.StorageServeProtocol) - serveHost := viper.GetString(keys.StorageServeHost) - serveBasePath := viper.GetString(keys.StorageServeBasePath) - URLbase := fmt.Sprintf("%s://%s%s", serveProtocol, serveHost, serveBasePath) - - // generate a id for the new emoji + // generate a ulid for the new emoji newEmojiID, err := id.NewRandomULID() if err != nil { return nil, err } - // webfinger uri for the emoji -- unrelated to actually serving the image - // will be something like https://example.org/emoji/70a7f3d7-7e35-4098-8ce3-9b5e8203bb9c - protocol := viper.GetString(keys.Protocol) - host := viper.GetString(keys.Host) - emojiURI := fmt.Sprintf("%s://%s/%s/%s", protocol, host, Emoji, newEmojiID) + // activitypub uri for the emoji -- unrelated to actually serving the image + // will be something like https://example.org/emoji/01FPSVBK3H8N7V8XK6KGSQ86EC + emojiURI := uris.GenerateURIForEmoji(newEmojiID) // serve url and storage path for the original emoji -- can be png or gif - emojiURL := fmt.Sprintf("%s/%s/%s/%s/%s.%s", URLbase, instanceAccount.ID, Emoji, Original, newEmojiID, extension) - emojiPath := fmt.Sprintf("%s/%s/%s/%s.%s", instanceAccount.ID, Emoji, Original, newEmojiID, extension) + emojiURL := uris.GenerateURIForAttachment(instanceAccount.ID, string(TypeEmoji), string(SizeOriginal), newEmojiID, extension) + emojiPath := fmt.Sprintf("%s/%s/%s/%s.%s", instanceAccount.ID, TypeEmoji, SizeOriginal, newEmojiID, extension) // serve url and storage path for the static version -- will always be png - emojiStaticURL := fmt.Sprintf("%s/%s/%s/%s/%s.png", URLbase, instanceAccount.ID, Emoji, Static, newEmojiID) - emojiStaticPath := fmt.Sprintf("%s/%s/%s/%s.png", instanceAccount.ID, Emoji, Static, newEmojiID) + emojiStaticURL := uris.GenerateURIForAttachment(instanceAccount.ID, string(TypeEmoji), string(SizeStatic), newEmojiID, "png") + emojiStaticPath := fmt.Sprintf("%s/%s/%s/%s.png", instanceAccount.ID, TypeEmoji, SizeStatic, newEmojiID) // Store the original emoji if err := mh.storage.Put(emojiPath, original.image); err != nil { @@ -307,9 +289,9 @@ func (mh *mediaHandler) ProcessRemoteHeaderOrAvatar(ctx context.Context, t trans var headerOrAvi Type if currentAttachment.Header { - headerOrAvi = Header + headerOrAvi = TypeHeader } else if currentAttachment.Avatar { - headerOrAvi = Avatar + headerOrAvi = TypeAvatar } if currentAttachment.RemoteURL == "" { diff --git a/internal/media/processicon.go b/internal/media/processicon.go index 5f4f8b138..c377033fc 100644 --- a/internal/media/processicon.go +++ b/internal/media/processicon.go @@ -24,10 +24,9 @@ import ( "strings" "time" - "github.com/spf13/viper" - "github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/id" + "github.com/superseriousbusiness/gotosocial/internal/uris" ) func (mh *mediaHandler) processHeaderOrAvi(imageBytes []byte, contentType string, mediaType Type, accountID string, remoteURL string) (*gtsmodel.MediaAttachment, error) { @@ -35,9 +34,9 @@ func (mh *mediaHandler) processHeaderOrAvi(imageBytes []byte, contentType string var isAvatar bool switch mediaType { - case Header: + case TypeHeader: isHeader = true - case Avatar: + case TypeAvatar: isAvatar = true default: return nil, errors.New("header or avatar not selected") @@ -81,23 +80,16 @@ func (mh *mediaHandler) processHeaderOrAvi(imageBytes []byte, contentType string return nil, err } - keys := config.Keys - serveProtocol := viper.GetString(keys.StorageServeProtocol) - serveHost := viper.GetString(keys.StorageServeHost) - serveBasePath := viper.GetString(keys.StorageServeBasePath) - - URLbase := fmt.Sprintf("%s://%s%s", serveProtocol, serveHost, serveBasePath) - originalURL := fmt.Sprintf("%s/%s/%s/original/%s.%s", URLbase, accountID, mediaType, newMediaID, extension) - smallURL := fmt.Sprintf("%s/%s/%s/small/%s.%s", URLbase, accountID, mediaType, newMediaID, extension) - + originalURL := uris.GenerateURIForAttachment(accountID, string(mediaType), string(SizeOriginal), newMediaID, extension) + smallURL := uris.GenerateURIForAttachment(accountID, string(mediaType), string(SizeSmall), newMediaID, extension) // we store the original... - originalPath := fmt.Sprintf("%s/%s/%s/%s.%s", accountID, mediaType, Original, newMediaID, extension) + originalPath := fmt.Sprintf("%s/%s/%s/%s.%s", accountID, mediaType, SizeOriginal, newMediaID, extension) if err := mh.storage.Put(originalPath, original.image); err != nil { return nil, fmt.Errorf("storage error: %s", err) } // and a thumbnail... - smallPath := fmt.Sprintf("%s/%s/%s/%s.%s", accountID, mediaType, Small, newMediaID, extension) + smallPath := fmt.Sprintf("%s/%s/%s/%s.%s", accountID, mediaType, SizeSmall, newMediaID, extension) if err := mh.storage.Put(smallPath, small.image); err != nil { return nil, fmt.Errorf("storage error: %s", err) } diff --git a/internal/media/processimage.go b/internal/media/processimage.go index f3b520ad9..435ce5714 100644 --- a/internal/media/processimage.go +++ b/internal/media/processimage.go @@ -24,10 +24,9 @@ import ( "strings" "time" - "github.com/spf13/viper" - "github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/id" + "github.com/superseriousbusiness/gotosocial/internal/uris" ) func (mh *mediaHandler) processImageAttachment(data []byte, minAttachment *gtsmodel.MediaAttachment) (*gtsmodel.MediaAttachment, error) { @@ -69,23 +68,17 @@ func (mh *mediaHandler) processImageAttachment(data []byte, minAttachment *gtsmo return nil, err } - keys := config.Keys - serveProtocol := viper.GetString(keys.StorageServeProtocol) - serveHost := viper.GetString(keys.StorageServeHost) - serveBasePath := viper.GetString(keys.StorageServeBasePath) - - URLbase := fmt.Sprintf("%s://%s%s", serveProtocol, serveHost, serveBasePath) - originalURL := fmt.Sprintf("%s/%s/attachment/original/%s.%s", URLbase, minAttachment.AccountID, newMediaID, extension) - smallURL := fmt.Sprintf("%s/%s/attachment/small/%s.jpeg", URLbase, minAttachment.AccountID, newMediaID) // all thumbnails/smalls are encoded as jpeg + originalURL := uris.GenerateURIForAttachment(minAttachment.AccountID, string(TypeAttachment), string(SizeOriginal), newMediaID, extension) + smallURL := uris.GenerateURIForAttachment(minAttachment.AccountID, string(TypeAttachment), string(SizeSmall), newMediaID, "jpeg") // all thumbnails/smalls are encoded as jpeg // we store the original... - originalPath := fmt.Sprintf("%s/%s/%s/%s.%s", minAttachment.AccountID, Attachment, Original, newMediaID, extension) + originalPath := fmt.Sprintf("%s/%s/%s/%s.%s", minAttachment.AccountID, TypeAttachment, SizeOriginal, newMediaID, extension) if err := mh.storage.Put(originalPath, original.image); err != nil { return nil, fmt.Errorf("storage error: %s", err) } // and a thumbnail... - smallPath := fmt.Sprintf("%s/%s/%s/%s.jpeg", minAttachment.AccountID, Attachment, Small, newMediaID) // all thumbnails/smalls are encoded as jpeg + smallPath := fmt.Sprintf("%s/%s/%s/%s.jpeg", minAttachment.AccountID, TypeAttachment, SizeSmall, newMediaID) // all thumbnails/smalls are encoded as jpeg if err := mh.storage.Put(smallPath, small.image); err != nil { return nil, fmt.Errorf("storage error: %s", err) } diff --git a/internal/media/util.go b/internal/media/util.go index 963e3c93f..147178d08 100644 --- a/internal/media/util.go +++ b/internal/media/util.go @@ -295,28 +295,28 @@ type imageAndMeta struct { // ParseMediaType converts s to a recognized MediaType, or returns an error if unrecognized func ParseMediaType(s string) (Type, error) { - switch Type(s) { - case Attachment: - return Attachment, nil - case Header: - return Header, nil - case Avatar: - return Avatar, nil - case Emoji: - return Emoji, nil + switch s { + case string(TypeAttachment): + return TypeAttachment, nil + case string(TypeHeader): + return TypeHeader, nil + case string(TypeAvatar): + return TypeAvatar, nil + case string(TypeEmoji): + return TypeEmoji, nil } return "", fmt.Errorf("%s not a recognized MediaType", s) } // ParseMediaSize converts s to a recognized MediaSize, or returns an error if unrecognized func ParseMediaSize(s string) (Size, error) { - switch Size(s) { - case Small: - return Small, nil - case Original: - return Original, nil - case Static: - return Static, nil + switch s { + case string(SizeSmall): + return SizeSmall, nil + case string(SizeOriginal): + return SizeOriginal, nil + case string(SizeStatic): + return SizeStatic, nil } return "", fmt.Errorf("%s not a recognized MediaSize", s) } |