mess about with media a bit more
This commit is contained in:
@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user