feat: adapt hash feature for some drivers (#5180)
* feat(pikpak,thunder): adaptation gcid hash * chore(weiyun): add note * feat(baidu_netdisk): adaptation rapid * feat(baidu_photo): adaptation hash * feat(189pc): adaptation rapid * feat(mopan):adaptation ctime * feat(139):adaptation hash and ctime --------- Co-authored-by: Andy Hsu <i@nn.ci>
This commit is contained in:
96
pkg/utils/hash/gcid.go
Normal file
96
pkg/utils/hash/gcid.go
Normal file
@ -0,0 +1,96 @@
|
||||
package hash_extend
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"encoding"
|
||||
"fmt"
|
||||
"hash"
|
||||
"strconv"
|
||||
|
||||
"github.com/alist-org/alist/v3/pkg/utils"
|
||||
)
|
||||
|
||||
var GCID = utils.RegisterHashWithParam("gcid", "GCID", 40, func(a ...any) hash.Hash {
|
||||
var (
|
||||
size int64
|
||||
err error
|
||||
)
|
||||
if len(a) > 0 {
|
||||
size, err = strconv.ParseInt(fmt.Sprint(a[0]), 10, 64)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
return NewGcid(size)
|
||||
})
|
||||
|
||||
func NewGcid(size int64) hash.Hash {
|
||||
calcBlockSize := func(j int64) int64 {
|
||||
var psize int64 = 0x40000
|
||||
for float64(j)/float64(psize) > 0x200 && psize < 0x200000 {
|
||||
psize = psize << 1
|
||||
}
|
||||
return psize
|
||||
}
|
||||
|
||||
return &gcid{
|
||||
hash: sha1.New(),
|
||||
hashState: sha1.New(),
|
||||
blockSize: int(calcBlockSize(size)),
|
||||
}
|
||||
}
|
||||
|
||||
type gcid struct {
|
||||
hash hash.Hash
|
||||
hashState hash.Hash
|
||||
blockSize int
|
||||
|
||||
offset int
|
||||
}
|
||||
|
||||
func (h *gcid) Write(p []byte) (n int, err error) {
|
||||
n = len(p)
|
||||
for len(p) > 0 {
|
||||
if h.offset < h.blockSize {
|
||||
var lastSize = h.blockSize - h.offset
|
||||
if lastSize > len(p) {
|
||||
lastSize = len(p)
|
||||
}
|
||||
|
||||
h.hashState.Write(p[:lastSize])
|
||||
h.offset += lastSize
|
||||
p = p[lastSize:]
|
||||
}
|
||||
|
||||
if h.offset >= h.blockSize {
|
||||
h.hash.Write(h.hashState.Sum(nil))
|
||||
h.hashState.Reset()
|
||||
h.offset = 0
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (h *gcid) Sum(b []byte) []byte {
|
||||
if hashm, ok := h.hash.(encoding.BinaryMarshaler); ok {
|
||||
if hashum, ok := h.hash.(encoding.BinaryUnmarshaler); ok {
|
||||
tempData, _ := hashm.MarshalBinary()
|
||||
h.hash.Write(h.hashState.Sum(nil))
|
||||
defer hashum.UnmarshalBinary(tempData)
|
||||
}
|
||||
}
|
||||
return h.hash.Sum(b)
|
||||
}
|
||||
|
||||
func (h *gcid) Reset() {
|
||||
h.hash.Reset()
|
||||
h.hashState.Reset()
|
||||
}
|
||||
|
||||
func (h *gcid) Size() int {
|
||||
return h.hash.Size()
|
||||
}
|
||||
|
||||
func (h *gcid) BlockSize() int {
|
||||
return h.blockSize
|
||||
}
|
Reference in New Issue
Block a user