diff --git a/config.go b/config.go index dcf5ca0..22dc2d6 100644 --- a/config.go +++ b/config.go @@ -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/" ) diff --git a/encoder.go b/encoder.go index cebc20a..d6193da 100644 --- a/encoder.go +++ b/encoder.go @@ -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 { diff --git a/helper.go b/helper.go index d9e3130..5ef45fb 100644 --- a/helper.go +++ b/helper.go @@ -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 { diff --git a/helper_test.go b/helper_test.go index 9217e8a..73144b1 100644 --- a/helper_test.go +++ b/helper_test.go @@ -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") diff --git a/prefetch.go b/prefetch.go index 1dbca1d..654b29e 100644 --- a/prefetch.go +++ b/prefetch.go @@ -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) diff --git a/router.go b/router.go index 5e09b4d..8e08a55 100644 --- a/router.go +++ b/router.go @@ -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 {