First rev with cache and gif
This commit is contained in:
		@@ -31,7 +31,7 @@ import (
 | 
			
		||||
 | 
			
		||||
	// plug in Caddy modules here
 | 
			
		||||
	_ "github.com/caddyserver/caddy/v2/modules/standard"
 | 
			
		||||
	_ "github.com/zhshch2002/caddy-webp"
 | 
			
		||||
	_ "git.teamworkapps.com/scs/caddy-webp"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										95
									
								
								caddywebp.go
									
									
									
									
									
								
							
							
						
						
									
										95
									
								
								caddywebp.go
									
									
									
									
									
								
							@@ -1,18 +1,22 @@
 | 
			
		||||
package github
 | 
			
		||||
package caddywebp
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/caddyserver/caddy/v2"
 | 
			
		||||
	"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
 | 
			
		||||
	"github.com/caddyserver/caddy/v2/modules/caddyhttp"
 | 
			
		||||
	"github.com/chai2010/webp"
 | 
			
		||||
	"golang.org/x/image/bmp"
 | 
			
		||||
	"image"
 | 
			
		||||
	"image/gif"
 | 
			
		||||
	"image/jpeg"
 | 
			
		||||
	"image/png"
 | 
			
		||||
	"io"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -20,10 +24,7 @@ const Quality = 80
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	log.Println("webp plugin")
 | 
			
		||||
	err := caddy.RegisterModule(Webp{})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	caddy.RegisterModule(Webp{})
 | 
			
		||||
	httpcaddyfile.RegisterHandlerDirective("webp", parseCaddyfile)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -41,33 +42,73 @@ func (Webp) CaddyModule() caddy.ModuleInfo {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func decodeGif(r io.Reader) (image.Image, error) {
 | 
			
		||||
	i, err := gif.DecodeAll(r)
 | 
			
		||||
	// Give up if we can't decode or decoded multiple images
 | 
			
		||||
	if err != nil || len(i.Image) != 1 {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	return i.Image[0], nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (s Webp) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
 | 
			
		||||
	ua := r.Header.Get("User-Agent")
 | 
			
		||||
	if strings.Contains(ua, "Safari") && !strings.Contains(ua, "Chrome") && !strings.Contains(ua, "Firefox") {
 | 
			
		||||
		return next.ServeHTTP(w, r) // 对Safari禁用webp
 | 
			
		||||
	if !strings.Contains(r.Header.Get("Accept"), "image/webp") {
 | 
			
		||||
		// Browser does not advertise webp support for this request so let's bail
 | 
			
		||||
		return next.ServeHTTP(w, r)
 | 
			
		||||
	}
 | 
			
		||||
	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))
 | 
			
		||||
	origFile:=caddyhttp.SanitizedPathJoin(rootPath, r.URL.Path)
 | 
			
		||||
	cstat, err:=os.Stat(cacheFile)
 | 
			
		||||
	if err==nil {
 | 
			
		||||
		// Cache file exists
 | 
			
		||||
		origStat, err:=os.Stat(origFile)
 | 
			
		||||
		if err == nil && origStat.ModTime().Before(cstat.ModTime()) {
 | 
			
		||||
			// Serve cached file and fly away
 | 
			
		||||
			fh,err:=os.Open(cacheFile)
 | 
			
		||||
			if err == nil {
 | 
			
		||||
				// We couldn't open for whatever reason so let's not serve it
 | 
			
		||||
				w.Header().Set("content-type", "image/webp")
 | 
			
		||||
				w.Header().Set("webpstatus", "fromcache")
 | 
			
		||||
				w.WriteHeader(http.StatusOK)
 | 
			
		||||
				io.Copy(w,fh)
 | 
			
		||||
				return nil
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
	// Let's encode and create a cached file
 | 
			
		||||
	cachePath,_:=path.Split(cacheFile)
 | 
			
		||||
	_,err=os.Stat(cachePath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		err=os.Mkdir(cachePath, 0600)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Printf("Creating cache path: " + err.Error())
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	resp := &response{}
 | 
			
		||||
	err := next.ServeHTTP(resp, r)
 | 
			
		||||
 | 
			
		||||
	err = next.ServeHTTP(resp, r)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ct := http.DetectContentType(resp.Body.Bytes())
 | 
			
		||||
 | 
			
		||||
	//fmt.Println("file len", resp.Body.Len(), "file type", ct)
 | 
			
		||||
 | 
			
		||||
	var decoder func(io.Reader) (image.Image, error)
 | 
			
		||||
	if strings.Contains(ct, "jpeg") {
 | 
			
		||||
	switch ct {
 | 
			
		||||
	case "image/jpeg":
 | 
			
		||||
		decoder = jpeg.Decode
 | 
			
		||||
	} else if strings.Contains(ct, "png") {
 | 
			
		||||
	case "image/png":
 | 
			
		||||
		decoder = png.Decode
 | 
			
		||||
	} else if strings.Contains(ct, "bmp") {
 | 
			
		||||
	case "image/bmp":
 | 
			
		||||
		decoder = bmp.Decode
 | 
			
		||||
		// } else if strings.HasSuffix(r.URL.String(), ".gif") { TODO need to support animated webp
 | 
			
		||||
		// 	decoder = gif.Decode
 | 
			
		||||
	} else {
 | 
			
		||||
		return next.ServeHTTP(w, r)
 | 
			
		||||
	case "image/gif":
 | 
			
		||||
		decoder = decodeGif
 | 
			
		||||
	default:
 | 
			
		||||
		next.ServeHTTP(w, r)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	img, err := decoder(bytes.NewReader(resp.Body.Bytes()))
 | 
			
		||||
	if err != nil || img == nil {
 | 
			
		||||
		log.Println(err)
 | 
			
		||||
@@ -76,16 +117,20 @@ func (s Webp) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.H
 | 
			
		||||
	var buf bytes.Buffer
 | 
			
		||||
	err = webp.Encode(&buf, img, &webp.Options{Lossless: false, Quality: Quality})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Println(err)
 | 
			
		||||
		log.Printf(err.Error())
 | 
			
		||||
		return next.ServeHTTP(w, r)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	fh,err:=os.Create(cacheFile)
 | 
			
		||||
	fh.Write(buf.Bytes())
 | 
			
		||||
	w.Write(buf.Bytes())
 | 
			
		||||
 | 
			
		||||
	buf.WriteTo(fh)
 | 
			
		||||
	fh.Close()
 | 
			
		||||
	w.Header().Set("Content-Type", "image/webp")
 | 
			
		||||
	w.WriteHeader(http.StatusOK)
 | 
			
		||||
	_, err = w.Write(buf.Bytes())
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Println(err)
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	buf.WriteTo(w)
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										11
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								go.mod
									
									
									
									
									
								
							@@ -1,10 +1,9 @@
 | 
			
		||||
module github.com/zhshch2002/caddy-webp
 | 
			
		||||
module git.teamworkapps.com/scs/caddy-webp
 | 
			
		||||
 | 
			
		||||
go 1.14
 | 
			
		||||
go 1.16
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	github.com/caddyserver/caddy/v2 v2.0.0-beta9.0.20200329195300-deba26d225c5
 | 
			
		||||
	github.com/chai2010/webp v1.1.1-0.20200323094621-c9e2c1a9877a
 | 
			
		||||
	golang.org/x/image v0.0.0-20200119044424-58c23975cae1
 | 
			
		||||
	golang.org/x/sys v0.0.0-20200327173247-9dae0f8f5775 // indirect
 | 
			
		||||
	github.com/caddyserver/caddy/v2 v2.4.3
 | 
			
		||||
	github.com/chai2010/webp v1.1.0 // indirect
 | 
			
		||||
	golang.org/x/image v0.0.0-20210622092929-e6eecd499c2c // indirect
 | 
			
		||||
)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user