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 (
"fmt"
log "github.com/sirupsen/logrus"
"hash/crc32"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
log "github.com/sirupsen/logrus"
"strings"
)
@ -61,6 +64,15 @@ func GenWebpAbs(RawImagePath string, ExhaustPath string, ImgFilename string, req
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 {
// for more information, please check test case
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)
if done {
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)
return
}
@ -46,6 +50,8 @@ func Convert(ImgPath string, ExhaustPath string, AllowedTypes []string, QUALITY
log.Warn(msg)
c.Send(msg)
if ImageExists(RawImageAbs) {
etag := GenEtag(RawImageAbs)
c.Set("ETag", etag)
c.SendFile(RawImageAbs)
}
return
@ -95,6 +101,8 @@ func Convert(ImgPath string, ExhaustPath string, AllowedTypes []string, QUALITY
}
finalFile = WebpAbsPath
}
etag := GenEtag(finalFile)
c.Set("ETag", etag)
c.SendFile(finalFile)
}
}