From a4b54aa935f60d392cdaec7c4d315ff274ce73b5 Mon Sep 17 00:00:00 2001 From: tobi Date: Thu, 17 Jul 2025 13:20:01 +0200 Subject: [feature] Add `avif` file support (#4331) # Description > If this is a code change, please include a summary of what you've coded, and link to the issue(s) it closes/implements. > > If this is a documentation change, please briefly describe what you've changed and why. This pull request implements support for reading avif images properly. closes https://codeberg.org/superseriousbusiness/gotosocial/issues/4330 ## Checklist Please put an x inside each checkbox to indicate that you've read and followed it: `[ ]` -> `[x]` If this is a documentation change, only the first checkbox must be filled (you can delete the others if you want). - [x] I/we have read the [GoToSocial contribution guidelines](https://codeberg.org/superseriousbusiness/gotosocial/src/branch/main/CONTRIBUTING.md). - [x] I/we have discussed the proposed changes already, either in an issue on the repository, or in the Matrix chat. - [x] I/we have not leveraged AI to create the proposed changes. - [x] I/we have performed a self-review of added code. - [x] I/we have written code that is legible and maintainable by others. - [x] I/we have commented the added code, particularly in hard-to-understand areas. - [x] I/we have made any necessary changes to documentation. - [x] I/we have added tests that cover new code. - [x] I/we have run tests and they pass locally with the changes. - [x] I/we have run `go fmt ./...` and `golangci-lint run`. Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4331 Co-authored-by: tobi Co-committed-by: tobi --- internal/api/client/instance/instancepatch_test.go | 6 +++ internal/media/ffmpeg.go | 15 ++++-- internal/media/manager.go | 1 + internal/media/manager_test.go | 56 +++++++++++++++++++++ internal/media/test/gotosocial-processed.avif | Bin 0 -> 3815 bytes internal/media/test/gotosocial-thumbnail.webp | Bin 0 -> 4198 bytes internal/media/test/gotosocial.avif | Bin 0 -> 3815 bytes internal/typeutils/internaltofrontend_test.go | 2 + 8 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 internal/media/test/gotosocial-processed.avif create mode 100644 internal/media/test/gotosocial-thumbnail.webp create mode 100644 internal/media/test/gotosocial.avif (limited to 'internal') diff --git a/internal/api/client/instance/instancepatch_test.go b/internal/api/client/instance/instancepatch_test.go index dc3eb84b1..f23a44dc8 100644 --- a/internal/api/client/instance/instancepatch_test.go +++ b/internal/api/client/instance/instancepatch_test.go @@ -112,6 +112,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch1() { "image/jpeg", "image/gif", "image/webp", + "image/avif", "audio/mp2", "audio/mp3", "audio/mpeg", @@ -255,6 +256,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch2() { "image/jpeg", "image/gif", "image/webp", + "image/avif", "audio/mp2", "audio/mp3", "audio/mpeg", @@ -398,6 +400,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch3() { "image/jpeg", "image/gif", "image/webp", + "image/avif", "audio/mp2", "audio/mp3", "audio/mpeg", @@ -592,6 +595,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch6() { "image/jpeg", "image/gif", "image/webp", + "image/avif", "audio/mp2", "audio/mp3", "audio/mpeg", @@ -757,6 +761,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch8() { "image/jpeg", "image/gif", "image/webp", + "image/avif", "audio/mp2", "audio/mp3", "audio/mpeg", @@ -941,6 +946,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch9() { "image/jpeg", "image/gif", "image/webp", + "image/avif", "audio/mp2", "audio/mp3", "audio/mpeg", diff --git a/internal/media/ffmpeg.go b/internal/media/ffmpeg.go index 676eba9be..05e8c69a6 100644 --- a/internal/media/ffmpeg.go +++ b/internal/media/ffmpeg.go @@ -342,16 +342,21 @@ func (res *result) GetFileType() (gtsmodel.FileType, string, string) { case "mov,mp4,m4a,3gp,3g2,mj2": switch { case len(res.video) > 0: - if len(res.audio) == 0 && - res.duration <= 30 { + switch { + case res.video[0].framerate == 0 && res.video[0].stream.codec == "av1": + // Looks like an avif image. + return gtsmodel.FileTypeImage, + "image/avif", "avif" + case len(res.audio) == 0 && res.duration <= 30: // Short, soundless // video file aka gifv. return gtsmodel.FileTypeGifv, "video/mp4", "mp4" + default: + // Video file (with or without audio). + return gtsmodel.FileTypeVideo, + "video/mp4", "mp4" } - // Video file (with or without audio). - return gtsmodel.FileTypeVideo, - "video/mp4", "mp4" case len(res.audio) > 0 && res.audio[0].codec == "aac": // m4a only supports [aac] audio. diff --git a/internal/media/manager.go b/internal/media/manager.go index a7bd8948b..2ec222f3c 100644 --- a/internal/media/manager.go +++ b/internal/media/manager.go @@ -38,6 +38,7 @@ var SupportedMIMETypes = []string{ "image/jpeg", // .jpeg "image/gif", // .gif "image/webp", // .webp + "image/avif", // .avif "audio/mp2", // .mp2 "audio/mp3", // .mp3 diff --git a/internal/media/manager_test.go b/internal/media/manager_test.go index 6182babbb..f6914acf0 100644 --- a/internal/media/manager_test.go +++ b/internal/media/manager_test.go @@ -789,6 +789,62 @@ func (suite *ManagerTestSuite) TestPngAlphaChannelProcess() { equalFiles(suite.T(), suite.state.Storage, dbAttachment.Thumbnail.Path, "./test/test-png-alphachannel-thumbnail.jpeg") } +func (suite *ManagerTestSuite) TestAvifProcess() { + ctx := suite.T().Context() + + data := func(_ context.Context) (io.ReadCloser, error) { + // load bytes from a test image + b, err := os.ReadFile("./test/gotosocial.avif") + if err != nil { + panic(err) + } + return io.NopCloser(bytes.NewBuffer(b)), nil + } + + accountID := "01FS1X72SK9ZPW0J1QQ68BD264" + + // process the media with no additional info provided + processing, err := suite.manager.CreateMedia(ctx, + accountID, + data, + media.AdditionalMediaInfo{}, + ) + suite.NoError(err) + suite.NotNil(processing) + + // do a blocking call to fetch the attachment + attachment, err := processing.Load(ctx) + suite.NoError(err) + suite.NotNil(attachment) + + // make sure it's got the stuff set on it that we expect + // the attachment ID and accountID we expect + suite.Equal(processing.ID(), attachment.ID) + suite.Equal(accountID, attachment.AccountID) + + // file meta should be correctly derived from the image + suite.EqualValues(gtsmodel.Original{ + Width: 1038, Height: 980, Size: 1017240, Aspect: 1.0591837, + }, attachment.FileMeta.Original) + suite.EqualValues(gtsmodel.Small{ + Width: 512, Height: 483, Size: 247296, Aspect: 1.0591837, + }, attachment.FileMeta.Small) + suite.Equal("image/avif", attachment.File.ContentType) + suite.Equal("image/webp", attachment.Thumbnail.ContentType) + suite.Equal(3815, attachment.File.FileSize) + suite.Equal(4198, attachment.Thumbnail.FileSize) + suite.Equal("LNQJQ7%M?w-;-pj[bbj[?^ofDiWB", attachment.Blurhash) + + // now make sure the attachment is in the database + dbAttachment, err := suite.db.GetAttachmentByID(ctx, attachment.ID) + suite.NoError(err) + suite.NotNil(dbAttachment) + + // ensure the files contain the expected data. + equalFiles(suite.T(), suite.state.Storage, dbAttachment.File.Path, "./test/gotosocial-processed.avif") + equalFiles(suite.T(), suite.state.Storage, dbAttachment.Thumbnail.Path, "./test/gotosocial-thumbnail.webp") +} + func (suite *ManagerTestSuite) TestSimpleJpegProcessWithCallback() { ctx := suite.T().Context() diff --git a/internal/media/test/gotosocial-processed.avif b/internal/media/test/gotosocial-processed.avif new file mode 100644 index 000000000..f0433b70b Binary files /dev/null and b/internal/media/test/gotosocial-processed.avif differ diff --git a/internal/media/test/gotosocial-thumbnail.webp b/internal/media/test/gotosocial-thumbnail.webp new file mode 100644 index 000000000..370ca9d0b Binary files /dev/null and b/internal/media/test/gotosocial-thumbnail.webp differ diff --git a/internal/media/test/gotosocial.avif b/internal/media/test/gotosocial.avif new file mode 100644 index 000000000..f0433b70b Binary files /dev/null and b/internal/media/test/gotosocial.avif differ diff --git a/internal/typeutils/internaltofrontend_test.go b/internal/typeutils/internaltofrontend_test.go index a26ff114e..47ad284e6 100644 --- a/internal/typeutils/internaltofrontend_test.go +++ b/internal/typeutils/internaltofrontend_test.go @@ -1773,6 +1773,7 @@ func (suite *InternalToFrontendTestSuite) TestInstanceV1ToFrontend() { "image/jpeg", "image/gif", "image/webp", + "image/avif", "audio/mp2", "audio/mp3", "audio/mpeg", @@ -1930,6 +1931,7 @@ func (suite *InternalToFrontendTestSuite) TestInstanceV2ToFrontend() { "image/jpeg", "image/gif", "image/webp", + "image/avif", "audio/mp2", "audio/mp3", "audio/mpeg", -- cgit v1.2.3