[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:
parent
e55382acd6
commit
23034ec145
|
@ -19,7 +19,7 @@
|
||||||
package fileserver
|
package fileserver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
@ -91,6 +91,15 @@ func (m *FileServer) ServeFile(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
// if the content is a ReadCloser, close it when we're done
|
||||||
|
if closer, ok := content.Content.(io.ReadCloser); ok {
|
||||||
|
if err := closer.Close(); err != nil {
|
||||||
|
l.Errorf("error closing readcloser: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// TODO: if the requester only accepts text/html we should try to serve them *something*.
|
// TODO: if the requester only accepts text/html we should try to serve them *something*.
|
||||||
// This is mostly needed because when sharing a link to a gts-hosted file on something like mastodon, the masto servers will
|
// This is mostly needed because when sharing a link to a gts-hosted file on something like mastodon, the masto servers will
|
||||||
// attempt to look up the content to provide a preview of the link, and they ask for text/html.
|
// attempt to look up the content to provide a preview of the link, and they ask for text/html.
|
||||||
|
@ -100,5 +109,5 @@ func (m *FileServer) ServeFile(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.DataFromReader(http.StatusOK, content.ContentLength, format, bytes.NewReader(content.Content), nil)
|
c.DataFromReader(http.StatusOK, content.ContentLength, format, content.Content, nil)
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,14 +18,16 @@
|
||||||
|
|
||||||
package model
|
package model
|
||||||
|
|
||||||
|
import "io"
|
||||||
|
|
||||||
// Content wraps everything needed to serve a blob of content (some kind of media) through the API.
|
// Content wraps everything needed to serve a blob of content (some kind of media) through the API.
|
||||||
type Content struct {
|
type Content struct {
|
||||||
// MIME content type
|
// MIME content type
|
||||||
ContentType string
|
ContentType string
|
||||||
// ContentLength in bytes
|
// ContentLength in bytes
|
||||||
ContentLength int64
|
ContentLength int64
|
||||||
// Actual content blob
|
// Actual content
|
||||||
Content []byte
|
Content io.Reader
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetContentRequestForm describes a piece of content desired by the caller of the fileserver API.
|
// GetContentRequestForm describes a piece of content desired by the caller of the fileserver API.
|
||||||
|
|
|
@ -83,9 +83,11 @@ func (p *processor) GetFile(ctx context.Context, account *gtsmodel.Account, form
|
||||||
switch mediaSize {
|
switch mediaSize {
|
||||||
case media.SizeOriginal:
|
case media.SizeOriginal:
|
||||||
content.ContentType = e.ImageContentType
|
content.ContentType = e.ImageContentType
|
||||||
|
content.ContentLength = int64(e.ImageFileSize)
|
||||||
storagePath = e.ImagePath
|
storagePath = e.ImagePath
|
||||||
case media.SizeStatic:
|
case media.SizeStatic:
|
||||||
content.ContentType = e.ImageStaticContentType
|
content.ContentType = e.ImageStaticContentType
|
||||||
|
content.ContentLength = int64(e.ImageStaticFileSize)
|
||||||
storagePath = e.ImageStaticPath
|
storagePath = e.ImageStaticPath
|
||||||
default:
|
default:
|
||||||
return nil, gtserror.NewErrorNotFound(fmt.Errorf("media size %s not recognized for emoji", mediaSize))
|
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 {
|
switch mediaSize {
|
||||||
case media.SizeOriginal:
|
case media.SizeOriginal:
|
||||||
content.ContentType = a.File.ContentType
|
content.ContentType = a.File.ContentType
|
||||||
|
content.ContentLength = int64(a.File.FileSize)
|
||||||
storagePath = a.File.Path
|
storagePath = a.File.Path
|
||||||
case media.SizeSmall:
|
case media.SizeSmall:
|
||||||
content.ContentType = a.Thumbnail.ContentType
|
content.ContentType = a.Thumbnail.ContentType
|
||||||
|
content.ContentLength = int64(a.Thumbnail.FileSize)
|
||||||
storagePath = a.Thumbnail.Path
|
storagePath = a.Thumbnail.Path
|
||||||
default:
|
default:
|
||||||
return nil, gtserror.NewErrorNotFound(fmt.Errorf("media size %s not recognized for attachment", mediaSize))
|
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 {
|
if err != nil {
|
||||||
return nil, gtserror.NewErrorNotFound(fmt.Errorf("error retrieving from storage: %s", err))
|
return nil, gtserror.NewErrorNotFound(fmt.Errorf("error retrieving from storage: %s", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
content.ContentLength = int64(len(bytes))
|
content.Content = reader
|
||||||
content.Content = bytes
|
|
||||||
return content, nil
|
return content, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue