From 61b46a1d8eec375c5c553a3c04210ee9cd0bb34a Mon Sep 17 00:00:00 2001 From: Cocoa <0xbbc@0xbbc.com> Date: Tue, 25 Feb 2020 20:50:29 +0800 Subject: [PATCH 1/4] generate new webp image if original file is modified since last time Signed-off-by: Cocoa <0xbbc@0xbbc.com> --- webp-server.go | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/webp-server.go b/webp-server.go index 75888bb..ddf3c84 100644 --- a/webp-server.go +++ b/webp-server.go @@ -12,6 +12,7 @@ import ( "log" "os" "path" + "path/filepath" "strconv" "strings" @@ -99,8 +100,15 @@ func main() { // /path/to DirPath := path.Dir(ImgPath) - // /path/to/tsuki.jpg.webp - WebpImgPath := DirPath + "/" + ImgName + ".webp" + // 1582558990 + STAT, err := os.Stat(ImgAbsolutePath) + if err != nil { + fmt.Println(err.Error()) + } + ModifiedTime := STAT.ModTime().Unix() + + // /path/to/tsuki.jpg.1582558990.webp + WebpImgPath := fmt.Sprintf("%s/%s.%d.webp", DirPath, ImgName, ModifiedTime) // /home/webp_server CurrentPath, err := os.Getwd() @@ -108,11 +116,11 @@ func main() { fmt.Println(err.Error()) } - // /home/webp_server/exhaust/path/to/tsuki.webp - WebpAbsolutePath := CurrentPath + "/exhaust" + WebpImgPath + // /home/webp_server/exhaust/path/to/tsuki.jpg.1582558990.webp + WebpAbsolutePath := path.Clean(CurrentPath + "/exhaust" + WebpImgPath) // /home/webp_server/exhaust/path/to - DirAbsolutePath := CurrentPath + "/exhaust" + DirPath + DirAbsolutePath := path.Clean(CurrentPath + "/exhaust" + DirPath) // Check file extension _, found := Find(AllowedTypes, ImgExt) @@ -154,6 +162,20 @@ func main() { fmt.Println(err) } c.SendFile(WebpAbsolutePath) + + // /home/webp_server/exhaust/path/to/tsuki.jpg.1582558100.webp <- older ones will be removed + // /home/webp_server/exhaust/path/to/tsuki.jpg.1582558990.webp <- keep the latest one + WebpCachedImgPath := path.Clean(fmt.Sprintf("%s/exhaust%s/%s-*.webp", CurrentPath, DirPath, ImgName)) + matches, err := filepath.Glob(WebpCachedImgPath) + if err != nil { + fmt.Println(err.Error()) + } else { + for _, path := range matches { + if strings.Compare(WebpAbsolutePath, path) != 0 { + os.Remove(path) + } + } + } } }) From 2b40d844a4e91e26ec628ac2cfde60863b212ffa Mon Sep 17 00:00:00 2001 From: Cocoa <0xbbc@0xbbc.com> Date: Tue, 25 Feb 2020 20:56:26 +0800 Subject: [PATCH 2/4] fixed typo in glob pattern, `-` -> `.` Signed-off-by: Cocoa <0xbbc@0xbbc.com> --- webp-server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webp-server.go b/webp-server.go index ddf3c84..4b21981 100644 --- a/webp-server.go +++ b/webp-server.go @@ -165,7 +165,7 @@ func main() { // /home/webp_server/exhaust/path/to/tsuki.jpg.1582558100.webp <- older ones will be removed // /home/webp_server/exhaust/path/to/tsuki.jpg.1582558990.webp <- keep the latest one - WebpCachedImgPath := path.Clean(fmt.Sprintf("%s/exhaust%s/%s-*.webp", CurrentPath, DirPath, ImgName)) + WebpCachedImgPath := path.Clean(fmt.Sprintf("%s/exhaust%s/%s.*.webp", CurrentPath, DirPath, ImgName)) matches, err := filepath.Glob(WebpCachedImgPath) if err != nil { fmt.Println(err.Error()) From 735b7d2cda87e0a827d46eefe82b6d9356785564 Mon Sep 17 00:00:00 2001 From: Cocoa <0xbbc@0xbbc.com> Date: Wed, 26 Feb 2020 00:50:19 +0800 Subject: [PATCH 3/4] check the original image for existence before os.Stat Signed-off-by: Cocoa <0xbbc@0xbbc.com> --- webp-server.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/webp-server.go b/webp-server.go index 4b21981..caa7785 100644 --- a/webp-server.go +++ b/webp-server.go @@ -100,6 +100,14 @@ func main() { // /path/to DirPath := path.Dir(ImgPath) + // Check the original image for existence + OriginalImgExists := imageExists(ImgAbsolutePath) + if !OriginalImgExists { + c.Send("File not found!") + c.SendStatus(404) + return + } + // 1582558990 STAT, err := os.Stat(ImgAbsolutePath) if err != nil { @@ -131,7 +139,7 @@ func main() { } // Check the original image for existence - if !imageExists(ImgAbsolutePath) { + if !OriginalImgExists { // The original image doesn't exist, check the webp image, delete if processed. if imageExists(WebpAbsolutePath) { os.Remove(WebpAbsolutePath) From 25d46611f14f5b8317a573f32f380a9c6503d03f Mon Sep 17 00:00:00 2001 From: Cocoa <0xbbc@0xbbc.com> Date: Wed, 26 Feb 2020 02:04:50 +0800 Subject: [PATCH 4/4] Copy `ImgName` before invoke c.SendFile since `stripTrailingSlashes` in fasthttp will change the content of ctx.URI().Path(), which was once obtained as `ImgPath := c.Path()` Signed-off-by: Cocoa <0xbbc@0xbbc.com> --- webp-server.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/webp-server.go b/webp-server.go index caa7785..6411320 100644 --- a/webp-server.go +++ b/webp-server.go @@ -169,11 +169,13 @@ func main() { if err != nil { fmt.Println(err) } + + ImgNameCopy := string([]byte(ImgName)) c.SendFile(WebpAbsolutePath) // /home/webp_server/exhaust/path/to/tsuki.jpg.1582558100.webp <- older ones will be removed // /home/webp_server/exhaust/path/to/tsuki.jpg.1582558990.webp <- keep the latest one - WebpCachedImgPath := path.Clean(fmt.Sprintf("%s/exhaust%s/%s.*.webp", CurrentPath, DirPath, ImgName)) + WebpCachedImgPath := path.Clean(fmt.Sprintf("%s/exhaust%s/%s.*.webp", CurrentPath, DirPath, ImgNameCopy)) matches, err := filepath.Glob(WebpCachedImgPath) if err != nil { fmt.Println(err.Error())