From 15d1e6b3a130547862a15ad2997bcce0fea45132 Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Mon, 21 Feb 2022 11:26:26 +0100 Subject: [PATCH] [bugfix] Fix thumbnail image type (#406) * fix thumbnail content-type * test fix thumbnail content-type --- .../api/client/fileserver/servefile_test.go | 62 ++++++++++++++++--- internal/media/manager_test.go | 4 ++ internal/media/processingmedia.go | 2 +- 3 files changed, 59 insertions(+), 9 deletions(-) diff --git a/internal/api/client/fileserver/servefile_test.go b/internal/api/client/fileserver/servefile_test.go index 5cd4b529b..b87b08ce8 100644 --- a/internal/api/client/fileserver/servefile_test.go +++ b/internal/api/client/fileserver/servefile_test.go @@ -29,7 +29,6 @@ import ( "codeberg.org/gruf/go-store/kv" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" "github.com/superseriousbusiness/gotosocial/internal/api/client/fileserver" "github.com/superseriousbusiness/gotosocial/internal/db" @@ -117,8 +116,8 @@ func (suite *ServeFileTestSuite) TearDownTest() { func (suite *ServeFileTestSuite) TestServeOriginalFileSuccessful() { targetAttachment, ok := suite.testAttachments["admin_account_status_1_attachment_1"] - assert.True(suite.T(), ok) - assert.NotNil(suite.T(), targetAttachment) + suite.True(ok) + suite.NotNil(targetAttachment) recorder := httptest.NewRecorder() ctx, _ := gin.CreateTestContext(recorder) @@ -149,15 +148,62 @@ func (suite *ServeFileTestSuite) TestServeOriginalFileSuccessful() { // call the function we're testing and check status code suite.fileServer.ServeFile(ctx) suite.EqualValues(http.StatusOK, recorder.Code) + suite.EqualValues("image/jpeg", recorder.Header().Get("content-type")) b, err := ioutil.ReadAll(recorder.Body) - assert.NoError(suite.T(), err) - assert.NotNil(suite.T(), b) + suite.NoError(err) + suite.NotNil(b) fileInStorage, err := suite.storage.Get(targetAttachment.File.Path) - assert.NoError(suite.T(), err) - assert.NotNil(suite.T(), fileInStorage) - assert.Equal(suite.T(), b, fileInStorage) + suite.NoError(err) + suite.NotNil(fileInStorage) + suite.Equal(b, fileInStorage) +} + +func (suite *ServeFileTestSuite) TestServeSmallFileSuccessful() { + targetAttachment, ok := suite.testAttachments["admin_account_status_1_attachment_1"] + suite.True(ok) + suite.NotNil(targetAttachment) + + recorder := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(recorder) + ctx.Request = httptest.NewRequest(http.MethodGet, targetAttachment.Thumbnail.URL, nil) + ctx.Request.Header.Set("accept", "*/*") + + // normally the router would populate these params from the path values, + // but because we're calling the ServeFile function directly, we need to set them manually. + ctx.Params = gin.Params{ + gin.Param{ + Key: fileserver.AccountIDKey, + Value: targetAttachment.AccountID, + }, + gin.Param{ + Key: fileserver.MediaTypeKey, + Value: string(media.TypeAttachment), + }, + gin.Param{ + Key: fileserver.MediaSizeKey, + Value: string(media.SizeSmall), + }, + gin.Param{ + Key: fileserver.FileNameKey, + Value: fmt.Sprintf("%s.jpeg", targetAttachment.ID), + }, + } + + // call the function we're testing and check status code + suite.fileServer.ServeFile(ctx) + suite.EqualValues(http.StatusOK, recorder.Code) + suite.EqualValues("image/jpeg", recorder.Header().Get("content-type")) + + b, err := ioutil.ReadAll(recorder.Body) + suite.NoError(err) + suite.NotNil(b) + + fileInStorage, err := suite.storage.Get(targetAttachment.Thumbnail.Path) + suite.NoError(err) + suite.NotNil(fileInStorage) + suite.Equal(b, fileInStorage) } func TestServeFileTestSuite(t *testing.T) { diff --git a/internal/media/manager_test.go b/internal/media/manager_test.go index a9419754c..8443f825e 100644 --- a/internal/media/manager_test.go +++ b/internal/media/manager_test.go @@ -77,6 +77,7 @@ func (suite *ManagerTestSuite) TestSimpleJpegProcessBlocking() { Width: 512, Height: 288, Size: 147456, Aspect: 1.7777777777777777, }, attachment.FileMeta.Small) suite.Equal("image/jpeg", attachment.File.ContentType) + suite.Equal("image/jpeg", attachment.Thumbnail.ContentType) suite.Equal(269739, attachment.File.FileSize) suite.Equal("LjBzUo#6RQR._NvzRjWF?urqV@a$", attachment.Blurhash) @@ -155,6 +156,7 @@ func (suite *ManagerTestSuite) TestSimpleJpegProcessAsync() { Width: 512, Height: 288, Size: 147456, Aspect: 1.7777777777777777, }, attachment.FileMeta.Small) suite.Equal("image/jpeg", attachment.File.ContentType) + suite.Equal("image/jpeg", attachment.Thumbnail.ContentType) suite.Equal(269739, attachment.File.FileSize) suite.Equal("LjBzUo#6RQR._NvzRjWF?urqV@a$", attachment.Blurhash) @@ -237,6 +239,7 @@ func (suite *ManagerTestSuite) TestSimpleJpegQueueSpamming() { Width: 512, Height: 288, Size: 147456, Aspect: 1.7777777777777777, }, attachment.FileMeta.Small) suite.Equal("image/jpeg", attachment.File.ContentType) + suite.Equal("image/jpeg", attachment.Thumbnail.ContentType) suite.Equal(269739, attachment.File.FileSize) suite.Equal("LjBzUo#6RQR._NvzRjWF?urqV@a$", attachment.Blurhash) @@ -325,6 +328,7 @@ func (suite *ManagerTestSuite) TestSimpleJpegProcessBlockingWithDiskStorage() { Width: 512, Height: 288, Size: 147456, Aspect: 1.7777777777777777, }, attachment.FileMeta.Small) suite.Equal("image/jpeg", attachment.File.ContentType) + suite.Equal("image/jpeg", attachment.Thumbnail.ContentType) suite.Equal(269739, attachment.File.FileSize) suite.Equal("LjBzUo#6RQR._NvzRjWF?urqV@a$", attachment.Blurhash) diff --git a/internal/media/processingmedia.go b/internal/media/processingmedia.go index 0f47ee4e6..2df5fe584 100644 --- a/internal/media/processingmedia.go +++ b/internal/media/processingmedia.go @@ -331,7 +331,7 @@ func (m *manager) preProcessMedia(ctx context.Context, data DataFunc, accountID thumbnail := gtsmodel.Thumbnail{ URL: uris.GenerateURIForAttachment(accountID, string(TypeAttachment), string(SizeSmall), id, mimeJpeg), // all thumbnails are encoded as jpeg, Path: fmt.Sprintf("%s/%s/%s/%s.%s", accountID, TypeAttachment, SizeSmall, id, mimeJpeg), // all thumbnails are encoded as jpeg, - ContentType: mimeJpeg, + ContentType: mimeImageJpeg, UpdatedAt: time.Now(), }