Merge pull request #2 from BlueCocoa/master

Generate new WebP image if original file is modified since last time
This commit is contained in:
Nova Kwok 2020-02-26 10:37:14 +08:00 committed by GitHub
commit 95b4443c13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,6 +13,7 @@ import (
"net/http" "net/http"
"os" "os"
"path" "path"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
@ -80,8 +81,23 @@ func main() {
// /path/to // /path/to
DirPath := path.Dir(ImgPath) DirPath := path.Dir(ImgPath)
// /path/to/tsuki.jpg.webp // Check the original image for existence
WebpImgPath := DirPath + "/" + ImgName + ".webp" OriginalImgExists := imageExists(ImgAbsolutePath)
if !OriginalImgExists {
c.Send("File not found!")
c.SendStatus(404)
return
}
// 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 // /home/webp_server
CurrentPath, err := os.Getwd() CurrentPath, err := os.Getwd()
@ -89,11 +105,11 @@ func main() {
fmt.Println(err.Error()) fmt.Println(err.Error())
} }
// /home/webp_server/exhaust/path/to/tsuki.webp // /home/webp_server/exhaust/path/to/tsuki.jpg.1582558990.webp
WebpAbsolutePath := CurrentPath + "/exhaust" + WebpImgPath WebpAbsolutePath := path.Clean(CurrentPath + "/exhaust" + WebpImgPath)
// /home/webp_server/exhaust/path/to // /home/webp_server/exhaust/path/to
DirAbsolutePath := CurrentPath + "/exhaust" + DirPath DirAbsolutePath := path.Clean(CurrentPath + "/exhaust" + DirPath)
// Check file extension // Check file extension
_, found := Find(AllowedTypes, ImgExt) _, found := Find(AllowedTypes, ImgExt)
@ -104,7 +120,7 @@ func main() {
} }
// Check the original image for existence // Check the original image for existence
if !imageExists(ImgAbsolutePath) { if !OriginalImgExists {
// The original image doesn't exist, check the webp image, delete if processed. // The original image doesn't exist, check the webp image, delete if processed.
if imageExists(WebpAbsolutePath) { if imageExists(WebpAbsolutePath) {
os.Remove(WebpAbsolutePath) os.Remove(WebpAbsolutePath)
@ -134,7 +150,23 @@ func main() {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
ImgNameCopy := string([]byte(ImgName))
c.SendFile(WebpAbsolutePath) 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, ImgNameCopy))
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)
}
}
}
} }
}) })