1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-01-23 11:14:48 +02:00
imgproxy/etag.go

48 lines
881 B
Go
Raw Normal View History

package main
import (
2018-10-05 22:20:29 +06:00
"context"
"crypto/sha256"
2018-10-06 01:45:14 +06:00
"encoding/hex"
"encoding/json"
"hash"
"sync"
2021-04-26 17:52:50 +06:00
"github.com/imgproxy/imgproxy/v2/imagedata"
"github.com/imgproxy/imgproxy/v2/options"
"github.com/imgproxy/imgproxy/v2/version"
)
type eTagCalc struct {
2018-10-06 01:45:14 +06:00
hash hash.Hash
enc *json.Encoder
}
var eTagCalcPool = sync.Pool{
New: func() interface{} {
h := sha256.New()
2018-10-06 01:45:14 +06:00
enc := json.NewEncoder(h)
enc.SetEscapeHTML(false)
enc.SetIndent("", "")
2018-10-06 01:45:14 +06:00
return &eTagCalc{h, enc}
},
2018-10-06 01:45:14 +06:00
}
2021-04-26 17:52:50 +06:00
func calcETag(ctx context.Context, imgdata *imagedata.ImageData, po *options.ProcessingOptions) string {
c := eTagCalcPool.Get().(*eTagCalc)
defer eTagCalcPool.Put(c)
2018-10-06 01:45:14 +06:00
c.hash.Reset()
2021-04-26 17:52:50 +06:00
c.hash.Write(imgdata.Data)
2018-10-06 01:45:14 +06:00
footprint := c.hash.Sum(nil)
c.hash.Reset()
c.hash.Write(footprint)
2021-04-26 17:52:50 +06:00
c.hash.Write([]byte(version.Version()))
c.enc.Encode(po)
return hex.EncodeToString(c.hash.Sum(nil))
}