Try add Etag on response by nova

This commit is contained in:
Nova Kwok 2020-06-30 17:58:58 +08:00 committed by Benny
parent 99f36b35f7
commit cfe84bf67c
No known key found for this signature in database
GPG Key ID: 9F29E5246B1A0B61
2 changed files with 21 additions and 1 deletions

View File

@ -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") {

View File

@ -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)
} }
} }