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:
foxxorcat
2023-09-06 14:46:35 +08:00
committed by GitHub
parent b313ac4daa
commit 7200344ace
17 changed files with 362 additions and 194 deletions

View File

@ -8,10 +8,11 @@ import (
"encoding/hex"
"encoding/json"
"errors"
"github.com/alist-org/alist/v3/internal/errs"
log "github.com/sirupsen/logrus"
"hash"
"io"
"github.com/alist-org/alist/v3/internal/errs"
log "github.com/sirupsen/logrus"
)
func GetMD5EncodeStr(data string) string {
@ -29,7 +30,7 @@ type HashType struct {
Width int
Name string
Alias string
NewFunc func() hash.Hash
NewFunc func(...any) hash.Hash
}
func (ht *HashType) MarshalJSON() ([]byte, error) {
@ -57,7 +58,10 @@ var (
// RegisterHash adds a new Hash to the list and returns its Type
func RegisterHash(name, alias string, width int, newFunc func() hash.Hash) *HashType {
return RegisterHashWithParam(name, alias, width, func(a ...any) hash.Hash { return newFunc() })
}
func RegisterHashWithParam(name, alias string, width int, newFunc func(...any) hash.Hash) *HashType {
newType := &HashType{
Name: name,
Alias: alias,
@ -83,15 +87,15 @@ var (
)
// HashData get hash of one hashType
func HashData(hashType *HashType, data []byte) string {
h := hashType.NewFunc()
func HashData(hashType *HashType, data []byte, params ...any) string {
h := hashType.NewFunc(params...)
h.Write(data)
return hex.EncodeToString(h.Sum(nil))
}
// HashReader get hash of one hashType from a reader
func HashReader(hashType *HashType, reader io.Reader) (string, error) {
h := hashType.NewFunc()
func HashReader(hashType *HashType, reader io.Reader, params ...any) (string, error) {
h := hashType.NewFunc(params...)
_, err := io.Copy(h, reader)
if err != nil {
return "", errs.NewErr(err, "HashReader error")
@ -100,8 +104,8 @@ func HashReader(hashType *HashType, reader io.Reader) (string, error) {
}
// HashFile get hash of one hashType from a model.File
func HashFile(hashType *HashType, file io.ReadSeeker) (string, error) {
str, err := HashReader(hashType, file)
func HashFile(hashType *HashType, file io.ReadSeeker, params ...any) (string, error) {
str, err := HashReader(hashType, file, params...)
if err != nil {
return "", err
}