[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
This commit is contained in:
tobi
2022-11-03 15:03:12 +01:00
committed by GitHub
parent bd05040133
commit 1dfa7fe0d5
15 changed files with 88 additions and 92 deletions

View File

@@ -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 == "" {