mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2025-06-05 21:59:39 +02:00
[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:
@@ -19,7 +19,7 @@
|
||||
package fileserver
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -91,6 +91,15 @@ func (m *FileServer) ServeFile(c *gin.Context) {
|
||||
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*.
|
||||
// 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.
|
||||
@@ -100,5 +109,5 @@ func (m *FileServer) ServeFile(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.DataFromReader(http.StatusOK, content.ContentLength, format, bytes.NewReader(content.Content), nil)
|
||||
c.DataFromReader(http.StatusOK, content.ContentLength, format, content.Content, nil)
|
||||
}
|
||||
|
Reference in New Issue
Block a user