summaryrefslogtreecommitdiff
path: root/internal/processing/media/create.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/processing/media/create.go')
-rw-r--r--internal/processing/media/create.go54
1 files changed, 16 insertions, 38 deletions
diff --git a/internal/processing/media/create.go b/internal/processing/media/create.go
index de15d3162..4047278eb 100644
--- a/internal/processing/media/create.go
+++ b/internal/processing/media/create.go
@@ -19,56 +19,39 @@
package media
import (
- "bytes"
"context"
- "errors"
"fmt"
"io"
- "time"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
- "github.com/superseriousbusiness/gotosocial/internal/text"
+ "github.com/superseriousbusiness/gotosocial/internal/media"
)
func (p *processor) Create(ctx context.Context, account *gtsmodel.Account, form *apimodel.AttachmentRequest) (*apimodel.Attachment, error) {
- // open the attachment and extract the bytes from it
- f, err := form.File.Open()
- if err != nil {
- return nil, fmt.Errorf("error opening attachment: %s", err)
- }
- buf := new(bytes.Buffer)
- size, err := io.Copy(buf, f)
- if err != nil {
- return nil, fmt.Errorf("error reading attachment: %s", err)
- }
- if size == 0 {
- return nil, errors.New("could not read provided attachment: size 0 bytes")
+ data := func(innerCtx context.Context) (io.Reader, int, error) {
+ f, err := form.File.Open()
+ return f, int(form.File.Size), err
}
- // now parse the focus parameter
- focusx, focusy, err := parseFocus(form.Focus)
+ focusX, focusY, err := parseFocus(form.Focus)
if err != nil {
- return nil, fmt.Errorf("couldn't parse attachment focus: %s", err)
+ return nil, fmt.Errorf("could not parse focus value %s: %s", form.Focus, err)
}
- minAttachment := &gtsmodel.MediaAttachment{
- CreatedAt: time.Now(),
- UpdatedAt: time.Now(),
- AccountID: account.ID,
- Description: text.SanitizeCaption(form.Description),
- FileMeta: gtsmodel.FileMeta{
- Focus: gtsmodel.Focus{
- X: focusx,
- Y: focusy,
- },
- },
+ // process the media attachment and load it immediately
+ media, err := p.mediaManager.ProcessMedia(ctx, data, account.ID, &media.AdditionalMediaInfo{
+ Description: &form.Description,
+ FocusX: &focusX,
+ FocusY: &focusY,
+ })
+ if err != nil {
+ return nil, err
}
- // allow the mediaHandler to work its magic of processing the attachment bytes, and putting them in whatever storage backend we're using
- attachment, err := p.mediaHandler.ProcessAttachment(ctx, buf.Bytes(), minAttachment)
+ attachment, err := media.LoadAttachment(ctx)
if err != nil {
- return nil, fmt.Errorf("error reading attachment: %s", err)
+ return nil, err
}
// prepare the frontend representation now -- if there are any errors here at least we can bail without
@@ -78,10 +61,5 @@ func (p *processor) Create(ctx context.Context, account *gtsmodel.Account, form
return nil, fmt.Errorf("error parsing media attachment to frontend type: %s", err)
}
- // now we can confidently put the attachment in the database
- if err := p.db.Put(ctx, attachment); err != nil {
- return nil, fmt.Errorf("error storing media attachment in db: %s", err)
- }
-
return &apiAttachment, nil
}