diff --git a/api4/file_test.go b/api4/file_test.go index d955dca1ef..9d3e739333 100644 --- a/api4/file_test.go +++ b/api4/file_test.go @@ -5,11 +5,10 @@ package api4 import ( "bytes" - "encoding/hex" + "crypto/rand" "fmt" "io" "io/ioutil" - "math/rand" "mime/multipart" "net/http" "net/textproto" @@ -222,13 +221,13 @@ func TestUploadFiles(t *testing.T) { // Upload a bunch of files, mixed images and non-images { title: "Happy", - names: []string{"test.png", "testgif.gif", "testplugin.tar.gz", "test-search.md", "test_compressed_tiff.tiff"}, + names: []string{"test.png", "testgif.gif", "testplugin.tar.gz", "test-search.md", "test.tiff"}, expectedCreatorId: th.BasicUser.Id, }, // Upload a bunch of files, with clientIds { title: "Happy client_ids", - names: []string{"test.png", "testgif.gif", "testplugin.tar.gz", "test-search.md", "test_compressed_tiff.tiff"}, + names: []string{"test.png", "testgif.gif", "testplugin.tar.gz", "test-search.md", "test.tiff"}, clientIds: []string{"1", "2", "3", "4", "5"}, expectedCreatorId: th.BasicUser.Id, }, @@ -357,18 +356,7 @@ func TestUploadFiles(t *testing.T) { // TIFF preview test { title: "Happy image thumbnail/preview 9", - names: []string{"test_compressed_tiff.tiff"}, - expectedImageThumbnailNames: []string{"test_expected_tiff_thumb.jpeg"}, - expectedImagePreviewNames: []string{"test_expected_tiff_preview.jpeg"}, - expectImage: true, - expectedImageWidths: []int{701}, - expectedImageHeights: []int{701}, - expectedImageHasPreview: []bool{true}, - expectedCreatorId: th.BasicUser.Id, - }, - { - title: "Happy image thumbnail/preview 10", - names: []string{"test_raw_tiff.tiff"}, + names: []string{"test.tiff"}, expectedImageThumbnailNames: []string{"test_expected_tiff_thumb.jpeg"}, expectedImagePreviewNames: []string{"test_expected_tiff_preview.jpeg"}, expectImage: true, @@ -625,12 +613,7 @@ func TestUploadFiles(t *testing.T) { require.Nil(t, err) _, err = io.Copy(tf, bytes.NewReader(data)) require.Nil(t, err) - if strings.Contains(name, "test_expected_tiff") { - // TODO: remove this once MM-22056 is fixed. - t.Errorf("Actual data mismatched %s, written to %q - expected %d bytes, got %d. Previewer Image: \n\n%s\n", name, tf.Name(), len(expected), len(data), hex.Dump(data)) - } else { - t.Errorf("Actual data mismatched %s, written to %q - expected %d bytes, got %d.", name, tf.Name(), len(expected), len(data)) - } + t.Errorf("Actual data mismatched %s, written to %q - expected %d bytes, got %d.", name, tf.Name(), len(expected), len(data)) } } if len(tc.expectedPayloadNames) == 0 { @@ -767,9 +750,6 @@ func TestGetFileThumbnail(t *testing.T) { fileId := fileResp.FileInfos[0].Id - // Wait a bit for files to ready - time.Sleep(2 * time.Second) - data, resp := Client.GetFileThumbnail(fileId) CheckNoError(t, resp) require.NotEqual(t, 0, len(data), "should not be empty") @@ -826,9 +806,6 @@ func TestGetFileLink(t *testing.T) { _, resp = Client.GetFileLink(fileId) CheckNotImplementedStatus(t, resp) - // Wait a bit for files to ready - time.Sleep(2 * time.Second) - th.App.UpdateConfig(func(cfg *model.Config) { *cfg.FileSettings.EnablePublicLink = true }) link, resp := Client.GetFileLink(fileId) CheckNoError(t, resp) @@ -875,9 +852,6 @@ func TestGetFilePreview(t *testing.T) { CheckNoError(t, resp) fileId := fileResp.FileInfos[0].Id - // Wait a bit for files to ready - time.Sleep(2 * time.Second) - data, resp := Client.GetFilePreview(fileId) CheckNoError(t, resp) require.NotEqual(t, 0, len(data), "should not be empty") @@ -920,9 +894,6 @@ func TestGetFileInfo(t *testing.T) { CheckNoError(t, resp) fileId := fileResp.FileInfos[0].Id - // Wait a bit for files to ready - time.Sleep(2 * time.Second) - info, resp := Client.GetFileInfo(fileId) CheckNoError(t, resp) @@ -980,9 +951,6 @@ func TestGetPublicFile(t *testing.T) { require.Nil(t, err) link := th.App.GeneratePublicLink(Client.Url, info) - // Wait a bit for files to ready - time.Sleep(2 * time.Second) - resp, err := http.Get(link) require.NoError(t, err) require.Equal(t, http.StatusOK, resp.StatusCode, "failed to get image with public link") diff --git a/app/file.go b/app/file.go index cb9392b210..40b9ee650c 100644 --- a/app/file.go +++ b/app/file.go @@ -26,6 +26,7 @@ import ( "github.com/disintegration/imaging" "github.com/rwcarlsen/goexif/exif" _ "golang.org/x/image/bmp" + _ "golang.org/x/image/tiff" "github.com/mattermost/mattermost-server/v5/mlog" "github.com/mattermost/mattermost-server/v5/model" @@ -796,29 +797,29 @@ func (t *UploadFileTask) postprocessImage() { return } + const jpegQuality = 90 writeJPEG := func(img image.Image, path string) { r, w := io.Pipe() go func() { - _, aerr := t.writeFile(r, path) - if aerr != nil { - mlog.Error("Unable to upload", mlog.String("path", path), mlog.Err(aerr)) - return + err := jpeg.Encode(w, img, &jpeg.Options{Quality: jpegQuality}) + if err != nil { + mlog.Error("Unable to encode image as jpeg", mlog.String("path", path), mlog.Err(err)) + w.CloseWithError(err) + } else { + w.Close() } }() - - err := jpeg.Encode(w, img, &jpeg.Options{Quality: 90}) - if err != nil { - mlog.Error("Unable to encode image as jpeg", mlog.String("path", path), mlog.Err(err)) - w.CloseWithError(err) - } else { - w.Close() + _, aerr := t.writeFile(r, path) + if aerr != nil { + mlog.Error("Unable to upload", mlog.String("path", path), mlog.Err(aerr)) + return } } w := decoded.Bounds().Dx() h := decoded.Bounds().Dy() - wg := &sync.WaitGroup{} + var wg sync.WaitGroup wg.Add(2) go func() { defer wg.Done() diff --git a/app/post_metadata_test.go b/app/post_metadata_test.go index 24bda46ac9..f65ab845ff 100644 --- a/app/post_metadata_test.go +++ b/app/post_metadata_test.go @@ -2181,7 +2181,7 @@ func TestParseImages(t *testing.T) { }, }, "tiff": { - FileName: "test_compressed_tiff.tiff", + FileName: "test.tiff", Expected: (*model.PostImage)(nil), }, "not an image": { diff --git a/tests/test_compressed_tiff.tiff b/tests/test.tiff similarity index 100% rename from tests/test_compressed_tiff.tiff rename to tests/test.tiff diff --git a/tests/test_raw_tiff.tiff b/tests/test_raw_tiff.tiff deleted file mode 100644 index 5d5ec68f67..0000000000 Binary files a/tests/test_raw_tiff.tiff and /dev/null differ