mirror of
https://github.com/woodchen-ink/webp_server_go.git
synced 2025-07-18 13:42:02 +08:00
Merge branch 'progressbar'
# Conflicts: # webp-server.go
This commit is contained in:
commit
d514865b64
@ -16,6 +16,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -61,15 +62,22 @@ func GetFileContentType(buffer []byte) string {
|
|||||||
return contentType
|
return contentType
|
||||||
}
|
}
|
||||||
|
|
||||||
func webpEncoder(p1, p2 string, quality float32, Log bool) (err error) {
|
func chanErr(ccc chan int) {
|
||||||
|
if ccc != nil {
|
||||||
|
ccc <- 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func webpEncoder(p1, p2 string, quality float32, Log bool, c chan int) (err error) {
|
||||||
// if convert fails, return error; success nil
|
// if convert fails, return error; success nil
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
var img image.Image
|
var img image.Image
|
||||||
|
|
||||||
data, err := ioutil.ReadFile(p1)
|
data, err := ioutil.ReadFile(p1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
chanErr(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
contentType := GetFileContentType(data[:512])
|
contentType := GetFileContentType(data[:512])
|
||||||
if strings.Contains(contentType, "jpeg") {
|
if strings.Contains(contentType, "jpeg") {
|
||||||
img, _ = jpeg.Decode(bytes.NewReader(data))
|
img, _ = jpeg.Decode(bytes.NewReader(data))
|
||||||
@ -84,21 +92,27 @@ func webpEncoder(p1, p2 string, quality float32, Log bool) (err error) {
|
|||||||
msg := "image file " + path.Base(p1) + " is corrupted or not supported"
|
msg := "image file " + path.Base(p1) + " is corrupted or not supported"
|
||||||
log.Println(msg)
|
log.Println(msg)
|
||||||
err = errors.New(msg)
|
err = errors.New(msg)
|
||||||
|
chanErr(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = webp.Encode(&buf, img, &webp.Options{Lossless: false, Quality: quality}); err != nil {
|
if err = webp.Encode(&buf, img, &webp.Options{Lossless: false, Quality: quality}); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
chanErr(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err = ioutil.WriteFile(p2, buf.Bytes(), os.ModePerm); err != nil {
|
if err = ioutil.WriteFile(p2, buf.Bytes(), os.ModePerm); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
chanErr(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if Log {
|
if Log {
|
||||||
fmt.Printf("Save to %s ok\n", p2)
|
fmt.Printf("Save to %s ok\n", p2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
chanErr(c)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,7 +183,7 @@ func Convert(ImgPath string, AllowedTypes []string, QUALITY string) func(c *fibe
|
|||||||
//for webp, we need to create dir first
|
//for webp, we need to create dir first
|
||||||
_ = os.MkdirAll(path.Dir(WebpAbsPath), os.ModePerm)
|
_ = os.MkdirAll(path.Dir(WebpAbsPath), os.ModePerm)
|
||||||
q, _ := strconv.ParseFloat(QUALITY, 32)
|
q, _ := strconv.ParseFloat(QUALITY, 32)
|
||||||
err = webpEncoder(RawImageAbs, WebpAbsPath, float32(q), true)
|
err = webpEncoder(RawImageAbs, WebpAbsPath, float32(q), true, nil)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
@ -215,6 +229,11 @@ func prefetchImages(confImgPath string, QUALITY string) {
|
|||||||
fmt.Println(`Prefetch will convert all your images to webp, it may take some time and consume a lot of CPU resource. Do you want to proceed(Y/n)`)
|
fmt.Println(`Prefetch will convert all your images to webp, it may take some time and consume a lot of CPU resource. Do you want to proceed(Y/n)`)
|
||||||
reader := bufio.NewReader(os.Stdin)
|
reader := bufio.NewReader(os.Stdin)
|
||||||
char, _, _ := reader.ReadRune() //y Y enter
|
char, _, _ := reader.ReadRune() //y Y enter
|
||||||
|
// maximum ongoing prefetch is depending on your core of CPU
|
||||||
|
var finishChan = make(chan int, runtime.NumCPU())
|
||||||
|
for i := 0; i < runtime.NumCPU(); i++ {
|
||||||
|
finishChan <- 0
|
||||||
|
}
|
||||||
if char == 121 || char == 10 || char == 89 {
|
if char == 121 || char == 10 || char == 89 {
|
||||||
//prefetch, recursive through the dir
|
//prefetch, recursive through the dir
|
||||||
all := fileCount(confImgPath)
|
all := fileCount(confImgPath)
|
||||||
@ -229,9 +248,9 @@ func prefetchImages(confImgPath string, QUALITY string) {
|
|||||||
_, p2 := genWebpAbs(picAbsPath, info.Name(), proposedURI)
|
_, p2 := genWebpAbs(picAbsPath, info.Name(), proposedURI)
|
||||||
q, _ := strconv.ParseFloat(QUALITY, 32)
|
q, _ := strconv.ParseFloat(QUALITY, 32)
|
||||||
_ = os.MkdirAll(path.Dir(p2), os.ModePerm)
|
_ = os.MkdirAll(path.Dir(p2), os.ModePerm)
|
||||||
go webpEncoder(picAbsPath, p2, float32(q), false)
|
go webpEncoder(picAbsPath, p2, float32(q), false, finishChan)
|
||||||
count += 1
|
count += <-finishChan
|
||||||
// progress bar
|
//progress bar
|
||||||
_, _ = fmt.Fprintf(os.Stdout, "Convert in progress: %d/%d\r", count, all)
|
_, _ = fmt.Fprintf(os.Stdout, "Convert in progress: %d/%d\r", count, all)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@ -239,7 +258,9 @@ func prefetchImages(confImgPath string, QUALITY string) {
|
|||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Fprintf(os.Stdout, "Prefetch complete,")
|
|
||||||
|
_, _ = fmt.Fprintf(os.Stdout, "Prefetch completeY(^_^)Y\n\n")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user