[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 <tobi.smethurst@protonmail.com>
Co-committed-by: tobi <tobi.smethurst@protonmail.com>
This commit is contained in:
tobi 2025-07-17 13:20:01 +02:00 committed by tobi
commit a4b54aa935
8 changed files with 75 additions and 5 deletions

View file

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

View file

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

View file

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

View file

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

View file

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