change webpEncoder signature for incoming refator,

auto update enchance: replace binary in place.
This commit is contained in:
BennyThink 2021-11-28 13:35:10 +08:00
parent aa0af31808
commit dbac67fe55
No known key found for this signature in database
GPG Key ID: 6CD0DBDA5235D481
7 changed files with 25 additions and 30 deletions

View File

@ -17,16 +17,14 @@ import (
"golang.org/x/image/bmp"
)
func webpEncoder(p1, p2 string, quality float32, Log bool, c chan int) (err error) {
func webpEncoder(p1, p2 string, quality float32) (err error) {
// if convert fails, return error; success nil
log.Debugf("target: %s with quality of %f", path.Base(p1), quality)
var buf bytes.Buffer
var img image.Image
data, err := ioutil.ReadFile(p1)
if err != nil {
chanErr(c)
return
}
@ -46,27 +44,19 @@ func webpEncoder(p1, p2 string, quality float32, Log bool, c chan int) (err erro
if img == nil {
msg := "image file " + path.Base(p1) + " is corrupted or not supported"
log.Debug(msg)
err = errors.New(msg)
chanErr(c)
return
return errors.New(msg)
}
if err = webp.Encode(&buf, img, &webp.Options{Lossless: false, Quality: quality}); err != nil {
log.Error(err)
chanErr(c)
return
}
if err = ioutil.WriteFile(p2, buf.Bytes(), 0644); err != nil {
log.Error(err)
chanErr(c)
return
}
if Log {
log.Info("Save to " + p2 + " ok!\n")
}
chanErr(c)
return nil
}

View File

@ -30,7 +30,7 @@ func TestWebpEncoder(t *testing.T) {
_ = os.Remove(webp)
// test error
err := webpEncoder("./pics/empty.jpg", webp, 80, true, nil)
err := webpEncoder("./pics/empty.jpg", webp, 80)
assert.NotNil(t, err)
_ = os.Remove(webp)
}
@ -38,21 +38,20 @@ func TestWebpEncoder(t *testing.T) {
func TestNonExistImage(t *testing.T) {
var webp = "/tmp/test-result.webp"
// test error
var err = webpEncoder("./pics/empty.jpg", webp, 80, true, nil)
var err = webpEncoder("./pics/empty.jpg", webp, 80)
assert.NotNil(t, err)
_ = os.Remove(webp)
}
func TestConvertFail(t *testing.T) {
var webp = "/tmp/test-result.webp"
var err = webpEncoder("./pics/webp_server.jpg", webp, -1, true, nil)
var err = webpEncoder("./pics/webp_server.jpg", webp, -1)
assert.NotNil(t, t, err)
}
func runEncoder(t *testing.T, file string, webp string) {
var c chan int
//t.Logf("convert from %s to %s", file, webp)
var err = webpEncoder(file, webp, 80, true, c)
var err = webpEncoder(file, webp, 80)
if file == "pics/empty.jpg" && err != nil {
t.Log("Empty file, that's okay.")
} else if err != nil {

1
go.mod
View File

@ -6,6 +6,7 @@ require (
github.com/chai2010/webp v1.1.0
github.com/gofiber/fiber/v2 v2.4.0
github.com/sirupsen/logrus v1.6.0
github.com/staktrace/go-update v0.0.0-20210525161054-fc019945f9a2
github.com/stretchr/testify v1.3.0
golang.org/x/image v0.0.0-20200119044424-58c23975cae1
)

6
go.sum
View File

@ -1,15 +1,21 @@
github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4=
github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/klauspost/compress v1.10.7 h1:7rix8v8GpI3ZBb0nSozFRgbtXKv+hOe+qfEpZqybrAg=
github.com/klauspost/compress v1.10.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/staktrace/go-update v0.0.0-20210525161054-fc019945f9a2 h1:kyTDvRL8TyTHOx0aK1PKMPVfkI7rCLDC1nrKF7SYOBc=
github.com/staktrace/go-update v0.0.0-20210525161054-fc019945f9a2/go.mod h1:yzHsbjWRHVF1vVGsluwqCc2TvogFSx9C8TeBh34cMb0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=

View File

@ -34,7 +34,7 @@ func prefetchImages(confImgPath string, ExhaustPath string, QUALITY string) {
_, p2 := genWebpAbs(picAbsPath, ExhaustPath, info.Name(), proposedURI)
q, _ := strconv.ParseFloat(QUALITY, 32)
_ = os.MkdirAll(path.Dir(p2), 0755)
go webpEncoder(picAbsPath, p2, float32(q), false, finishChan)
go webpEncoder(picAbsPath, p2, float32(q))
count += <-finishChan
//progress bar
_, _ = fmt.Fprintf(os.Stdout, "[Webp Server started] - convert in progress: %d/%d\r", count, all)

View File

@ -106,7 +106,7 @@ func convert(c *fiber.Ctx) error {
//for webp, we need to create dir first
err = os.MkdirAll(path.Dir(webpAbsPath), 0755)
q, _ := strconv.ParseFloat(config.Quality, 32)
err = webpEncoder(rawImageAbs, webpAbsPath, float32(q), true, nil)
err = webpEncoder(rawImageAbs, webpAbsPath, float32(q))
if err != nil {
log.Error(err)
@ -143,7 +143,7 @@ func proxyHandler(c *fiber.Ctx, reqURI string) error {
_ = fetchRemoteImage(localRawImagePath, realRemoteAddr)
q, _ := strconv.ParseFloat(config.Quality, 32)
_ = os.MkdirAll(path.Dir(localEtagWebPPath), 0755)
err := webpEncoder(localRawImagePath, localEtagWebPPath, float32(q), true, nil)
err := webpEncoder(localRawImagePath, localEtagWebPPath, float32(q))
if err != nil {
log.Warning(err)
}

View File

@ -3,13 +3,11 @@ package main
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/staktrace/go-update"
"io/ioutil"
"net/http"
"os"
"path"
"runtime"
log "github.com/sirupsen/logrus"
)
func autoUpdate() {
@ -47,12 +45,13 @@ func autoUpdate() {
log.Debugf("%s-%s not found on release.", runtime.GOOS, runtime.GOARCH)
return
}
data, _ := ioutil.ReadAll(resp.Body)
_ = os.Mkdir("update", 0755)
err := ioutil.WriteFile(path.Join("update", filename), data, 0755)
if err == nil {
log.Info("Update complete. Please find your binary from update directory.")
err := update.Apply(resp.Body, update.Options{})
if err != nil {
// error handling
log.Errorf("Update error. %v", err)
} else {
log.Info("Update complete. Please restart to apply changes.")
}
_ = resp.Body.Close()
}