Skip to content

Commit

Permalink
[feature] Allow webp emoji uploads / derefs (#2484)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsmethurst committed Jan 5, 2024
1 parent d5e3996 commit 72d0f46
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 8 deletions.
1 change: 1 addition & 0 deletions internal/media/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var SupportedMIMETypes = []string{
var SupportedEmojiMIMETypes = []string{
mimeImageGif,
mimeImagePng,
mimeImageWebp,
}

type Manager struct {
Expand Down
61 changes: 61 additions & 0 deletions internal/media/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
10 changes: 3 additions & 7 deletions internal/media/processingemoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"bytes"
"context"
"io"
"slices"

"codeberg.org/gruf/go-bytesize"
"codeberg.org/gruf/go-errors/v2"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down
Binary file added internal/media/test/nb-flag-original.webp
Binary file not shown.
Binary file added internal/media/test/nb-flag-static.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion web/source/settings/admin/emoji/local/new-emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ module.exports = function NewEmojiForm() {
<form onSubmit={submitForm} className="form-flex">
<FileInput
field={image}
accept="image/png,image/gif"
accept="image/png,image/gif,image/webp"
/>

<TextInput
Expand Down

0 comments on commit 72d0f46

Please sign in to comment.