tests are passing, but there's still much to be done

This commit is contained in:
tsmethurst 2022-01-09 18:41:22 +01:00
commit dccf21dd87
18 changed files with 259 additions and 170 deletions

View file

@ -108,7 +108,7 @@ func decodeImage(b []byte, contentType string) (*ImageMeta, error) {
//
// Note that the aspect ratio of the image will be retained,
// so it will not necessarily be a square, even if x and y are set as the same value.
func deriveThumbnail(b []byte, contentType string) (*ImageMeta, error) {
func deriveThumbnail(b []byte, contentType string, createBlurhash bool) (*ImageMeta, error) {
var i image.Image
var err error
@ -138,10 +138,20 @@ func deriveThumbnail(b []byte, contentType string) (*ImageMeta, error) {
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
im := &ImageMeta{
width: width,
height: height,
size: size,
aspect: aspect,
}
if createBlurhash {
tiny := resize.Thumbnail(32, 32, thumb, resize.NearestNeighbor)
bh, err := blurhash.Encode(4, 3, tiny)
if err != nil {
return nil, err
}
im.blurhash = bh
}
out := &bytes.Buffer{}
@ -150,14 +160,10 @@ func deriveThumbnail(b []byte, contentType string) (*ImageMeta, error) {
}); err != nil {
return nil, err
}
return &ImageMeta{
image: out.Bytes(),
width: width,
height: height,
size: size,
aspect: aspect,
blurhash: bh,
}, nil
im.image = out.Bytes()
return im, nil
}
// deriveStaticEmojji takes a given gif or png of an emoji, decodes it, and re-encodes it as a static png.

View file

@ -45,9 +45,9 @@ type Manager interface {
//
// RemoteURL is optional, and can be an empty string. Setting this to a non-empty string indicates that
// the piece of media originated on a remote instance and has been dereferenced to be cached locally.
ProcessMedia(ctx context.Context, data []byte, accountID string, remoteURL string) (*Media, error)
ProcessMedia(ctx context.Context, data []byte, accountID string, ai *AdditionalInfo) (*Media, error)
ProcessEmoji(ctx context.Context, data []byte, accountID string, remoteURL string) (*Media, error)
ProcessEmoji(ctx context.Context, data []byte, accountID string) (*Media, error)
}
type manager struct {
@ -80,7 +80,7 @@ func New(database db.DB, storage *kv.KVStore) (Manager, error) {
INTERFACE FUNCTIONS
*/
func (m *manager) ProcessMedia(ctx context.Context, data []byte, accountID string, remoteURL string) (*Media, error) {
func (m *manager) ProcessMedia(ctx context.Context, data []byte, accountID string, ai *AdditionalInfo) (*Media, error) {
contentType, err := parseContentType(data)
if err != nil {
return nil, err
@ -95,7 +95,7 @@ func (m *manager) ProcessMedia(ctx context.Context, data []byte, accountID strin
switch mainType {
case mimeImage:
media, err := m.preProcessImage(ctx, data, contentType, accountID, remoteURL)
media, err := m.preProcessImage(ctx, data, contentType, accountID, ai)
if err != nil {
return nil, err
}
@ -117,12 +117,12 @@ func (m *manager) ProcessMedia(ctx context.Context, data []byte, accountID strin
}
}
func (m *manager) ProcessEmoji(ctx context.Context, data []byte, accountID string, remoteURL string) (*Media, error) {
func (m *manager) ProcessEmoji(ctx context.Context, data []byte, accountID string) (*Media, error) {
return nil, nil
}
// preProcessImage initializes processing
func (m *manager) preProcessImage(ctx context.Context, data []byte, contentType string, accountID string, remoteURL string) (*Media, error) {
func (m *manager) preProcessImage(ctx context.Context, data []byte, contentType string, accountID string, ai *AdditionalInfo) (*Media, error) {
if !supportedImage(contentType) {
return nil, fmt.Errorf("image type %s not supported", contentType)
}
@ -139,13 +139,24 @@ func (m *manager) preProcessImage(ctx context.Context, data []byte, contentType
extension := strings.Split(contentType, "/")[1]
attachment := &gtsmodel.MediaAttachment{
ID: id,
UpdatedAt: time.Now(),
URL: uris.GenerateURIForAttachment(accountID, string(TypeAttachment), string(SizeOriginal), id, extension),
RemoteURL: remoteURL,
Type: gtsmodel.FileTypeImage,
AccountID: accountID,
Processing: 0,
ID: id,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
StatusID: "",
URL: uris.GenerateURIForAttachment(accountID, string(TypeAttachment), string(SizeOriginal), id, extension),
RemoteURL: "",
Type: gtsmodel.FileTypeImage,
FileMeta: gtsmodel.FileMeta{
Focus: gtsmodel.Focus{
X: 0,
Y: 0,
},
},
AccountID: accountID,
Description: "",
ScheduledStatusID: "",
Blurhash: "",
Processing: 0,
File: gtsmodel.File{
Path: fmt.Sprintf("%s/%s/%s/%s.%s", accountID, TypeAttachment, SizeOriginal, id, extension),
ContentType: contentType,
@ -161,6 +172,49 @@ func (m *manager) preProcessImage(ctx context.Context, data []byte, contentType
Header: false,
}
// check if we have additional info to add to the attachment
if ai != nil {
if ai.CreatedAt != nil {
attachment.CreatedAt = *ai.CreatedAt
}
if ai.StatusID != nil {
attachment.StatusID = *ai.StatusID
}
if ai.RemoteURL != nil {
attachment.RemoteURL = *ai.RemoteURL
}
if ai.Description != nil {
attachment.Description = *ai.Description
}
if ai.ScheduledStatusID != nil {
attachment.ScheduledStatusID = *ai.ScheduledStatusID
}
if ai.Blurhash != nil {
attachment.Blurhash = *ai.Blurhash
}
if ai.Avatar != nil {
attachment.Avatar = *ai.Avatar
}
if ai.Header != nil {
attachment.Header = *ai.Header
}
if ai.FocusX != nil {
attachment.FileMeta.Focus.X = *ai.FocusX
}
if ai.FocusY != nil {
attachment.FileMeta.Focus.Y = *ai.FocusY
}
}
media := &Media{
attachment: attachment,
rawData: data,

View file

@ -1,4 +1,54 @@
/*
GoToSocial
Copyright (C) 2021-2022 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_test
import (
"codeberg.org/gruf/go-store/kv"
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/media"
"github.com/superseriousbusiness/gotosocial/testrig"
)
type MediaManagerStandardTestSuite struct {
suite.Suite
db db.DB
storage *kv.KVStore
manager media.Manager
}
func (suite *MediaManagerStandardTestSuite) SetupSuite() {
testrig.InitTestLog()
testrig.InitTestConfig()
suite.db = testrig.NewTestDB()
suite.storage = testrig.NewTestStorage()
}
func (suite *MediaManagerStandardTestSuite) SetupTest() {
testrig.StandardStorageSetup(suite.storage, "../../testrig/media")
testrig.StandardDBSetup(suite.db, nil)
suite.manager = testrig.NewTestMediaManager(suite.db, suite.storage)
}
func (suite *MediaManagerStandardTestSuite) TearDownTest() {
testrig.StandardDBTeardown(suite.db)
testrig.StandardStorageTeardown(suite.storage)
}

View file

@ -47,7 +47,8 @@ type Media struct {
attachment *gtsmodel.MediaAttachment // will only be set if the media is an attachment
emoji *gtsmodel.Emoji // will only be set if the media is an emoji
rawData []byte
rawData []byte
/*
below fields represent the processing state of the media thumbnail
@ -81,7 +82,15 @@ func (m *Media) Thumb(ctx context.Context) (*ImageMeta, error) {
switch m.thumbstate {
case received:
// we haven't processed a thumbnail for this media yet so do it now
thumb, err := deriveThumbnail(m.rawData, m.attachment.File.ContentType)
// check if we need to create a blurhash or if there's already one set
var createBlurhash bool
if m.attachment.Blurhash == "" {
// no blurhash created yet
createBlurhash = true
}
thumb, err := deriveThumbnail(m.rawData, m.attachment.File.ContentType, createBlurhash)
if err != nil {
m.err = fmt.Errorf("error deriving thumbnail: %s", err)
m.thumbstate = errored
@ -96,7 +105,10 @@ func (m *Media) Thumb(ctx context.Context) (*ImageMeta, error) {
}
// set appropriate fields on the attachment based on the thumbnail we derived
m.attachment.Blurhash = thumb.blurhash
if createBlurhash {
m.attachment.Blurhash = thumb.blurhash
}
m.attachment.FileMeta.Small = gtsmodel.Small{
Width: thumb.width,
Height: thumb.height,
@ -105,7 +117,6 @@ func (m *Media) Thumb(ctx context.Context) (*ImageMeta, error) {
}
m.attachment.Thumbnail.FileSize = thumb.size
// put or update the attachment in the database
if err := putOrUpdateAttachment(ctx, m.database, m.attachment); err != nil {
m.err = err
m.thumbstate = errored
@ -177,8 +188,8 @@ func (m *Media) FullSize(ctx context.Context) (*ImageMeta, error) {
}
m.attachment.File.FileSize = decoded.size
m.attachment.File.UpdatedAt = time.Now()
m.attachment.Processing = gtsmodel.ProcessingStatusProcessed
// put or update the attachment in the database
if err := putOrUpdateAttachment(ctx, m.database, m.attachment); err != nil {
m.err = err
m.fullSizeState = errored
@ -200,30 +211,6 @@ func (m *Media) FullSize(ctx context.Context) (*ImageMeta, error) {
return nil, fmt.Errorf("full size processing status %d unknown", m.fullSizeState)
}
func (m *Media) SetAsAvatar(ctx context.Context) error {
m.mu.Lock()
defer m.mu.Unlock()
m.attachment.Avatar = true
return putOrUpdateAttachment(ctx, m.database, m.attachment)
}
func (m *Media) SetAsHeader(ctx context.Context) error {
m.mu.Lock()
defer m.mu.Unlock()
m.attachment.Header = true
return putOrUpdateAttachment(ctx, m.database, m.attachment)
}
func (m *Media) SetStatusID(ctx context.Context, statusID string) error {
m.mu.Lock()
defer m.mu.Unlock()
m.attachment.StatusID = statusID
return putOrUpdateAttachment(ctx, m.database, m.attachment)
}
// AttachmentID returns the ID of the underlying media attachment without blocking processing.
func (m *Media) AttachmentID() string {
return m.attachment.ID
@ -237,8 +224,8 @@ func (m *Media) preLoad(ctx context.Context) {
go m.FullSize(ctx)
}
// Load is the blocking equivalent of pre-load. It makes sure the thumbnail and full-size image
// have been processed, then it returns the full-size image.
// Load is the blocking equivalent of pre-load. It makes sure the thumbnail and full-size
// image have been processed, then it returns the completed attachment.
func (m *Media) LoadAttachment(ctx context.Context) (*gtsmodel.MediaAttachment, error) {
if _, err := m.Thumb(ctx); err != nil {
return nil, err
@ -255,6 +242,8 @@ func (m *Media) LoadEmoji(ctx context.Context) (*gtsmodel.Emoji, error) {
return nil, nil
}
// putOrUpdateAttachment is just a convenience function for first trying to PUT the attachment in the database,
// and then if that doesn't work because the attachment already exists, updating it instead.
func putOrUpdateAttachment(ctx context.Context, database db.DB, attachment *gtsmodel.MediaAttachment) error {
if err := database.Put(ctx, attachment); err != nil {
if err != db.ErrAlreadyExists {

View file

@ -1,65 +0,0 @@
/*
GoToSocial
Copyright (C) 2021-2022 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_test
import (
"testing"
"codeberg.org/gruf/go-store/kv"
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/media"
"github.com/superseriousbusiness/gotosocial/testrig"
)
type MediaStandardTestSuite struct {
suite.Suite
db db.DB
storage *kv.KVStore
manager media.Manager
}
func (suite *MediaStandardTestSuite) SetupSuite() {
testrig.InitTestLog()
testrig.InitTestConfig()
suite.db = testrig.NewTestDB()
suite.storage = testrig.NewTestStorage()
}
func (suite *MediaStandardTestSuite) SetupTest() {
testrig.StandardStorageSetup(suite.storage, "../../testrig/media")
testrig.StandardDBSetup(suite.db, nil)
m, err := media.New(suite.db, suite.storage)
if err != nil {
panic(err)
}
suite.manager = m
}
func (suite *MediaStandardTestSuite) TearDownTest() {
testrig.StandardDBTeardown(suite.db)
testrig.StandardStorageTeardown(suite.storage)
}
func TestMediaStandardTestSuite(t *testing.T) {
suite.Run(t, &MediaStandardTestSuite{})
}

View file

@ -22,6 +22,7 @@ import (
"bytes"
"errors"
"fmt"
"time"
"github.com/h2non/filetype"
)
@ -67,6 +68,31 @@ const (
TypeEmoji Type = "emoji" // TypeEmoji is the key for emoji type requests
)
// AdditionalInfo represents additional information that should be added to an attachment
// when processing a piece of media.
type AdditionalInfo struct {
// Time that this media was created; defaults to time.Now().
CreatedAt *time.Time
// ID of the status to which this media is attached; defaults to "".
StatusID *string
// URL of the media on a remote instance; defaults to "".
RemoteURL *string
// Image description of this media; defaults to "".
Description *string
// Blurhash of this media; defaults to "".
Blurhash *string
// ID of the scheduled status to which this media is attached; defaults to "".
ScheduledStatusID *string
// Mark this media as in-use as an avatar; defaults to false.
Avatar *bool
// Mark this media as in-use as a header; defaults to false.
Header *bool
// X focus coordinate for this media; defaults to 0.
FocusX *float32
// Y focus coordinate for this media; defaults to 0.
FocusY *float32
}
// 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(content []byte) (string, error) {