[feature] Allow webp emoji uploads / derefs (#2484)

This commit is contained in:
tobi 2024-01-05 13:39:53 +01:00 committed by GitHub
parent d5e3996a18
commit 72d0f46b0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 66 additions and 8 deletions

View File

@ -45,6 +45,7 @@ var SupportedMIMETypes = []string{
var SupportedEmojiMIMETypes = []string{
mimeImageGif,
mimeImagePng,
mimeImageWebp,
}
type Manager struct {

View File

@ -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()

View File

@ -21,6 +21,7 @@ import (
"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)
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 294 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 878 B

View File

@ -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