[feature] Stream files via reader (#404)

* serve files via reader rather than byte slice

* close readcloser when we're done with it

* cast reader to readcloser
This commit is contained in:
tobi
2022-02-19 11:44:56 +01:00
committed by GitHub
parent e55382acd6
commit 23034ec145
3 changed files with 21 additions and 7 deletions

View File

@@ -83,9 +83,11 @@ func (p *processor) GetFile(ctx context.Context, account *gtsmodel.Account, form
switch mediaSize {
case media.SizeOriginal:
content.ContentType = e.ImageContentType
content.ContentLength = int64(e.ImageFileSize)
storagePath = e.ImagePath
case media.SizeStatic:
content.ContentType = e.ImageStaticContentType
content.ContentLength = int64(e.ImageStaticFileSize)
storagePath = e.ImageStaticPath
default:
return nil, gtserror.NewErrorNotFound(fmt.Errorf("media size %s not recognized for emoji", mediaSize))
@@ -101,21 +103,22 @@ func (p *processor) GetFile(ctx context.Context, account *gtsmodel.Account, form
switch mediaSize {
case media.SizeOriginal:
content.ContentType = a.File.ContentType
content.ContentLength = int64(a.File.FileSize)
storagePath = a.File.Path
case media.SizeSmall:
content.ContentType = a.Thumbnail.ContentType
content.ContentLength = int64(a.Thumbnail.FileSize)
storagePath = a.Thumbnail.Path
default:
return nil, gtserror.NewErrorNotFound(fmt.Errorf("media size %s not recognized for attachment", mediaSize))
}
}
bytes, err := p.storage.Get(storagePath)
reader, err := p.storage.GetStream(storagePath)
if err != nil {
return nil, gtserror.NewErrorNotFound(fmt.Errorf("error retrieving from storage: %s", err))
}
content.ContentLength = int64(len(bytes))
content.Content = bytes
content.Content = reader
return content, nil
}