shiori/internal/http/response/file.go
Felipe Martin cc7c75116d
refactor: migrate bookmark static pages to new http server (#775)
* migrate bookmark content route to new http server

* new archive page

* remove unused go generate comment

* database mock

* utils cleanup

* unused var

* domains refactor and tests

* fixed secret key type

* redirect to login on ui errors

* fixed archive folder with storage domain

* webroot documentation

* some bookmark route tests

* fixed error in bookmark domain for non existant bookmarks

* centralice errors

* add coverage data to unittests

* added tests, refactor storage to use afero

* removed mock to avoid increasing complexity

* using deps to copy files around

* remove config usage (to deps)

* remove handler-ui file
2023-12-28 18:18:32 +01:00

42 lines
1 KiB
Go

package response
import (
"fmt"
"io"
"net/http"
"github.com/gin-gonic/gin"
"github.com/go-shiori/shiori/internal/model"
)
// SendFile sends file to client with caching header
func SendFile(c *gin.Context, storageDomain model.StorageDomain, path string) {
c.Header("Cache-Control", "public, max-age=86400")
if !storageDomain.FileExists(path) {
c.AbortWithStatus(http.StatusNotFound)
return
}
info, err := storageDomain.Stat(path)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.Header("ETag", fmt.Sprintf("W/%x-%x", info.ModTime().Unix(), info.Size()))
// TODO: Find a better way to send the file to the client from the FS, probably making a
// conversion between afero.Fs and http.FileSystem to use c.FileFromFS.
fileContent, err := storageDomain.FS().Open(path)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
_, err = io.Copy(c.Writer, fileContent)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
}