From 1dfa7fe0d51b75792db7b0c28ffad7d1f650834d Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Thu, 3 Nov 2022 15:03:12 +0100 Subject: [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 --- internal/media/processingemoji.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'internal/media/processingemoji.go') diff --git a/internal/media/processingemoji.go b/internal/media/processingemoji.go index e1c6f2efb..79bc23998 100644 --- a/internal/media/processingemoji.go +++ b/internal/media/processingemoji.go @@ -193,24 +193,22 @@ func (p *ProcessingEmoji) store(ctx context.Context) error { return nil } - // execute the data function to get the reader out of it - reader, fileSize, err := p.data(ctx) + // execute the data function to get the readcloser out of it + rc, fileSize, err := p.data(ctx) if err != nil { return fmt.Errorf("store: error executing data function: %s", err) } // defer closing the reader when we're done with it defer func() { - if rc, ok := reader.(io.ReadCloser); ok { - if err := rc.Close(); err != nil { - log.Errorf("store: error closing readcloser: %s", err) - } + if err := rc.Close(); err != nil { + log.Errorf("store: error closing readcloser: %s", err) } }() // extract no more than 261 bytes from the beginning of the file -- this is the header firstBytes := make([]byte, maxFileHeaderBytes) - if _, err := reader.Read(firstBytes); err != nil { + if _, err := rc.Read(firstBytes); err != nil { return fmt.Errorf("store: error reading initial %d bytes: %s", maxFileHeaderBytes, err) } @@ -242,7 +240,7 @@ func (p *ProcessingEmoji) store(ctx context.Context) error { p.emoji.ImageContentType = contentType // concatenate the first bytes with the existing bytes still in the reader (thanks Mara) - readerToStore := io.MultiReader(bytes.NewBuffer(firstBytes), reader) + readerToStore := io.MultiReader(bytes.NewBuffer(firstBytes), rc) var maxEmojiSize int64 if p.emoji.Domain == "" { -- cgit v1.2.3