summaryrefslogtreecommitdiff
path: root/internal/media
diff options
context:
space:
mode:
Diffstat (limited to 'internal/media')
-rw-r--r--internal/media/handler.go13
-rw-r--r--internal/media/processimage.go71
2 files changed, 45 insertions, 39 deletions
diff --git a/internal/media/handler.go b/internal/media/handler.go
index b467100b0..9c1d3227e 100644
--- a/internal/media/handler.go
+++ b/internal/media/handler.go
@@ -73,7 +73,7 @@ type Handler interface {
// 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. It's the caller's responsibility to put the returned struct
// in the database.
- ProcessAttachment(ctx context.Context, attachment []byte, accountID string, remoteURL string) (*gtsmodel.MediaAttachment, error)
+ ProcessAttachment(ctx context.Context, attachmentBytes []byte, minAttachment *gtsmodel.MediaAttachment) (*gtsmodel.MediaAttachment, error)
// ProcessLocalEmoji takes a new emoji and a shortcode, cleans it up, puts it in storage, and creates a new
// *gts.Emoji for it, then returns it to the caller. It's the caller's responsibility to put the returned struct
@@ -145,11 +145,14 @@ func (mh *mediaHandler) ProcessHeaderOrAvatar(ctx context.Context, attachment []
// ProcessAttachment takes a new attachment and the owning 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.
-func (mh *mediaHandler) ProcessAttachment(ctx context.Context, attachment []byte, accountID string, remoteURL string) (*gtsmodel.MediaAttachment, error) {
- contentType, err := parseContentType(attachment)
+func (mh *mediaHandler) ProcessAttachment(ctx context.Context, attachmentBytes []byte, minAttachment *gtsmodel.MediaAttachment) (*gtsmodel.MediaAttachment, error) {
+ contentType, err := parseContentType(attachmentBytes)
if err != nil {
return nil, err
}
+
+ minAttachment.File.ContentType = contentType
+
mainType := strings.Split(contentType, "/")[0]
switch mainType {
// case MIMEVideo:
@@ -164,10 +167,10 @@ func (mh *mediaHandler) ProcessAttachment(ctx context.Context, attachment []byte
if !SupportedImageType(contentType) {
return nil, fmt.Errorf("image type %s not supported", contentType)
}
- if len(attachment) == 0 {
+ if len(attachmentBytes) == 0 {
return nil, errors.New("image was of size 0")
}
- return mh.processImageAttachment(attachment, accountID, contentType, remoteURL)
+ return mh.processImageAttachment(attachmentBytes, minAttachment)
default:
break
}
diff --git a/internal/media/processimage.go b/internal/media/processimage.go
index d4add027a..a8a6d0716 100644
--- a/internal/media/processimage.go
+++ b/internal/media/processimage.go
@@ -28,12 +28,14 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/id"
)
-func (mh *mediaHandler) processImageAttachment(data []byte, accountID string, contentType string, remoteURL string) (*gtsmodel.MediaAttachment, error) {
+func (mh *mediaHandler) processImageAttachment(data []byte, minAttachment *gtsmodel.MediaAttachment) (*gtsmodel.MediaAttachment, error) {
var clean []byte
var err error
var original *imageAndMeta
var small *imageAndMeta
+ contentType := minAttachment.File.ContentType
+
switch contentType {
case MIMEJpeg, MIMEPng:
if clean, err = purgeExif(data); err != nil {
@@ -66,46 +68,47 @@ func (mh *mediaHandler) processImageAttachment(data []byte, accountID string, co
}
URLbase := fmt.Sprintf("%s://%s%s", mh.config.StorageConfig.ServeProtocol, mh.config.StorageConfig.ServeHost, mh.config.StorageConfig.ServeBasePath)
- originalURL := fmt.Sprintf("%s/%s/attachment/original/%s.%s", URLbase, accountID, newMediaID, extension)
- smallURL := fmt.Sprintf("%s/%s/attachment/small/%s.jpeg", URLbase, accountID, newMediaID) // all thumbnails/smalls are encoded as jpeg
+ originalURL := fmt.Sprintf("%s/%s/attachment/original/%s.%s", URLbase, minAttachment.AccountID, newMediaID, extension)
+ smallURL := fmt.Sprintf("%s/%s/attachment/small/%s.jpeg", URLbase, minAttachment.AccountID, newMediaID) // all thumbnails/smalls are encoded as jpeg
// we store the original...
- originalPath := fmt.Sprintf("%s/%s/%s/%s/%s.%s", mh.config.StorageConfig.BasePath, accountID, Attachment, Original, newMediaID, extension)
+ originalPath := fmt.Sprintf("%s/%s/%s/%s/%s.%s", mh.config.StorageConfig.BasePath, minAttachment.AccountID, Attachment, Original, newMediaID, extension)
if err := mh.storage.StoreFileAt(originalPath, original.image); err != nil {
return nil, fmt.Errorf("storage error: %s", err)
}
// and a thumbnail...
- smallPath := fmt.Sprintf("%s/%s/%s/%s/%s.jpeg", mh.config.StorageConfig.BasePath, accountID, Attachment, Small, newMediaID) // all thumbnails/smalls are encoded as jpeg
+ smallPath := fmt.Sprintf("%s/%s/%s/%s/%s.jpeg", mh.config.StorageConfig.BasePath, minAttachment.AccountID, Attachment, Small, newMediaID) // all thumbnails/smalls are encoded as jpeg
if err := mh.storage.StoreFileAt(smallPath, small.image); err != nil {
return nil, fmt.Errorf("storage error: %s", err)
}
- ma := &gtsmodel.MediaAttachment{
- ID: newMediaID,
- StatusID: "",
- URL: originalURL,
- RemoteURL: remoteURL,
- CreatedAt: time.Now(),
- UpdatedAt: time.Now(),
- Type: gtsmodel.FileTypeImage,
- FileMeta: gtsmodel.FileMeta{
- Original: gtsmodel.Original{
- Width: original.width,
- Height: original.height,
- Size: original.size,
- Aspect: original.aspect,
- },
- Small: gtsmodel.Small{
- Width: small.width,
- Height: small.height,
- Size: small.size,
- Aspect: small.aspect,
- },
- },
- AccountID: accountID,
- Description: "",
- ScheduledStatusID: "",
+ minAttachment.FileMeta.Original = gtsmodel.Original{
+ Width: original.width,
+ Height: original.height,
+ Size: original.size,
+ Aspect: original.aspect,
+ }
+
+ minAttachment.FileMeta.Small = gtsmodel.Small{
+ Width: small.width,
+ Height: small.height,
+ Size: small.size,
+ Aspect: small.aspect,
+ }
+
+ attachment := &gtsmodel.MediaAttachment{
+ ID: newMediaID,
+ StatusID: minAttachment.StatusID,
+ URL: originalURL,
+ RemoteURL: minAttachment.RemoteURL,
+ CreatedAt: minAttachment.CreatedAt,
+ UpdatedAt: minAttachment.UpdatedAt,
+ Type: gtsmodel.FileTypeImage,
+ FileMeta: minAttachment.FileMeta,
+ AccountID: minAttachment.AccountID,
+ Description: minAttachment.Description,
+ ScheduledStatusID: minAttachment.ScheduledStatusID,
Blurhash: original.blurhash,
Processing: 2,
File: gtsmodel.File{
@@ -120,12 +123,12 @@ func (mh *mediaHandler) processImageAttachment(data []byte, accountID string, co
FileSize: len(small.image),
UpdatedAt: time.Now(),
URL: smallURL,
- RemoteURL: "",
+ RemoteURL: minAttachment.Thumbnail.RemoteURL,
},
- Avatar: false,
- Header: false,
+ Avatar: minAttachment.Avatar,
+ Header: minAttachment.Header,
}
- return ma, nil
+ return attachment, nil
}