Skip to content

Commit c26dfb9

Browse files
committed
Merge branch 'master' into unit-test
2 parents 6f2ec5d + bfb34a8 commit c26dfb9

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"HOST": "127.0.0.1",
33
"PORT": "3333",
44
"QUALITY": "80",
5-
"IMG_PATH": "/path/to/pics",
5+
"IMG_PATH": "./pics",
66
"EXHAUST_PATH": "",
7-
"ALLOWED_TYPES": ["jpg","png","jpeg","bmp","gif"]
7+
"ALLOWED_TYPES": ["jpg","png","jpeg","bmp"]
88
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ go 1.13
55
require (
66
github.com/chai2010/webp v1.1.0
77
github.com/gofiber/fiber v1.4.0
8-
github.com/sirupsen/logrus v1.4.2
8+
github.com/sirupsen/logrus v1.6.0
99
golang.org/x/image v0.0.0-20200119044424-58c23975cae1
1010
)

helper.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package main
22

33
import (
44
"fmt"
5+
"github.com/gofiber/fiber"
56
log "github.com/sirupsen/logrus"
67
"net/http"
78
"os"
89
"path"
910
"path/filepath"
11+
"strings"
1012
)
1113

1214
func ChanErr(ccc chan int) {
@@ -59,3 +61,45 @@ func GenWebpAbs(RawImagePath string, ExhaustPath string, ImgFilename string, req
5961
WebpAbsolutePath := path.Clean(path.Join(ExhaustPath, path.Dir(reqURI), WebpFilename))
6062
return cwd, WebpAbsolutePath
6163
}
64+
65+
func CheckUA(c *fiber.Ctx, RawImageAbs string) (string, bool) {
66+
// reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent/Firefox
67+
// https://developer.chrome.com/multidevice/user-agent#chrome_for_ios_user_agent
68+
// Chrome
69+
// ✅ Windows: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36
70+
// ✅ macOS: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36
71+
// ✅ Linux: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36
72+
// ✅ iOS: Mozilla/5.0 (iPhone; CPU iPhone OS 13_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/83.0.4103.63 Mobile/15E148 Safari/604.1
73+
// ✅ Android: Mozilla/5.0 (Linux; Android 10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.60 Mobile Safari/537.36
74+
75+
// Firefox
76+
// ✅ Windows: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0
77+
// ✅ macOS: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:76.0) Gecko/20100101 Firefox/76.0
78+
// ✅ Linux: Mozilla/5.0 (X11; Linux i686; rv:76.0) Gecko/20100101 Firefox/76.0
79+
// ✅ iOS: Mozilla/5.0 (iPad; CPU OS 10_15_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/25.0 Mobile/15E148 Safari/605.1.15
80+
// ✅ Android: Mozilla/5.0 (Android 10; Mobile; rv:68.0) Gecko/68.0 Firefox/68.0
81+
82+
// Safari
83+
// ❎ macOS: 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
84+
// ❎ iOS: Mozilla/5.0 (iPad; CPU OS 13_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Mobile/15E148 Safari/604.1
85+
86+
// WeChat
87+
// ❎ iOS: Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Mobile/14G60 wxwork/2.1.5 MicroMessenger/6.3.22
88+
// ✅ Windows: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 MicroMessenger/6.5.2.501 NetType/WIFI WindowsWechat QBCore/3.43.691.400 QQBrowser/9.0.2524.400
89+
// ✅ Android: Mozilla/5.0 (Linux; Android 7.0; LG-H831 Build/NRD90U; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/68.0.3440.91 Mobile Safari/537.36 MicroMessenger/6.6.7.1303(0x26060743) NetType/WIFI Language/zh_TW
90+
91+
UA := c.Get("User-Agent")
92+
93+
if strings.Contains(UA, "Firefox") || strings.Contains(UA, "Chrome") {
94+
// Chrome or firefox on macOS Windows
95+
} else if strings.Contains(UA, "Android") || strings.Contains(UA, "Windows") || strings.Contains(UA, "Linux") {
96+
// on Android, Windows and Linux
97+
} else if strings.Contains(UA, "FxiOS") || strings.Contains(UA, "CriOS") {
98+
//firefox and Chrome on iOS
99+
} else {
100+
log.Infof("A Safari user has arrived...%s", UA)
101+
c.SendFile(RawImageAbs)
102+
return "", true
103+
}
104+
return UA, false
105+
}

router.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ func Convert(ImgPath string, ExhaustPath string, AllowedTypes []string, QUALITY
1919
var RawImageAbs = path.Join(ImgPath, reqURI) // /home/xxx/mypic/123.jpg
2020
var ImgFilename = path.Base(reqURI) // pure filename, 123.jpg
2121
var finalFile string // We'll only need one c.sendFile()
22-
// Check for Safari users. If they're Safari or the UA contains `AppleWebKit`(Might be the WeChat Browser), just simply ignore everything.
23-
UA := c.Get("User-Agent")
24-
if (strings.Contains(UA, "Safari") && !strings.Contains(UA, "Chrome") && !strings.Contains(UA, "Firefox")) || (strings.Contains(UA, "AppleWebKit")) {
25-
log.Info("A Safari user has arrived...")
26-
c.SendFile(RawImageAbs)
22+
23+
UA, done := CheckUA(c, RawImageAbs)
24+
if done {
2725
return
2826
}
2927
log.Debugf("Incoming connection from %s@%s with %s", UA, c.IP(), ImgFilename)

0 commit comments

Comments
 (0)