This commit is contained in:
Benny~ 2020-02-09 18:28:54 +08:00
parent 4213dff391
commit 8f58bc4930
No known key found for this signature in database
GPG Key ID: 7B84C5D3219A9B31

View File

@ -18,11 +18,11 @@ import (
) )
type Config struct { type Config struct {
HOST string HOST string
PORT string PORT string
IMG_PATH string ImgPath string `json:"IMG_PATH"`
QUALITY string QUALITY string
ALLOWED_TYPES []string AllowedTypes []string `json:"ALLOWED_TYPES"`
} }
func webpEncoder(p1, p2 string, quality float32) { func webpEncoder(p1, p2 string, quality float32) {
@ -55,57 +55,57 @@ func main() {
HOST := config.HOST HOST := config.HOST
PORT := config.PORT PORT := config.PORT
IMG_PATH := config.IMG_PATH ImgPath := config.ImgPath
QUALITY := config.QUALITY QUALITY := config.QUALITY
ALLOWED_TYPES := config.ALLOWED_TYPES AllowedTypes := config.AllowedTypes
LISTEN_ADDRESS := HOST + ":" + PORT ListenAddress := HOST + ":" + PORT
// Server Info // Server Info
SERVER_INFO := "WebP Server is running at " + LISTEN_ADDRESS ServerInfo := "WebP Server is running at " + ListenAddress
fmt.Println(SERVER_INFO) fmt.Println(ServerInfo)
app.Get("/*", func(c *fiber.Ctx) { app.Get("/*", func(c *fiber.Ctx) {
// /var/www/IMG_PATH/path/to/tsuki.jpg // /var/www/IMG_PATH/path/to/tsuki.jpg
IMG_ABSOLUTE_PATH := IMG_PATH + c.Path() ImgAbsolutePath := ImgPath + c.Path()
// /path/to/tsuki.jpg // /path/to/tsuki.jpg
IMG_PATH := c.Path() ImgPath := c.Path()
// jpg // jpg
seps := strings.Split(path.Ext(IMG_PATH), ".") seps := strings.Split(path.Ext(ImgPath), ".")
var IMG_EXT string var ImgExt string
if len(seps) >= 2 { if len(seps) >= 2 {
IMG_EXT = seps[1] ImgExt = seps[1]
} else { } else {
c.Send("Invalid request") c.Send("Invalid request")
return return
} }
// tsuki.jpg // tsuki.jpg
IMG_NAME := path.Base(IMG_PATH) ImgName := path.Base(ImgPath)
// /path/to // /path/to
DIR_PATH := path.Dir(IMG_PATH) DirPath := path.Dir(ImgPath)
// /path/to/tsuki.jpg.webp // /path/to/tsuki.jpg.webp
WEBP_IMG_PATH := DIR_PATH + "/" + IMG_NAME + ".webp" WebpImgPath := DirPath + "/" + ImgName + ".webp"
// /home/webp_server // /home/webp_server
CURRENT_PATH, err := os.Getwd() CurrentPath, err := os.Getwd()
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
} }
// /home/webp_server/exhaust/path/to/tsuki.webp // /home/webp_server/exhaust/path/to/tsuki.webp
WEBP_ABSOLUTE_PATH := CURRENT_PATH + "/exhaust" + WEBP_IMG_PATH WebpAbsolutePath := CurrentPath + "/exhaust" + WebpImgPath
// /home/webp_server/exhaust/path/to // /home/webp_server/exhaust/path/to
DIR_ABSOLUTE_PATH := CURRENT_PATH + "/exhaust" + DIR_PATH DirAbsolutePath := CurrentPath + "/exhaust" + DirPath
// Check file extension // Check file extension
_, found := Find(ALLOWED_TYPES, IMG_EXT) _, found := Find(AllowedTypes, ImgExt)
if !found { if !found {
c.Send("File extension not allowed!") c.Send("File extension not allowed!")
c.SendStatus(403) c.SendStatus(403)
@ -113,44 +113,44 @@ func main() {
} }
// Check the original image for existence // Check the original image for existence
if !imageExists(IMG_ABSOLUTE_PATH) { if !imageExists(ImgAbsolutePath) {
// 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(WEBP_ABSOLUTE_PATH) { if imageExists(WebpAbsolutePath) {
os.Remove(WEBP_ABSOLUTE_PATH) os.Remove(WebpAbsolutePath)
} }
c.Send("File not found!") c.Send("File not found!")
c.SendStatus(404) c.SendStatus(404)
return return
} }
if imageExists(WEBP_ABSOLUTE_PATH) { if imageExists(WebpAbsolutePath) {
c.SendFile(WEBP_ABSOLUTE_PATH) c.SendFile(WebpAbsolutePath)
} else { } else {
// Mkdir // Mkdir
os.MkdirAll(DIR_ABSOLUTE_PATH, os.ModePerm) os.MkdirAll(DirAbsolutePath, os.ModePerm)
// cwebp -q 60 Cute-Baby-Girl.png -o Cute-Baby-Girl.webp // cwebp -q 60 Cute-Baby-Girl.png -o Cute-Baby-Girl.webp
q, _ := strconv.ParseFloat(QUALITY, 32) q, _ := strconv.ParseFloat(QUALITY, 32)
webpEncoder(IMG_ABSOLUTE_PATH, WEBP_ABSOLUTE_PATH, float32(q)) webpEncoder(ImgAbsolutePath, WebpAbsolutePath, float32(q))
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
c.SendFile(WEBP_ABSOLUTE_PATH) c.SendFile(WebpAbsolutePath)
} }
}) })
app.Listen(LISTEN_ADDRESS) app.Listen(ListenAddress)
} }
func load_config(path string) Config { func load_config(path string) Config {
var config Config var config Config
json_object, err := os.Open(path) jsonObject, err := os.Open(path)
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
} }
defer json_object.Close() defer jsonObject.Close()
decoder := json.NewDecoder(json_object) decoder := json.NewDecoder(jsonObject)
decoder.Decode(&config) decoder.Decode(&config)
return config return config
} }