Skip to content

Commit

Permalink
[feature] Stream files via reader (#404)
Browse files Browse the repository at this point in the history
* serve files via reader rather than byte slice

* close readcloser when we're done with it

* cast reader to readcloser
  • Loading branch information
tsmethurst authored Feb 19, 2022
1 parent e55382a commit 23034ec
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
13 changes: 11 additions & 2 deletions internal/api/client/fileserver/servefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package fileserver

import (
"bytes"
"io"
"net/http"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -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.
Expand All @@ -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)
}
6 changes: 4 additions & 2 deletions internal/api/model/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 6 additions & 3 deletions internal/processing/media/getfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
}

0 comments on commit 23034ec

Please sign in to comment.