mirror of
https://github.com/woodchen-ink/webp_server_go.git
synced 2025-07-18 13:42:02 +08:00
generate new webp image if original file is modified since last time
Signed-off-by: Cocoa <0xbbc@0xbbc.com>
This commit is contained in:
parent
8bdffcc246
commit
61b46a1d8e
@ -12,6 +12,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -99,8 +100,15 @@ func main() {
|
|||||||
// /path/to
|
// /path/to
|
||||||
DirPath := path.Dir(ImgPath)
|
DirPath := path.Dir(ImgPath)
|
||||||
|
|
||||||
// /path/to/tsuki.jpg.webp
|
// 1582558990
|
||||||
WebpImgPath := DirPath + "/" + ImgName + ".webp"
|
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()
|
||||||
@ -108,11 +116,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)
|
||||||
@ -154,6 +162,20 @@ func main() {
|
|||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
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, 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user