Add support for Go 1.21, bump version, optimize code (#264)

This commit is contained in:
Nova Kwok 2023-08-09 22:49:40 +08:00 committed by GitHub
parent 4003b03022
commit 6790ddda46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 34 deletions

View File

@ -19,7 +19,7 @@ jobs:
- name: Install Go - name: Install Go
uses: actions/setup-go@v3 uses: actions/setup-go@v3
with: with:
go-version: '1.20' go-version: '1.21'
- name: Setup necessary packages - name: Setup necessary packages
run: | run: |

View File

@ -19,7 +19,7 @@ jobs:
- name: Install Go - name: Install Go
uses: actions/setup-go@v3 uses: actions/setup-go@v3
with: with:
go-version: '1.20' go-version: '1.21'
- name: Setup necessary packages - name: Setup necessary packages
run: | run: |

View File

@ -1,4 +1,4 @@
FROM golang:1.20-bookworm as builder FROM golang:1.21-bookworm as builder
ARG IMG_PATH=/opt/pics ARG IMG_PATH=/opt/pics
ARG EXHAUST_PATH=/opt/exhaust ARG EXHAUST_PATH=/opt/exhaust

View File

@ -51,18 +51,18 @@ WantedBy=multi-user.target`
) )
var ( var (
ConfigPath string ConfigPath string
Jobs int Jobs int
DumpSystemd bool DumpSystemd bool
DumpConfig bool DumpConfig bool
ShowVersion bool ShowVersion bool
ProxyMode bool ProxyMode bool
Prefetch bool Prefetch bool
Config jsonFile Config jsonFile
Version = "0.9.8" Version = "0.9.9"
WriteLock = cache.New(5*time.Minute, 10*time.Minute) WriteLock = cache.New(5*time.Minute, 10*time.Minute)
RemoteRaw = "./remote-raw" RemoteRaw = "./remote-raw"
Metadata = "./metadata" Metadata = "./metadata"
LocalHostAlias = "local" LocalHostAlias = "local"
) )

View File

@ -9,6 +9,8 @@ import (
"time" "time"
"webp_server_go/config" "webp_server_go/config"
"slices"
"github.com/h2non/filetype" "github.com/h2non/filetype"
"github.com/cespare/xxhash" "github.com/cespare/xxhash"
@ -83,14 +85,13 @@ func ImageExists(filename string) bool {
} }
func CheckAllowedType(imgFilename string) bool { func CheckAllowedType(imgFilename string) bool {
for _, allowedType := range config.Config.AllowedTypes { if config.Config.AllowedTypes[0] == "*" {
if allowedType == "*" { return true
return true }
} imgFilenameExtension := strings.ToLower(path.Ext(imgFilename))
allowedType = "." + strings.ToLower(allowedType) imgFilenameExtension = strings.TrimPrefix(imgFilenameExtension, ".") // .jpg -> jpg
if strings.HasSuffix(strings.ToLower(imgFilename), allowedType) { if slices.Contains(config.Config.AllowedTypes, imgFilenameExtension) {
return true return true
}
} }
return false return false
} }
@ -137,19 +138,20 @@ func GuessSupportedFormat(header *fasthttp.RequestHeader) []string {
supported["avif"] = true supported["avif"] = true
} }
// chrome on iOS will not send valid image accept header 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"}
if strings.Contains(ua, "iPhone OS 14") || strings.Contains(ua, "CPU OS 14") || for _, version := range supportedWebPs {
strings.Contains(ua, "iPhone OS 15") || strings.Contains(ua, "CPU OS 15") || if strings.Contains(ua, version) {
strings.Contains(ua, "iPhone OS 16") || strings.Contains(ua, "CPU OS 16") || supported["webp"] = true
strings.Contains(ua, "iPhone OS 17") || strings.Contains(ua, "CPU OS 17") || break
strings.Contains(ua, "Android") || strings.Contains(ua, "Linux") { }
supported["webp"] = true
} }
// iOS 16 supports AVIF supportedAVIFs := []string{"iPhone OS 16", "CPU OS 16", "iPhone OS 17", "CPU OS 17"}
if strings.Contains(ua, "iPhone OS 16") || strings.Contains(ua, "CPU OS 16") || for _, version := range supportedAVIFs {
strings.Contains(ua, "iPhone OS 17") || strings.Contains(ua, "CPU OS 17") { if strings.Contains(ua, version) {
supported["avif"] = true supported["avif"] = true
break
}
} }
// save true value's key to slice // save true value's key to slice