2023-03-12 18:00:57 +03:00
|
|
|
// GoToSocial
|
|
|
|
// Copyright (C) GoToSocial Authors admin@gotosocial.org
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2022-01-09 20:41:22 +03:00
|
|
|
|
2022-01-08 19:17:01 +03:00
|
|
|
package media_test
|
|
|
|
|
2022-01-09 20:41:22 +03:00
|
|
|
import (
|
2022-01-16 20:52:55 +03:00
|
|
|
"bytes"
|
2022-01-10 20:36:09 +03:00
|
|
|
"context"
|
2024-07-12 12:39:47 +03:00
|
|
|
"crypto/md5"
|
2022-01-10 20:36:09 +03:00
|
|
|
"fmt"
|
2022-01-16 20:52:55 +03:00
|
|
|
"io"
|
2022-01-10 20:36:09 +03:00
|
|
|
"os"
|
|
|
|
"testing"
|
2023-01-11 14:13:13 +03:00
|
|
|
"time"
|
2022-01-10 20:36:09 +03:00
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
"codeberg.org/gruf/go-iotools"
|
2024-05-22 12:46:24 +03:00
|
|
|
"codeberg.org/gruf/go-storage/disk"
|
2022-01-09 20:41:22 +03:00
|
|
|
"github.com/stretchr/testify/suite"
|
2022-03-07 13:08:26 +03:00
|
|
|
gtsmodel "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2022-01-09 20:41:22 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/media"
|
2023-02-13 21:40:48 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/state"
|
2024-05-22 12:46:24 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/storage"
|
2022-07-03 13:08:30 +03:00
|
|
|
gtsstorage "github.com/superseriousbusiness/gotosocial/internal/storage"
|
2024-07-12 12:39:47 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/util"
|
2024-04-11 12:45:35 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/testrig"
|
2022-01-09 20:41:22 +03:00
|
|
|
)
|
|
|
|
|
2022-01-10 20:36:09 +03:00
|
|
|
type ManagerTestSuite struct {
|
|
|
|
MediaStandardTestSuite
|
|
|
|
}
|
|
|
|
|
2024-06-26 18:01:16 +03:00
|
|
|
func (suite *ManagerTestSuite) TestEmojiProcess() {
|
2022-09-24 12:11:47 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
data := func(_ context.Context) (io.ReadCloser, error) {
|
2022-09-24 12:11:47 +03:00
|
|
|
// load bytes from a test image
|
|
|
|
b, err := os.ReadFile("./test/rainbow-original.png")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-07-12 12:39:47 +03:00
|
|
|
return io.NopCloser(bytes.NewBuffer(b)), nil
|
2022-09-24 12:11:47 +03:00
|
|
|
}
|
|
|
|
|
2024-06-26 18:01:16 +03:00
|
|
|
processing, err := suite.manager.CreateEmoji(ctx,
|
|
|
|
"rainbow_test",
|
|
|
|
"",
|
|
|
|
data,
|
|
|
|
media.AdditionalEmojiInfo{},
|
|
|
|
)
|
2022-09-24 12:11:47 +03:00
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
// do a blocking call to fetch the emoji
|
2024-06-26 18:01:16 +03:00
|
|
|
emoji, err := processing.Load(ctx)
|
2022-09-24 12:11:47 +03:00
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(emoji)
|
|
|
|
|
|
|
|
// file meta should be correctly derived from the image
|
2024-07-12 12:39:47 +03:00
|
|
|
suite.Equal("image/apng", emoji.ImageContentType)
|
2022-09-24 12:11:47 +03:00
|
|
|
suite.Equal("image/png", emoji.ImageStaticContentType)
|
|
|
|
suite.Equal(36702, emoji.ImageFileSize)
|
|
|
|
|
|
|
|
// now make sure the emoji is in the database
|
2024-06-26 18:01:16 +03:00
|
|
|
dbEmoji, err := suite.db.GetEmojiByID(ctx, emoji.ID)
|
2022-09-24 12:11:47 +03:00
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(dbEmoji)
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
// ensure the files contain the expected data.
|
|
|
|
equalFiles(suite.T(), suite.state.Storage, dbEmoji.ImagePath, "./test/rainbow-original.png")
|
|
|
|
equalFiles(suite.T(), suite.state.Storage, dbEmoji.ImageStaticPath, "./test/rainbow-static.png")
|
2022-09-24 12:11:47 +03:00
|
|
|
}
|
|
|
|
|
2024-06-26 18:01:16 +03:00
|
|
|
func (suite *ManagerTestSuite) TestEmojiProcessRefresh() {
|
2022-10-13 16:16:24 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// we're going to 'refresh' the remote 'yell' emoji by changing the image url to the pixellated gts logo
|
|
|
|
originalEmoji := suite.testEmojis["yell"]
|
|
|
|
|
2024-06-26 18:01:16 +03:00
|
|
|
emojiToUpdate, err := suite.db.GetEmojiByID(ctx, originalEmoji.ID)
|
|
|
|
suite.NoError(err)
|
|
|
|
|
2022-10-13 16:16:24 +03:00
|
|
|
newImageRemoteURL := "http://fossbros-anonymous.io/some/image/path.png"
|
|
|
|
|
|
|
|
oldEmojiImagePath := emojiToUpdate.ImagePath
|
|
|
|
oldEmojiImageStaticPath := emojiToUpdate.ImageStaticPath
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
data := func(_ context.Context) (io.ReadCloser, error) {
|
2022-10-13 16:16:24 +03:00
|
|
|
b, err := os.ReadFile("./test/gts_pixellated-original.png")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-07-12 12:39:47 +03:00
|
|
|
return io.NopCloser(bytes.NewBuffer(b)), nil
|
2022-10-13 16:16:24 +03:00
|
|
|
}
|
|
|
|
|
2024-06-26 18:01:16 +03:00
|
|
|
processing, err := suite.manager.RefreshEmoji(ctx,
|
|
|
|
emojiToUpdate,
|
|
|
|
data,
|
|
|
|
media.AdditionalEmojiInfo{
|
|
|
|
CreatedAt: &emojiToUpdate.CreatedAt,
|
|
|
|
Domain: &emojiToUpdate.Domain,
|
|
|
|
ImageRemoteURL: &newImageRemoteURL,
|
|
|
|
},
|
|
|
|
)
|
2022-10-13 16:16:24 +03:00
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
// do a blocking call to fetch the emoji
|
2024-06-26 18:01:16 +03:00
|
|
|
emoji, err := processing.Load(ctx)
|
2022-10-13 16:16:24 +03:00
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(emoji)
|
|
|
|
|
|
|
|
// make sure it's got the stuff set on it that we expect
|
2024-06-26 18:01:16 +03:00
|
|
|
suite.Equal(originalEmoji.ID, emoji.ID)
|
2022-10-13 16:16:24 +03:00
|
|
|
|
|
|
|
// file meta should be correctly derived from the image
|
|
|
|
suite.Equal("image/png", emoji.ImageContentType)
|
|
|
|
suite.Equal("image/png", emoji.ImageStaticContentType)
|
|
|
|
suite.Equal(10296, emoji.ImageFileSize)
|
|
|
|
|
|
|
|
// now make sure the emoji is in the database
|
2024-06-26 18:01:16 +03:00
|
|
|
dbEmoji, err := suite.db.GetEmojiByID(ctx, emoji.ID)
|
2022-10-13 16:16:24 +03:00
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(dbEmoji)
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
// ensure the files contain the expected data.
|
|
|
|
equalFiles(suite.T(), suite.state.Storage, dbEmoji.ImagePath, "./test/gts_pixellated-original.png")
|
|
|
|
equalFiles(suite.T(), suite.state.Storage, dbEmoji.ImageStaticPath, "./test/gts_pixellated-static.png")
|
2022-10-13 16:16:24 +03:00
|
|
|
|
|
|
|
// most fields should be different on the emoji now from what they were before
|
|
|
|
suite.Equal(originalEmoji.ID, dbEmoji.ID)
|
|
|
|
suite.NotEqual(originalEmoji.ImageRemoteURL, dbEmoji.ImageRemoteURL)
|
|
|
|
suite.NotEqual(originalEmoji.ImageURL, dbEmoji.ImageURL)
|
|
|
|
suite.NotEqual(originalEmoji.ImageStaticURL, dbEmoji.ImageStaticURL)
|
|
|
|
suite.NotEqual(originalEmoji.ImageFileSize, dbEmoji.ImageFileSize)
|
|
|
|
suite.NotEqual(originalEmoji.ImageStaticFileSize, dbEmoji.ImageStaticFileSize)
|
|
|
|
suite.NotEqual(originalEmoji.ImagePath, dbEmoji.ImagePath)
|
|
|
|
suite.NotEqual(originalEmoji.ImageStaticPath, dbEmoji.ImageStaticPath)
|
|
|
|
suite.NotEqual(originalEmoji.ImageStaticPath, dbEmoji.ImageStaticPath)
|
|
|
|
suite.NotEqual(originalEmoji.UpdatedAt, dbEmoji.UpdatedAt)
|
|
|
|
|
|
|
|
// the old image files should no longer be in storage
|
|
|
|
_, err = suite.storage.Get(ctx, oldEmojiImagePath)
|
2024-05-22 12:46:24 +03:00
|
|
|
suite.True(storage.IsNotFound(err))
|
2022-10-13 16:16:24 +03:00
|
|
|
_, err = suite.storage.Get(ctx, oldEmojiImageStaticPath)
|
2024-05-22 12:46:24 +03:00
|
|
|
suite.True(storage.IsNotFound(err))
|
2022-10-13 16:16:24 +03:00
|
|
|
}
|
|
|
|
|
2024-06-26 18:01:16 +03:00
|
|
|
func (suite *ManagerTestSuite) TestEmojiProcessTooLarge() {
|
2022-09-24 12:11:47 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
// Open test image as file for reading.
|
|
|
|
file, err := os.Open("./test/big-panda.gif")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2022-09-24 12:11:47 +03:00
|
|
|
}
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
// Get file size info.
|
|
|
|
stat, err := file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2022-09-24 12:11:47 +03:00
|
|
|
}
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
// Set max allowed size UNDER image size.
|
|
|
|
lr := io.LimitReader(file, stat.Size()-10)
|
|
|
|
rc := iotools.ReadCloser(lr, file)
|
|
|
|
|
2024-06-26 18:01:16 +03:00
|
|
|
processing, err := suite.manager.CreateEmoji(ctx,
|
|
|
|
"big_panda",
|
|
|
|
"",
|
2024-07-12 12:39:47 +03:00
|
|
|
func(ctx context.Context) (reader io.ReadCloser, err error) {
|
|
|
|
return rc, nil
|
|
|
|
},
|
2024-06-26 18:01:16 +03:00
|
|
|
media.AdditionalEmojiInfo{},
|
|
|
|
)
|
2022-09-24 12:11:47 +03:00
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
// do a blocking call to fetch the emoji
|
2024-06-26 18:01:16 +03:00
|
|
|
_, err = processing.Load(ctx)
|
2024-07-12 12:39:47 +03:00
|
|
|
suite.EqualError(err, "store: error draining data to tmp: reached read limit 630kiB")
|
2022-09-24 12:11:47 +03:00
|
|
|
}
|
|
|
|
|
2024-01-05 15:39:53 +03:00
|
|
|
func (suite *ManagerTestSuite) TestEmojiWebpProcess() {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
data := func(_ context.Context) (io.ReadCloser, error) {
|
2024-01-05 15:39:53 +03:00
|
|
|
// load bytes from a test image
|
|
|
|
b, err := os.ReadFile("./test/nb-flag-original.webp")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-07-12 12:39:47 +03:00
|
|
|
return io.NopCloser(bytes.NewBuffer(b)), nil
|
2024-01-05 15:39:53 +03:00
|
|
|
}
|
|
|
|
|
2024-06-26 18:01:16 +03:00
|
|
|
// process the media with no additional info provided
|
|
|
|
processing, err := suite.manager.CreateEmoji(ctx,
|
|
|
|
"nb-flag",
|
|
|
|
"",
|
|
|
|
data,
|
|
|
|
media.AdditionalEmojiInfo{},
|
|
|
|
)
|
2024-01-05 15:39:53 +03:00
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
// do a blocking call to fetch the emoji
|
2024-06-26 18:01:16 +03:00
|
|
|
emoji, err := processing.Load(ctx)
|
2024-01-05 15:39:53 +03:00
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(emoji)
|
|
|
|
|
|
|
|
// file meta should be correctly derived from the image
|
|
|
|
suite.Equal("image/webp", emoji.ImageContentType)
|
|
|
|
suite.Equal("image/png", emoji.ImageStaticContentType)
|
|
|
|
suite.Equal(294, emoji.ImageFileSize)
|
|
|
|
|
|
|
|
// now make sure the emoji is in the database
|
2024-06-26 18:01:16 +03:00
|
|
|
dbEmoji, err := suite.db.GetEmojiByID(ctx, emoji.ID)
|
2024-01-05 15:39:53 +03:00
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(dbEmoji)
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
// ensure files are equal
|
|
|
|
equalFiles(suite.T(), suite.state.Storage, dbEmoji.ImagePath, "./test/nb-flag-original.webp")
|
|
|
|
equalFiles(suite.T(), suite.state.Storage, dbEmoji.ImageStaticPath, "./test/nb-flag-static.png")
|
2024-01-05 15:39:53 +03:00
|
|
|
}
|
|
|
|
|
2024-06-26 18:01:16 +03:00
|
|
|
func (suite *ManagerTestSuite) TestSimpleJpegProcess() {
|
2022-01-10 20:36:09 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
data := func(_ context.Context) (io.ReadCloser, error) {
|
2022-01-11 19:49:14 +03:00
|
|
|
// load bytes from a test image
|
2022-01-16 20:52:55 +03:00
|
|
|
b, err := os.ReadFile("./test/test-jpeg.jpg")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-07-12 12:39:47 +03:00
|
|
|
return io.NopCloser(bytes.NewBuffer(b)), nil
|
2022-01-11 19:49:14 +03:00
|
|
|
}
|
2022-01-10 20:36:09 +03:00
|
|
|
|
|
|
|
accountID := "01FS1X72SK9ZPW0J1QQ68BD264"
|
|
|
|
|
|
|
|
// process the media with no additional info provided
|
2024-06-26 18:01:16 +03:00
|
|
|
processing, err := suite.manager.CreateMedia(ctx,
|
|
|
|
accountID,
|
|
|
|
data,
|
|
|
|
media.AdditionalMediaInfo{},
|
|
|
|
)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(processing)
|
2022-09-24 12:11:47 +03:00
|
|
|
|
|
|
|
// do a blocking call to fetch the attachment
|
2024-06-26 18:01:16 +03:00
|
|
|
attachment, err := processing.Load(ctx)
|
2022-09-24 12:11:47 +03:00
|
|
|
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
|
2024-06-26 18:01:16 +03:00
|
|
|
suite.Equal(processing.ID(), attachment.ID)
|
2022-09-24 12:11:47 +03:00
|
|
|
suite.Equal(accountID, attachment.AccountID)
|
|
|
|
|
|
|
|
// file meta should be correctly derived from the image
|
|
|
|
suite.EqualValues(gtsmodel.Original{
|
|
|
|
Width: 1920, Height: 1080, Size: 2073600, Aspect: 1.7777777777777777,
|
|
|
|
}, attachment.FileMeta.Original)
|
|
|
|
suite.EqualValues(gtsmodel.Small{
|
|
|
|
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)
|
2024-07-12 12:39:47 +03:00
|
|
|
suite.Equal("LjCGfG#6RkRn_NvzRjWF?urqV@a$", attachment.Blurhash)
|
2022-09-24 12:11:47 +03:00
|
|
|
|
|
|
|
// now make sure the attachment is in the database
|
2024-06-26 18:01:16 +03:00
|
|
|
dbAttachment, err := suite.db.GetAttachmentByID(ctx, attachment.ID)
|
2022-09-24 12:11:47 +03:00
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(dbAttachment)
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
// ensure the files contain the expected data.
|
|
|
|
equalFiles(suite.T(), suite.state.Storage, dbAttachment.File.Path, "./test/test-jpeg-processed.jpg")
|
|
|
|
equalFiles(suite.T(), suite.state.Storage, dbAttachment.Thumbnail.Path, "./test/test-jpeg-thumbnail.jpg")
|
2022-09-24 12:11:47 +03:00
|
|
|
}
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
func (suite *ManagerTestSuite) TestSimpleJpegProcessTooLarge() {
|
2023-11-10 21:29:26 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
// Open test image as file for reading.
|
|
|
|
file, err := os.Open("./test/test-jpeg.jpg")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-11-10 21:29:26 +03:00
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
// Get file size info.
|
|
|
|
stat, err := file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2023-11-10 21:29:26 +03:00
|
|
|
}
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
// Set max allowed size UNDER image size.
|
|
|
|
lr := io.LimitReader(file, stat.Size()-10)
|
|
|
|
rc := iotools.ReadCloser(lr, file)
|
|
|
|
|
2023-11-10 21:29:26 +03:00
|
|
|
accountID := "01FS1X72SK9ZPW0J1QQ68BD264"
|
|
|
|
|
|
|
|
// process the media with no additional info provided
|
2024-06-26 18:01:16 +03:00
|
|
|
processing, err := suite.manager.CreateMedia(ctx,
|
|
|
|
accountID,
|
2024-07-12 12:39:47 +03:00
|
|
|
func(ctx context.Context) (reader io.ReadCloser, err error) {
|
|
|
|
return rc, nil
|
|
|
|
},
|
2024-06-26 18:01:16 +03:00
|
|
|
media.AdditionalMediaInfo{},
|
|
|
|
)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(processing)
|
2023-11-10 21:29:26 +03:00
|
|
|
|
|
|
|
// do a blocking call to fetch the attachment
|
2024-07-12 12:39:47 +03:00
|
|
|
_, err = processing.Load(ctx)
|
|
|
|
suite.EqualError(err, "store: error draining data to tmp: reached read limit 263kiB")
|
2023-11-10 21:29:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *ManagerTestSuite) TestPDFProcess() {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
data := func(_ context.Context) (io.ReadCloser, error) {
|
2023-11-10 21:29:26 +03:00
|
|
|
// load bytes from Frantz
|
|
|
|
b, err := os.ReadFile("./test/Frantz-Fanon-The-Wretched-of-the-Earth-1965.pdf")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
return io.NopCloser(bytes.NewBuffer(b)), nil
|
2023-11-10 21:29:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
accountID := "01FS1X72SK9ZPW0J1QQ68BD264"
|
|
|
|
|
|
|
|
// process the media with no additional info provided
|
2024-06-26 18:01:16 +03:00
|
|
|
processing, err := suite.manager.CreateMedia(ctx,
|
|
|
|
accountID,
|
|
|
|
data,
|
|
|
|
media.AdditionalMediaInfo{},
|
|
|
|
)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(processing)
|
2023-11-10 21:29:26 +03:00
|
|
|
|
|
|
|
// do a blocking call to fetch the attachment
|
2024-06-26 18:01:16 +03:00
|
|
|
attachment, err := processing.Load(ctx)
|
2023-11-10 21:29:26 +03:00
|
|
|
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
|
2024-06-26 18:01:16 +03:00
|
|
|
suite.Equal(processing.ID(), attachment.ID)
|
2023-11-10 21:29:26 +03:00
|
|
|
suite.Equal(accountID, attachment.AccountID)
|
|
|
|
|
|
|
|
// file meta should be correctly derived from the image
|
|
|
|
suite.Zero(attachment.FileMeta)
|
2024-07-12 12:39:47 +03:00
|
|
|
suite.Equal("application/octet-stream", attachment.File.ContentType)
|
2023-11-10 21:29:26 +03:00
|
|
|
suite.Equal("image/jpeg", attachment.Thumbnail.ContentType)
|
|
|
|
suite.Empty(attachment.Blurhash)
|
|
|
|
|
|
|
|
// now make sure the attachment is in the database
|
2024-06-26 18:01:16 +03:00
|
|
|
dbAttachment, err := suite.db.GetAttachmentByID(ctx, attachment.ID)
|
2023-11-10 21:29:26 +03:00
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(dbAttachment)
|
|
|
|
|
|
|
|
// Attachment should have type unknown
|
|
|
|
suite.Equal(gtsmodel.FileTypeUnknown, dbAttachment.Type)
|
|
|
|
|
|
|
|
// Nothing should be in storage for this attachment.
|
|
|
|
stored, err := suite.storage.Has(ctx, attachment.File.Path)
|
2024-07-12 12:39:47 +03:00
|
|
|
suite.NoError(err)
|
2023-11-10 21:29:26 +03:00
|
|
|
suite.False(stored)
|
|
|
|
|
|
|
|
stored, err = suite.storage.Has(ctx, attachment.Thumbnail.Path)
|
2024-07-12 12:39:47 +03:00
|
|
|
suite.NoError(err)
|
2023-11-10 21:29:26 +03:00
|
|
|
suite.False(stored)
|
|
|
|
}
|
|
|
|
|
2024-06-26 18:01:16 +03:00
|
|
|
func (suite *ManagerTestSuite) TestSlothVineProcess() {
|
2022-12-17 07:38:56 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
data := func(_ context.Context) (io.ReadCloser, error) {
|
2022-12-17 07:38:56 +03:00
|
|
|
// load bytes from a test video
|
|
|
|
b, err := os.ReadFile("./test/test-mp4-original.mp4")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-07-12 12:39:47 +03:00
|
|
|
return io.NopCloser(bytes.NewBuffer(b)), nil
|
2022-12-17 07:38:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
accountID := "01FS1X72SK9ZPW0J1QQ68BD264"
|
|
|
|
|
|
|
|
// process the media with no additional info provided
|
2024-06-26 18:01:16 +03:00
|
|
|
processing, err := suite.manager.CreateMedia(ctx,
|
|
|
|
accountID,
|
|
|
|
data,
|
|
|
|
media.AdditionalMediaInfo{},
|
|
|
|
)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(processing)
|
2022-12-17 07:38:56 +03:00
|
|
|
|
|
|
|
// do a blocking call to fetch the attachment
|
2024-06-26 18:01:16 +03:00
|
|
|
attachment, err := processing.Load(ctx)
|
2022-12-17 07:38:56 +03:00
|
|
|
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
|
2024-06-26 18:01:16 +03:00
|
|
|
suite.Equal(processing.ID(), attachment.ID)
|
2022-12-17 07:38:56 +03:00
|
|
|
suite.Equal(accountID, attachment.AccountID)
|
|
|
|
|
|
|
|
// file meta should be correctly derived from the video
|
2022-12-22 13:48:28 +03:00
|
|
|
suite.Equal(338, attachment.FileMeta.Original.Width)
|
|
|
|
suite.Equal(240, attachment.FileMeta.Original.Height)
|
|
|
|
suite.Equal(81120, attachment.FileMeta.Original.Size)
|
2024-03-04 18:05:24 +03:00
|
|
|
suite.EqualValues(float32(1.4083333), attachment.FileMeta.Original.Aspect)
|
2024-07-12 12:39:47 +03:00
|
|
|
suite.EqualValues(float32(6.641), *attachment.FileMeta.Original.Duration)
|
2024-07-15 17:24:53 +03:00
|
|
|
suite.EqualValues(float32(29), *attachment.FileMeta.Original.Framerate)
|
2024-07-12 12:39:47 +03:00
|
|
|
suite.EqualValues(0x5be18, *attachment.FileMeta.Original.Bitrate)
|
2022-12-17 07:38:56 +03:00
|
|
|
suite.EqualValues(gtsmodel.Small{
|
|
|
|
Width: 338, Height: 240, Size: 81120, Aspect: 1.4083333333333334,
|
|
|
|
}, attachment.FileMeta.Small)
|
|
|
|
suite.Equal("video/mp4", attachment.File.ContentType)
|
|
|
|
suite.Equal("image/jpeg", attachment.Thumbnail.ContentType)
|
2024-07-12 12:39:47 +03:00
|
|
|
suite.Equal(312453, attachment.File.FileSize)
|
|
|
|
suite.Equal("LrJuJat6NZkBt7ayW.j[_4WBsWoL", attachment.Blurhash)
|
2022-12-17 07:38:56 +03:00
|
|
|
|
|
|
|
// now make sure the attachment is in the database
|
2024-06-26 18:01:16 +03:00
|
|
|
dbAttachment, err := suite.db.GetAttachmentByID(ctx, attachment.ID)
|
2022-12-17 07:38:56 +03:00
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(dbAttachment)
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
// ensure the files contain the expected data.
|
|
|
|
equalFiles(suite.T(), suite.state.Storage, dbAttachment.File.Path, "./test/test-mp4-processed.mp4")
|
|
|
|
equalFiles(suite.T(), suite.state.Storage, dbAttachment.Thumbnail.Path, "./test/test-mp4-thumbnail.jpg")
|
2022-12-17 07:38:56 +03:00
|
|
|
}
|
|
|
|
|
2024-06-26 18:01:16 +03:00
|
|
|
func (suite *ManagerTestSuite) TestLongerMp4Process() {
|
2022-12-22 13:48:28 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
data := func(_ context.Context) (io.ReadCloser, error) {
|
2022-12-22 13:48:28 +03:00
|
|
|
// load bytes from a test video
|
|
|
|
b, err := os.ReadFile("./test/longer-mp4-original.mp4")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-07-12 12:39:47 +03:00
|
|
|
return io.NopCloser(bytes.NewBuffer(b)), nil
|
2022-12-22 13:48:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
accountID := "01FS1X72SK9ZPW0J1QQ68BD264"
|
|
|
|
|
|
|
|
// process the media with no additional info provided
|
2024-06-26 18:01:16 +03:00
|
|
|
processing, err := suite.manager.CreateMedia(ctx,
|
|
|
|
accountID,
|
|
|
|
data,
|
|
|
|
media.AdditionalMediaInfo{},
|
|
|
|
)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(processing)
|
2022-12-22 13:48:28 +03:00
|
|
|
|
|
|
|
// do a blocking call to fetch the attachment
|
2024-06-26 18:01:16 +03:00
|
|
|
attachment, err := processing.Load(ctx)
|
2022-12-22 13:48:28 +03:00
|
|
|
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
|
2024-06-26 18:01:16 +03:00
|
|
|
suite.Equal(processing.ID(), attachment.ID)
|
2022-12-22 13:48:28 +03:00
|
|
|
suite.Equal(accountID, attachment.AccountID)
|
|
|
|
|
|
|
|
// file meta should be correctly derived from the video
|
|
|
|
suite.Equal(600, attachment.FileMeta.Original.Width)
|
|
|
|
suite.Equal(330, attachment.FileMeta.Original.Height)
|
|
|
|
suite.Equal(198000, attachment.FileMeta.Original.Size)
|
2024-03-04 18:05:24 +03:00
|
|
|
suite.EqualValues(float32(1.8181819), attachment.FileMeta.Original.Aspect)
|
|
|
|
suite.EqualValues(float32(16.6), *attachment.FileMeta.Original.Duration)
|
|
|
|
suite.EqualValues(float32(10), *attachment.FileMeta.Original.Framerate)
|
2024-07-12 12:39:47 +03:00
|
|
|
suite.EqualValues(0xce3a, *attachment.FileMeta.Original.Bitrate)
|
2022-12-22 13:48:28 +03:00
|
|
|
suite.EqualValues(gtsmodel.Small{
|
2023-01-11 14:13:13 +03:00
|
|
|
Width: 512, Height: 281, Size: 143872, Aspect: 1.822064,
|
2022-12-22 13:48:28 +03:00
|
|
|
}, attachment.FileMeta.Small)
|
|
|
|
suite.Equal("video/mp4", attachment.File.ContentType)
|
|
|
|
suite.Equal("image/jpeg", attachment.Thumbnail.ContentType)
|
2024-07-12 12:39:47 +03:00
|
|
|
suite.Equal(109569, attachment.File.FileSize)
|
|
|
|
suite.Equal("LASY{q~qD%_3~qD%ofRjM{ofofRj", attachment.Blurhash)
|
2022-12-22 13:48:28 +03:00
|
|
|
|
|
|
|
// now make sure the attachment is in the database
|
2024-06-26 18:01:16 +03:00
|
|
|
dbAttachment, err := suite.db.GetAttachmentByID(ctx, attachment.ID)
|
2022-12-22 13:48:28 +03:00
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(dbAttachment)
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
// ensure the files contain the expected data.
|
|
|
|
equalFiles(suite.T(), suite.state.Storage, dbAttachment.File.Path, "./test/longer-mp4-processed.mp4")
|
|
|
|
equalFiles(suite.T(), suite.state.Storage, dbAttachment.Thumbnail.Path, "./test/longer-mp4-thumbnail.jpg")
|
2022-12-22 13:48:28 +03:00
|
|
|
}
|
|
|
|
|
2024-06-26 18:01:16 +03:00
|
|
|
func (suite *ManagerTestSuite) TestBirdnestMp4Process() {
|
2023-01-16 18:19:17 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
data := func(_ context.Context) (io.ReadCloser, error) {
|
2023-01-16 18:19:17 +03:00
|
|
|
// load bytes from a test video
|
|
|
|
b, err := os.ReadFile("./test/birdnest-original.mp4")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-07-12 12:39:47 +03:00
|
|
|
return io.NopCloser(bytes.NewBuffer(b)), nil
|
2023-01-16 18:19:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
accountID := "01FS1X72SK9ZPW0J1QQ68BD264"
|
|
|
|
|
|
|
|
// process the media with no additional info provided
|
2024-06-26 18:01:16 +03:00
|
|
|
processing, err := suite.manager.CreateMedia(ctx,
|
|
|
|
accountID,
|
|
|
|
data,
|
|
|
|
media.AdditionalMediaInfo{},
|
|
|
|
)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(processing)
|
2023-01-16 18:19:17 +03:00
|
|
|
|
|
|
|
// do a blocking call to fetch the attachment
|
2024-06-26 18:01:16 +03:00
|
|
|
attachment, err := processing.Load(ctx)
|
2023-01-16 18:19:17 +03:00
|
|
|
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
|
2024-06-26 18:01:16 +03:00
|
|
|
suite.Equal(processing.ID(), attachment.ID)
|
2023-01-16 18:19:17 +03:00
|
|
|
suite.Equal(accountID, attachment.AccountID)
|
|
|
|
|
|
|
|
// file meta should be correctly derived from the video
|
|
|
|
suite.Equal(404, attachment.FileMeta.Original.Width)
|
|
|
|
suite.Equal(720, attachment.FileMeta.Original.Height)
|
|
|
|
suite.Equal(290880, attachment.FileMeta.Original.Size)
|
2024-03-04 18:05:24 +03:00
|
|
|
suite.EqualValues(float32(0.5611111), attachment.FileMeta.Original.Aspect)
|
2024-07-12 12:39:47 +03:00
|
|
|
suite.EqualValues(float32(9.823), *attachment.FileMeta.Original.Duration)
|
2024-03-04 18:05:24 +03:00
|
|
|
suite.EqualValues(float32(30), *attachment.FileMeta.Original.Framerate)
|
2024-07-12 12:39:47 +03:00
|
|
|
suite.EqualValues(0x11844c, *attachment.FileMeta.Original.Bitrate)
|
2023-01-16 18:19:17 +03:00
|
|
|
suite.EqualValues(gtsmodel.Small{
|
|
|
|
Width: 287, Height: 512, Size: 146944, Aspect: 0.5605469,
|
|
|
|
}, attachment.FileMeta.Small)
|
|
|
|
suite.Equal("video/mp4", attachment.File.ContentType)
|
|
|
|
suite.Equal("image/jpeg", attachment.Thumbnail.ContentType)
|
2024-07-12 12:39:47 +03:00
|
|
|
suite.Equal(1409625, attachment.File.FileSize)
|
|
|
|
suite.Equal("LOGb||RjRO.99DRORPaetkV?afMw", attachment.Blurhash)
|
2023-01-16 18:19:17 +03:00
|
|
|
|
|
|
|
// now make sure the attachment is in the database
|
2024-06-26 18:01:16 +03:00
|
|
|
dbAttachment, err := suite.db.GetAttachmentByID(ctx, attachment.ID)
|
2023-01-16 18:19:17 +03:00
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(dbAttachment)
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
// ensure the files contain the expected data.
|
|
|
|
equalFiles(suite.T(), suite.state.Storage, dbAttachment.File.Path, "./test/birdnest-processed.mp4")
|
|
|
|
equalFiles(suite.T(), suite.state.Storage, dbAttachment.Thumbnail.Path, "./test/birdnest-thumbnail.jpg")
|
2023-01-16 18:19:17 +03:00
|
|
|
}
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
func (suite *ManagerTestSuite) TestOpusProcess() {
|
2022-12-22 13:48:28 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
data := func(_ context.Context) (io.ReadCloser, error) {
|
2022-09-24 12:11:47 +03:00
|
|
|
// load bytes from a test image
|
2024-07-12 12:39:47 +03:00
|
|
|
b, err := os.ReadFile("./test/test-opus-original.opus")
|
2022-09-24 12:11:47 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-07-12 12:39:47 +03:00
|
|
|
return io.NopCloser(bytes.NewBuffer(b)), nil
|
2022-09-24 12:11:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
accountID := "01FS1X72SK9ZPW0J1QQ68BD264"
|
|
|
|
|
|
|
|
// process the media with no additional info provided
|
2024-06-26 18:01:16 +03:00
|
|
|
processing, err := suite.manager.CreateMedia(ctx,
|
|
|
|
accountID,
|
|
|
|
data,
|
|
|
|
media.AdditionalMediaInfo{},
|
|
|
|
)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(processing)
|
2022-01-10 20:36:09 +03:00
|
|
|
|
|
|
|
// do a blocking call to fetch the attachment
|
2024-06-26 18:01:16 +03:00
|
|
|
attachment, err := processing.Load(ctx)
|
2022-01-10 20:36:09 +03:00
|
|
|
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
|
2024-06-26 18:01:16 +03:00
|
|
|
suite.Equal(processing.ID(), attachment.ID)
|
2022-01-10 20:36:09 +03:00
|
|
|
suite.Equal(accountID, attachment.AccountID)
|
|
|
|
|
|
|
|
// file meta should be correctly derived from the image
|
|
|
|
suite.EqualValues(gtsmodel.Original{
|
2024-07-12 12:39:47 +03:00
|
|
|
Duration: util.Ptr(float32(122.10006)),
|
|
|
|
Bitrate: util.Ptr(uint64(116426)),
|
2022-01-10 20:36:09 +03:00
|
|
|
}, attachment.FileMeta.Original)
|
2024-07-12 12:39:47 +03:00
|
|
|
suite.Equal("audio/ogg", attachment.File.ContentType)
|
|
|
|
suite.Equal(1776956, attachment.File.FileSize)
|
|
|
|
suite.Empty(attachment.Blurhash)
|
2022-01-10 20:36:09 +03:00
|
|
|
|
|
|
|
// now make sure the attachment is in the database
|
2024-06-26 18:01:16 +03:00
|
|
|
dbAttachment, err := suite.db.GetAttachmentByID(ctx, attachment.ID)
|
2022-01-10 20:36:09 +03:00
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(dbAttachment)
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
// ensure the files contain the expected data.
|
|
|
|
equalFiles(suite.T(), suite.state.Storage, dbAttachment.File.Path, "./test/test-opus-processed.opus")
|
|
|
|
suite.Zero(dbAttachment.Thumbnail.FileSize)
|
2022-01-09 20:41:22 +03:00
|
|
|
}
|
|
|
|
|
2024-06-26 18:01:16 +03:00
|
|
|
func (suite *ManagerTestSuite) TestPngNoAlphaChannelProcess() {
|
2022-04-25 15:45:44 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
data := func(_ context.Context) (io.ReadCloser, error) {
|
2022-04-25 15:45:44 +03:00
|
|
|
// load bytes from a test image
|
|
|
|
b, err := os.ReadFile("./test/test-png-noalphachannel.png")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-07-12 12:39:47 +03:00
|
|
|
return io.NopCloser(bytes.NewBuffer(b)), nil
|
2022-04-25 15:45:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
accountID := "01FS1X72SK9ZPW0J1QQ68BD264"
|
|
|
|
|
|
|
|
// process the media with no additional info provided
|
2024-06-26 18:01:16 +03:00
|
|
|
processing, err := suite.manager.CreateMedia(ctx,
|
|
|
|
accountID,
|
|
|
|
data,
|
|
|
|
media.AdditionalMediaInfo{},
|
|
|
|
)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(processing)
|
2022-04-25 15:45:44 +03:00
|
|
|
|
|
|
|
// do a blocking call to fetch the attachment
|
2024-06-26 18:01:16 +03:00
|
|
|
attachment, err := processing.Load(ctx)
|
2022-04-25 15:45:44 +03:00
|
|
|
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
|
2024-06-26 18:01:16 +03:00
|
|
|
suite.Equal(processing.ID(), attachment.ID)
|
2022-04-25 15:45:44 +03:00
|
|
|
suite.Equal(accountID, attachment.AccountID)
|
|
|
|
|
|
|
|
// file meta should be correctly derived from the image
|
|
|
|
suite.EqualValues(gtsmodel.Original{
|
|
|
|
Width: 186, Height: 187, Size: 34782, Aspect: 0.9946524064171123,
|
|
|
|
}, attachment.FileMeta.Original)
|
|
|
|
suite.EqualValues(gtsmodel.Small{
|
|
|
|
Width: 186, Height: 187, Size: 34782, Aspect: 0.9946524064171123,
|
|
|
|
}, attachment.FileMeta.Small)
|
|
|
|
suite.Equal("image/png", attachment.File.ContentType)
|
|
|
|
suite.Equal("image/jpeg", attachment.Thumbnail.ContentType)
|
|
|
|
suite.Equal(17471, attachment.File.FileSize)
|
2024-07-12 12:39:47 +03:00
|
|
|
suite.Equal("LDQJl?%i-?WG%go#RURP~of3~UxV", attachment.Blurhash)
|
2022-04-25 15:45:44 +03:00
|
|
|
|
|
|
|
// now make sure the attachment is in the database
|
2024-06-26 18:01:16 +03:00
|
|
|
dbAttachment, err := suite.db.GetAttachmentByID(ctx, attachment.ID)
|
2022-04-25 15:45:44 +03:00
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(dbAttachment)
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
// ensure the files contain the expected data.
|
|
|
|
equalFiles(suite.T(), suite.state.Storage, dbAttachment.File.Path, "./test/test-png-noalphachannel-processed.png")
|
|
|
|
equalFiles(suite.T(), suite.state.Storage, dbAttachment.Thumbnail.Path, "./test/test-png-noalphachannel-thumbnail.jpg")
|
2022-04-25 15:45:44 +03:00
|
|
|
}
|
|
|
|
|
2024-06-26 18:01:16 +03:00
|
|
|
func (suite *ManagerTestSuite) TestPngAlphaChannelProcess() {
|
2022-04-25 15:45:44 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
data := func(_ context.Context) (io.ReadCloser, error) {
|
2022-04-25 15:45:44 +03:00
|
|
|
// load bytes from a test image
|
|
|
|
b, err := os.ReadFile("./test/test-png-alphachannel.png")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-07-12 12:39:47 +03:00
|
|
|
return io.NopCloser(bytes.NewBuffer(b)), nil
|
2022-04-25 15:45:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
accountID := "01FS1X72SK9ZPW0J1QQ68BD264"
|
|
|
|
|
|
|
|
// process the media with no additional info provided
|
2024-06-26 18:01:16 +03:00
|
|
|
processing, err := suite.manager.CreateMedia(ctx,
|
|
|
|
accountID,
|
|
|
|
data,
|
|
|
|
media.AdditionalMediaInfo{},
|
|
|
|
)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(processing)
|
2022-04-25 15:45:44 +03:00
|
|
|
|
|
|
|
// do a blocking call to fetch the attachment
|
2024-06-26 18:01:16 +03:00
|
|
|
attachment, err := processing.Load(ctx)
|
2022-04-25 15:45:44 +03:00
|
|
|
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
|
2024-06-26 18:01:16 +03:00
|
|
|
suite.Equal(processing.ID(), attachment.ID)
|
2022-04-25 15:45:44 +03:00
|
|
|
suite.Equal(accountID, attachment.AccountID)
|
|
|
|
|
|
|
|
// file meta should be correctly derived from the image
|
|
|
|
suite.EqualValues(gtsmodel.Original{
|
|
|
|
Width: 186, Height: 187, Size: 34782, Aspect: 0.9946524064171123,
|
|
|
|
}, attachment.FileMeta.Original)
|
|
|
|
suite.EqualValues(gtsmodel.Small{
|
|
|
|
Width: 186, Height: 187, Size: 34782, Aspect: 0.9946524064171123,
|
|
|
|
}, attachment.FileMeta.Small)
|
|
|
|
suite.Equal("image/png", attachment.File.ContentType)
|
|
|
|
suite.Equal("image/jpeg", attachment.Thumbnail.ContentType)
|
|
|
|
suite.Equal(18904, attachment.File.FileSize)
|
2024-07-12 12:39:47 +03:00
|
|
|
suite.Equal("LDQJl?%i-?WG%go#RURP~of3~UxV", attachment.Blurhash)
|
2022-04-25 15:45:44 +03:00
|
|
|
|
|
|
|
// now make sure the attachment is in the database
|
2024-06-26 18:01:16 +03:00
|
|
|
dbAttachment, err := suite.db.GetAttachmentByID(ctx, attachment.ID)
|
2022-04-25 15:45:44 +03:00
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(dbAttachment)
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
// ensure the files contain the expected data.
|
|
|
|
equalFiles(suite.T(), suite.state.Storage, dbAttachment.File.Path, "./test/test-png-alphachannel-processed.png")
|
|
|
|
equalFiles(suite.T(), suite.state.Storage, dbAttachment.Thumbnail.Path, "./test/test-png-alphachannel-thumbnail.jpg")
|
2022-04-25 15:45:44 +03:00
|
|
|
}
|
|
|
|
|
2024-06-26 18:01:16 +03:00
|
|
|
func (suite *ManagerTestSuite) TestSimpleJpegProcessWithCallback() {
|
2022-02-22 15:50:33 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
data := func(_ context.Context) (io.ReadCloser, error) {
|
2022-02-22 15:50:33 +03:00
|
|
|
// load bytes from a test image
|
|
|
|
b, err := os.ReadFile("./test/test-jpeg.jpg")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-07-12 12:39:47 +03:00
|
|
|
return io.NopCloser(bytes.NewBuffer(b)), nil
|
2022-02-22 15:50:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
accountID := "01FS1X72SK9ZPW0J1QQ68BD264"
|
|
|
|
|
|
|
|
// process the media with no additional info provided
|
2024-06-26 18:01:16 +03:00
|
|
|
processing, err := suite.manager.CreateMedia(ctx,
|
|
|
|
accountID,
|
|
|
|
data,
|
|
|
|
media.AdditionalMediaInfo{},
|
|
|
|
)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(processing)
|
2022-02-22 15:50:33 +03:00
|
|
|
|
|
|
|
// do a blocking call to fetch the attachment
|
2024-06-26 18:01:16 +03:00
|
|
|
attachment, err := processing.Load(ctx)
|
2022-02-22 15:50:33 +03:00
|
|
|
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
|
2024-06-26 18:01:16 +03:00
|
|
|
suite.Equal(processing.ID(), attachment.ID)
|
2022-02-22 15:50:33 +03:00
|
|
|
suite.Equal(accountID, attachment.AccountID)
|
|
|
|
|
|
|
|
// file meta should be correctly derived from the image
|
|
|
|
suite.EqualValues(gtsmodel.Original{
|
|
|
|
Width: 1920, Height: 1080, Size: 2073600, Aspect: 1.7777777777777777,
|
|
|
|
}, attachment.FileMeta.Original)
|
|
|
|
suite.EqualValues(gtsmodel.Small{
|
|
|
|
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)
|
2024-07-12 12:39:47 +03:00
|
|
|
suite.Equal("LjCGfG#6RkRn_NvzRjWF?urqV@a$", attachment.Blurhash)
|
2022-02-22 15:50:33 +03:00
|
|
|
|
|
|
|
// now make sure the attachment is in the database
|
2024-06-26 18:01:16 +03:00
|
|
|
dbAttachment, err := suite.db.GetAttachmentByID(ctx, attachment.ID)
|
2022-02-22 15:50:33 +03:00
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(dbAttachment)
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
// ensure the files contain the expected data.
|
|
|
|
equalFiles(suite.T(), suite.state.Storage, dbAttachment.File.Path, "./test/test-jpeg-processed.jpg")
|
|
|
|
equalFiles(suite.T(), suite.state.Storage, dbAttachment.Thumbnail.Path, "./test/test-jpeg-thumbnail.jpg")
|
2022-02-22 15:50:33 +03:00
|
|
|
}
|
|
|
|
|
2024-06-26 18:01:16 +03:00
|
|
|
func (suite *ManagerTestSuite) TestSimpleJpegProcessWithDiskStorage() {
|
2022-01-24 20:11:27 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
data := func(_ context.Context) (io.ReadCloser, error) {
|
2022-01-24 20:11:27 +03:00
|
|
|
// load bytes from a test image
|
|
|
|
b, err := os.ReadFile("./test/test-jpeg.jpg")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-07-12 12:39:47 +03:00
|
|
|
return io.NopCloser(bytes.NewBuffer(b)), nil
|
2022-01-24 20:11:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
accountID := "01FS1X72SK9ZPW0J1QQ68BD264"
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
temp := fmt.Sprintf("./%s/gotosocial-test", os.TempDir())
|
2022-01-24 20:11:27 +03:00
|
|
|
defer os.RemoveAll(temp)
|
|
|
|
|
2024-05-22 12:46:24 +03:00
|
|
|
disk, err := disk.Open(temp, nil)
|
2022-01-24 20:11:27 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-02-13 21:40:48 +03:00
|
|
|
var state state.State
|
|
|
|
|
2024-04-11 12:45:35 +03:00
|
|
|
testrig.StartNoopWorkers(&state)
|
|
|
|
defer testrig.StopWorkers(&state)
|
2023-02-13 21:40:48 +03:00
|
|
|
|
2022-11-24 11:35:46 +03:00
|
|
|
storage := >sstorage.Driver{
|
|
|
|
Storage: disk,
|
|
|
|
}
|
2023-02-13 21:40:48 +03:00
|
|
|
state.Storage = storage
|
|
|
|
state.DB = suite.db
|
2022-11-24 11:35:46 +03:00
|
|
|
|
2023-02-13 21:40:48 +03:00
|
|
|
diskManager := media.NewManager(&state)
|
2022-01-24 20:11:27 +03:00
|
|
|
suite.manager = diskManager
|
|
|
|
|
|
|
|
// process the media with no additional info provided
|
2024-06-26 18:01:16 +03:00
|
|
|
processing, err := suite.manager.CreateMedia(ctx,
|
|
|
|
accountID,
|
|
|
|
data,
|
|
|
|
media.AdditionalMediaInfo{},
|
|
|
|
)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(processing)
|
2022-01-24 20:11:27 +03:00
|
|
|
|
|
|
|
// do a blocking call to fetch the attachment
|
2024-06-26 18:01:16 +03:00
|
|
|
attachment, err := processing.Load(ctx)
|
2022-01-24 20:11:27 +03:00
|
|
|
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
|
2024-06-26 18:01:16 +03:00
|
|
|
suite.Equal(processing.ID(), attachment.ID)
|
2022-01-24 20:11:27 +03:00
|
|
|
suite.Equal(accountID, attachment.AccountID)
|
|
|
|
|
|
|
|
// file meta should be correctly derived from the image
|
|
|
|
suite.EqualValues(gtsmodel.Original{
|
|
|
|
Width: 1920, Height: 1080, Size: 2073600, Aspect: 1.7777777777777777,
|
|
|
|
}, attachment.FileMeta.Original)
|
|
|
|
suite.EqualValues(gtsmodel.Small{
|
|
|
|
Width: 512, Height: 288, Size: 147456, Aspect: 1.7777777777777777,
|
|
|
|
}, attachment.FileMeta.Small)
|
|
|
|
suite.Equal("image/jpeg", attachment.File.ContentType)
|
2022-02-21 13:26:26 +03:00
|
|
|
suite.Equal("image/jpeg", attachment.Thumbnail.ContentType)
|
2022-01-24 20:11:27 +03:00
|
|
|
suite.Equal(269739, attachment.File.FileSize)
|
2024-07-12 12:39:47 +03:00
|
|
|
suite.Equal("LjCGfG#6RkRn_NvzRjWF?urqV@a$", attachment.Blurhash)
|
2022-01-24 20:11:27 +03:00
|
|
|
|
|
|
|
// now make sure the attachment is in the database
|
2024-06-26 18:01:16 +03:00
|
|
|
dbAttachment, err := suite.db.GetAttachmentByID(ctx, attachment.ID)
|
2022-01-24 20:11:27 +03:00
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(dbAttachment)
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
// ensure the files contain the expected data.
|
|
|
|
equalFiles(suite.T(), storage, dbAttachment.File.Path, "./test/test-jpeg-processed.jpg")
|
|
|
|
equalFiles(suite.T(), storage, dbAttachment.Thumbnail.Path, "./test/test-jpeg-thumbnail.jpg")
|
2022-01-24 20:11:27 +03:00
|
|
|
}
|
|
|
|
|
2023-10-25 15:58:45 +03:00
|
|
|
func (suite *ManagerTestSuite) TestSmallSizedMediaTypeDetection_issue2263() {
|
|
|
|
for index, test := range []struct {
|
|
|
|
name string // Test title
|
|
|
|
path string // File path
|
|
|
|
expected string // Expected ContentType
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "big size JPEG",
|
|
|
|
path: "./test/test-jpeg.jpg",
|
|
|
|
expected: "image/jpeg",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "big size PNG",
|
|
|
|
path: "./test/test-png-noalphachannel.png",
|
|
|
|
expected: "image/png",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "small size JPEG",
|
|
|
|
path: "./test/test-jpeg-1x1px-white.jpg",
|
|
|
|
expected: "image/jpeg",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "golden case PNG (big size)",
|
|
|
|
path: "./test/test-png-alphachannel-1x1px.png",
|
|
|
|
expected: "image/png",
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
suite.Run(test.name, func() {
|
|
|
|
ctx, cncl := context.WithTimeout(context.Background(), time.Second*60)
|
|
|
|
defer cncl()
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
data := func(_ context.Context) (io.ReadCloser, error) {
|
2023-10-25 15:58:45 +03:00
|
|
|
// load bytes from a test image
|
|
|
|
b, err := os.ReadFile(test.path)
|
|
|
|
suite.NoError(err, "Test %d: failed during test setup", index+1)
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
return io.NopCloser(bytes.NewBuffer(b)), nil
|
2023-10-25 15:58:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
accountID := "01FS1X72SK9ZPW0J1QQ68BD264"
|
|
|
|
|
|
|
|
// process the media with no additional info provided
|
2024-06-26 18:01:16 +03:00
|
|
|
processing, err := suite.manager.CreateMedia(ctx,
|
|
|
|
accountID,
|
|
|
|
data,
|
|
|
|
media.AdditionalMediaInfo{},
|
|
|
|
)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(processing)
|
|
|
|
|
|
|
|
// Load the attachment (but ignore return).
|
|
|
|
_, err = processing.Load(ctx)
|
|
|
|
suite.NoError(err)
|
2023-10-25 15:58:45 +03:00
|
|
|
|
2023-11-10 21:29:26 +03:00
|
|
|
// fetch the attachment id from the processing media
|
2024-06-26 18:01:16 +03:00
|
|
|
attachment, err := suite.db.GetAttachmentByID(ctx, processing.ID())
|
2023-11-10 21:29:26 +03:00
|
|
|
if err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
2023-10-25 15:58:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// make sure it's got the stuff set on it that we expect
|
|
|
|
// the attachment ID and accountID we expect
|
2024-06-26 18:01:16 +03:00
|
|
|
suite.Equal(processing.ID(), attachment.ID)
|
2023-10-25 15:58:45 +03:00
|
|
|
suite.Equal(accountID, attachment.AccountID)
|
|
|
|
|
|
|
|
actual := attachment.File.ContentType
|
|
|
|
expect := test.expected
|
|
|
|
|
|
|
|
suite.Equal(expect, actual, "Test %d: %s", index+1, test.name)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
func TestManagerTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, &ManagerTestSuite{})
|
2023-11-10 21:29:26 +03:00
|
|
|
}
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
// equalFiles checks whether
|
|
|
|
func equalFiles(t *testing.T, st *storage.Driver, storagePath, testPath string) {
|
|
|
|
b1, err := st.Get(context.Background(), storagePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error reading file %s: %v", storagePath, err)
|
2023-11-10 21:29:26 +03:00
|
|
|
}
|
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
b2, err := os.ReadFile(testPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error reading file %s: %v", testPath, err)
|
|
|
|
}
|
2023-11-10 21:29:26 +03:00
|
|
|
|
2024-07-12 12:39:47 +03:00
|
|
|
if md5.Sum(b1) != md5.Sum(b2) {
|
|
|
|
t.Errorf("%s != %s", storagePath, testPath)
|
|
|
|
}
|
2022-01-09 20:41:22 +03:00
|
|
|
}
|