Try to mitigate cocurrency problem (#107)

* Try to mitigate cocurrency problem

* Remove getFileContentType in funcion

* Bump to 0.4.3 after fix
This commit is contained in:
Nova Kwok 2022-03-20 16:45:07 +08:00 committed by GitHub
parent 88daad2a05
commit 64db6cc199
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 24 deletions

View File

@ -22,7 +22,7 @@ var (
prefetch, proxyMode bool prefetch, proxyMode bool
remoteRaw = "remote-raw" remoteRaw = "remote-raw"
config Config config Config
version = "0.4.2" version = "0.4.3"
releaseUrl = "https://github.com/webp-sh/webp_server_go/releases/latest/download/" releaseUrl = "https://github.com/webp-sh/webp_server_go/releases/latest/download/"
) )

View File

@ -4,10 +4,6 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"github.com/Kagami/go-avif"
"github.com/chai2010/webp"
log "github.com/sirupsen/logrus"
"golang.org/x/image/bmp"
"image" "image"
"image/jpeg" "image/jpeg"
"image/png" "image/png"
@ -17,6 +13,11 @@ import (
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
"github.com/Kagami/go-avif"
"github.com/chai2010/webp"
log "github.com/sirupsen/logrus"
"golang.org/x/image/bmp"
) )
func convertFilter(raw, avifPath, webpPath string, c chan int) { func convertFilter(raw, avifPath, webpPath string, c chan int) {
@ -72,12 +73,12 @@ func readRawImage(imgPath string, maxPixel int) (img image.Image, err error) {
log.Errorln(err) log.Errorln(err)
} }
contentType := getFileContentType(data[:512]) imgExtension := strings.ToLower(path.Ext(imgPath))
if strings.Contains(contentType, "jpeg") { if strings.Contains(imgExtension, "jpeg") || strings.Contains(imgExtension, "jpg") {
img, err = jpeg.Decode(bytes.NewReader(data)) img, err = jpeg.Decode(bytes.NewReader(data))
} else if strings.Contains(contentType, "png") { } else if strings.Contains(imgExtension, "png") {
img, err = png.Decode(bytes.NewReader(data)) img, err = png.Decode(bytes.NewReader(data))
} else if strings.Contains(contentType, "bmp") { } else if strings.Contains(imgExtension, "bmp") {
img, err = bmp.Decode(bytes.NewReader(data)) img, err = bmp.Decode(bytes.NewReader(data))
} }
if err != nil || img == nil { if err != nil || img == nil {

View File

@ -3,6 +3,7 @@ package main
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/h2non/filetype"
"hash/crc32" "hash/crc32"
"io" "io"
"io/ioutil" "io/ioutil"
@ -16,7 +17,6 @@ import (
"strings" "strings"
"github.com/h2non/filetype"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -33,6 +33,7 @@ func avifMatcher(buf []byte) bool {
}) })
} }
func getFileContentType(buffer []byte) string { func getFileContentType(buffer []byte) string {
// TODO deprecated.
var avifType = filetype.NewType("avif", "image/avif") var avifType = filetype.NewType("avif", "image/avif")
filetype.AddMatcher(avifType, avifMatcher) filetype.AddMatcher(avifType, avifMatcher)
kind, _ := filetype.Match(buffer) kind, _ := filetype.Match(buffer)
@ -127,7 +128,7 @@ func cleanProxyCache(cacheImagePath string) {
} }
} }
func genOptimizedAbs(rawImagePath string, exhaustPath string, imageName string, reqURI string) (string, string) { func genOptimizedAbsPath(rawImagePath string, exhaustPath string, imageName string, reqURI string) (string, string) {
// get file mod time // get file mod time
STAT, err := os.Stat(rawImagePath) STAT, err := os.Stat(rawImagePath)
if err != nil { if err != nil {

View File

@ -45,7 +45,7 @@ func TestImageExists(t *testing.T) {
} }
func TestGenWebpAbs(t *testing.T) { func TestGenWebpAbs(t *testing.T) {
cwd, cooked := genOptimizedAbs("./pics/webp_server.png", "/tmp", cwd, cooked := genOptimizedAbsPath("./pics/webp_server.png", "/tmp",
"test", "a") "test", "a")
if !strings.Contains(cwd, "webp_server_go") { if !strings.Contains(cwd, "webp_server_go") {
t.Logf("Result: [%v], Expected: [%v]", cwd, "webp_server_go") t.Logf("Result: [%v], Expected: [%v]", cwd, "webp_server_go")

View File

@ -2,13 +2,14 @@ package main
import ( import (
"fmt" "fmt"
"github.com/schollz/progressbar/v3"
log "github.com/sirupsen/logrus"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
"github.com/schollz/progressbar/v3"
log "github.com/sirupsen/logrus"
) )
func prefetchImages(confImgPath string, ExhaustPath string) { func prefetchImages(confImgPath string, ExhaustPath string) {
@ -33,7 +34,7 @@ func prefetchImages(confImgPath string, ExhaustPath string) {
} }
// RawImagePath string, ImgFilename string, reqURI string // RawImagePath string, ImgFilename string, reqURI string
proposedURI := strings.Replace(picAbsPath, confImgPath, "", 1) proposedURI := strings.Replace(picAbsPath, confImgPath, "", 1)
avif, webp := genOptimizedAbs(picAbsPath, ExhaustPath, info.Name(), proposedURI) avif, webp := genOptimizedAbsPath(picAbsPath, ExhaustPath, info.Name(), proposedURI)
_ = os.MkdirAll(path.Dir(avif), 0755) _ = os.MkdirAll(path.Dir(avif), 0755)
log.Infof("Prefetching %s", picAbsPath) log.Infof("Prefetching %s", picAbsPath)
go convertFilter(picAbsPath, avif, webp, finishChan) go convertFilter(picAbsPath, avif, webp, finishChan)

View File

@ -3,7 +3,6 @@ package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -65,7 +64,7 @@ func convert(c *fiber.Ctx) error {
} }
// generate with timestamp to make sure files are update-to-date // generate with timestamp to make sure files are update-to-date
avifAbs, webpAbs := genOptimizedAbs(rawImageAbs, config.ExhaustPath, imgFilename, reqURI) avifAbs, webpAbs := genOptimizedAbsPath(rawImageAbs, config.ExhaustPath, imgFilename, reqURI)
convertFilter(rawImageAbs, avifAbs, webpAbs, nil) convertFilter(rawImageAbs, avifAbs, webpAbs, nil)
var availableFiles = []string{rawImageAbs} var availableFiles = []string{rawImageAbs}
@ -78,14 +77,18 @@ func convert(c *fiber.Ctx) error {
} }
} }
var finalFile = findSmallestFiles(availableFiles) var finalFileName = findSmallestFiles(availableFiles)
etag := genEtag(finalFile) var finalFileExtension = path.Ext(finalFileName)
c.Set("ETag", etag) if finalFileExtension == ".webp" {
c.Set("X-Compression-Rate", getCompressionRate(rawImageAbs, finalFile)) c.Set("Content-Type", "image/webp")
buf, _ := ioutil.ReadFile(finalFile) } else if finalFileExtension == ".avif" {
c.Set("content-type", getFileContentType(buf)) c.Set("Content-Type", "image/avif")
return c.SendFile(finalFile) }
etag := genEtag(finalFileName)
c.Set("ETag", etag)
c.Set("X-Compression-Rate", getCompressionRate(rawImageAbs, finalFileName))
return c.SendFile(finalFileName)
} }
func proxyHandler(c *fiber.Ctx, reqURI string) error { func proxyHandler(c *fiber.Ctx, reqURI string) error {