Refine codes (#184)

* Refine code

* Remove log
This commit is contained in:
Nova Kwok 2023-03-21 13:33:58 +08:00 committed by GitHub
parent dc4da7bae4
commit 090b11fc9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 12 deletions

View File

@ -12,6 +12,7 @@ import (
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
"sync"
"github.com/Kagami/go-avif" "github.com/Kagami/go-avif"
"github.com/chai2010/webp" "github.com/chai2010/webp"
@ -22,20 +23,39 @@ import (
func convertFilter(raw, avifPath, webpPath string, c chan int) { func convertFilter(raw, avifPath, webpPath string, c chan int) {
// all absolute paths // all absolute paths
var wg sync.WaitGroup
wg.Add(2)
if !imageExists(avifPath) && config.EnableAVIF { if !imageExists(avifPath) && config.EnableAVIF {
convertImage(raw, avifPath, "avif") go func() {
err := convertImage(raw, avifPath, "avif")
if err != nil {
log.Errorln(err)
}
defer wg.Done()
}()
} else {
wg.Done()
} }
if !imageExists(webpPath) { if !imageExists(webpPath) {
convertImage(raw, webpPath, "webp") go func() {
err := convertImage(raw, webpPath, "webp")
if err != nil {
log.Errorln(err)
}
defer wg.Done()
}()
} else {
wg.Done()
} }
wg.Wait()
if c != nil { if c != nil {
c <- 1 c <- 1
} }
} }
func convertImage(raw, optimized, itype string) { func convertImage(raw, optimized, itype string) error {
// we don't have abc.jpg.png1582558990.webp // we don't have abc.jpg.png1582558990.webp
// delete the old pic and convert a new one. // delete the old pic and convert a new one.
// optimized: /home/webp_server/exhaust/path/to/tsuki.jpg.1582558990.webp // optimized: /home/webp_server/exhaust/path/to/tsuki.jpg.1582558990.webp
@ -62,11 +82,11 @@ func convertImage(raw, optimized, itype string) {
switch itype { switch itype {
case "webp": case "webp":
_ = webpEncoder(raw, optimized, config.Quality) err = webpEncoder(raw, optimized, config.Quality)
case "avif": case "avif":
avifEncoder(raw, optimized, config.Quality) err = avifEncoder(raw, optimized, config.Quality)
} }
return err
} }
func readRawImage(imgPath string, maxPixel int) (img image.Image, err error) { func readRawImage(imgPath string, maxPixel int) (img image.Image, err error) {
@ -91,7 +111,7 @@ func readRawImage(imgPath string, maxPixel int) (img image.Image, err error) {
x, y := img.Bounds().Max.X, img.Bounds().Max.Y x, y := img.Bounds().Max.X, img.Bounds().Max.Y
if x > maxPixel || y > maxPixel { if x > maxPixel || y > maxPixel {
errinfo := fmt.Sprintf("WebP: %s(%dx%d) is too large", imgPath, x, y) errinfo := fmt.Sprintf("Read image error: %s(%dx%d) is too large", imgPath, x, y)
log.Warnf(errinfo) log.Warnf(errinfo)
return nil, errors.New(errinfo) return nil, errors.New(errinfo)
} }
@ -99,7 +119,7 @@ func readRawImage(imgPath string, maxPixel int) (img image.Image, err error) {
return img, nil return img, nil
} }
func avifEncoder(p1, p2 string, quality float32) { func avifEncoder(p1, p2 string, quality float32) error {
var img image.Image var img image.Image
dst, err := os.Create(p2) dst, err := os.Create(p2)
if err != nil { if err != nil {
@ -108,7 +128,7 @@ func avifEncoder(p1, p2 string, quality float32) {
// AVIF has a maximum resolution of 65536 x 65536 pixels. // AVIF has a maximum resolution of 65536 x 65536 pixels.
img, err = readRawImage(p1, avifMax) img, err = readRawImage(p1, avifMax)
if err != nil { if err != nil {
return return err
} }
err = avif.Encode(dst, img, &avif.Options{ err = avif.Encode(dst, img, &avif.Options{
@ -123,6 +143,7 @@ func avifEncoder(p1, p2 string, quality float32) {
} }
convertLog("AVIF", p1, p2, quality) convertLog("AVIF", p1, p2, quality)
return nil
} }
func webpEncoder(p1, p2 string, quality float32) error { func webpEncoder(p1, p2 string, quality float32) error {

View File

@ -40,13 +40,13 @@ func TestAvifEncoder(t *testing.T) {
func TestNonExistImage(t *testing.T) { func TestNonExistImage(t *testing.T) {
var dest = "/tmp/test-result" var dest = "/tmp/test-result"
_ = webpEncoder("./pics/empty.jpg", dest, 80) webpEncoder("./pics/empty.jpg", dest, 80)
avifEncoder("./pics/empty.jpg", dest, 80) avifEncoder("./pics/empty.jpg", dest, 80)
} }
func TestConvertFail(t *testing.T) { func TestConvertFail(t *testing.T) {
var dest = "/tmp/test-result" var dest = "/tmp/test-result"
_ = webpEncoder("./pics/webp_server.jpg", dest, -1) webpEncoder("./pics/webp_server.jpg", dest, -1)
avifEncoder("./pics/webp_server.jpg", dest, -1) avifEncoder("./pics/webp_server.jpg", dest, -1)
} }
@ -54,7 +54,7 @@ func runEncoder(t *testing.T, file string, dest string) {
if file == "pics/empty.jpg" { if file == "pics/empty.jpg" {
t.Log("Empty file, that's okay.") t.Log("Empty file, that's okay.")
} }
_ = webpEncoder(file, dest, 80) webpEncoder(file, dest, 80)
assertType(t, dest, "image/webp") assertType(t, dest, "image/webp")
} }