Skip to content

Commit

Permalink
update media processing (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsmethurst authored Sep 23, 2021
1 parent 7e6350b commit ddfd83d
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 81 deletions.
8 changes: 4 additions & 4 deletions internal/api/client/media/mediacreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,17 @@ 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{
X: -0.5,
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)
Expand Down
4 changes: 2 additions & 2 deletions internal/federation/dereferencing/attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/media/processicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions internal/media/processimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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,
Expand Down
Binary file modified internal/media/test/test-jpeg-processed.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified internal/media/test/test-jpeg-thumbnail.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 23 additions & 40 deletions internal/media/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
63 changes: 31 additions & 32 deletions internal/media/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit ddfd83d

Please sign in to comment.