mirror of
				https://github.com/superseriousbusiness/gotosocial.git
				synced 2025-11-03 18:42:25 -06:00 
			
		
		
		
	Working on emojis a bit more
This commit is contained in:
		
					parent
					
						
							
								046c5e91fb
							
						
					
				
			
			
				commit
				
					
						de9718c566
					
				
			
		
					 8 changed files with 154 additions and 58 deletions
				
			
		| 
						 | 
					@ -29,6 +29,7 @@ import (
 | 
				
			||||||
	"github.com/gin-gonic/gin"
 | 
						"github.com/gin-gonic/gin"
 | 
				
			||||||
	"github.com/superseriousbusiness/gotosocial/internal/db/gtsmodel"
 | 
						"github.com/superseriousbusiness/gotosocial/internal/db/gtsmodel"
 | 
				
			||||||
	mastotypes "github.com/superseriousbusiness/gotosocial/internal/mastotypes/mastomodel"
 | 
						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/oauth"
 | 
				
			||||||
	"github.com/superseriousbusiness/gotosocial/internal/util"
 | 
						"github.com/superseriousbusiness/gotosocial/internal/util"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
| 
						 | 
					@ -217,7 +218,7 @@ func (m *accountModule) UpdateAccountAvatar(avatar *multipart.FileHeader, accoun
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// do the setting
 | 
						// 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 {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, fmt.Errorf("error processing avatar: %s", err)
 | 
							return nil, fmt.Errorf("error processing avatar: %s", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -250,7 +251,7 @@ func (m *accountModule) UpdateAccountHeader(header *multipart.FileHeader, accoun
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// do the setting
 | 
						// 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 {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, fmt.Errorf("error processing header: %s", err)
 | 
							return nil, fmt.Errorf("error processing header: %s", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -39,7 +39,7 @@ const (
 | 
				
			||||||
	shortcodeKey = "shortcode"
 | 
						shortcodeKey = "shortcode"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	emojisPath = "emojis"
 | 
						emojisPath = "emojis"
 | 
				
			||||||
	filesPath = "files"
 | 
						filesPath  = "files"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// fileServer implements the RESTAPIModule interface.
 | 
					// fileServer implements the RESTAPIModule interface.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -75,19 +75,16 @@ func (m *fileServer) ServeFile(c *gin.Context) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Only serve media types that are defined in our internal media module
 | 
						// Only serve media types that are defined in our internal media module
 | 
				
			||||||
	switch mediaType {
 | 
						switch mediaType {
 | 
				
			||||||
	case media.MediaHeader:
 | 
						case media.MediaHeader, media.MediaAvatar, media.MediaAttachment, media.MediaEmoji:
 | 
				
			||||||
	case media.MediaAvatar:
 | 
					 | 
				
			||||||
	case media.MediaAttachment:
 | 
					 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
		l.Debugf("mediatype %s not recognized", mediaType)
 | 
							l.Debugf("mediatype %s not recognized", mediaType)
 | 
				
			||||||
		c.String(http.StatusNotFound, "404 page not found")
 | 
							c.String(http.StatusNotFound, "404 page not found")
 | 
				
			||||||
		return
 | 
							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 {
 | 
						switch mediaSize {
 | 
				
			||||||
	case media.MediaOriginal:
 | 
						case media.MediaOriginal, media.MediaSmall, media.MediaStatic:
 | 
				
			||||||
	case media.MediaSmall:
 | 
					 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
		l.Debugf("mediasize %s not recognized", mediaSize)
 | 
							l.Debugf("mediasize %s not recognized", mediaSize)
 | 
				
			||||||
		c.String(http.StatusNotFound, "404 page not found")
 | 
							c.String(http.StatusNotFound, "404 page not found")
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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
 | 
						// 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 {
 | 
						if err != nil {
 | 
				
			||||||
		l.Debugf("error reading attachment: %s", err)
 | 
							l.Debugf("error reading attachment: %s", err)
 | 
				
			||||||
		c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("could not process attachment: %s", err)})
 | 
							c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("could not process attachment: %s", err)})
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -317,12 +317,12 @@ func (ps *postgresService) CreateInstanceAccount() error {
 | 
				
			||||||
	instanceAccount := >smodel.Account{
 | 
						instanceAccount := >smodel.Account{
 | 
				
			||||||
		Username: username,
 | 
							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 {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if inserted {
 | 
						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 {
 | 
						} else {
 | 
				
			||||||
		ps.log.Infof("instance account %s already exists with id %s", username, instanceAccount.ID)
 | 
							ps.log.Infof("instance account %s already exists with id %s", username, instanceAccount.ID)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -35,23 +35,26 @@ import (
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
	MediaSmall      = "small"
 | 
						MediaSmall      = "small"
 | 
				
			||||||
	MediaOriginal   = "original"
 | 
						MediaOriginal   = "original"
 | 
				
			||||||
 | 
						MediaStatic     = "static"
 | 
				
			||||||
	MediaAttachment = "attachment"
 | 
						MediaAttachment = "attachment"
 | 
				
			||||||
	MediaHeader     = "header"
 | 
						MediaHeader     = "header"
 | 
				
			||||||
	MediaAvatar     = "avatar"
 | 
						MediaAvatar     = "avatar"
 | 
				
			||||||
	MediaEmoji      = "emoji"
 | 
						MediaEmoji      = "emoji"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						emojiMaxBytes = 51200
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// MediaHandler provides an interface for parsing, storing, and retrieving media objects like photos, videos, and gifs.
 | 
					// MediaHandler provides an interface for parsing, storing, and retrieving media objects like photos, videos, and gifs.
 | 
				
			||||||
type MediaHandler interface {
 | 
					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,
 | 
						// 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.
 | 
						// 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,
 | 
						// 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.
 | 
						// 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 {
 | 
					type mediaHandler struct {
 | 
				
			||||||
| 
						 | 
					@ -74,7 +77,7 @@ func New(config *config.Config, database db.DB, storage storage.Storage, log *lo
 | 
				
			||||||
	INTERFACE FUNCTIONS
 | 
						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")
 | 
						l := mh.log.WithField("func", "SetHeaderForAccountID")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if headerOrAvi != MediaHeader && headerOrAvi != MediaAvatar {
 | 
						if headerOrAvi != MediaHeader && headerOrAvi != MediaAvatar {
 | 
				
			||||||
| 
						 | 
					@ -109,7 +112,7 @@ func (mh *mediaHandler) SetHeaderOrAvatarForAccountID(attachment []byte, account
 | 
				
			||||||
	return ma, nil
 | 
						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)
 | 
						contentType, err := parseContentType(attachment)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
| 
						 | 
					@ -126,7 +129,7 @@ func (mh *mediaHandler) ProcessAttachment(attachment []byte, accountID string) (
 | 
				
			||||||
		if len(attachment) > mh.config.MediaConfig.MaxVideoSize {
 | 
							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 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":
 | 
						case "image":
 | 
				
			||||||
		if !supportedImageType(contentType) {
 | 
							if !supportedImageType(contentType) {
 | 
				
			||||||
			return nil, fmt.Errorf("image type %s not supported", 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 {
 | 
							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 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:
 | 
						default:
 | 
				
			||||||
		break
 | 
							break
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -145,51 +148,110 @@ func (mh *mediaHandler) ProcessAttachment(attachment []byte, accountID string) (
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (mh *mediaHandler) ProcessLocalEmoji(emojiBytes []byte, shortcode string) (*gtsmodel.Emoji, error) {
 | 
					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)
 | 
						contentType, err := parseContentType(emojiBytes)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
					 | 
				
			||||||
	if !supportedEmojiType(contentType) {
 | 
						if !supportedEmojiType(contentType) {
 | 
				
			||||||
		return nil, fmt.Errorf("content type %s not supported for emojis", 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 := >smodel.Account{}
 | 
						instanceAccount := >smodel.Account{}
 | 
				
			||||||
	if err := mh.db.GetWhere("username", mh.config.Host, instanceAccount); err != nil {
 | 
						if err := mh.db.GetWhere("username", mh.config.Host, instanceAccount); err != nil {
 | 
				
			||||||
		return nil, fmt.Errorf("error fetching instance account: %s", err)
 | 
							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]
 | 
						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)
 | 
						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)
 | 
						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)
 | 
						// serve url and storage path for the original emoji -- can be png or gif
 | 
				
			||||||
	if err := mh.storage.StoreFileAt(emojiPath, emojiBytes); err != nil {
 | 
						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)
 | 
							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 := >smodel.Emoji{
 | 
						e := >smodel.Emoji{
 | 
				
			||||||
		ID:               newEmojiID,
 | 
							ID:                   newEmojiID,
 | 
				
			||||||
		Shortcode:        shortcode,
 | 
							Shortcode:            shortcode,
 | 
				
			||||||
		Domain:           "", // empty because this is a local emoji
 | 
							Domain:               "", // empty because this is a local emoji
 | 
				
			||||||
		CreatedAt:        time.Now(),
 | 
							CreatedAt:            time.Now(),
 | 
				
			||||||
		UpdatedAt:        time.Now(),
 | 
							UpdatedAt:            time.Now(),
 | 
				
			||||||
		ImageRemoteURL:   "", // empty because this is a local emoji
 | 
							ImageRemoteURL:       "", // empty because this is a local emoji
 | 
				
			||||||
		ImageStaticRemoteURL: "",
 | 
							ImageStaticRemoteURL: "", // empty because this is a local emoji
 | 
				
			||||||
		ImageURL:         emojiURL,
 | 
							ImageURL:             emojiURL,
 | 
				
			||||||
		ImageStaticURL:   "",
 | 
							ImageStaticURL:       emojiStaticURL,
 | 
				
			||||||
		ImagePath:        emojiPath,
 | 
							ImagePath:            emojiPath,
 | 
				
			||||||
		ImageStaticPath: "",
 | 
							ImageStaticPath:      emojiStaticPath,
 | 
				
			||||||
		ImageContentType: contentType,
 | 
							ImageContentType:     contentType,
 | 
				
			||||||
		ImageFileSize:    0,
 | 
							ImageFileSize:        len(original.image),
 | 
				
			||||||
		ImageStaticFileSize: 0,
 | 
							ImageStaticFileSize:  len(static.image),
 | 
				
			||||||
		ImageUpdatedAt:   time.Now(),
 | 
							ImageUpdatedAt:       time.Now(),
 | 
				
			||||||
		Disabled:         false,
 | 
							Disabled:             false,
 | 
				
			||||||
		URI:              emojiURI,
 | 
							URI:                  emojiURI,
 | 
				
			||||||
		VisibleInPicker:  true,
 | 
							VisibleInPicker:      true,
 | 
				
			||||||
		CategoryID:       "", // empty because this is a new emoji -- no category yet
 | 
							CategoryID:           "", // empty because this is a new emoji -- no category yet
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return e, nil
 | 
						return e, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -198,11 +260,11 @@ func (mh *mediaHandler) ProcessLocalEmoji(emojiBytes []byte, shortcode string) (
 | 
				
			||||||
	HELPER FUNCTIONS
 | 
						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
 | 
						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 clean []byte
 | 
				
			||||||
	var err error
 | 
						var err error
 | 
				
			||||||
	var original *imageAndMeta
 | 
						var original *imageAndMeta
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -95,7 +95,6 @@ func (suite *MediaTestSuite) SetupSuite() {
 | 
				
			||||||
		storage: suite.mockStorage,
 | 
							storage: suite.mockStorage,
 | 
				
			||||||
		log:     log,
 | 
							log:     log,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (suite *MediaTestSuite) TearDownSuite() {
 | 
					func (suite *MediaTestSuite) TearDownSuite() {
 | 
				
			||||||
| 
						 | 
					@ -142,7 +141,7 @@ func (suite *MediaTestSuite) TestSetHeaderOrAvatarForAccountID() {
 | 
				
			||||||
	f, err := ioutil.ReadFile("../../testrig/media/test-jpeg.jpg")
 | 
						f, err := ioutil.ReadFile("../../testrig/media/test-jpeg.jpg")
 | 
				
			||||||
	assert.Nil(suite.T(), err)
 | 
						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)
 | 
						assert.Nil(suite.T(), err)
 | 
				
			||||||
	suite.log.Debugf("%+v", ma)
 | 
						suite.log.Debugf("%+v", ma)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -88,7 +88,16 @@ func supportedVideoType(mimeType string) bool {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// supportedEmojiType checks that the content type is image/png -- the only type supported for emoji.
 | 
					// supportedEmojiType checks that the content type is image/png -- the only type supported for emoji.
 | 
				
			||||||
func supportedEmojiType(mimeType string) bool {
 | 
					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.
 | 
					// 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
 | 
						}, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func deriveImage(b []byte, extension string) (*imageAndMeta, error) {
 | 
					func deriveImage(b []byte, contentType string) (*imageAndMeta, error) {
 | 
				
			||||||
	var i image.Image
 | 
						var i image.Image
 | 
				
			||||||
	var err error
 | 
						var err error
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	switch extension {
 | 
						switch contentType {
 | 
				
			||||||
	case "image/jpeg":
 | 
						case "image/jpeg":
 | 
				
			||||||
		i, err = jpeg.Decode(bytes.NewReader(b))
 | 
							i, err = jpeg.Decode(bytes.NewReader(b))
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
| 
						 | 
					@ -163,7 +172,7 @@ func deriveImage(b []byte, extension string) (*imageAndMeta, error) {
 | 
				
			||||||
			return nil, err
 | 
								return nil, err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	default:
 | 
						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
 | 
						width := i.Bounds().Size().X
 | 
				
			||||||
| 
						 | 
					@ -191,16 +200,16 @@ func deriveImage(b []byte, extension string) (*imageAndMeta, error) {
 | 
				
			||||||
	}, nil
 | 
						}, 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.
 | 
					// 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,
 | 
					// Note that the aspect ratio of the image will be retained,
 | 
				
			||||||
// so it will not necessarily be a square.
 | 
					// so it will not necessarily be a square, even if x and y are set as the same value.
 | 
				
			||||||
func deriveThumbnail(b []byte, extension string, x uint, y uint) (*imageAndMeta, error) {
 | 
					func deriveThumbnail(b []byte, contentType string, x uint, y uint) (*imageAndMeta, error) {
 | 
				
			||||||
	var i image.Image
 | 
						var i image.Image
 | 
				
			||||||
	var err error
 | 
						var err error
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	switch extension {
 | 
						switch contentType {
 | 
				
			||||||
	case "image/jpeg":
 | 
						case "image/jpeg":
 | 
				
			||||||
		i, err = jpeg.Decode(bytes.NewReader(b))
 | 
							i, err = jpeg.Decode(bytes.NewReader(b))
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
| 
						 | 
					@ -217,7 +226,7 @@ func deriveThumbnail(b []byte, extension string, x uint, y uint) (*imageAndMeta,
 | 
				
			||||||
			return nil, err
 | 
								return nil, err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	default:
 | 
						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)
 | 
						thumb := resize.Thumbnail(x, y, i, resize.NearestNeighbor)
 | 
				
			||||||
| 
						 | 
					@ -239,6 +248,34 @@ func deriveThumbnail(b []byte, extension string, x uint, y uint) (*imageAndMeta,
 | 
				
			||||||
	}, nil
 | 
						}, 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 {
 | 
					type imageAndMeta struct {
 | 
				
			||||||
	image    []byte
 | 
						image    []byte
 | 
				
			||||||
	width    int
 | 
						width    int
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue