mirror of
https://github.com/woodchen-ink/webp_server_go.git
synced 2025-07-18 13:42:02 +08:00
60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
)
|
|
|
|
func ChanErr(ccc chan int) {
|
|
if ccc != nil {
|
|
ccc <- 1
|
|
}
|
|
}
|
|
|
|
func GetFileContentType(buffer []byte) string {
|
|
// Use the net/http package's handy DectectContentType function. Always returns a valid
|
|
// content-type by returning "application/octet-stream" if no others seemed to match.
|
|
contentType := http.DetectContentType(buffer)
|
|
return contentType
|
|
}
|
|
|
|
func FileCount(dir string) int {
|
|
count := 0
|
|
_ = filepath.Walk(dir,
|
|
func(path string, info os.FileInfo, err error) error {
|
|
if !info.IsDir() {
|
|
count += 1
|
|
}
|
|
return nil
|
|
})
|
|
return count
|
|
}
|
|
|
|
func ImageExists(filename string) bool {
|
|
info, err := os.Stat(filename)
|
|
if os.IsNotExist(err) {
|
|
return false
|
|
}
|
|
return !info.IsDir()
|
|
}
|
|
|
|
func GenWebpAbs(RawImagePath string, ExhaustPath string, ImgFilename string, reqURI string) (string, string) {
|
|
// get file mod time
|
|
STAT, err := os.Stat(RawImagePath)
|
|
if err != nil {
|
|
fmt.Println(err.Error())
|
|
}
|
|
ModifiedTime := STAT.ModTime().Unix()
|
|
// webpFilename: abc.jpg.png -> abc.jpg.png1582558990.webp
|
|
var WebpFilename = fmt.Sprintf("%s.%d.webp", ImgFilename, ModifiedTime)
|
|
cwd, _ := os.Getwd()
|
|
|
|
// /home/webp_server/exhaust/path/to/tsuki.jpg.1582558990.webp
|
|
// 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
|
|
}
|