Add file ETag support (fix #138) (#152)

This commit is contained in:
mutantmonkey 2019-01-09 04:28:01 +00:00 committed by Andrei Marcu
parent 10938a3e0b
commit 5f4f16e08b
3 changed files with 20 additions and 11 deletions

View File

@ -29,7 +29,7 @@ func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) {
fileName := c.URLParams["name"]
err := checkFile(fileName)
_, err := checkFile(fileName)
if err == NotFoundErr {
notFoundHandler(c, w, r)
return

View File

@ -6,19 +6,23 @@ import (
"strings"
"github.com/andreimarcu/linx-server/backends"
"github.com/andreimarcu/linx-server/expiry"
"github.com/zenazn/goji/web"
)
func fileServeHandler(c web.C, w http.ResponseWriter, r *http.Request) {
fileName := c.URLParams["name"]
err := checkFile(fileName)
metadata, err := checkFile(fileName)
if err == NotFoundErr {
notFoundHandler(c, w, r)
return
} else if err == backends.BadMetadata {
oopsHandler(c, w, r, RespAUTO, "Corrupt metadata.")
return
} else if err != nil {
oopsHandler(c, w, r, RespAUTO, err.Error())
return
}
if !Config.allowHotlink {
@ -34,6 +38,9 @@ func fileServeHandler(c web.C, w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Security-Policy", Config.fileContentSecurityPolicy)
w.Header().Set("Referrer-Policy", Config.fileReferrerPolicy)
w.Header().Set("Etag", metadata.Sha256sum)
w.Header().Set("Cache-Control", "max-age=0")
fileBackend.ServeFile(fileName, w, r)
}
@ -61,22 +68,24 @@ func staticHandler(c web.C, w http.ResponseWriter, r *http.Request) {
}
}
func checkFile(filename string) error {
_, err := fileBackend.Exists(filename)
func checkFile(filename string) (metadata backends.Metadata, err error) {
_, err = fileBackend.Exists(filename)
if err != nil {
return NotFoundErr
err = NotFoundErr
return
}
expired, err := isFileExpired(filename)
metadata, err = metadataRead(filename)
if err != nil {
return err
return
}
if expired {
if expiry.IsTsExpired(metadata.Expiry) {
fileBackend.Delete(filename)
metaStorageBackend.Delete(filename)
return NotFoundErr
err = NotFoundErr
return
}
return nil
return
}

View File

@ -71,7 +71,7 @@ func createTorrent(fileName string, f io.ReadCloser, r *http.Request) ([]byte, e
func fileTorrentHandler(c web.C, w http.ResponseWriter, r *http.Request) {
fileName := c.URLParams["name"]
err := checkFile(fileName)
_, err := checkFile(fileName)
if err == NotFoundErr {
notFoundHandler(c, w, r)
return