Skip to content

Commit

Permalink
Better Export params and UA handle
Browse files Browse the repository at this point in the history
  • Loading branch information
n0vad3v committed Mar 17, 2024
1 parent f9485a2 commit 1ba0c8f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
8 changes: 6 additions & 2 deletions encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,18 @@ func jxlEncoder(img *vips.ImageRef, rawPath string, optimizedPath string) error
// If quality >= 100, we use lossless mode
if quality >= 100 {
buf, _, err = img.ExportJxl(&vips.JxlExportParams{
Effort: 2,
Effort: 1,
Tier: 4,
Lossless: true,
Distance: 1.0,
})
} else {
buf, _, err = img.ExportJxl(&vips.JxlExportParams{
Effort: 2,
Effort: 1,
Tier: 4,
Quality: quality,
Lossless: false,
Distance: 1.0,
})
}

Expand Down
2 changes: 2 additions & 0 deletions handler/router.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handler

import (
"fmt"
"net/http"
"net/url"
"regexp"
Expand Down Expand Up @@ -137,6 +138,7 @@ func Convert(c *fiber.Ctx) error {
}

supportedFormats := helper.GuessSupportedFormat(reqHeader)
fmt.Println("Supported formats:", supportedFormats)
// resize itself and return if only one format(raw) is supported
if len(supportedFormats) == 1 {
dest := path.Join(config.Config.ExhaustPath, targetHostName, metadata.Id)
Expand Down
8 changes: 4 additions & 4 deletions handler/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import (
)

var (
chromeUA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36"
safariUA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15"
curlUA = "curl/7.64.1"
chromeUA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36"
safariUA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15"
safari17UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.3.1 Safari/605.1.15" // <- Mac with Safari 17
curlUA = "curl/7.64.1"

acceptWebP = "image/webp,image/apng,image/*,*/*;q=0.8"
acceptAvif = "image/avif,image/*,*/*;q=0.8"
acceptLegacy = "image/jpeg,image/png"
acceptJxl = "image/jxl"
)

func setupParam() {
Expand Down
18 changes: 10 additions & 8 deletions helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,6 @@ func GuessSupportedFormat(header *fasthttp.RequestHeader) []string {
if strings.Contains(accept, "image/avif") {
supported["avif"] = true
}
if strings.Contains(accept, "image/jxl") {
supported["jxl"] = true
}

supportedWebPs := []string{"iPhone OS 14", "CPU OS 14", "iPhone OS 15", "CPU OS 15", "iPhone OS 16", "CPU OS 16", "iPhone OS 17", "CPU OS 17"}
for _, version := range supportedWebPs {
Expand All @@ -160,11 +157,16 @@ func GuessSupportedFormat(header *fasthttp.RequestHeader) []string {
}
}

supportedJXLs := []string{"iPhone OS 17", "CPU OS 17"}
for _, version := range supportedJXLs {
if strings.Contains(ua, version) {
supported["jxl"] = true
break
// 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
// Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.3.1 Safari/605.1.15 <- Mac
// Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Mobile/15E148 Safari/604.1 <- iPhone @ Safari
supportedJXLs := []string{"iPhone OS 17", "CPU OS 17", "Version/17"}
if strings.Contains(ua, "iPhone") || strings.Contains(ua, "Macintosh") {
for _, version := range supportedJXLs {
if strings.Contains(ua, version) {
supported["jxl"] = true
break
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions helper/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ func TestGuessSupportedFormat(t *testing.T) {
accept string
expected []string
}{
{
name: "WebP/AVIF/JXL Supported",
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: []string{"raw", "webp", "avif", "jxl"},
},
{
name: "WebP/AVIF Supported",
userAgent: "iPhone OS 16",
Expand Down

0 comments on commit 1ba0c8f

Please sign in to comment.