mirror of
https://github.com/woodchen-ink/webp_server_go.git
synced 2025-07-18 13:42:02 +08:00
change webpEncoder signature for incoming refator,
auto update enchance: replace binary in place.
This commit is contained in:
parent
aa0af31808
commit
dbac67fe55
14
encoder.go
14
encoder.go
@ -17,16 +17,14 @@ import (
|
|||||||
"golang.org/x/image/bmp"
|
"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
|
// if convert fails, return error; success nil
|
||||||
|
|
||||||
log.Debugf("target: %s with quality of %f", path.Base(p1), quality)
|
log.Debugf("target: %s with quality of %f", path.Base(p1), quality)
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,27 +44,19 @@ func webpEncoder(p1, p2 string, quality float32, Log bool, c chan int) (err erro
|
|||||||
if img == nil {
|
if img == nil {
|
||||||
msg := "image file " + path.Base(p1) + " is corrupted or not supported"
|
msg := "image file " + path.Base(p1) + " is corrupted or not supported"
|
||||||
log.Debug(msg)
|
log.Debug(msg)
|
||||||
err = errors.New(msg)
|
return errors.New(msg)
|
||||||
chanErr(c)
|
|
||||||
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.Error(err)
|
log.Error(err)
|
||||||
chanErr(c)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err = ioutil.WriteFile(p2, buf.Bytes(), 0644); err != nil {
|
if err = ioutil.WriteFile(p2, buf.Bytes(), 0644); err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
chanErr(c)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if Log {
|
|
||||||
log.Info("Save to " + p2 + " ok!\n")
|
log.Info("Save to " + p2 + " ok!\n")
|
||||||
}
|
|
||||||
|
|
||||||
chanErr(c)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ func TestWebpEncoder(t *testing.T) {
|
|||||||
_ = os.Remove(webp)
|
_ = os.Remove(webp)
|
||||||
|
|
||||||
// test error
|
// test error
|
||||||
err := webpEncoder("./pics/empty.jpg", webp, 80, true, nil)
|
err := webpEncoder("./pics/empty.jpg", webp, 80)
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
_ = os.Remove(webp)
|
_ = os.Remove(webp)
|
||||||
}
|
}
|
||||||
@ -38,21 +38,20 @@ func TestWebpEncoder(t *testing.T) {
|
|||||||
func TestNonExistImage(t *testing.T) {
|
func TestNonExistImage(t *testing.T) {
|
||||||
var webp = "/tmp/test-result.webp"
|
var webp = "/tmp/test-result.webp"
|
||||||
// test error
|
// test error
|
||||||
var err = webpEncoder("./pics/empty.jpg", webp, 80, true, nil)
|
var err = webpEncoder("./pics/empty.jpg", webp, 80)
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
_ = os.Remove(webp)
|
_ = os.Remove(webp)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConvertFail(t *testing.T) {
|
func TestConvertFail(t *testing.T) {
|
||||||
var webp = "/tmp/test-result.webp"
|
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)
|
assert.NotNil(t, t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func runEncoder(t *testing.T, file string, webp string) {
|
func runEncoder(t *testing.T, file string, webp string) {
|
||||||
var c chan int
|
|
||||||
//t.Logf("convert from %s to %s", file, webp)
|
//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 {
|
if file == "pics/empty.jpg" && err != nil {
|
||||||
t.Log("Empty file, that's okay.")
|
t.Log("Empty file, that's okay.")
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
|
1
go.mod
1
go.mod
@ -6,6 +6,7 @@ require (
|
|||||||
github.com/chai2010/webp v1.1.0
|
github.com/chai2010/webp v1.1.0
|
||||||
github.com/gofiber/fiber/v2 v2.4.0
|
github.com/gofiber/fiber/v2 v2.4.0
|
||||||
github.com/sirupsen/logrus v1.6.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
|
github.com/stretchr/testify v1.3.0
|
||||||
golang.org/x/image v0.0.0-20200119044424-58c23975cae1
|
golang.org/x/image v0.0.0-20200119044424-58c23975cae1
|
||||||
)
|
)
|
||||||
|
6
go.sum
6
go.sum
@ -1,15 +1,21 @@
|
|||||||
github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4=
|
github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4=
|
||||||
github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
|
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.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/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 h1:7rix8v8GpI3ZBb0nSozFRgbtXKv+hOe+qfEpZqybrAg=
|
||||||
github.com/klauspost/compress v1.10.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
|
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/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/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 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
|
||||||
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
|
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/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.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/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 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||||
|
@ -34,7 +34,7 @@ func prefetchImages(confImgPath string, ExhaustPath string, QUALITY string) {
|
|||||||
_, p2 := genWebpAbs(picAbsPath, ExhaustPath, info.Name(), proposedURI)
|
_, p2 := genWebpAbs(picAbsPath, ExhaustPath, info.Name(), proposedURI)
|
||||||
q, _ := strconv.ParseFloat(QUALITY, 32)
|
q, _ := strconv.ParseFloat(QUALITY, 32)
|
||||||
_ = os.MkdirAll(path.Dir(p2), 0755)
|
_ = os.MkdirAll(path.Dir(p2), 0755)
|
||||||
go webpEncoder(picAbsPath, p2, float32(q), false, finishChan)
|
go webpEncoder(picAbsPath, p2, float32(q))
|
||||||
count += <-finishChan
|
count += <-finishChan
|
||||||
//progress bar
|
//progress bar
|
||||||
_, _ = fmt.Fprintf(os.Stdout, "[Webp Server started] - convert in progress: %d/%d\r", count, all)
|
_, _ = fmt.Fprintf(os.Stdout, "[Webp Server started] - convert in progress: %d/%d\r", count, all)
|
||||||
|
@ -106,7 +106,7 @@ func convert(c *fiber.Ctx) error {
|
|||||||
//for webp, we need to create dir first
|
//for webp, we need to create dir first
|
||||||
err = os.MkdirAll(path.Dir(webpAbsPath), 0755)
|
err = os.MkdirAll(path.Dir(webpAbsPath), 0755)
|
||||||
q, _ := strconv.ParseFloat(config.Quality, 32)
|
q, _ := strconv.ParseFloat(config.Quality, 32)
|
||||||
err = webpEncoder(rawImageAbs, webpAbsPath, float32(q), true, nil)
|
err = webpEncoder(rawImageAbs, webpAbsPath, float32(q))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
@ -143,7 +143,7 @@ func proxyHandler(c *fiber.Ctx, reqURI string) error {
|
|||||||
_ = fetchRemoteImage(localRawImagePath, realRemoteAddr)
|
_ = fetchRemoteImage(localRawImagePath, realRemoteAddr)
|
||||||
q, _ := strconv.ParseFloat(config.Quality, 32)
|
q, _ := strconv.ParseFloat(config.Quality, 32)
|
||||||
_ = os.MkdirAll(path.Dir(localEtagWebPPath), 0755)
|
_ = os.MkdirAll(path.Dir(localEtagWebPPath), 0755)
|
||||||
err := webpEncoder(localRawImagePath, localEtagWebPPath, float32(q), true, nil)
|
err := webpEncoder(localRawImagePath, localEtagWebPPath, float32(q))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warning(err)
|
log.Warning(err)
|
||||||
}
|
}
|
||||||
|
17
update.go
17
update.go
@ -3,13 +3,11 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"github.com/staktrace/go-update"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func autoUpdate() {
|
func autoUpdate() {
|
||||||
@ -47,12 +45,13 @@ func autoUpdate() {
|
|||||||
log.Debugf("%s-%s not found on release.", runtime.GOOS, runtime.GOARCH)
|
log.Debugf("%s-%s not found on release.", runtime.GOOS, runtime.GOARCH)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
data, _ := ioutil.ReadAll(resp.Body)
|
|
||||||
_ = os.Mkdir("update", 0755)
|
|
||||||
err := ioutil.WriteFile(path.Join("update", filename), data, 0755)
|
|
||||||
|
|
||||||
if err == nil {
|
err := update.Apply(resp.Body, update.Options{})
|
||||||
log.Info("Update complete. Please find your binary from update directory.")
|
if err != nil {
|
||||||
|
// error handling
|
||||||
|
log.Errorf("Update error. %v", err)
|
||||||
|
} else {
|
||||||
|
log.Info("Update complete. Please restart to apply changes.")
|
||||||
}
|
}
|
||||||
_ = resp.Body.Close()
|
_ = resp.Body.Close()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user