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

47 lines
802 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"
)
2018-10-06 02:29:55 +06:00
var errNotModified = newError(304, "Not modified", "Not modified")
2018-10-06 01:45:14 +06:00
type eTagCalc struct {
hash hash.Hash
enc *json.Encoder
}
var eTagCalcPool = sync.Pool{
New: func() interface{} {
h := sha256.New()
enc := json.NewEncoder(h)
enc.SetEscapeHTML(false)
enc.SetIndent("", "")
return &eTagCalc{h, enc}
},
}
func calcETag(ctx context.Context) string {
2018-10-06 01:45:14 +06:00
c := eTagCalcPool.Get().(*eTagCalc)
defer eTagCalcPool.Put(c)
c.hash.Reset()
c.hash.Write(getImageData(ctx).Bytes())
footprint := c.hash.Sum(nil)
c.hash.Reset()
c.hash.Write(footprint)
c.hash.Write([]byte(version))
c.enc.Encode(conf)
c.enc.Encode(getProcessingOptions(ctx))
return hex.EncodeToString(c.hash.Sum(nil))
}