mirror of
https://github.com/mickael-kerjean/filestash.git
synced 2025-11-01 10:56:31 +08:00
improve (compression): on the fly gzip compression for a few endpoints
This commit is contained in:
@ -1,6 +1,8 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"compress/gzip"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
@ -33,17 +35,32 @@ func SendSuccessResult(res http.ResponseWriter, data interface{}) {
|
|||||||
encoder.Encode(APISuccessResult{"ok", data})
|
encoder.Encode(APISuccessResult{"ok", data})
|
||||||
}
|
}
|
||||||
|
|
||||||
func SendSuccessResultWithEtag(res http.ResponseWriter, req *http.Request, data interface{}) {
|
func SendSuccessResultWithEtagAndGzip(res http.ResponseWriter, req *http.Request, data interface{}) {
|
||||||
json, _ := json.Marshal(APISuccessResult{"ok", data})
|
dataToSend, _ := json.Marshal(APISuccessResult{"ok", data})
|
||||||
hash := QuickHash(string(json), 20)
|
mode := "normal"
|
||||||
|
if strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") == true {
|
||||||
|
mode = "gzip"
|
||||||
|
}
|
||||||
|
hash := QuickHash(mode + string(dataToSend), 20)
|
||||||
if req.Header.Get("If-None-Match") == hash {
|
if req.Header.Get("If-None-Match") == hash {
|
||||||
res.WriteHeader(http.StatusNotModified)
|
res.WriteHeader(http.StatusNotModified)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
res.Header().Set("Etag", hash)
|
head := res.Header()
|
||||||
res.Write(json)
|
head.Set("Etag", hash)
|
||||||
|
if mode == "gzip" {
|
||||||
|
head.Set("Content-Encoding", "gzip")
|
||||||
|
|
||||||
|
var b bytes.Buffer
|
||||||
|
w, _ := gzip.NewWriterLevel(&b, 1)
|
||||||
|
w.Write(dataToSend)
|
||||||
|
w.Close()
|
||||||
|
dataToSend = b.Bytes()
|
||||||
|
}
|
||||||
|
res.Write(dataToSend)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func SendSuccessResults(res http.ResponseWriter, data interface{}) {
|
func SendSuccessResults(res http.ResponseWriter, data interface{}) {
|
||||||
encoder := json.NewEncoder(res)
|
encoder := json.NewEncoder(res)
|
||||||
encoder.SetEscapeHTML(false)
|
encoder.SetEscapeHTML(false)
|
||||||
|
|||||||
@ -82,6 +82,6 @@ func AdminBackend(ctx App, res http.ResponseWriter, req *http.Request) {
|
|||||||
for key := range drivers {
|
for key := range drivers {
|
||||||
backends[key] = drivers[key].LoginForm()
|
backends[key] = drivers[key].LoginForm()
|
||||||
}
|
}
|
||||||
SendSuccessResultWithEtag(res, req, backends)
|
SendSuccessResultWithEtagAndGzip(res, req, backends)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@ -71,5 +71,5 @@ func PrivateConfigUpdateHandler(ctx App, res http.ResponseWriter, req *http.Requ
|
|||||||
|
|
||||||
func PublicConfigHandler(ctx App, res http.ResponseWriter, req *http.Request) {
|
func PublicConfigHandler(ctx App, res http.ResponseWriter, req *http.Request) {
|
||||||
cfg := Config.Export()
|
cfg := Config.Export()
|
||||||
SendSuccessResultWithEtag(res, req, cfg)
|
SendSuccessResultWithEtagAndGzip(res, req, cfg)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user