From 95183ab1c35fd3b53452b0861663cc6573d8b6ba Mon Sep 17 00:00:00 2001 From: Nova Kwok Date: Mon, 2 Mar 2020 21:17:44 +0800 Subject: [PATCH] Added custom exhaust path. (#16) * Added custom exhaust path. Bump to 0.0.4. * Fix typo --- README.md | 7 ++++++- config.json | 1 + scripts/webp_server.spec | 2 +- webp-server.go | 19 +++++++++++-------- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index dd912cc..4eb16af 100644 --- a/README.md +++ b/README.md @@ -33,11 +33,13 @@ It's basically between `ExpressJS` and `Fiber`, much faster than the `http` pack Regarding the `IMG_PATH` section in `config.json`. If you are serving images at `https://example.com/pics/tsuki.jpg` and your files are at `/var/www/image/pics/tsuki.jpg`, then `IMG_PATH` shall be `/var/www/image`. +`EXHAUST_PATH` is cache folder for output `webp` images, with `EXHAUST_PATH` set to `/var/cache/webp` +in the example above, your `webp` image will be saved at `/var/cache/webp/pics/tsuki.jpg.1582558990.webp`. ## 1. Download or build the binary Download the `webp-server` from [release](https://github.com/n0vad3v/webp_server_go/releases) page. -Wanna build your own binary? Check out [build](#build-your-own-binaries) section +Wanna build your own binary? Check out [build](#build-your-own-binaries) section ## 2. config file Create a `config.json` as follows to face your need, default convert quality is 80%. @@ -47,6 +49,7 @@ Create a `config.json` as follows to face your need, default convert quality is "PORT": "3333", "QUALITY": "80", "IMG_PATH": "/path/to/pics", + "EXHAUST_PATH": "/path/to/exhaust", "ALLOWED_TYPES": ["jpg","png","jpeg"] } ``` @@ -95,6 +98,8 @@ But the binary will work instantly on your platform and arch** - [x] A better way to supervise the program. - [ ] Get rid of render-blocking effect on first render. - [x] Prefetch on server initialization. +- [x] Custom exhaust path. +- [ ] Multiple listen address. ## Related Articles(In chronological order) diff --git a/config.json b/config.json index f42f283..65cc745 100644 --- a/config.json +++ b/config.json @@ -3,5 +3,6 @@ "PORT": "3333", "QUALITY": "80", "IMG_PATH": "/path/to/pics", + "EXHAUST_PATH": "/path/to/exhaust", "ALLOWED_TYPES": ["jpg","png","jpeg"] } diff --git a/scripts/webp_server.spec b/scripts/webp_server.spec index 8972327..10253de 100644 --- a/scripts/webp_server.spec +++ b/scripts/webp_server.spec @@ -1,5 +1,5 @@ Name: webp-server -Version: 0.0.3 +Version: 0.0.4 Release: 1%{?dist} Summary: Go version of WebP Server. A tool that will serve your JPG/PNGs as WebP format with compression, on-the-fly. diff --git a/webp-server.go b/webp-server.go index 1f81cfc..75030f0 100644 --- a/webp-server.go +++ b/webp-server.go @@ -30,6 +30,7 @@ type Config struct { ImgPath string `json:"IMG_PATH"` QUALITY string AllowedTypes []string `json:"ALLOWED_TYPES"` + ExhaustPath string `json:"EXHAUST_PATH"` } var configPath string @@ -122,7 +123,7 @@ func init() { flag.Parse() } -func Convert(ImgPath string, AllowedTypes []string, QUALITY string) func(c *fiber.Ctx) { +func Convert(ImgPath string, ExhaustPath string, AllowedTypes []string, QUALITY string) func(c *fiber.Ctx) { return func(c *fiber.Ctx) { //basic vars var reqURI = c.Path() // mypic/123.jpg @@ -163,7 +164,7 @@ func Convert(ImgPath string, AllowedTypes []string, QUALITY string) func(c *fibe return } - cwd, WebpAbsPath := genWebpAbs(RawImageAbs, ImgFilename, reqURI) + cwd, WebpAbsPath := genWebpAbs(RawImageAbs, ExhaustPath, ImgFilename, reqURI) if imageExists(WebpAbsPath) { finalFile = WebpAbsPath @@ -214,7 +215,7 @@ func fileCount(dir string) int { return count } -func genWebpAbs(RawImagePath string, ImgFilename string, reqURI string) (string, string) { +func genWebpAbs(RawImagePath string, ExhaustPath string, ImgFilename string, reqURI string) (string, string) { // get file mod time STAT, err := os.Stat(RawImagePath) if err != nil { @@ -226,11 +227,12 @@ func genWebpAbs(RawImagePath string, ImgFilename string, reqURI string) (string, cwd, _ := os.Getwd() // /home/webp_server/exhaust/path/to/tsuki.jpg.1582558990.webp - WebpAbsolutePath := path.Clean(path.Join(cwd, "exhaust", path.Dir(reqURI), WebpFilename)) + // Custom Exhaust: /path/to/exhaust/web_path/web_to/tsuki.jpg.1582558990.webp + WebpAbsolutePath := path.Clean(path.Join(ExhaustPath, path.Dir(reqURI), WebpFilename)) return cwd, WebpAbsolutePath } -func prefetchImages(confImgPath string, QUALITY string) { +func prefetchImages(confImgPath string, ExhaustPath string, QUALITY string) { fmt.Println(`Prefetch will convert all your images to webp, it may take some time and consume a lot of CPU resource. Do you want to proceed(Y/n)`) reader := bufio.NewReader(os.Stdin) char, _, _ := reader.ReadRune() //y Y enter @@ -250,7 +252,7 @@ func prefetchImages(confImgPath string, QUALITY string) { } // RawImagePath string, ImgFilename string, reqURI string proposedURI := strings.Replace(picAbsPath, confImgPath, "", 1) - _, p2 := genWebpAbs(picAbsPath, info.Name(), proposedURI) + _, p2 := genWebpAbs(picAbsPath, ExhaustPath, info.Name(), proposedURI) q, _ := strconv.ParseFloat(QUALITY, 32) _ = os.MkdirAll(path.Dir(p2), 0755) go webpEncoder(picAbsPath, p2, float32(q), false, finishChan) @@ -276,9 +278,10 @@ func main() { confImgPath := path.Clean(config.ImgPath) QUALITY := config.QUALITY AllowedTypes := config.AllowedTypes + ExhaustPath := config.ExhaustPath if prefetch { - prefetchImages(confImgPath, QUALITY) + prefetchImages(confImgPath, ExhaustPath, QUALITY) } app := fiber.New() @@ -291,7 +294,7 @@ func main() { ServerInfo := "WebP Server is running at " + ListenAddress fmt.Println(ServerInfo) - app.Get("/*", Convert(confImgPath, AllowedTypes, QUALITY)) + app.Get("/*", Convert(confImgPath, ExhaustPath, AllowedTypes, QUALITY)) app.Listen(ListenAddress) }