Unverified Commit 90e5e279 authored by Christopher Speller's avatar Christopher Speller Committed by GitHub

Fix file streaming to close files. (#9168)

parent bae26ec2
......@@ -160,6 +160,7 @@ func getFile(c *Context, w http.ResponseWriter, r *http.Request) {
c.Err.StatusCode = http.StatusNotFound
return
}
defer fileReader.Close()
err = writeFileResponse(info.Name, info.MimeType, info.Size, fileReader, forceDownload, w, r)
if err != nil {
......@@ -195,10 +196,16 @@ func getFileThumbnail(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
if fileReader, err := c.App.FileReader(info.ThumbnailPath); err != nil {
fileReader, err := c.App.FileReader(info.ThumbnailPath)
if err != nil {
c.Err = err
c.Err.StatusCode = http.StatusNotFound
} else if err := writeFileResponse(info.Name, THUMBNAIL_IMAGE_TYPE, 0, fileReader, forceDownload, w, r); err != nil {
return
}
defer fileReader.Close()
err = writeFileResponse(info.Name, THUMBNAIL_IMAGE_TYPE, 0, fileReader, forceDownload, w, r)
if err != nil {
c.Err = err
return
}
......@@ -264,10 +271,15 @@ func getFilePreview(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
if fileReader, err := c.App.FileReader(info.PreviewPath); err != nil {
fileReader, err := c.App.FileReader(info.PreviewPath)
if err != nil {
c.Err = err
c.Err.StatusCode = http.StatusNotFound
} else if err := writeFileResponse(info.Name, PREVIEW_IMAGE_TYPE, 0, fileReader, forceDownload, w, r); err != nil {
}
defer fileReader.Close()
err = writeFileResponse(info.Name, PREVIEW_IMAGE_TYPE, 0, fileReader, forceDownload, w, r)
if err != nil {
c.Err = err
return
}
......@@ -325,10 +337,15 @@ func getPublicFile(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
if fileReader, err := c.App.FileReader(info.Path); err != nil {
fileReader, err := c.App.FileReader(info.Path)
if err != nil {
c.Err = err
c.Err.StatusCode = http.StatusNotFound
} else if err := writeFileResponse(info.Name, info.MimeType, info.Size, fileReader, true, w, r); err != nil {
}
defer fileReader.Close()
err = writeFileResponse(info.Name, info.MimeType, info.Size, fileReader, true, w, r)
if err != nil {
c.Err = err
return
}
......
......@@ -70,7 +70,8 @@ func (a *App) ReadFile(path string) ([]byte, *model.AppError) {
return backend.ReadFile(path)
}
func (a *App) FileReader(path string) (io.Reader, *model.AppError) {
// Caller must close the first return value
func (a *App) FileReader(path string) (io.ReadCloser, *model.AppError) {
backend, err := a.FileBackend()
if err != nil {
return nil, err
......
......@@ -82,6 +82,7 @@ func (b *S3FileBackend) TestConnection() *model.AppError {
return nil
}
// Caller must close the first return value
func (b *S3FileBackend) Reader(path string) (io.ReadCloser, *model.AppError) {
s3Clnt, err := b.s3New()
if err != nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment