diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 5a9047f..0000000 --- a/.gitattributes +++ /dev/null @@ -1,4 +0,0 @@ -*.jpg filter=lfs diff=lfs merge=lfs -text -*.png filter=lfs diff=lfs merge=lfs -text -*.webp filter=lfs diff=lfs merge=lfs -text -*.jpeg filter=lfs diff=lfs merge=lfs -text diff --git a/README.md b/README.md index 2a606b4..00a57c6 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,6 @@

-After the [n0vad3v/webp_server](https://github.com/n0vad3v/webp_server), I decide to rewrite the whole program with Go, as there will be no more `npm install`s or `docker-compose`s. - This is a Server based on Golang, which allows you to serve WebP images on the fly. It will convert `jpg,jpeg,png` files by default, this can be customized by editing the `config.json`.. * currently supported image format: JPEG, PNG, BMP, GIF(static image for now) @@ -14,34 +12,15 @@ It will convert `jpg,jpeg,png` files by default, this can be customized by editi > > For Safari and Opera users, the original image will be used. -## Compare to [n0vad3v/webp_server](https://github.com/n0vad3v/webp_server) - -### Size - -* `webp_server` with `node_modules`: 43M -* `webp-server(go)` single binary: 15M - -### Performance - -It's basically between `ExpressJS` and `Fiber`, much faster than the `http` package of course. - -### Convenience - -* `webp_server`: Clone the repo -> `npm install` -> run with `pm2` -* `webp-server(go)`: Download a single binary -> Run - -### Auto update -This tool will check for new release whenever you run it. The updated binary will be save to `update` dir. ## General Usage Steps - -## 1. Download or build the binary +### 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 -## 2. Dump config file +### 2. Dump config file ``` ./webp-server -dump-config > config.json @@ -65,7 +44,7 @@ If you are serving images at `https://example.com/pics/tsuki.jpg` and your files `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`. -## 3. Run +### 3. Run ``` ./webp-server --help @@ -85,7 +64,7 @@ Usage of ./webp-server: -prefork use prefork ``` -### Prefetch +#### Prefetch Prefetch will convert all your images to webp. Don't worry, WebP Server will start, you don't have to wait until prefetch completes. ``` ./webp-server -prefetch @@ -97,14 +76,14 @@ By default, it will utilize all your CPU cores. ./webp-server -prefetch -jobs=4 ``` -### dump systemd service file +#### dump systemd service file The standard systemd service file will show on your screen. You many want to use `>` to redirect to a file. ``` ./webp-server -dump-systemd ``` -### screen or tmux +#### screen or tmux Use `screen` or `tmux` to avoid being terminated. Let's take `screen` for example ``` screen -S webp @@ -112,7 +91,7 @@ screen -S webp ``` (Use Ctrl-A-D to detach the `screen` with `webp-server` running.) -### systemd +#### systemd Don't worry, we've got you covered! Download `webp-server` to `/opt/webps/webp-server`, and create a config file to `/opt/webps/config.json`, then, @@ -123,9 +102,9 @@ systemctl daemon-reload systemctl enable webp-server.service systemctl start webp-server.service ``` -## 4. Nginx proxy_pass +### 4. Nginx proxy_pass Let Nginx to `proxy_pass http://localhost:3333/;`, and your webp-server is on-the-fly -### WordPress example +#### WordPress example ``` location ^~ /wp-content/uploads/ { proxy_pass http://127.0.0.1:3333; @@ -133,7 +112,8 @@ location ^~ /wp-content/uploads/ { ``` If you use Caddy, you may refer to [优雅的让 Halo 支持 webp 图片输出](https://halo.run/archives/halo-and-webp). -## Advanced usage +### Auto update +This tool will check for new release whenever you run it. The updated binary will be save to `update` dir. ## Build your own binaries Install latest version of golang, enable go module, clone the repo, and then... @@ -143,6 +123,23 @@ make **Due to the limitations of webp module, you can't cross compile this tool. But the binary will work instantly on your platform and arch** + +## Compare to [n0vad3v/webp_server](https://github.com/n0vad3v/webp_server) + +### Size + +* `webp_server` with `node_modules`: 43M +* `webp-server(go)` single binary: 15M + +### Performance + +It's basically between `ExpressJS` and `Fiber`, much faster than the `http` package of course. + +### Convenience + +* `webp_server`: Clone the repo -> `npm install` -> run with `pm2` +* `webp-server(go)`: Download a single binary -> Run + ## TODO - [x] This version doesn't support header-based-output, which means Safari users will not see the converted `webp` images, this should be fixed in later releases. - [ ] Multi platform support. diff --git a/encoder.go b/encoder.go index 07574bc..5528527 100644 --- a/encoder.go +++ b/encoder.go @@ -56,7 +56,7 @@ func WebpEncoder(p1, p2 string, quality float32, Log bool, c chan int) (err erro ChanErr(c) return } - if err = ioutil.WriteFile(p2, buf.Bytes(), 0755); err != nil { + if err = ioutil.WriteFile(p2, buf.Bytes(), 0644); err != nil { log.Error(err) ChanErr(c) return diff --git a/pics/dir1/inside.jpg b/pics/dir1/inside.jpg index 8d2ecb7..f639a0f 100644 Binary files a/pics/dir1/inside.jpg and b/pics/dir1/inside.jpg differ diff --git a/pics/png.jpg b/pics/png.jpg index 0a570f5..b2321c1 100644 Binary files a/pics/png.jpg and b/pics/png.jpg differ diff --git a/pics/webp_server.jpg b/pics/webp_server.jpg index 8d2ecb7..f639a0f 100644 Binary files a/pics/webp_server.jpg and b/pics/webp_server.jpg differ diff --git a/pics/webp_server.png b/pics/webp_server.png old mode 100755 new mode 100644 index 3737c1f..865a4f4 Binary files a/pics/webp_server.png and b/pics/webp_server.png differ diff --git a/prefetch.go b/prefetch.go index f0f10bf..2a1ad64 100644 --- a/prefetch.go +++ b/prefetch.go @@ -1,7 +1,6 @@ package main import ( - "bufio" "fmt" "os" "path" @@ -13,40 +12,34 @@ import ( ) 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 // maximum ongoing prefetch is depending on your core of CPU log.Infof("Prefetching using %d cores", jobs) var finishChan = make(chan int, jobs) for i := 0; i < jobs; i++ { finishChan <- 0 } - if char == 121 || char == 10 || char == 89 { - //prefetch, recursive through the dir - all := FileCount(confImgPath) - count := 0 - err := filepath.Walk(confImgPath, - func(picAbsPath string, info os.FileInfo, err error) error { - if err != nil { - return err - } - // RawImagePath string, ImgFilename string, reqURI string - proposedURI := strings.Replace(picAbsPath, confImgPath, "", 1) - _, 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) - count += <-finishChan - //progress bar - _, _ = fmt.Fprintf(os.Stdout, "[Webp Server started] - convert in progress: %d/%d\r", count, all) - return nil - }) - if err != nil { - log.Debug(err) - } + + //prefetch, recursive through the dir + all := FileCount(confImgPath) + count := 0 + err := filepath.Walk(confImgPath, + func(picAbsPath string, info os.FileInfo, err error) error { + if err != nil { + return err + } + // RawImagePath string, ImgFilename string, reqURI string + proposedURI := strings.Replace(picAbsPath, confImgPath, "", 1) + _, 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) + count += <-finishChan + //progress bar + _, _ = fmt.Fprintf(os.Stdout, "[Webp Server started] - convert in progress: %d/%d\r", count, all) + return nil + }) + if err != nil { + log.Debug(err) } _, _ = fmt.Fprintf(os.Stdout, "Prefetch completeY(^_^)Y\n\n") diff --git a/update.go b/update.go index 507dbd2..e897d65 100644 --- a/update.go +++ b/update.go @@ -15,7 +15,7 @@ import ( func autoUpdate() { defer func() { if err := recover(); err != nil { - log.Error("Download error.", err) + log.Errorf("Download error.", err) } }()