From 5f4f16e08be7935eba4d4c465ae97eb10aa1af53 Mon Sep 17 00:00:00 2001 From: mutantmonkey Date: Wed, 9 Jan 2019 04:28:01 +0000 Subject: [PATCH] Add file ETag support (fix #138) (#152) --- display.go | 2 +- fileserve.go | 27 ++++++++++++++++++--------- torrent.go | 2 +- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/display.go b/display.go index c17fea6..d897e5a 100644 --- a/display.go +++ b/display.go @@ -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 diff --git a/fileserve.go b/fileserve.go index 951bea2..3b20c4c 100644 --- a/fileserve.go +++ b/fileserve.go @@ -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 } diff --git a/torrent.go b/torrent.go index 23361b5..77cd2fd 100644 --- a/torrent.go +++ b/torrent.go @@ -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