diff --git a/internal/media/manager.go b/internal/media/manager.go index 5f496a71d..61f4e73dd 100644 --- a/internal/media/manager.go +++ b/internal/media/manager.go @@ -45,6 +45,7 @@ var SupportedEmojiMIMETypes = []string{ mimeImageGif, mimeImagePng, + mimeImageWebp, } type Manager struct { diff --git a/internal/media/manager_test.go b/internal/media/manager_test.go index f90c182fe..bb8afdfd2 100644 --- a/internal/media/manager_test.go +++ b/internal/media/manager_test.go @@ -303,6 +303,67 @@ func (suite *ManagerTestSuite) TestEmojiProcessBlockingNoFileSizeGiven() { suite.Equal(processedStaticBytesExpected, processedStaticBytes) } +func (suite *ManagerTestSuite) TestEmojiWebpProcess() { + ctx := context.Background() + + data := func(_ context.Context) (io.ReadCloser, int64, error) { + // load bytes from a test image + b, err := os.ReadFile("./test/nb-flag-original.webp") + if err != nil { + panic(err) + } + return io.NopCloser(bytes.NewBuffer(b)), int64(len(b)), nil + } + + emojiID := "01GDQ9G782X42BAMFASKP64343" + emojiURI := "http://localhost:8080/emoji/01GDQ9G782X42BAMFASKP64343" + + processingEmoji, err := suite.manager.ProcessEmoji(ctx, data, "nb-flag", emojiID, emojiURI, nil, false) + suite.NoError(err) + + // do a blocking call to fetch the emoji + emoji, err := processingEmoji.LoadEmoji(ctx) + suite.NoError(err) + suite.NotNil(emoji) + + // make sure it's got the stuff set on it that we expect + suite.Equal(emojiID, emoji.ID) + + // file meta should be correctly derived from the image + suite.Equal("image/webp", emoji.ImageContentType) + suite.Equal("image/png", emoji.ImageStaticContentType) + suite.Equal(294, emoji.ImageFileSize) + + // now make sure the emoji is in the database + dbEmoji, err := suite.db.GetEmojiByID(ctx, emojiID) + suite.NoError(err) + suite.NotNil(dbEmoji) + + // make sure the processed emoji file is in storage + processedFullBytes, err := suite.storage.Get(ctx, emoji.ImagePath) + suite.NoError(err) + suite.NotEmpty(processedFullBytes) + + // load the processed bytes from our test folder, to compare + processedFullBytesExpected, err := os.ReadFile("./test/nb-flag-original.webp") + suite.NoError(err) + suite.NotEmpty(processedFullBytesExpected) + + // the bytes in storage should be what we expected + suite.Equal(processedFullBytesExpected, processedFullBytes) + + // now do the same for the thumbnail and make sure it's what we expected + processedStaticBytes, err := suite.storage.Get(ctx, emoji.ImageStaticPath) + suite.NoError(err) + suite.NotEmpty(processedStaticBytes) + + processedStaticBytesExpected, err := os.ReadFile("./test/nb-flag-static.png") + suite.NoError(err) + suite.NotEmpty(processedStaticBytesExpected) + + suite.Equal(processedStaticBytesExpected, processedStaticBytes) +} + func (suite *ManagerTestSuite) TestSimpleJpegProcessBlocking() { ctx := context.Background() diff --git a/internal/media/processingemoji.go b/internal/media/processingemoji.go index 56113b27b..9287f14ce 100644 --- a/internal/media/processingemoji.go +++ b/internal/media/processingemoji.go @@ -21,6 +21,7 @@ "bytes" "context" "io" + "slices" "codeberg.org/gruf/go-bytesize" "codeberg.org/gruf/go-errors/v2" @@ -57,7 +58,6 @@ func (p *ProcessingEmoji) EmojiID() string { func (p *ProcessingEmoji) LoadEmoji(ctx context.Context) (*gtsmodel.Emoji, error) { // Attempt to load synchronously. emoji, done, err := p.load(ctx) - if err == nil { // No issue, return media. return emoji, nil @@ -209,12 +209,8 @@ func (p *ProcessingEmoji) store(ctx context.Context) error { return gtserror.Newf("error parsing file type: %w", err) } - switch info.Extension { - // only supported emoji types - case "gif", "png": - - // unhandled - default: + // Ensure supported emoji img type. + if !slices.Contains(SupportedEmojiMIMETypes, info.MIME.Value) { return gtserror.Newf("unsupported emoji filetype: %s", info.Extension) } diff --git a/internal/media/test/nb-flag-original.webp b/internal/media/test/nb-flag-original.webp new file mode 100644 index 000000000..a03d49ce3 Binary files /dev/null and b/internal/media/test/nb-flag-original.webp differ diff --git a/internal/media/test/nb-flag-static.png b/internal/media/test/nb-flag-static.png new file mode 100644 index 000000000..399eae5e5 Binary files /dev/null and b/internal/media/test/nb-flag-static.png differ diff --git a/web/source/settings/admin/emoji/local/new-emoji.js b/web/source/settings/admin/emoji/local/new-emoji.js index ecb0465cb..6c0d0f2f4 100644 --- a/web/source/settings/admin/emoji/local/new-emoji.js +++ b/web/source/settings/admin/emoji/local/new-emoji.js @@ -93,7 +93,7 @@ module.exports = function NewEmojiForm() {