mess about with media a bit more

This commit is contained in:
tsmethurst
2021-03-29 17:03:25 +02:00
parent 6ed16ace85
commit 39aca2025f
14 changed files with 953 additions and 79 deletions

View File

@ -21,31 +21,22 @@ package media
import (
"errors"
"fmt"
"mime/multipart"
"io"
"github.com/google/uuid"
"github.com/h2non/filetype"
exifremove "github.com/scottleedavis/go-exif-remove"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/db/model"
"github.com/superseriousbusiness/gotosocial/internal/storage"
)
var (
acceptedImageTypes = []string{
"jpeg",
"gif",
"png",
}
)
// MediaHandler provides an interface for parsing, storing, and retrieving media objects like photos, videos, and gifs.
type MediaHandler interface {
// SetHeaderForAccountID takes a new header image for an account, checks it out, removes exif data from it,
// SetHeaderOrAvatarForAccountID takes a new header image for an account, checks it out, removes exif data from it,
// puts it in whatever storage backend we're using, sets the relevant fields in the database for the new image,
// and then returns information to the caller about the new header's web location.
SetHeaderForAccountID(f multipart.File, id string) (*HeaderInfo, error)
// and then returns information to the caller about the new header.
SetHeaderOrAvatarForAccountID(f io.Reader, accountID string, headerOrAvi string) (*model.MediaAttachment, error)
}
type mediaHandler struct {
@ -72,14 +63,24 @@ type HeaderInfo struct {
HeaderStatic string
}
func (mh *mediaHandler) SetHeaderForAccountID(f multipart.File, accountID string) (*HeaderInfo, error) {
/*
INTERFACE FUNCTIONS
*/
func (mh *mediaHandler) SetHeaderOrAvatarForAccountID(f io.Reader, accountID string, headerOrAvi string) (*model.MediaAttachment, error) {
l := mh.log.WithField("func", "SetHeaderForAccountID")
// make sure we can handle this
extension, err := processableHeaderOrAvi(f)
if headerOrAvi != "header" && headerOrAvi != "avatar" {
return nil, errors.New("header or avatar not selected")
}
// make sure we have an image we can handle
contentType, err := parseContentType(f)
if err != nil {
return nil, err
}
if !supportedImageType(contentType) {
return nil, fmt.Errorf("%s is not an accepted image type", contentType)
}
// extract the bytes
imageBytes := []byte{}
@ -89,64 +90,97 @@ func (mh *mediaHandler) SetHeaderForAccountID(f multipart.File, accountID string
}
l.Tracef("read %d bytes of file", size)
// close the open file--we don't need it anymore now we have the bytes
if err := f.Close(); err != nil {
return nil, fmt.Errorf("error closing file: %s", err)
// // close the open file--we don't need it anymore now we have the bytes
// if err := f.Close(); err != nil {
// return nil, fmt.Errorf("error closing file: %s", err)
// }
// process it
return mh.processHeaderOrAvi(imageBytes, contentType, headerOrAvi, accountID)
}
/*
HELPER FUNCTIONS
*/
func (mh *mediaHandler) processHeaderOrAvi(imageBytes []byte, contentType string, headerOrAvi string, accountID string) (*model.MediaAttachment, error) {
if headerOrAvi != "header" && headerOrAvi != "avatar" {
return nil, errors.New("header or avatar not selected")
}
// remove exif data from images because fuck that shit
cleanBytes := []byte{}
if extension == "jpeg" || extension == "png" {
cleanBytes, err = exifremove.Remove(imageBytes)
if err != nil {
return nil, fmt.Errorf("error removing exif from image: %s", err)
clean := []byte{}
var err error
switch contentType {
case "image/jpeg":
if clean, err = purgeExif(imageBytes); err != nil {
return nil, fmt.Errorf("error cleaning exif data: %s", err)
}
} else {
// our only other accepted image type (gif) doesn't need cleaning
cleanBytes = imageBytes
case "image/png":
if clean, err = purgeExif(imageBytes); err != nil {
return nil, fmt.Errorf("error cleaning exif data: %s", err)
}
case "image/gif":
clean = imageBytes
}
original, err := deriveImage(clean, contentType)
if err != nil {
return nil, fmt.Errorf("error parsing image: %s", err)
}
small, err := deriveThumbnail(clean, contentType)
if err != nil {
return nil, fmt.Errorf("error deriving thumbnail: %s", err)
}
// now put it in storage, take a new uuid for the name of the file so we don't store any unnecessary info about it
path := fmt.Sprintf("/%s/media/headers/%s.%s", accountID, uuid.NewString(), extension)
if err := mh.storage.StoreFileAt(path, cleanBytes); err != nil {
newMediaID := uuid.NewString()
originalPath := fmt.Sprintf("/%s/media/%s/original/%s.%s", accountID, headerOrAvi, newMediaID, contentType)
if err := mh.storage.StoreFileAt(originalPath, original.image); err != nil {
return nil, fmt.Errorf("storage error: %s", err)
}
smallPath := fmt.Sprintf("/%s/media/%s/small/%s.%s", accountID, headerOrAvi, newMediaID, contentType)
if err := mh.storage.StoreFileAt(smallPath, small.image); err != nil {
return nil, fmt.Errorf("storage error: %s", err)
}
return nil, nil
}
func processableHeaderOrAvi(f multipart.File) (string, error) {
extension := ""
head := make([]byte, 261)
_, err := f.Read(head)
if err != nil {
return extension, fmt.Errorf("could not read first magic bytes of file: %s", err)
}
kind, err := filetype.Match(head)
if err != nil {
return extension, err
}
if kind == filetype.Unknown || !filetype.IsImage(head) {
return extension, errors.New("filetype is not an image")
}
if !supportedImageType(kind.MIME.Subtype) {
return extension, fmt.Errorf("%s is not an accepted image type", kind.MIME.Value)
}
extension = kind.MIME.Subtype
return extension, nil
}
func supportedImageType(have string) bool {
for _, accepted := range acceptedImageTypes {
if have == accepted {
return true
}
}
return false
ma := &model.MediaAttachment{
ID: newMediaID,
StatusID: "",
RemoteURL: "",
Type: "",
FileMeta: model.ImageFileMeta{
Original: model.ImageOriginal{
Width: original.width,
Height: original.height,
Size: original.size,
Aspect: original.aspect,
},
Small: model.Small{
Width: small.width,
Height: small.height,
Size: small.size,
Aspect: small.aspect,
},
},
AccountID: accountID,
Description: "",
ScheduledStatusID: "",
Blurhash: "",
Processing: 2,
File: model.File{
Path: originalPath,
ContentType: contentType,
FileSize: len(original.image),
},
Thumbnail: model.Thumbnail{
Path: smallPath,
ContentType: contentType,
FileSize: len(small.image),
RemoteURL: "",
},
}
return ma, nil
}

View File

@ -0,0 +1,127 @@
/*
GoToSocial
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
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/>.
*/
package media
import (
"context"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/db/model"
"github.com/superseriousbusiness/gotosocial/internal/storage"
)
type MediaTestSuite struct {
suite.Suite
config *config.Config
log *logrus.Logger
db db.DB
mediaHandler *mediaHandler
mockStorage storage.Storage
}
/*
TEST INFRASTRUCTURE
*/
// SetupSuite sets some variables on the suite that we can use as consts (more or less) throughout
func (suite *MediaTestSuite) SetupSuite() {
// some of our subsequent entities need a log so create this here
log := logrus.New()
log.SetLevel(logrus.TraceLevel)
suite.log = log
// Direct config to local postgres instance
c := config.Empty()
c.DBConfig = &config.DBConfig{
Type: "postgres",
Address: "localhost",
Port: 5432,
User: "postgres",
Password: "postgres",
Database: "postgres",
ApplicationName: "gotosocial",
}
suite.config = c
suite.config.MediaConfig.MaxImageSize = 2 << 20 // 2 megabits
// use an actual database for this, because it's just easier than mocking one out
database, err := db.New(context.Background(), c, log)
if err != nil {
suite.FailNow(err.Error())
}
suite.db = database
suite.mockStorage = &storage.MockStorage{}
// and finally here's the thing we're actually testing!
suite.mediaHandler = &mediaHandler{
config: suite.config,
db: suite.db,
storage: &storage.MockStorage{},
log: log,
}
}
func (suite *MediaTestSuite) TearDownSuite() {
if err := suite.db.Stop(context.Background()); err != nil {
logrus.Panicf("error closing db connection: %s", err)
}
}
// SetupTest creates a db connection and creates necessary tables before each test
func (suite *MediaTestSuite) SetupTest() {
// create all the tables we might need in thie suite
models := []interface{}{
&model.Account{},
&model.MediaAttachment{},
}
for _, m := range models {
if err := suite.db.CreateTable(m); err != nil {
logrus.Panicf("db connection error: %s", err)
}
}
}
// TearDownTest drops tables to make sure there's no data in the db
func (suite *MediaTestSuite) TearDownTest() {
// remove all the tables we might have used so it's clear for the next test
models := []interface{}{
&model.Account{},
&model.MediaAttachment{},
}
for _, m := range models {
if err := suite.db.DropTable(m); err != nil {
logrus.Panicf("error dropping table: %s", err)
}
}
}
/*
ACTUAL TESTS
*/
func TestMediaTestSuite(t *testing.T) {
suite.Run(t, new(MediaTestSuite))
}

View File

@ -3,9 +3,10 @@
package media
import (
multipart "mime/multipart"
io "io"
mock "github.com/stretchr/testify/mock"
model "github.com/superseriousbusiness/gotosocial/internal/db/model"
)
// MockMediaHandler is an autogenerated mock type for the MediaHandler type
@ -13,22 +14,22 @@ type MockMediaHandler struct {
mock.Mock
}
// SetHeaderForAccountID provides a mock function with given fields: f, id
func (_m *MockMediaHandler) SetHeaderForAccountID(f multipart.File, id string) (*HeaderInfo, error) {
ret := _m.Called(f, id)
// SetHeaderOrAvatarForAccountID provides a mock function with given fields: f, accountID, headerOrAvi
func (_m *MockMediaHandler) SetHeaderOrAvatarForAccountID(f io.Reader, accountID string, headerOrAvi string) (*model.MediaAttachment, error) {
ret := _m.Called(f, accountID, headerOrAvi)
var r0 *HeaderInfo
if rf, ok := ret.Get(0).(func(multipart.File, string) *HeaderInfo); ok {
r0 = rf(f, id)
var r0 *model.MediaAttachment
if rf, ok := ret.Get(0).(func(io.Reader, string, string) *model.MediaAttachment); ok {
r0 = rf(f, accountID, headerOrAvi)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*HeaderInfo)
r0 = ret.Get(0).(*model.MediaAttachment)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(multipart.File, string) error); ok {
r1 = rf(f, id)
if rf, ok := ret.Get(1).(func(io.Reader, string, string) error); ok {
r1 = rf(f, accountID, headerOrAvi)
} else {
r1 = ret.Error(1)
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 263 KiB

175
internal/media/util.go Normal file
View File

@ -0,0 +1,175 @@
/*
GoToSocial
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
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/>.
*/
package media
import (
"bytes"
"errors"
"fmt"
"image"
"image/gif"
"image/jpeg"
"image/png"
"io"
"github.com/h2non/filetype"
"github.com/nfnt/resize"
exifremove "github.com/scottleedavis/go-exif-remove"
)
// parseContentType parses the MIME content type from a file, returning it as a string in the form (eg., "image/jpeg").
// Returns an error if the content type is not something we can process.
func parseContentType(f io.Reader) (string, error) {
head := make([]byte, 261)
_, err := f.Read(head)
if err != nil {
return "", fmt.Errorf("could not read first magic bytes of file: %s", err)
}
kind, err := filetype.Match(head)
if err != nil {
return "", err
}
if kind == filetype.Unknown {
return "", errors.New("filetype unknown")
}
return kind.MIME.Value, nil
}
// supportedImageType checks mime type of an image against a slice of accepted types,
// and returns True if the mime type is accepted.
func supportedImageType(mimeType string) bool {
acceptedImageTypes := []string{
"image/jpeg",
"image/gif",
"image/png",
}
for _, accepted := range acceptedImageTypes {
if mimeType == accepted {
return true
}
}
return false
}
// purgeExif is a little wrapper for the action of removing exif data from an image.
// Only pass pngs or jpegs to this function.
func purgeExif(b []byte) ([]byte, error) {
return exifremove.Remove(b)
}
func deriveImage(b []byte, extension string) (*imageAndMeta, error) {
var i image.Image
var err error
switch extension {
case "image/jpeg":
i, err = jpeg.Decode(bytes.NewReader(b))
if err != nil {
return nil, err
}
case "image/png":
i, err = png.Decode(bytes.NewReader(b))
if err != nil {
return nil, err
}
case "image/gif":
i, err = gif.Decode(bytes.NewReader(b))
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("extension %s not recognised", extension)
}
width := i.Bounds().Size().X
height := i.Bounds().Size().Y
size := width * height
aspect := float64(width) / float64(height)
out := &bytes.Buffer{}
if err := jpeg.Encode(out, i, nil); err != nil {
return nil, err
}
return &imageAndMeta{
image: out.Bytes(),
width: width,
height: height,
size: size,
aspect: aspect,
}, nil
}
// deriveThumbnailFromImage returns a byte slice of an 80-pixel-width thumbnail
// of a given jpeg, png, or gif, or an error if something goes wrong.
//
// Note that the aspect ratio of the image will be retained,
// so it will not necessarily be a square.
func deriveThumbnail(b []byte, extension string) (*imageAndMeta, error) {
var i image.Image
var err error
switch extension {
case "image/jpeg":
i, err = jpeg.Decode(bytes.NewReader(b))
if err != nil {
return nil, err
}
case "image/png":
i, err = png.Decode(bytes.NewReader(b))
if err != nil {
return nil, err
}
case "image/gif":
i, err = gif.Decode(bytes.NewReader(b))
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("extension %s not recognised", extension)
}
thumb := resize.Thumbnail(80, 0, i, resize.NearestNeighbor)
width := thumb.Bounds().Size().X
height := thumb.Bounds().Size().Y
size := width * height
aspect := float64(width) / float64(height)
out := &bytes.Buffer{}
if err := jpeg.Encode(out, thumb, nil); err != nil {
return nil, err
}
return &imageAndMeta{
image: out.Bytes(),
width: width,
height: height,
size: size,
aspect: aspect,
}, nil
}
type imageAndMeta struct {
image []byte
width int
height int
size int
aspect float64
}

View File

@ -0,0 +1,75 @@
/*
GoToSocial
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
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/>.
*/
package media
import (
"os"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/suite"
)
type MediaUtilTestSuite struct {
suite.Suite
log *logrus.Logger
}
/*
TEST INFRASTRUCTURE
*/
// SetupSuite sets some variables on the suite that we can use as consts (more or less) throughout
func (suite *MediaUtilTestSuite) SetupSuite() {
// some of our subsequent entities need a log so create this here
log := logrus.New()
log.SetLevel(logrus.TraceLevel)
suite.log = log
}
func (suite *MediaUtilTestSuite) TearDownSuite() {
}
// SetupTest creates a db connection and creates necessary tables before each test
func (suite *MediaUtilTestSuite) SetupTest() {
}
// TearDownTest drops tables to make sure there's no data in the db
func (suite *MediaUtilTestSuite) TearDownTest() {
}
/*
ACTUAL TESTS
*/
func (suite *MediaUtilTestSuite) TestParseContentType() {
f, err := os.Open("./test/test-jpeg.jpg")
if err != nil {
suite.FailNow(err.Error())
}
ct, err := parseContentType(f)
suite.log.Debug(ct)
}
func TestMediaUtilTestSuite(t *testing.T) {
suite.Run(t, new(MediaUtilTestSuite))
}