working version
This commit is contained in:
parent
f064ddb668
commit
c4988518f3
|
@ -34,3 +34,4 @@ go.mod
|
||||||
go.sum
|
go.sum
|
||||||
index.html
|
index.html
|
||||||
dist
|
dist
|
||||||
|
caddy/caddy
|
||||||
|
|
16
caddywebp.go
16
caddywebp.go
|
@ -57,9 +57,11 @@ func (s Webp) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.H
|
||||||
return next.ServeHTTP(w, r)
|
return next.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
||||||
rootPath,_:=repl.GetString("{http.vars.root}")
|
|
||||||
cacheFile:=fmt.Sprintf("%s%s",caddyhttp.SanitizedPathJoin(rootPath + "/.webpcache/", r.URL.Path +"?" + r.URL.RawQuery))
|
root := repl.ReplaceAll("{http.vars.root}", ".")
|
||||||
origFile:=caddyhttp.SanitizedPathJoin(rootPath, r.URL.Path)
|
cacheFile:=fmt.Sprintf("%s%s",caddyhttp.SanitizedPathJoin(root + "/.webpcache/", r.URL.Path +"?"), r.URL.RawQuery)
|
||||||
|
origFile := caddyhttp.SanitizedPathJoin(root, r.URL.Path)
|
||||||
|
|
||||||
cstat, err:=os.Stat(cacheFile)
|
cstat, err:=os.Stat(cacheFile)
|
||||||
if err==nil {
|
if err==nil {
|
||||||
// Cache file exists
|
// Cache file exists
|
||||||
|
@ -70,7 +72,7 @@ func (s Webp) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.H
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// We couldn't open for whatever reason so let's not serve it
|
// We couldn't open for whatever reason so let's not serve it
|
||||||
w.Header().Set("content-type", "image/webp")
|
w.Header().Set("content-type", "image/webp")
|
||||||
w.Header().Set("webpstatus", "fromcache")
|
w.Header().Add("webpstatus", "fromcache")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
io.Copy(w,fh)
|
io.Copy(w,fh)
|
||||||
return nil
|
return nil
|
||||||
|
@ -82,7 +84,7 @@ func (s Webp) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.H
|
||||||
cachePath,_:=path.Split(cacheFile)
|
cachePath,_:=path.Split(cacheFile)
|
||||||
_,err=os.Stat(cachePath)
|
_,err=os.Stat(cachePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err=os.Mkdir(cachePath, 0600)
|
err=os.MkdirAll(cachePath, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Creating cache path: " + err.Error())
|
log.Printf("Creating cache path: " + err.Error())
|
||||||
}
|
}
|
||||||
|
@ -128,7 +130,9 @@ func (s Webp) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.H
|
||||||
|
|
||||||
buf.WriteTo(fh)
|
buf.WriteTo(fh)
|
||||||
fh.Close()
|
fh.Close()
|
||||||
w.Header().Set("Content-Type", "image/webp")
|
w.Header().Set("content-Type", "image/webp")
|
||||||
|
w.Header().Add("webpstatus", "tocache")
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
buf.WriteTo(w)
|
buf.WriteTo(w)
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in New Issue