gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit ddfd83d0fbdf4d6259bda25a57776f8341888b49
parent 7e6350b44807d55e678e7e88b74cff258ef98425
Author: tobi <31960611+tsmethurst@users.noreply.github.com>
Date:   Thu, 23 Sep 2021 11:13:11 +0200

update media processing (#244)


Diffstat:
Minternal/api/client/media/mediacreate_test.go | 8++++----
Minternal/federation/dereferencing/attachment_test.go | 4++--
Minternal/media/processicon.go | 2+-
Minternal/media/processimage.go | 4++--
Minternal/media/test/test-jpeg-processed.jpg | 0
Minternal/media/test/test-jpeg-thumbnail.jpg | 0
Minternal/media/util.go | 63+++++++++++++++++++++++----------------------------------------
Minternal/media/util_test.go | 63+++++++++++++++++++++++++++++++--------------------------------
8 files changed, 63 insertions(+), 81 deletions(-)

diff --git a/internal/api/client/media/mediacreate_test.go b/internal/api/client/media/mediacreate_test.go @@ -187,9 +187,9 @@ func (suite *MediaCreateTestSuite) TestStatusCreatePOSTImageHandlerSuccessful() Aspect: 1.7777778, }, Small: model.MediaDimensions{ - Width: 256, - Height: 144, - Size: "256x144", + Width: 512, + Height: 288, + Size: "512x288", Aspect: 1.7777778, }, Focus: model.MediaFocus{ @@ -197,7 +197,7 @@ func (suite *MediaCreateTestSuite) TestStatusCreatePOSTImageHandlerSuccessful() Y: 0.5, }, }, attachmentReply.Meta) - assert.Equal(suite.T(), "LjCZnlvyRkRn_NvzRjWF?urqV@f9", attachmentReply.Blurhash) + assert.Equal(suite.T(), "LjBzUo#6RQR._NvzRjWF?urqV@a$", attachmentReply.Blurhash) assert.NotEmpty(suite.T(), attachmentReply.ID) assert.NotEmpty(suite.T(), attachmentReply.URL) assert.NotEmpty(suite.T(), attachmentReply.PreviewURL) diff --git a/internal/federation/dereferencing/attachment_test.go b/internal/federation/dereferencing/attachment_test.go @@ -65,7 +65,7 @@ func (suite *AttachmentTestSuite) TestDereferenceAttachmentOK() { suite.Equal(2071680, attachment.FileMeta.Original.Size) suite.Equal(1245, attachment.FileMeta.Original.Height) suite.Equal(1664, attachment.FileMeta.Original.Width) - suite.Equal("LwQ9yKRP_4t8t7RjWBt7%hozM_ay", attachment.Blurhash) + suite.Equal("LwP?p=aK_4%N%MRjWXt7%hozM_a}", attachment.Blurhash) suite.Equal(gtsmodel.ProcessingStatusProcessed, attachment.Processing) suite.NotEmpty(attachment.File.Path) suite.Equal(attachmentContentType, attachment.File.ContentType) @@ -91,7 +91,7 @@ func (suite *AttachmentTestSuite) TestDereferenceAttachmentOK() { suite.Equal(2071680, dbAttachment.FileMeta.Original.Size) suite.Equal(1245, dbAttachment.FileMeta.Original.Height) suite.Equal(1664, dbAttachment.FileMeta.Original.Width) - suite.Equal("LwQ9yKRP_4t8t7RjWBt7%hozM_ay", dbAttachment.Blurhash) + suite.Equal("LwP?p=aK_4%N%MRjWXt7%hozM_a}", dbAttachment.Blurhash) suite.Equal(gtsmodel.ProcessingStatusProcessed, dbAttachment.Processing) suite.NotEmpty(dbAttachment.File.Path) suite.Equal(attachmentContentType, dbAttachment.File.ContentType) diff --git a/internal/media/processicon.go b/internal/media/processicon.go @@ -120,7 +120,7 @@ func (mh *mediaHandler) processHeaderOrAvi(imageBytes []byte, contentType string AccountID: accountID, Description: "", ScheduledStatusID: "", - Blurhash: original.blurhash, + Blurhash: small.blurhash, Processing: 2, File: gtsmodel.File{ Path: originalPath, diff --git a/internal/media/processimage.go b/internal/media/processimage.go @@ -55,7 +55,7 @@ func (mh *mediaHandler) processImageAttachment(data []byte, minAttachment *gtsmo return nil, errors.New("media type unrecognized") } - small, err = deriveThumbnail(clean, contentType, 256, 256) + small, err = deriveThumbnail(clean, contentType, 512, 512) if err != nil { return nil, fmt.Errorf("error deriving thumbnail: %s", err) } @@ -109,7 +109,7 @@ func (mh *mediaHandler) processImageAttachment(data []byte, minAttachment *gtsmo AccountID: minAttachment.AccountID, Description: minAttachment.Description, ScheduledStatusID: minAttachment.ScheduledStatusID, - Blurhash: original.blurhash, + Blurhash: small.blurhash, Processing: 2, File: gtsmodel.File{ Path: originalPath, diff --git a/internal/media/test/test-jpeg-processed.jpg b/internal/media/test/test-jpeg-processed.jpg Binary files differ. diff --git a/internal/media/test/test-jpeg-thumbnail.jpg b/internal/media/test/test-jpeg-thumbnail.jpg Binary files differ. diff --git a/internal/media/util.go b/internal/media/util.go @@ -156,23 +156,12 @@ func deriveGif(b []byte, extension string) (*imageAndMeta, error) { size := width * height aspect := float64(width) / float64(height) - bh, err := blurhash.Encode(4, 3, g.Image[0]) - if err != nil || bh == "" { - return nil, err - } - - out := &bytes.Buffer{} - if err := gif.EncodeAll(out, g); err != nil { - return nil, err - } - return &imageAndMeta{ - image: out.Bytes(), - width: width, - height: height, - size: size, - aspect: aspect, - blurhash: bh, + image: b, + width: width, + height: height, + size: size, + aspect: aspect, }, nil } @@ -200,25 +189,12 @@ func deriveImage(b []byte, contentType string) (*imageAndMeta, error) { size := width * height aspect := float64(width) / float64(height) - bh, err := blurhash.Encode(4, 3, i) - if err != nil { - return nil, err - } - - out := &bytes.Buffer{} - if err := jpeg.Encode(out, i, &jpeg.Options{ - Quality: 100, - }); err != nil { - return nil, err - } - return &imageAndMeta{ - image: out.Bytes(), - width: width, - height: height, - size: size, - aspect: aspect, - blurhash: bh, + image: b, + width: width, + height: height, + size: size, + aspect: aspect, }, nil } @@ -257,18 +233,25 @@ func deriveThumbnail(b []byte, contentType string, x uint, y uint) (*imageAndMet size := width * height aspect := float64(width) / float64(height) + tiny := resize.Thumbnail(32, 32, thumb, resize.NearestNeighbor) + bh, err := blurhash.Encode(4, 3, tiny) + if err != nil { + return nil, err + } + out := &bytes.Buffer{} if err := jpeg.Encode(out, thumb, &jpeg.Options{ - Quality: 100, + Quality: 75, }); err != nil { return nil, err } return &imageAndMeta{ - image: out.Bytes(), - width: width, - height: height, - size: size, - aspect: aspect, + image: out.Bytes(), + width: width, + height: height, + size: size, + aspect: aspect, + blurhash: bh, }, nil } diff --git a/internal/media/util_test.go b/internal/media/util_test.go @@ -23,7 +23,6 @@ import ( "testing" "github.com/sirupsen/logrus" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" ) @@ -64,82 +63,82 @@ func (suite *MediaUtilTestSuite) TearDownTest() { func (suite *MediaUtilTestSuite) TestParseContentTypeOK() { f, err := ioutil.ReadFile("./test/test-jpeg.jpg") - assert.Nil(suite.T(), err) + suite.NoError(err) ct, err := parseContentType(f) - assert.Nil(suite.T(), err) - assert.Equal(suite.T(), "image/jpeg", ct) + suite.NoError(err) + suite.Equal("image/jpeg", ct) } func (suite *MediaUtilTestSuite) TestParseContentTypeNotOK() { f, err := ioutil.ReadFile("./test/test-corrupted.jpg") - assert.Nil(suite.T(), err) + suite.NoError(err) ct, err := parseContentType(f) - assert.NotNil(suite.T(), err) - assert.Equal(suite.T(), "", ct) - assert.Equal(suite.T(), "filetype unknown", err.Error()) + suite.NotNil(err) + suite.Equal("", ct) + suite.Equal("filetype unknown", err.Error()) } func (suite *MediaUtilTestSuite) TestRemoveEXIF() { // load and validate image b, err := ioutil.ReadFile("./test/test-with-exif.jpg") - assert.Nil(suite.T(), err) + suite.NoError(err) // clean it up and validate the clean version clean, err := purgeExif(b) - assert.Nil(suite.T(), err) + suite.NoError(err) // compare it to our stored sample sampleBytes, err := ioutil.ReadFile("./test/test-without-exif.jpg") - assert.Nil(suite.T(), err) - assert.EqualValues(suite.T(), sampleBytes, clean) + suite.NoError(err) + suite.EqualValues(sampleBytes, clean) } func (suite *MediaUtilTestSuite) TestDeriveImageFromJPEG() { // load image b, err := ioutil.ReadFile("./test/test-jpeg.jpg") - assert.Nil(suite.T(), err) + suite.NoError(err) // clean it up and validate the clean version imageAndMeta, err := deriveImage(b, "image/jpeg") - assert.Nil(suite.T(), err) + suite.NoError(err) - assert.Equal(suite.T(), 1920, imageAndMeta.width) - assert.Equal(suite.T(), 1080, imageAndMeta.height) - assert.Equal(suite.T(), 1.7777777777777777, imageAndMeta.aspect) - assert.Equal(suite.T(), 2073600, imageAndMeta.size) - assert.Equal(suite.T(), "LjCZnlvyRkRn_NvzRjWF?urqV@f9", imageAndMeta.blurhash) + suite.Equal(1920, imageAndMeta.width) + suite.Equal(1080, imageAndMeta.height) + suite.Equal(1.7777777777777777, imageAndMeta.aspect) + suite.Equal(2073600, imageAndMeta.size) // assert that the final image is what we would expect sampleBytes, err := ioutil.ReadFile("./test/test-jpeg-processed.jpg") - assert.Nil(suite.T(), err) - assert.EqualValues(suite.T(), sampleBytes, imageAndMeta.image) + suite.NoError(err) + suite.EqualValues(sampleBytes, imageAndMeta.image) } func (suite *MediaUtilTestSuite) TestDeriveThumbnailFromJPEG() { // load image b, err := ioutil.ReadFile("./test/test-jpeg.jpg") - assert.Nil(suite.T(), err) + suite.NoError(err) // clean it up and validate the clean version - imageAndMeta, err := deriveThumbnail(b, "image/jpeg", 256, 256) - assert.Nil(suite.T(), err) + imageAndMeta, err := deriveThumbnail(b, "image/jpeg", 512, 512) + suite.NoError(err) - assert.Equal(suite.T(), 256, imageAndMeta.width) - assert.Equal(suite.T(), 144, imageAndMeta.height) - assert.Equal(suite.T(), 1.7777777777777777, imageAndMeta.aspect) - assert.Equal(suite.T(), 36864, imageAndMeta.size) + suite.Equal(512, imageAndMeta.width) + suite.Equal(288, imageAndMeta.height) + suite.Equal(1.7777777777777777, imageAndMeta.aspect) + suite.Equal(147456, imageAndMeta.size) + suite.Equal("LjBzUo#6RQR._NvzRjWF?urqV@a$", imageAndMeta.blurhash) sampleBytes, err := ioutil.ReadFile("./test/test-jpeg-thumbnail.jpg") - assert.Nil(suite.T(), err) - assert.EqualValues(suite.T(), sampleBytes, imageAndMeta.image) + suite.NoError(err) + suite.EqualValues(sampleBytes, imageAndMeta.image) } func (suite *MediaUtilTestSuite) TestSupportedImageTypes() { ok := SupportedImageType("image/jpeg") - assert.True(suite.T(), ok) + suite.True(ok) ok = SupportedImageType("image/bmp") - assert.False(suite.T(), ok) + suite.False(ok) } func TestMediaUtilTestSuite(t *testing.T) {