2018-02-26 08:45:52 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-10-05 22:20:29 +06:00
|
|
|
"context"
|
2018-09-06 19:02:21 +06:00
|
|
|
"crypto/sha256"
|
2018-10-06 01:45:14 +06:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"hash"
|
|
|
|
"sync"
|
2018-02-26 08:45:52 +00:00
|
|
|
)
|
|
|
|
|
2019-05-08 17:42:48 +06:00
|
|
|
type eTagCalc struct {
|
2018-10-06 01:45:14 +06:00
|
|
|
hash hash.Hash
|
|
|
|
enc *json.Encoder
|
|
|
|
}
|
|
|
|
|
2019-05-08 17:42:48 +06:00
|
|
|
var eTagCalcPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
h := sha256.New()
|
2018-10-06 01:45:14 +06:00
|
|
|
|
2019-05-08 17:42:48 +06:00
|
|
|
enc := json.NewEncoder(h)
|
|
|
|
enc.SetEscapeHTML(false)
|
|
|
|
enc.SetIndent("", "")
|
2018-10-06 01:45:14 +06:00
|
|
|
|
2019-05-08 17:42:48 +06:00
|
|
|
return &eTagCalc{h, enc}
|
|
|
|
},
|
2018-10-06 01:45:14 +06:00
|
|
|
}
|
|
|
|
|
2019-05-08 17:42:48 +06:00
|
|
|
func calcETag(ctx context.Context) string {
|
|
|
|
c := eTagCalcPool.Get().(*eTagCalc)
|
|
|
|
defer eTagCalcPool.Put(c)
|
2018-10-06 01:45:14 +06:00
|
|
|
|
|
|
|
c.hash.Reset()
|
2019-09-20 17:01:00 +06:00
|
|
|
c.hash.Write(getImageData(ctx).Data)
|
2018-10-06 01:45:14 +06:00
|
|
|
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))
|
2018-02-26 08:45:52 +00:00
|
|
|
|
2019-05-08 17:42:48 +06:00
|
|
|
return hex.EncodeToString(c.hash.Sum(nil))
|
2018-02-26 08:45:52 +00:00
|
|
|
}
|