gotosocial/internal/media/media.go

212 lines
6.2 KiB
Go
Raw Normal View History

2021-02-28 15:17:18 +01:00
/*
2021-03-01 15:41:43 +01:00
GoToSocial
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
2021-02-28 15:17:18 +01:00
2021-03-01 15:41:43 +01:00
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.
2021-02-28 15:17:18 +01:00
2021-03-01 15:41:43 +01:00
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.
2021-02-28 15:17:18 +01:00
2021-03-01 15:41:43 +01:00
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/>.
2021-02-28 15:17:18 +01:00
*/
package media
2021-03-28 18:48:07 +02:00
import (
"errors"
"fmt"
2021-03-30 16:06:08 +02:00
"strings"
"time"
2021-03-28 18:48:07 +02:00
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
2021-03-29 17:03:25 +02:00
"github.com/superseriousbusiness/gotosocial/internal/db/model"
2021-03-28 18:48:07 +02:00
"github.com/superseriousbusiness/gotosocial/internal/storage"
)
// MediaHandler provides an interface for parsing, storing, and retrieving media objects like photos, videos, and gifs.
type MediaHandler interface {
2021-03-29 17:03:25 +02:00
// SetHeaderOrAvatarForAccountID takes a new header image for an account, checks it out, removes exif data from it,
2021-03-28 18:48:07 +02:00
// puts it in whatever storage backend we're using, sets the relevant fields in the database for the new image,
2021-03-29 17:03:25 +02:00
// and then returns information to the caller about the new header.
2021-03-30 16:06:08 +02:00
SetHeaderOrAvatarForAccountID(img []byte, accountID string, headerOrAvi string) (*model.MediaAttachment, error)
2021-03-28 18:48:07 +02:00
}
type mediaHandler struct {
config *config.Config
db db.DB
storage storage.Storage
log *logrus.Logger
}
func New(config *config.Config, database db.DB, storage storage.Storage, log *logrus.Logger) MediaHandler {
return &mediaHandler{
config: config,
db: database,
storage: storage,
log: log,
}
}
// HeaderInfo wraps the urls at which a Header and a StaticHeader is available from the server.
type HeaderInfo struct {
// URL to the header
Header string
// Static version of the above (eg., a path to a still image if the header is a gif)
HeaderStatic string
}
2021-03-29 17:03:25 +02:00
/*
INTERFACE FUNCTIONS
*/
2021-03-30 13:41:44 +02:00
2021-03-30 16:06:08 +02:00
func (mh *mediaHandler) SetHeaderOrAvatarForAccountID(img []byte, accountID string, headerOrAvi string) (*model.MediaAttachment, error) {
2021-03-28 18:48:07 +02:00
l := mh.log.WithField("func", "SetHeaderForAccountID")
2021-03-29 17:03:25 +02:00
if headerOrAvi != "header" && headerOrAvi != "avatar" {
return nil, errors.New("header or avatar not selected")
}
// make sure we have an image we can handle
2021-03-30 16:06:08 +02:00
contentType, err := parseContentType(img)
2021-03-28 18:48:07 +02:00
if err != nil {
return nil, err
}
2021-03-29 17:03:25 +02:00
if !supportedImageType(contentType) {
return nil, fmt.Errorf("%s is not an accepted image type", contentType)
}
2021-03-28 18:48:07 +02:00
2021-03-30 16:06:08 +02:00
if len(img) == 0 {
return nil, fmt.Errorf("passed reader was of size 0")
}
l.Tracef("read %d bytes of file", len(img))
// process it
ma, err := mh.processHeaderOrAvi(img, contentType, headerOrAvi, accountID)
2021-03-28 18:48:07 +02:00
if err != nil {
2021-03-30 16:06:08 +02:00
return nil, fmt.Errorf("error processing %s: %s", headerOrAvi, err)
2021-03-28 18:48:07 +02:00
}
2021-03-30 16:06:08 +02:00
// set it in the database
if err := mh.db.SetHeaderOrAvatarForAccountID(ma, accountID); err != nil {
return nil, fmt.Errorf("error putting %s in database: %s", headerOrAvi, err)
}
2021-03-28 18:48:07 +02:00
2021-03-30 16:06:08 +02:00
return ma, nil
2021-03-29 17:03:25 +02:00
}
2021-03-28 18:48:07 +02:00
2021-03-29 17:03:25 +02:00
/*
HELPER FUNCTIONS
*/
func (mh *mediaHandler) processHeaderOrAvi(imageBytes []byte, contentType string, headerOrAvi string, accountID string) (*model.MediaAttachment, error) {
2021-03-30 13:41:44 +02:00
var isHeader bool
var isAvatar bool
switch headerOrAvi {
case "header":
isHeader = true
case "avatar":
isAvatar = true
default:
2021-03-29 17:03:25 +02:00
return nil, errors.New("header or avatar not selected")
2021-03-28 18:48:07 +02:00
}
2021-03-29 17:03:25 +02:00
clean := []byte{}
var err error
2021-03-28 18:48:07 +02:00
2021-03-29 17:03:25 +02:00
switch contentType {
case "image/jpeg":
if clean, err = purgeExif(imageBytes); err != nil {
return nil, fmt.Errorf("error cleaning exif data: %s", err)
}
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
2021-03-30 16:06:08 +02:00
default:
return nil, errors.New("media type unrecognized")
2021-03-29 17:03:25 +02:00
}
2021-03-28 18:48:07 +02:00
2021-03-29 17:03:25 +02:00
original, err := deriveImage(clean, contentType)
2021-03-28 18:48:07 +02:00
if err != nil {
2021-03-29 17:03:25 +02:00
return nil, fmt.Errorf("error parsing image: %s", err)
2021-03-28 18:48:07 +02:00
}
2021-03-29 17:03:25 +02:00
small, err := deriveThumbnail(clean, contentType)
2021-03-28 18:48:07 +02:00
if err != nil {
2021-03-29 17:03:25 +02:00
return nil, fmt.Errorf("error deriving thumbnail: %s", err)
2021-03-28 18:48:07 +02:00
}
2021-03-29 17:03:25 +02:00
// 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
2021-03-30 16:06:08 +02:00
extension := strings.Split(contentType, "/")[1]
2021-03-29 17:03:25 +02:00
newMediaID := uuid.NewString()
2021-03-31 15:24:27 +02:00
base := fmt.Sprintf("%s://%s%s", mh.config.StorageConfig.ServeProtocol, mh.config.StorageConfig.ServeHost, mh.config.StorageConfig.ServeBasePath, )
2021-03-30 16:06:08 +02:00
// we store the original...
2021-03-31 15:24:27 +02:00
originalPath := fmt.Sprintf("%s/%s/%s/original/%s.%s", base, accountID, headerOrAvi, newMediaID, extension)
2021-03-29 17:03:25 +02:00
if err := mh.storage.StoreFileAt(originalPath, original.image); err != nil {
return nil, fmt.Errorf("storage error: %s", err)
2021-03-28 18:48:07 +02:00
}
2021-03-30 16:06:08 +02:00
// and a thumbnail...
2021-03-31 15:24:27 +02:00
smallPath := fmt.Sprintf("%s/%s/%s/small/%s.%s", base, accountID, headerOrAvi, newMediaID, extension)
2021-03-29 17:03:25 +02:00
if err := mh.storage.StoreFileAt(smallPath, small.image); err != nil {
return nil, fmt.Errorf("storage error: %s", err)
2021-03-28 18:48:07 +02:00
}
2021-03-29 17:03:25 +02:00
ma := &model.MediaAttachment{
ID: newMediaID,
StatusID: "",
RemoteURL: "",
2021-03-30 16:06:08 +02:00
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
2021-03-30 13:41:44 +02:00
Type: model.FileTypeImage,
2021-03-30 16:06:08 +02:00
FileMeta: model.FileMeta{
Original: model.Original{
2021-03-29 17:03:25 +02:00
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: "",
2021-03-30 13:41:44 +02:00
Blurhash: original.blurhash,
2021-03-29 17:03:25 +02:00
Processing: 2,
File: model.File{
Path: originalPath,
ContentType: contentType,
FileSize: len(original.image),
2021-03-30 16:06:08 +02:00
UpdatedAt: time.Now(),
2021-03-29 17:03:25 +02:00
},
Thumbnail: model.Thumbnail{
Path: smallPath,
ContentType: contentType,
FileSize: len(small.image),
2021-03-30 16:06:08 +02:00
UpdatedAt: time.Now(),
2021-03-29 17:03:25 +02:00
RemoteURL: "",
},
2021-03-30 13:41:44 +02:00
Avatar: isAvatar,
Header: isHeader,
2021-03-28 18:48:07 +02:00
}
2021-03-29 17:03:25 +02:00
return ma, nil
}