trying emoji stuff
This commit is contained in:
@ -144,6 +144,56 @@ func (mh *mediaHandler) ProcessAttachment(attachment []byte, accountID string) (
|
||||
return nil, fmt.Errorf("content type %s not (yet) supported", contentType)
|
||||
}
|
||||
|
||||
func (mh *mediaHandler) ProcessLocalEmoji(emojiBytes []byte, shortcode string) (*gtsmodel.Emoji, error) {
|
||||
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()
|
||||
instanceAccount := >smodel.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
|
||||
extension := strings.Split(contentType, "/")[1]
|
||||
|
||||
URLbase := fmt.Sprintf("%s://%s%s", mh.config.StorageConfig.ServeProtocol, mh.config.StorageConfig.ServeHost, mh.config.StorageConfig.ServeBasePath)
|
||||
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 {
|
||||
return nil, fmt.Errorf("storage error: %s", err)
|
||||
}
|
||||
|
||||
e := >smodel.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
|
||||
}
|
||||
return e, nil
|
||||
}
|
||||
|
||||
/*
|
||||
HELPER FUNCTIONS
|
||||
*/
|
||||
@ -177,7 +227,7 @@ func (mh *mediaHandler) processImage(data []byte, accountID string, contentType
|
||||
return nil, errors.New("media type unrecognized")
|
||||
}
|
||||
|
||||
small, err = deriveThumbnail(clean, contentType)
|
||||
small, err = deriveThumbnail(clean, contentType, 256, 256)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error deriving thumbnail: %s", err)
|
||||
}
|
||||
@ -287,7 +337,7 @@ func (mh *mediaHandler) processHeaderOrAvi(imageBytes []byte, contentType string
|
||||
return nil, fmt.Errorf("error parsing image: %s", err)
|
||||
}
|
||||
|
||||
small, err := deriveThumbnail(clean, contentType)
|
||||
small, err := deriveThumbnail(clean, contentType, 256, 256)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error deriving thumbnail: %s", err)
|
||||
}
|
||||
|
||||
@ -86,6 +86,11 @@ func supportedVideoType(mimeType string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// supportedEmojiType checks that the content type is image/png -- the only type supported for emoji.
|
||||
func supportedEmojiType(mimeType string) bool {
|
||||
return mimeType == "image/png"
|
||||
}
|
||||
|
||||
// purgeExif is a little wrapper for the action of removing exif data from an image.
|
||||
// Only pass pngs or jpegs to this function.
|
||||
func purgeExif(b []byte) ([]byte, error) {
|
||||
@ -191,7 +196,7 @@ func deriveImage(b []byte, extension string) (*imageAndMeta, error) {
|
||||
//
|
||||
// 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) (*imageAndMeta, error) {
|
||||
func deriveThumbnail(b []byte, extension string, x uint, y uint) (*imageAndMeta, error) {
|
||||
var i image.Image
|
||||
var err error
|
||||
|
||||
@ -215,7 +220,7 @@ func deriveThumbnail(b []byte, extension string) (*imageAndMeta, error) {
|
||||
return nil, fmt.Errorf("extension %s not recognised", extension)
|
||||
}
|
||||
|
||||
thumb := resize.Thumbnail(256, 256, i, resize.NearestNeighbor)
|
||||
thumb := resize.Thumbnail(x, y, i, resize.NearestNeighbor)
|
||||
width := thumb.Bounds().Size().X
|
||||
height := thumb.Bounds().Size().Y
|
||||
size := width * height
|
||||
|
||||
@ -121,7 +121,7 @@ func (suite *MediaUtilTestSuite) TestDeriveThumbnailFromJPEG() {
|
||||
assert.Nil(suite.T(), err)
|
||||
|
||||
// clean it up and validate the clean version
|
||||
imageAndMeta, err := deriveThumbnail(b, "image/jpeg")
|
||||
imageAndMeta, err := deriveThumbnail(b, "image/jpeg", 256, 256)
|
||||
assert.Nil(suite.T(), err)
|
||||
|
||||
assert.Equal(suite.T(), 256, imageAndMeta.width)
|
||||
|
||||
Reference in New Issue
Block a user