diff options
author | 2022-11-03 15:03:12 +0100 | |
---|---|---|
committer | 2022-11-03 15:03:12 +0100 | |
commit | 1dfa7fe0d51b75792db7b0c28ffad7d1f650834d (patch) | |
tree | 0af4de062d33e6c292d8b42dbf4d13f16125b959 /internal/processing | |
parent | [bugfix] Use []rune to check length of user-submitted text (#948) (diff) | |
download | gotosocial-1dfa7fe0d51b75792db7b0c28ffad7d1f650834d.tar.xz |
[bugfix] Wrap media in read closer (#941)
* use readcloser for content.Content
* call media postdata function no matter what
* return a readcloser from data func
* tidy of logic of readertostore
* fix whoopsie
Diffstat (limited to 'internal/processing')
-rw-r--r-- | internal/processing/account/update.go | 4 | ||||
-rw-r--r-- | internal/processing/admin/createemoji.go | 2 | ||||
-rw-r--r-- | internal/processing/media/create.go | 2 | ||||
-rw-r--r-- | internal/processing/media/getfile.go | 27 | ||||
-rw-r--r-- | internal/processing/media/getfile_test.go | 14 |
5 files changed, 20 insertions, 29 deletions
diff --git a/internal/processing/account/update.go b/internal/processing/account/update.go index f39361c06..2ef3bfe25 100644 --- a/internal/processing/account/update.go +++ b/internal/processing/account/update.go @@ -192,7 +192,7 @@ func (p *processor) UpdateAvatar(ctx context.Context, avatar *multipart.FileHead return nil, fmt.Errorf("UpdateAvatar: avatar with size %d exceeded max image size of %d bytes", avatar.Size, maxImageSize) } - dataFunc := func(innerCtx context.Context) (io.Reader, int64, error) { + dataFunc := func(innerCtx context.Context) (io.ReadCloser, int64, error) { f, err := avatar.Open() return f, avatar.Size, err } @@ -219,7 +219,7 @@ func (p *processor) UpdateHeader(ctx context.Context, header *multipart.FileHead return nil, fmt.Errorf("UpdateHeader: header with size %d exceeded max image size of %d bytes", header.Size, maxImageSize) } - dataFunc := func(innerCtx context.Context) (io.Reader, int64, error) { + dataFunc := func(innerCtx context.Context) (io.ReadCloser, int64, error) { f, err := header.Open() return f, header.Size, err } diff --git a/internal/processing/admin/createemoji.go b/internal/processing/admin/createemoji.go index 93ae17496..a315e144e 100644 --- a/internal/processing/admin/createemoji.go +++ b/internal/processing/admin/createemoji.go @@ -52,7 +52,7 @@ func (p *processor) EmojiCreate(ctx context.Context, account *gtsmodel.Account, emojiURI := uris.GenerateURIForEmoji(emojiID) - data := func(innerCtx context.Context) (io.Reader, int64, error) { + data := func(innerCtx context.Context) (io.ReadCloser, int64, error) { f, err := form.Image.Open() return f, form.Image.Size, err } diff --git a/internal/processing/media/create.go b/internal/processing/media/create.go index eb0c251e9..451a77391 100644 --- a/internal/processing/media/create.go +++ b/internal/processing/media/create.go @@ -30,7 +30,7 @@ import ( ) func (p *processor) Create(ctx context.Context, account *gtsmodel.Account, form *apimodel.AttachmentRequest) (*apimodel.Attachment, gtserror.WithCode) { - data := func(innerCtx context.Context) (io.Reader, int64, error) { + data := func(innerCtx context.Context) (io.ReadCloser, int64, error) { f, err := form.File.Open() return f, form.File.Size, err } diff --git a/internal/processing/media/getfile.go b/internal/processing/media/getfile.go index 693b8685b..522d47435 100644 --- a/internal/processing/media/getfile.go +++ b/internal/processing/media/getfile.go @@ -29,6 +29,7 @@ import ( apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" "github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/log" "github.com/superseriousbusiness/gotosocial/internal/media" "github.com/superseriousbusiness/gotosocial/internal/uris" ) @@ -139,7 +140,7 @@ func (p *processor) getAttachmentContent(ctx context.Context, requestingAccount // if it's the thumbnail that's requested then the user will have to wait a bit while we process the // large version and derive a thumbnail from it, so use the normal recaching procedure: fetch the media, // process it, then return the thumbnail data - data = func(innerCtx context.Context) (io.Reader, int64, error) { + data = func(innerCtx context.Context) (io.ReadCloser, int64, error) { transport, err := p.transportController.NewTransportForUsername(innerCtx, requestingUsername) if err != nil { return nil, 0, err @@ -168,9 +169,9 @@ func (p *processor) getAttachmentContent(ctx context.Context, requestingAccount bufferedReader := bufio.NewReaderSize(pipeReader, int(attachmentContent.ContentLength)) // the caller will read from the buffered reader, so it doesn't matter if they drop out without reading everything - attachmentContent.Content = bufferedReader + attachmentContent.Content = io.NopCloser(bufferedReader) - data = func(innerCtx context.Context) (io.Reader, int64, error) { + data = func(innerCtx context.Context) (io.ReadCloser, int64, error) { transport, err := p.transportController.NewTransportForUsername(innerCtx, requestingUsername) if err != nil { return nil, 0, err @@ -195,17 +196,15 @@ func (p *processor) getAttachmentContent(ctx context.Context, requestingAccount // close the pipewriter after data has been piped into it, so the reader on the other side doesn't block; // we don't need to close the reader here because that's the caller's responsibility postDataCallback = func(innerCtx context.Context) error { - // flush the buffered writer into the buffer of the reader... - if err := bufferedWriter.Flush(); err != nil { - return err - } - - // and close the underlying pipe writer - if err := pipeWriter.Close(); err != nil { - return err - } - - return nil + // close the underlying pipe writer when we're done with it + defer func() { + if err := pipeWriter.Close(); err != nil { + log.Errorf("getAttachmentContent: error closing pipeWriter: %s", err) + } + }() + + // and flush the buffered writer into the buffer of the reader + return bufferedWriter.Flush() } } diff --git a/internal/processing/media/getfile_test.go b/internal/processing/media/getfile_test.go index 6e5271607..ba7269535 100644 --- a/internal/processing/media/getfile_test.go +++ b/internal/processing/media/getfile_test.go @@ -91,10 +91,7 @@ func (suite *GetFileTestSuite) TestGetRemoteFileUncached() { suite.NotNil(content) b, err := io.ReadAll(content.Content) suite.NoError(err) - - if closer, ok := content.Content.(io.Closer); ok { - suite.NoError(closer.Close()) - } + suite.NoError(content.Content.Close()) suite.Equal(suite.testRemoteAttachments[testAttachment.RemoteURL].Data, b) suite.Equal(suite.testRemoteAttachments[testAttachment.RemoteURL].ContentType, content.ContentType) @@ -151,9 +148,7 @@ func (suite *GetFileTestSuite) TestGetRemoteFileUncachedInterrupted() { suite.NoError(err) // close the reader - if closer, ok := content.Content.(io.Closer); ok { - suite.NoError(closer.Close()) - } + suite.NoError(content.Content.Close()) // the attachment should still be updated in the database even though the caller hung up if !testrig.WaitFor(func() bool { @@ -201,10 +196,7 @@ func (suite *GetFileTestSuite) TestGetRemoteFileThumbnailUncached() { suite.NotNil(content) b, err := io.ReadAll(content.Content) suite.NoError(err) - - if closer, ok := content.Content.(io.Closer); ok { - suite.NoError(closer.Close()) - } + suite.NoError(content.Content.Close()) suite.Equal(thumbnailBytes, b) suite.Equal("image/jpeg", content.ContentType) |