Skip to content

Commit

Permalink
Always return supported image format
Browse files Browse the repository at this point in the history
  • Loading branch information
n0vad3v committed Dec 27, 2024
1 parent 7b62794 commit cf04036
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 28 deletions.
23 changes: 12 additions & 11 deletions handler/router.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package handler

import (
"fmt"
"net/http"
"net/url"
"regexp"
"slices"
"strings"
"webp_server_go/config"
"webp_server_go/encoder"
Expand Down Expand Up @@ -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))
}

Expand Down Expand Up @@ -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 &&
Expand All @@ -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)
}
Expand All @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions handler/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down
24 changes: 14 additions & 10 deletions helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 35 additions & 5 deletions helper/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
Expand All @@ -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,
},
},
Expand All @@ -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,
},
},
Expand All @@ -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,
},
},
Expand All @@ -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,
},
},
Expand Down
Binary file added pics/kimono.avif
Binary file not shown.

0 comments on commit cf04036

Please sign in to comment.