diff options
| author | 2022-02-12 18:27:58 +0000 | |
|---|---|---|
| committer | 2022-02-12 18:27:58 +0000 | |
| commit | 31935ee206107f077878d3cdb6a26b82436b6893 (patch) | |
| tree | 2d522bf98013dc5a4539133561b645fd7457eb06 /internal/processing/media | |
| parent | [chore] Add nightly mirror to Codeberg.org (#392) (diff) | |
| parent | Go mod tidy (diff) | |
| download | gotosocial-31935ee206107f077878d3cdb6a26b82436b6893.tar.xz | |
Merge pull request #361 from superseriousbusiness/media_refactorv0.2.0
Refactor media handler to allow async media resolution
Diffstat (limited to 'internal/processing/media')
| -rw-r--r-- | internal/processing/media/create.go | 54 | ||||
| -rw-r--r-- | internal/processing/media/media.go | 6 | 
2 files changed, 19 insertions, 41 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 := >smodel.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  } diff --git a/internal/processing/media/media.go b/internal/processing/media/media.go index 9e050fe84..3d4ae5009 100644 --- a/internal/processing/media/media.go +++ b/internal/processing/media/media.go @@ -43,16 +43,16 @@ type Processor interface {  type processor struct {  	tc           typeutils.TypeConverter -	mediaHandler media.Handler +	mediaManager media.Manager  	storage      *kv.KVStore  	db           db.DB  }  // New returns a new media processor. -func New(db db.DB, tc typeutils.TypeConverter, mediaHandler media.Handler, storage *kv.KVStore) Processor { +func New(db db.DB, tc typeutils.TypeConverter, mediaManager media.Manager, storage *kv.KVStore) Processor {  	return &processor{  		tc:           tc, -		mediaHandler: mediaHandler, +		mediaManager: mediaManager,  		storage:      storage,  		db:           db,  	} | 
