mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-10-31 20:12:24 -05:00
[bugfix] Close reader gracefully when streaming recache of remote media to fileserver api caller (#1281)
* close pipereader on failed data function * gently slurp the bytes * readability updates * go fmt * tidy up file server tests + add more cases * start moving io wrappers to separate iotools package. Remove use of buffering while piping recache stream Signed-off-by: kim <grufwub@gmail.com> * add license text Signed-off-by: kim <grufwub@gmail.com> Co-authored-by: kim <grufwub@gmail.com>
This commit is contained in:
parent
0871f5d181
commit
6ebdc306ed
8 changed files with 503 additions and 214 deletions
|
|
@ -19,7 +19,9 @@
|
|||
package fileserver
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
|
|
@ -120,5 +122,14 @@ func (m *FileServer) ServeFile(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
c.DataFromReader(http.StatusOK, content.ContentLength, format, content.Content, nil)
|
||||
// try to slurp the first few bytes to make sure we have something
|
||||
b := bytes.NewBuffer(make([]byte, 0, 64))
|
||||
if _, err := io.CopyN(b, content.Content, 64); err != nil {
|
||||
err = fmt.Errorf("ServeFile: error reading from content: %w", err)
|
||||
api.ErrorHandler(c, gtserror.NewErrorNotFound(err, err.Error()), m.processor.InstanceGet)
|
||||
return
|
||||
}
|
||||
|
||||
// we're good, return the slurped bytes + the rest of the content
|
||||
c.DataFromReader(http.StatusOK, content.ContentLength, format, io.MultiReader(b, content.Content), nil)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue