summaryrefslogtreecommitdiff
path: root/internal/api
diff options
context:
space:
mode:
Diffstat (limited to 'internal/api')
-rw-r--r--internal/api/client/fileserver/servefile.go13
-rw-r--r--internal/api/model/content.go6
2 files changed, 15 insertions, 4 deletions
diff --git a/internal/api/client/fileserver/servefile.go b/internal/api/client/fileserver/servefile.go
index 0caa81926..5a884dc47 100644
--- a/internal/api/client/fileserver/servefile.go
+++ b/internal/api/client/fileserver/servefile.go
@@ -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)
}
diff --git a/internal/api/model/content.go b/internal/api/model/content.go
index 754495738..2f38b2351 100644
--- a/internal/api/model/content.go
+++ b/internal/api/model/content.go
@@ -18,14 +18,16 @@
package model
+import "io"
+
// Content wraps everything needed to serve a blob of content (some kind of media) through the API.
type Content struct {
// MIME content type
ContentType string
// ContentLength in bytes
ContentLength int64
- // Actual content blob
- Content []byte
+ // Actual content
+ Content io.Reader
}
// GetContentRequestForm describes a piece of content desired by the caller of the fileserver API.