mirror of
https://github.com/woodchen-ink/webp_server_go.git
synced 2025-07-18 21:52:01 +08:00
Try add Etag on response by nova
This commit is contained in:
parent
99f36b35f7
commit
cfe84bf67c
14
helper.go
14
helper.go
@ -2,11 +2,14 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
log "github.com/sirupsen/logrus"
|
"hash/crc32"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -61,6 +64,15 @@ func GenWebpAbs(RawImagePath string, ExhaustPath string, ImgFilename string, req
|
|||||||
return cwd, WebpAbsolutePath
|
return cwd, WebpAbsolutePath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GenEtag(ImgAbsPath string) string {
|
||||||
|
data, err := ioutil.ReadFile(ImgAbsPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Info(err)
|
||||||
|
}
|
||||||
|
crc := crc32.ChecksumIEEE(data)
|
||||||
|
return fmt.Sprintf(`W/"%d-%08X"`, len(data), crc)
|
||||||
|
}
|
||||||
|
|
||||||
func goOrigin(UA string) bool {
|
func goOrigin(UA string) bool {
|
||||||
// for more information, please check test case
|
// for more information, please check test case
|
||||||
if strings.Contains(UA, "Firefox") || strings.Contains(UA, "Chrome") {
|
if strings.Contains(UA, "Firefox") || strings.Contains(UA, "Chrome") {
|
||||||
|
@ -23,6 +23,10 @@ func Convert(ImgPath string, ExhaustPath string, AllowedTypes []string, QUALITY
|
|||||||
done := goOrigin(UA)
|
done := goOrigin(UA)
|
||||||
if done {
|
if done {
|
||||||
log.Infof("A Safari/IE/whatever user has arrived...%s", UA)
|
log.Infof("A Safari/IE/whatever user has arrived...%s", UA)
|
||||||
|
// Check for Safari users. If they're Safari, just simply ignore everything.
|
||||||
|
|
||||||
|
etag := GenEtag(RawImageAbs)
|
||||||
|
c.Set("ETag", etag)
|
||||||
c.SendFile(RawImageAbs)
|
c.SendFile(RawImageAbs)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -46,6 +50,8 @@ func Convert(ImgPath string, ExhaustPath string, AllowedTypes []string, QUALITY
|
|||||||
log.Warn(msg)
|
log.Warn(msg)
|
||||||
c.Send(msg)
|
c.Send(msg)
|
||||||
if ImageExists(RawImageAbs) {
|
if ImageExists(RawImageAbs) {
|
||||||
|
etag := GenEtag(RawImageAbs)
|
||||||
|
c.Set("ETag", etag)
|
||||||
c.SendFile(RawImageAbs)
|
c.SendFile(RawImageAbs)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -95,6 +101,8 @@ func Convert(ImgPath string, ExhaustPath string, AllowedTypes []string, QUALITY
|
|||||||
}
|
}
|
||||||
finalFile = WebpAbsPath
|
finalFile = WebpAbsPath
|
||||||
}
|
}
|
||||||
|
etag := GenEtag(finalFile)
|
||||||
|
c.Set("ETag", etag)
|
||||||
c.SendFile(finalFile)
|
c.SendFile(finalFile)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user