mirror of
https://github.com/mickael-kerjean/filestash
synced 2025-12-15 12:55:41 +01:00
feature (plugin): handle compression in opener plugin
This commit is contained in:
parent
2c1edd85e9
commit
f584807fac
1 changed files with 31 additions and 10 deletions
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
. "github.com/mickael-kerjean/filestash/server/common"
|
. "github.com/mickael-kerjean/filestash/server/common"
|
||||||
"github.com/mickael-kerjean/filestash/server/model"
|
"github.com/mickael-kerjean/filestash/server/model"
|
||||||
|
|
@ -42,16 +43,36 @@ func PluginExportHandler(ctx *App, res http.ResponseWriter, req *http.Request) {
|
||||||
func PluginStaticHandler(ctx *App, res http.ResponseWriter, req *http.Request) {
|
func PluginStaticHandler(ctx *App, res http.ResponseWriter, req *http.Request) {
|
||||||
path := mux.Vars(req)["path"]
|
path := mux.Vars(req)["path"]
|
||||||
mtype := GetMimeType(path)
|
mtype := GetMimeType(path)
|
||||||
file, err := model.GetPluginFile(mux.Vars(req)["name"], path)
|
staticConfig := []struct {
|
||||||
if err != nil {
|
ContentType string
|
||||||
SendErrorResult(res, err)
|
FileExt string
|
||||||
return
|
}{
|
||||||
}
|
{"br", ".br"},
|
||||||
defer file.Close()
|
{"gzip", ".gz"},
|
||||||
res.Header().Set("Content-Type", mtype)
|
{"", ""},
|
||||||
_, err = io.Copy(res, file)
|
}
|
||||||
if err != nil {
|
|
||||||
SendErrorResult(res, err)
|
var file io.ReadCloser
|
||||||
|
var err error
|
||||||
|
head := res.Header()
|
||||||
|
acceptEncoding := req.Header.Get("Accept-Encoding")
|
||||||
|
for _, cfg := range staticConfig {
|
||||||
|
if strings.Contains(acceptEncoding, cfg.ContentType) == false {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
file, err = model.GetPluginFile(mux.Vars(req)["name"], path+cfg.FileExt)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
head.Set("Content-Type", mtype)
|
||||||
|
if cfg.ContentType != "" {
|
||||||
|
head.Set("Content-Encoding", cfg.ContentType)
|
||||||
|
}
|
||||||
|
io.Copy(res, file)
|
||||||
|
file.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SendErrorResult(res, err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue