Added custom exhaust path. (#16)

* Added custom exhaust path.
Bump to 0.0.4.

* Fix typo
This commit is contained in:
Nova Kwok 2020-03-02 21:17:44 +08:00 committed by GitHub
parent cb1b48647c
commit 95183ab1c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 10 deletions

View File

@ -33,6 +33,8 @@ It's basically between `ExpressJS` and `Fiber`, much faster than the `http` pack
Regarding the `IMG_PATH` section in `config.json`. Regarding the `IMG_PATH` section in `config.json`.
If you are serving images at `https://example.com/pics/tsuki.jpg` and 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`. 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 ## 1. Download or build the binary
Download the `webp-server` from [release](https://github.com/n0vad3v/webp_server_go/releases) page. Download the `webp-server` from [release](https://github.com/n0vad3v/webp_server_go/releases) page.
@ -47,6 +49,7 @@ Create a `config.json` as follows to face your need, default convert quality is
"PORT": "3333", "PORT": "3333",
"QUALITY": "80", "QUALITY": "80",
"IMG_PATH": "/path/to/pics", "IMG_PATH": "/path/to/pics",
"EXHAUST_PATH": "/path/to/exhaust",
"ALLOWED_TYPES": ["jpg","png","jpeg"] "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. - [x] A better way to supervise the program.
- [ ] Get rid of render-blocking effect on first render. - [ ] Get rid of render-blocking effect on first render.
- [x] Prefetch on server initialization. - [x] Prefetch on server initialization.
- [x] Custom exhaust path.
- [ ] Multiple listen address.
## Related Articles(In chronological order) ## Related Articles(In chronological order)

View File

@ -3,5 +3,6 @@
"PORT": "3333", "PORT": "3333",
"QUALITY": "80", "QUALITY": "80",
"IMG_PATH": "/path/to/pics", "IMG_PATH": "/path/to/pics",
"EXHAUST_PATH": "/path/to/exhaust",
"ALLOWED_TYPES": ["jpg","png","jpeg"] "ALLOWED_TYPES": ["jpg","png","jpeg"]
} }

View File

@ -1,5 +1,5 @@
Name: webp-server Name: webp-server
Version: 0.0.3 Version: 0.0.4
Release: 1%{?dist} 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. Summary: Go version of WebP Server. A tool that will serve your JPG/PNGs as WebP format with compression, on-the-fly.

View File

@ -30,6 +30,7 @@ type Config struct {
ImgPath string `json:"IMG_PATH"` ImgPath string `json:"IMG_PATH"`
QUALITY string QUALITY string
AllowedTypes []string `json:"ALLOWED_TYPES"` AllowedTypes []string `json:"ALLOWED_TYPES"`
ExhaustPath string `json:"EXHAUST_PATH"`
} }
var configPath string var configPath string
@ -122,7 +123,7 @@ func init() {
flag.Parse() 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) { return func(c *fiber.Ctx) {
//basic vars //basic vars
var reqURI = c.Path() // mypic/123.jpg var reqURI = c.Path() // mypic/123.jpg
@ -163,7 +164,7 @@ func Convert(ImgPath string, AllowedTypes []string, QUALITY string) func(c *fibe
return return
} }
cwd, WebpAbsPath := genWebpAbs(RawImageAbs, ImgFilename, reqURI) cwd, WebpAbsPath := genWebpAbs(RawImageAbs, ExhaustPath, ImgFilename, reqURI)
if imageExists(WebpAbsPath) { if imageExists(WebpAbsPath) {
finalFile = WebpAbsPath finalFile = WebpAbsPath
@ -214,7 +215,7 @@ func fileCount(dir string) int {
return count 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 // get file mod time
STAT, err := os.Stat(RawImagePath) STAT, err := os.Stat(RawImagePath)
if err != nil { if err != nil {
@ -226,11 +227,12 @@ func genWebpAbs(RawImagePath string, ImgFilename string, reqURI string) (string,
cwd, _ := os.Getwd() cwd, _ := os.Getwd()
// /home/webp_server/exhaust/path/to/tsuki.jpg.1582558990.webp // /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 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)`) 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) reader := bufio.NewReader(os.Stdin)
char, _, _ := reader.ReadRune() //y Y enter char, _, _ := reader.ReadRune() //y Y enter
@ -250,7 +252,7 @@ func prefetchImages(confImgPath string, QUALITY string) {
} }
// RawImagePath string, ImgFilename string, reqURI string // RawImagePath string, ImgFilename string, reqURI string
proposedURI := strings.Replace(picAbsPath, confImgPath, "", 1) proposedURI := strings.Replace(picAbsPath, confImgPath, "", 1)
_, p2 := genWebpAbs(picAbsPath, info.Name(), proposedURI) _, p2 := genWebpAbs(picAbsPath, ExhaustPath, info.Name(), proposedURI)
q, _ := strconv.ParseFloat(QUALITY, 32) q, _ := strconv.ParseFloat(QUALITY, 32)
_ = os.MkdirAll(path.Dir(p2), 0755) _ = os.MkdirAll(path.Dir(p2), 0755)
go webpEncoder(picAbsPath, p2, float32(q), false, finishChan) go webpEncoder(picAbsPath, p2, float32(q), false, finishChan)
@ -276,9 +278,10 @@ func main() {
confImgPath := path.Clean(config.ImgPath) confImgPath := path.Clean(config.ImgPath)
QUALITY := config.QUALITY QUALITY := config.QUALITY
AllowedTypes := config.AllowedTypes AllowedTypes := config.AllowedTypes
ExhaustPath := config.ExhaustPath
if prefetch { if prefetch {
prefetchImages(confImgPath, QUALITY) prefetchImages(confImgPath, ExhaustPath, QUALITY)
} }
app := fiber.New() app := fiber.New()
@ -291,7 +294,7 @@ func main() {
ServerInfo := "WebP Server is running at " + ListenAddress ServerInfo := "WebP Server is running at " + ListenAddress
fmt.Println(ServerInfo) fmt.Println(ServerInfo)
app.Get("/*", Convert(confImgPath, AllowedTypes, QUALITY)) app.Get("/*", Convert(confImgPath, ExhaustPath, AllowedTypes, QUALITY))
app.Listen(ListenAddress) app.Listen(ListenAddress)
} }