diff --git a/.github/workflows/CI.yaml b/.github/workflows/CI.yaml index 56e5832..d8df207 100644 --- a/.github/workflows/CI.yaml +++ b/.github/workflows/CI.yaml @@ -19,7 +19,7 @@ jobs: - name: Install Go uses: actions/setup-go@v3 with: - go-version: '1.20' + go-version: '1.21' - name: Setup necessary packages run: | diff --git a/.github/workflows/codecov.yaml b/.github/workflows/codecov.yaml index be556f6..d617e55 100644 --- a/.github/workflows/codecov.yaml +++ b/.github/workflows/codecov.yaml @@ -19,7 +19,7 @@ jobs: - name: Install Go uses: actions/setup-go@v3 with: - go-version: '1.20' + go-version: '1.21' - name: Setup necessary packages run: | diff --git a/Dockerfile b/Dockerfile index 052a98a..f0aab14 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.20-bookworm as builder +FROM golang:1.21-bookworm as builder ARG IMG_PATH=/opt/pics ARG EXHAUST_PATH=/opt/exhaust diff --git a/config/config.go b/config/config.go index f2cf158..9cf6003 100644 --- a/config/config.go +++ b/config/config.go @@ -51,18 +51,18 @@ WantedBy=multi-user.target` ) var ( - ConfigPath string - Jobs int - DumpSystemd bool - DumpConfig bool - ShowVersion bool - ProxyMode bool - Prefetch bool - Config jsonFile - Version = "0.9.8" - WriteLock = cache.New(5*time.Minute, 10*time.Minute) - RemoteRaw = "./remote-raw" - Metadata = "./metadata" + ConfigPath string + Jobs int + DumpSystemd bool + DumpConfig bool + ShowVersion bool + ProxyMode bool + Prefetch bool + Config jsonFile + Version = "0.9.9" + WriteLock = cache.New(5*time.Minute, 10*time.Minute) + RemoteRaw = "./remote-raw" + Metadata = "./metadata" LocalHostAlias = "local" ) diff --git a/helper/helper.go b/helper/helper.go index 89aa870..8965d58 100644 --- a/helper/helper.go +++ b/helper/helper.go @@ -9,6 +9,8 @@ import ( "time" "webp_server_go/config" + "slices" + "github.com/h2non/filetype" "github.com/cespare/xxhash" @@ -83,14 +85,13 @@ func ImageExists(filename string) bool { } func CheckAllowedType(imgFilename string) bool { - for _, allowedType := range config.Config.AllowedTypes { - if allowedType == "*" { - return true - } - allowedType = "." + strings.ToLower(allowedType) - if strings.HasSuffix(strings.ToLower(imgFilename), allowedType) { - return true - } + if config.Config.AllowedTypes[0] == "*" { + return true + } + imgFilenameExtension := strings.ToLower(path.Ext(imgFilename)) + imgFilenameExtension = strings.TrimPrefix(imgFilenameExtension, ".") // .jpg -> jpg + if slices.Contains(config.Config.AllowedTypes, imgFilenameExtension) { + return true } return false } @@ -137,19 +138,20 @@ func GuessSupportedFormat(header *fasthttp.RequestHeader) []string { supported["avif"] = true } - // chrome on iOS will not send valid image accept header - if strings.Contains(ua, "iPhone OS 14") || strings.Contains(ua, "CPU OS 14") || - strings.Contains(ua, "iPhone OS 15") || strings.Contains(ua, "CPU OS 15") || - strings.Contains(ua, "iPhone OS 16") || strings.Contains(ua, "CPU OS 16") || - strings.Contains(ua, "iPhone OS 17") || strings.Contains(ua, "CPU OS 17") || - strings.Contains(ua, "Android") || strings.Contains(ua, "Linux") { - supported["webp"] = 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 { + if strings.Contains(ua, version) { + supported["webp"] = true + break + } } - // iOS 16 supports AVIF - if strings.Contains(ua, "iPhone OS 16") || strings.Contains(ua, "CPU OS 16") || - strings.Contains(ua, "iPhone OS 17") || strings.Contains(ua, "CPU OS 17") { - supported["avif"] = true + supportedAVIFs := []string{"iPhone OS 16", "CPU OS 16", "iPhone OS 17", "CPU OS 17"} + for _, version := range supportedAVIFs { + if strings.Contains(ua, version) { + supported["avif"] = true + break + } } // save true value's key to slice