mirror of
https://github.com/mickael-kerjean/filestash
synced 2025-12-06 08:22:24 +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"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
. "github.com/mickael-kerjean/filestash/server/common"
|
||||
"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) {
|
||||
path := mux.Vars(req)["path"]
|
||||
mtype := GetMimeType(path)
|
||||
file, err := model.GetPluginFile(mux.Vars(req)["name"], path)
|
||||
if err != nil {
|
||||
SendErrorResult(res, err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
res.Header().Set("Content-Type", mtype)
|
||||
_, err = io.Copy(res, file)
|
||||
if err != nil {
|
||||
SendErrorResult(res, err)
|
||||
staticConfig := []struct {
|
||||
ContentType string
|
||||
FileExt string
|
||||
}{
|
||||
{"br", ".br"},
|
||||
{"gzip", ".gz"},
|
||||
{"", ""},
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
SendErrorResult(res, err)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue