Working on emojis a bit more

This commit is contained in:
tsmethurst 2021-04-13 22:53:27 +02:00
parent 046c5e91fb
commit de9718c566
8 changed files with 154 additions and 58 deletions

View File

@ -29,6 +29,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/superseriousbusiness/gotosocial/internal/db/gtsmodel"
mastotypes "github.com/superseriousbusiness/gotosocial/internal/mastotypes/mastomodel"
"github.com/superseriousbusiness/gotosocial/internal/media"
"github.com/superseriousbusiness/gotosocial/internal/oauth"
"github.com/superseriousbusiness/gotosocial/internal/util"
)
@ -217,7 +218,7 @@ func (m *accountModule) UpdateAccountAvatar(avatar *multipart.FileHeader, accoun
}
// do the setting
avatarInfo, err := m.mediaHandler.SetHeaderOrAvatarForAccountID(buf.Bytes(), accountID, "avatar")
avatarInfo, err := m.mediaHandler.ProcessHeaderOrAvatar(buf.Bytes(), accountID, media.MediaAvatar)
if err != nil {
return nil, fmt.Errorf("error processing avatar: %s", err)
}
@ -250,7 +251,7 @@ func (m *accountModule) UpdateAccountHeader(header *multipart.FileHeader, accoun
}
// do the setting
headerInfo, err := m.mediaHandler.SetHeaderOrAvatarForAccountID(buf.Bytes(), accountID, "header")
headerInfo, err := m.mediaHandler.ProcessHeaderOrAvatar(buf.Bytes(), accountID, media.MediaHeader)
if err != nil {
return nil, fmt.Errorf("error processing header: %s", err)
}

View File

@ -39,7 +39,7 @@ const (
shortcodeKey = "shortcode"
emojisPath = "emojis"
filesPath = "files"
filesPath = "files"
)
// fileServer implements the RESTAPIModule interface.

View File

@ -75,19 +75,16 @@ func (m *fileServer) ServeFile(c *gin.Context) {
// Only serve media types that are defined in our internal media module
switch mediaType {
case media.MediaHeader:
case media.MediaAvatar:
case media.MediaAttachment:
case media.MediaHeader, media.MediaAvatar, media.MediaAttachment, media.MediaEmoji:
default:
l.Debugf("mediatype %s not recognized", mediaType)
c.String(http.StatusNotFound, "404 page not found")
return
}
// This corresponds to original-sized image as it was uploaded, or small, which is the thumbnail
// This corresponds to original-sized image as it was uploaded, small (which is the thumbnail), or static
switch mediaSize {
case media.MediaOriginal:
case media.MediaSmall:
case media.MediaOriginal, media.MediaSmall, media.MediaStatic:
default:
l.Debugf("mediasize %s not recognized", mediaSize)
c.String(http.StatusNotFound, "404 page not found")

View File

@ -88,7 +88,7 @@ func (m *mediaModule) mediaCreatePOSTHandler(c *gin.Context) {
}
// allow the mediaHandler to work its magic of processing the attachment bytes, and putting them in whatever storage backend we're using
attachment, err := m.mediaHandler.ProcessAttachment(buf.Bytes(), authed.Account.ID)
attachment, err := m.mediaHandler.ProcessLocalAttachment(buf.Bytes(), authed.Account.ID)
if err != nil {
l.Debugf("error reading attachment: %s", err)
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("could not process attachment: %s", err)})

View File

@ -317,12 +317,12 @@ func (ps *postgresService) CreateInstanceAccount() error {
instanceAccount := &gtsmodel.Account{
Username: username,
}
inserted, err := ps.conn.Model(instanceAccount).Where("username = ?", username).SelectOrInsert();
inserted, err := ps.conn.Model(instanceAccount).Where("username = ?", username).SelectOrInsert()
if err != nil {
return err
}
if inserted {
ps.log.Infof("created instance account %s with id %s",username, instanceAccount.ID)
ps.log.Infof("created instance account %s with id %s", username, instanceAccount.ID)
} else {
ps.log.Infof("instance account %s already exists with id %s", username, instanceAccount.ID)
}

View File

@ -35,23 +35,26 @@ import (
const (
MediaSmall = "small"
MediaOriginal = "original"
MediaStatic = "static"
MediaAttachment = "attachment"
MediaHeader = "header"
MediaAvatar = "avatar"
MediaEmoji = "emoji"
emojiMaxBytes = 51200
)
// MediaHandler provides an interface for parsing, storing, and retrieving media objects like photos, videos, and gifs.
type MediaHandler interface {
// SetHeaderOrAvatarForAccountID takes a new header image for an account, checks it out, removes exif data from it,
// ProcessHeaderOrAvatar 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.
SetHeaderOrAvatarForAccountID(img []byte, accountID string, headerOrAvi string) (*gtsmodel.MediaAttachment, error)
ProcessHeaderOrAvatar(img []byte, accountID string, headerOrAvi string) (*gtsmodel.MediaAttachment, error)
// ProcessAttachment takes a new attachment and the requesting account, checks it out, removes exif data from it,
// ProcessLocalAttachment takes a new attachment and the requesting 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 media,
// and then returns information to the caller about the attachment.
ProcessAttachment(attachment []byte, accountID string) (*gtsmodel.MediaAttachment, error)
ProcessLocalAttachment(attachment []byte, accountID string) (*gtsmodel.MediaAttachment, error)
}
type mediaHandler struct {
@ -74,7 +77,7 @@ func New(config *config.Config, database db.DB, storage storage.Storage, log *lo
INTERFACE FUNCTIONS
*/
func (mh *mediaHandler) SetHeaderOrAvatarForAccountID(attachment []byte, accountID string, headerOrAvi string) (*gtsmodel.MediaAttachment, error) {
func (mh *mediaHandler) ProcessHeaderOrAvatar(attachment []byte, accountID string, headerOrAvi string) (*gtsmodel.MediaAttachment, error) {
l := mh.log.WithField("func", "SetHeaderForAccountID")
if headerOrAvi != MediaHeader && headerOrAvi != MediaAvatar {
@ -109,7 +112,7 @@ func (mh *mediaHandler) SetHeaderOrAvatarForAccountID(attachment []byte, account
return ma, nil
}
func (mh *mediaHandler) ProcessAttachment(attachment []byte, accountID string) (*gtsmodel.MediaAttachment, error) {
func (mh *mediaHandler) ProcessLocalAttachment(attachment []byte, accountID string) (*gtsmodel.MediaAttachment, error) {
contentType, err := parseContentType(attachment)
if err != nil {
return nil, err
@ -126,7 +129,7 @@ func (mh *mediaHandler) ProcessAttachment(attachment []byte, accountID string) (
if len(attachment) > mh.config.MediaConfig.MaxVideoSize {
return nil, fmt.Errorf("video size %d bytes exceeded max video size of %d bytes", len(attachment), mh.config.MediaConfig.MaxVideoSize)
}
return mh.processVideo(attachment, accountID, contentType)
return mh.processVideoAttachment(attachment, accountID, contentType)
case "image":
if !supportedImageType(contentType) {
return nil, fmt.Errorf("image type %s not supported", contentType)
@ -137,7 +140,7 @@ func (mh *mediaHandler) ProcessAttachment(attachment []byte, accountID string) (
if len(attachment) > mh.config.MediaConfig.MaxImageSize {
return nil, fmt.Errorf("image size %d bytes exceeded max image size of %d bytes", len(attachment), mh.config.MediaConfig.MaxImageSize)
}
return mh.processImage(attachment, accountID, contentType)
return mh.processImageAttachment(attachment, accountID, contentType)
default:
break
}
@ -145,51 +148,110 @@ func (mh *mediaHandler) ProcessAttachment(attachment []byte, accountID string) (
}
func (mh *mediaHandler) ProcessLocalEmoji(emojiBytes []byte, shortcode string) (*gtsmodel.Emoji, error) {
var clean []byte
var err error
var original *imageAndMeta
var static *imageAndMeta
// check content type of the submitted emoji and make sure it's supported by us
contentType, err := parseContentType(emojiBytes)
if err != nil {
return nil, err
}
if !supportedEmojiType(contentType) {
return nil, fmt.Errorf("content type %s not supported for emojis", contentType)
}
newEmojiID := uuid.NewString()
if len(emojiBytes) == 0 {
return nil, errors.New("emoji was of size 0")
}
if len(emojiBytes) > emojiMaxBytes {
return nil, fmt.Errorf("emoji size %d bytes exceeded max emoji size of %d bytes", len(emojiBytes), emojiMaxBytes)
}
// clean any exif data from image/png type but leave gifs alone
switch contentType {
case "image/png":
if clean, err = purgeExif(emojiBytes); err != nil {
return nil, fmt.Errorf("error cleaning exif data: %s", err)
}
case "image/gif":
clean = emojiBytes
default:
return nil, errors.New("media type unrecognized")
}
// unlike with other attachments we don't need to derive anything here because we don't care about the width/height etc
original = &imageAndMeta{
image: clean,
}
static, err = deriveStaticEmoji(clean, contentType)
if err != nil {
return nil, fmt.Errorf("error deriving static emoji: %s", err)
}
// since emoji aren't 'owned' by an account, but we still want to use the same pattern for serving them through the filserver,
// (ie., fileserver/ACCOUNT_ID/etc etc) we need to fetch the INSTANCE ACCOUNT from the database. That is, the account that's created
// with the same username as the instance hostname, which doesn't belong to any particular user.
instanceAccount := &gtsmodel.Account{}
if err := mh.db.GetWhere("username", mh.config.Host, instanceAccount); err != nil {
return nil, fmt.Errorf("error fetching instance account: %s", err)
}
instanceAccountID := instanceAccount.ID
// the file extension (either png or gif)
extension := strings.Split(contentType, "/")[1]
// create the urls and storage paths
URLbase := fmt.Sprintf("%s://%s%s", mh.config.StorageConfig.ServeProtocol, mh.config.StorageConfig.ServeHost, mh.config.StorageConfig.ServeBasePath)
// generate a uuid for the new emoji -- normally we could let the database do this for us,
// but we need it below so we should create it here instead.
newEmojiID := uuid.NewString()
// webfinger uri for the emoji -- unrelated to actually serving the image
// will be something like https://example.org/emoji/70a7f3d7-7e35-4098-8ce3-9b5e8203bb9c
emojiURI := fmt.Sprintf("%s://%s/%s/%s", mh.config.Protocol, mh.config.Host, MediaEmoji, newEmojiID)
emojiURL := fmt.Sprintf("%s/%s/%s/%s/%s.%s", URLbase, instanceAccountID, MediaEmoji, MediaOriginal, newEmojiID, extension)
emojiPath := fmt.Sprintf("%s/%s/%s/%s/%s.%s", mh.config.StorageConfig.BasePath, instanceAccountID, MediaEmoji, MediaOriginal, newEmojiID, extension)
if err := mh.storage.StoreFileAt(emojiPath, emojiBytes); err != nil {
// serve url and storage path for the original emoji -- can be png or gif
emojiURL := fmt.Sprintf("%s/%s/%s/%s/%s.%s", URLbase, instanceAccount.ID, MediaEmoji, MediaOriginal, newEmojiID, extension)
emojiPath := fmt.Sprintf("%s/%s/%s/%s/%s.%s", mh.config.StorageConfig.BasePath, instanceAccount.ID, MediaEmoji, MediaOriginal, newEmojiID, extension)
// serve url and storage path for the static version -- will always be png
emojiStaticURL := fmt.Sprintf("%s/%s/%s/%s/%s.png", URLbase, instanceAccount.ID, MediaEmoji, MediaStatic, newEmojiID)
emojiStaticPath := fmt.Sprintf("%s/%s/%s/%s/%s.png", mh.config.StorageConfig.BasePath, instanceAccount.ID, MediaEmoji, MediaStatic, newEmojiID)
// store the original
if err := mh.storage.StoreFileAt(emojiPath, original.image); err != nil {
return nil, fmt.Errorf("storage error: %s", err)
}
// store the static
if err := mh.storage.StoreFileAt(emojiPath, static.image); err != nil {
return nil, fmt.Errorf("storage error: %s", err)
}
// and finally return the new emoji data to the caller -- it's up to them what to do with it
e := &gtsmodel.Emoji{
ID: newEmojiID,
Shortcode: shortcode,
Domain: "", // empty because this is a local emoji
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
ImageRemoteURL: "", // empty because this is a local emoji
ImageStaticRemoteURL: "",
ImageURL: emojiURL,
ImageStaticURL: "",
ImagePath: emojiPath,
ImageStaticPath: "",
ImageContentType: contentType,
ImageFileSize: 0,
ImageStaticFileSize: 0,
ImageUpdatedAt: time.Now(),
Disabled: false,
URI: emojiURI,
VisibleInPicker: true,
CategoryID: "", // empty because this is a new emoji -- no category yet
ID: newEmojiID,
Shortcode: shortcode,
Domain: "", // empty because this is a local emoji
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
ImageRemoteURL: "", // empty because this is a local emoji
ImageStaticRemoteURL: "", // empty because this is a local emoji
ImageURL: emojiURL,
ImageStaticURL: emojiStaticURL,
ImagePath: emojiPath,
ImageStaticPath: emojiStaticPath,
ImageContentType: contentType,
ImageFileSize: len(original.image),
ImageStaticFileSize: len(static.image),
ImageUpdatedAt: time.Now(),
Disabled: false,
URI: emojiURI,
VisibleInPicker: true,
CategoryID: "", // empty because this is a new emoji -- no category yet
}
return e, nil
}
@ -198,11 +260,11 @@ func (mh *mediaHandler) ProcessLocalEmoji(emojiBytes []byte, shortcode string) (
HELPER FUNCTIONS
*/
func (mh *mediaHandler) processVideo(data []byte, accountID string, contentType string) (*gtsmodel.MediaAttachment, error) {
func (mh *mediaHandler) processVideoAttachment(data []byte, accountID string, contentType string) (*gtsmodel.MediaAttachment, error) {
return nil, nil
}
func (mh *mediaHandler) processImage(data []byte, accountID string, contentType string) (*gtsmodel.MediaAttachment, error) {
func (mh *mediaHandler) processImageAttachment(data []byte, accountID string, contentType string) (*gtsmodel.MediaAttachment, error) {
var clean []byte
var err error
var original *imageAndMeta

View File

@ -95,7 +95,6 @@ func (suite *MediaTestSuite) SetupSuite() {
storage: suite.mockStorage,
log: log,
}
}
func (suite *MediaTestSuite) TearDownSuite() {
@ -142,7 +141,7 @@ func (suite *MediaTestSuite) TestSetHeaderOrAvatarForAccountID() {
f, err := ioutil.ReadFile("../../testrig/media/test-jpeg.jpg")
assert.Nil(suite.T(), err)
ma, err := suite.mediaHandler.SetHeaderOrAvatarForAccountID(f, "weeeeeee", "header")
ma, err := suite.mediaHandler.ProcessHeaderOrAvatar(f, "weeeeeee", "header")
assert.Nil(suite.T(), err)
suite.log.Debugf("%+v", ma)

View File

@ -88,7 +88,16 @@ func supportedVideoType(mimeType string) bool {
// supportedEmojiType checks that the content type is image/png -- the only type supported for emoji.
func supportedEmojiType(mimeType string) bool {
return mimeType == "image/png"
acceptedEmojiTypes := []string{
"image/gif",
"image/png",
}
for _, accepted := range acceptedEmojiTypes {
if mimeType == accepted {
return true
}
}
return false
}
// purgeExif is a little wrapper for the action of removing exif data from an image.
@ -147,11 +156,11 @@ func deriveGif(b []byte, extension string) (*imageAndMeta, error) {
}, nil
}
func deriveImage(b []byte, extension string) (*imageAndMeta, error) {
func deriveImage(b []byte, contentType string) (*imageAndMeta, error) {
var i image.Image
var err error
switch extension {
switch contentType {
case "image/jpeg":
i, err = jpeg.Decode(bytes.NewReader(b))
if err != nil {
@ -163,7 +172,7 @@ func deriveImage(b []byte, extension string) (*imageAndMeta, error) {
return nil, err
}
default:
return nil, fmt.Errorf("extension %s not recognised", extension)
return nil, fmt.Errorf("content type %s not recognised", contentType)
}
width := i.Bounds().Size().X
@ -191,16 +200,16 @@ func deriveImage(b []byte, extension string) (*imageAndMeta, error) {
}, nil
}
// deriveThumbnail returns a byte slice and metadata for a 256-pixel-width thumbnail
// deriveThumbnail returns a byte slice and metadata for a thumbnail of width x and height y,
// 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, x uint, y uint) (*imageAndMeta, error) {
// 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, x uint, y uint) (*imageAndMeta, error) {
var i image.Image
var err error
switch extension {
switch contentType {
case "image/jpeg":
i, err = jpeg.Decode(bytes.NewReader(b))
if err != nil {
@ -217,7 +226,7 @@ func deriveThumbnail(b []byte, extension string, x uint, y uint) (*imageAndMeta,
return nil, err
}
default:
return nil, fmt.Errorf("extension %s not recognised", extension)
return nil, fmt.Errorf("content type %s not recognised", contentType)
}
thumb := resize.Thumbnail(x, y, i, resize.NearestNeighbor)
@ -239,6 +248,34 @@ func deriveThumbnail(b []byte, extension string, x uint, y uint) (*imageAndMeta,
}, nil
}
func deriveStaticEmoji(b []byte, contentType string) (*imageAndMeta, error) {
var i image.Image
var err error
switch contentType {
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("content type %s not allowed for emoji", contentType)
}
out := &bytes.Buffer{}
if err := png.Encode(out, i); err != nil {
return nil, err
}
return &imageAndMeta{
image: out.Bytes(),
}, nil
}
type imageAndMeta struct {
image []byte
width int