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
remoteRaw = "remote-raw"
config Config
version = "0.4.2"
version = "0.4.3"
releaseUrl = "https://github.com/webp-sh/webp_server_go/releases/latest/download/"
)

View File

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

View File

@ -3,6 +3,7 @@ package main
import (
"bytes"
"fmt"
"github.com/h2non/filetype"
"hash/crc32"
"io"
"io/ioutil"
@ -16,7 +17,6 @@ import (
"strings"
"github.com/h2non/filetype"
log "github.com/sirupsen/logrus"
)
@ -33,6 +33,7 @@ func avifMatcher(buf []byte) bool {
})
}
func getFileContentType(buffer []byte) string {
// TODO deprecated.
var avifType = filetype.NewType("avif", "image/avif")
filetype.AddMatcher(avifType, avifMatcher)
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
STAT, err := os.Stat(rawImagePath)
if err != nil {

View File

@ -45,7 +45,7 @@ func TestImageExists(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")
if !strings.Contains(cwd, "webp_server_go") {
t.Logf("Result: [%v], Expected: [%v]", cwd, "webp_server_go")

View File

@ -2,13 +2,14 @@ package main
import (
"fmt"
"github.com/schollz/progressbar/v3"
log "github.com/sirupsen/logrus"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/schollz/progressbar/v3"
log "github.com/sirupsen/logrus"
)
func prefetchImages(confImgPath string, ExhaustPath string) {
@ -33,7 +34,7 @@ func prefetchImages(confImgPath string, ExhaustPath string) {
}
// RawImagePath string, ImgFilename string, reqURI string
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)
log.Infof("Prefetching %s", picAbsPath)
go convertFilter(picAbsPath, avif, webp, finishChan)

View File

@ -3,7 +3,6 @@ package main
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
@ -65,7 +64,7 @@ func convert(c *fiber.Ctx) error {
}
// 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)
var availableFiles = []string{rawImageAbs}
@ -78,14 +77,18 @@ func convert(c *fiber.Ctx) error {
}
}
var finalFile = findSmallestFiles(availableFiles)
etag := genEtag(finalFile)
c.Set("ETag", etag)
c.Set("X-Compression-Rate", getCompressionRate(rawImageAbs, finalFile))
buf, _ := ioutil.ReadFile(finalFile)
c.Set("content-type", getFileContentType(buf))
return c.SendFile(finalFile)
var finalFileName = findSmallestFiles(availableFiles)
var finalFileExtension = path.Ext(finalFileName)
if finalFileExtension == ".webp" {
c.Set("Content-Type", "image/webp")
} else if finalFileExtension == ".avif" {
c.Set("Content-Type", "image/avif")
}
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 {