diff --git a/handler/router.go b/handler/router.go index cf7dd089..8ee068d2 100644 --- a/handler/router.go +++ b/handler/router.go @@ -1,10 +1,10 @@ package handler import ( - "fmt" "net/http" "net/url" "regexp" + "slices" "strings" "webp_server_go/config" "webp_server_go/encoder" @@ -72,8 +72,8 @@ func Convert(c *fiber.Ctx) error { // Check if the file extension is allowed and not with image extension // In this case we will serve the file directly + // Since here we've already sent non-image file, "raw" is not supported by default in the following code if helper.CheckAllowedExtension(filename) && !helper.CheckImageExtension(filename) { - fmt.Println("File extension is allowed and not with image extension") return c.SendFile(path.Join(config.Config.ImgPath, reqURI)) } @@ -165,8 +165,11 @@ func Convert(c *fiber.Ctx) error { } supportedFormats := helper.GuessSupportedFormat(reqHeader) - // resize itself and return if only raw(original format) is supported - if supportedFormats["raw"] == true && + // resize itself and return if only raw(jpg,jpeg,png,gif) is supported + if supportedFormats["jpg"] == true && + supportedFormats["jpeg"] == true && + supportedFormats["png"] == true && + supportedFormats["gif"] == true && supportedFormats["webp"] == false && supportedFormats["avif"] == false && supportedFormats["jxl"] == false && @@ -192,7 +195,11 @@ func Convert(c *fiber.Ctx) error { // Do the convertion based on supported formats and config encoder.ConvertFilter(rawImageAbs, jxlAbs, avifAbs, webpAbs, extraParams, supportedFormats, nil) - var availableFiles = []string{rawImageAbs} + var availableFiles = []string{} + // If source image is in jpg/jpeg/png/gif, we can add it to the available files + if slices.Contains([]string{"jpg", "jpeg", "png", "gif"}, helper.GetImageExtension(rawImageAbs)) { + availableFiles = append(availableFiles, rawImageAbs) + } if supportedFormats["avif"] { availableFiles = append(availableFiles, avifAbs) } @@ -202,12 +209,6 @@ func Convert(c *fiber.Ctx) error { if supportedFormats["jxl"] { availableFiles = append(availableFiles, jxlAbs) } - // If raw format is not supported(e,g: heic), remove it from the list - // Because we shouldn't serve the heic format if it's not supported - if !supportedFormats["heic"] && helper.GetImageExtension(rawImageAbs) == "heic" { - // Remove the "raw" from the list - availableFiles = availableFiles[1:] - } finalFilename := helper.FindSmallestFiles(availableFiles) contentType := helper.GetFileContentType(finalFilename) diff --git a/handler/router_test.go b/handler/router_test.go index 0765d7c5..a46900be 100644 --- a/handler/router_test.go +++ b/handler/router_test.go @@ -32,7 +32,7 @@ func setupParam() { // setup parameters here... config.Config.ImgPath = "../pics" config.Config.ExhaustPath = "../exhaust_test" - config.Config.AllowedTypes = []string{"jpg", "png", "jpeg", "bmp", "heic"} + config.Config.AllowedTypes = []string{"jpg", "png", "jpeg", "bmp", "heic", "avif"} config.Config.MetadataPath = "../metadata" config.Config.RemoteRawPath = "../remote-raw" config.ProxyMode = false @@ -128,7 +128,10 @@ func TestConvert(t *testing.T) { "http://127.0.0.1:3333/dir1/inside.jpg": "image/webp", "http://127.0.0.1:3333/%e5%a4%aa%e7%a5%9e%e5%95%a6.png": "image/webp", "http://127.0.0.1:3333/太神啦.png": "image/webp", - "http://127.0.0.1:3333/sample3.heic": "image/webp", // webp because browser does not support heic + // Source: https://filesamples.com/formats/heic + "http://127.0.0.1:3333/sample3.heic": "image/webp", // webp because browser does not support heic + // Source: https://raw.githubusercontent.com/link-u/avif-sample-images/refs/heads/master/kimono.avif + "http://127.0.0.1:3333/kimono.avif": "image/webp", // webp because browser does not support avif } var testChromeAvifLink = map[string]string{ diff --git a/helper/helper.go b/helper/helper.go index 5fd3773f..d26cbd9c 100644 --- a/helper/helper.go +++ b/helper/helper.go @@ -135,17 +135,21 @@ func GetCompressionRate(RawImagePath string, optimizedImg string) string { func GuessSupportedFormat(header *fasthttp.RequestHeader) map[string]bool { var ( - supported = map[string]bool{ - "raw": true, - "webp": false, - "avif": false, - "jxl": false, - "heic": false, - } - - ua = string(header.Peek("user-agent")) - accept = strings.ToLower(string(header.Peek("accept"))) + ua = string(header.Peek("user-agent")) + accept = strings.ToLower(string(header.Peek("accept"))) + supported = map[string]bool{} ) + // Initialize all supported formats to false + for _, item := range config.DefaultAllowedTypes { + supported[item] = false + } + // raw format(jpg,jpeg,png,gif) is always supported + supported["jpg"] = true + supported["jpeg"] = true + supported["png"] = true + supported["gif"] = true + supported["svg"] = true + supported["bmp"] = true if strings.Contains(accept, "image/webp") { supported["webp"] = true diff --git a/helper/helper_test.go b/helper/helper_test.go index abf9705f..ece44508 100644 --- a/helper/helper_test.go +++ b/helper/helper_test.go @@ -59,10 +59,16 @@ func TestGuessSupportedFormat(t *testing.T) { userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Safari/605.1.15", // iPad accept: "image/webp, image/avif", expected: map[string]bool{ - "raw": true, + "jpg": true, + "jpeg": true, + "png": true, + "gif": true, + "svg": true, + "bmp": true, "webp": true, "avif": true, "jxl": false, + "nef": false, "heic": true, }, }, @@ -71,10 +77,16 @@ func TestGuessSupportedFormat(t *testing.T) { userAgent: "iPhone OS 15", accept: "image/webp, image/png", expected: map[string]bool{ - "raw": true, + "jpg": true, + "jpeg": true, + "png": true, + "gif": true, + "svg": true, + "bmp": true, "webp": true, "avif": false, "jxl": false, + "nef": false, "heic": false, }, }, @@ -83,10 +95,16 @@ func TestGuessSupportedFormat(t *testing.T) { userAgent: "iPhone OS 16", accept: "image/webp, image/png", expected: map[string]bool{ - "raw": true, + "jpg": true, + "jpeg": true, + "png": true, + "gif": true, + "svg": true, + "bmp": true, "webp": true, "avif": false, "jxl": false, + "nef": false, "heic": false, }, }, @@ -95,10 +113,16 @@ func TestGuessSupportedFormat(t *testing.T) { userAgent: "iPhone OS 16", accept: "image/webp, image/avif", expected: map[string]bool{ - "raw": true, + "jpg": true, + "jpeg": true, + "png": true, + "gif": true, + "svg": true, + "bmp": true, "webp": true, "avif": true, "jxl": false, + "nef": false, "heic": false, }, }, @@ -107,10 +131,16 @@ func TestGuessSupportedFormat(t *testing.T) { userAgent: "Unknown OS", accept: "image/jpeg, image/gif", expected: map[string]bool{ - "raw": true, + "jpg": true, + "jpeg": true, + "png": true, + "gif": true, + "svg": true, + "bmp": true, "webp": false, "avif": false, "jxl": false, + "nef": false, "heic": false, }, }, diff --git a/pics/kimono.avif b/pics/kimono.avif new file mode 100644 index 00000000..e91fe56e Binary files /dev/null and b/pics/kimono.avif differ