alist/drivers/xunlei/util.go

64 lines
1.2 KiB
Go
Raw Normal View History

2022-03-03 07:44:41 +00:00
package xunlei
import (
"crypto/sha1"
"encoding/hex"
"io"
"net/url"
)
2022-03-17 13:13:13 +00:00
const (
API_URL = "https://api-pan.xunlei.com/drive/v1"
FILE_API_URL = API_URL + "/files"
XLUSER_API_URL = "https://xluser-ssl.xunlei.com/v1"
)
2022-03-03 07:44:41 +00:00
const (
2022-04-30 13:35:54 +00:00
FOLDER = "drive#folder"
FILE = "drive#file"
2022-03-03 07:44:41 +00:00
RESUMABLE = "drive#resumable"
)
const (
UPLOAD_TYPE_UNKNOWN = "UPLOAD_TYPE_UNKNOWN"
//UPLOAD_TYPE_FORM = "UPLOAD_TYPE_FORM"
UPLOAD_TYPE_RESUMABLE = "UPLOAD_TYPE_RESUMABLE"
UPLOAD_TYPE_URL = "UPLOAD_TYPE_URL"
)
func getAction(method string, u string) string {
c, _ := url.Parse(u)
2022-04-30 13:35:54 +00:00
return method + ":" + c.Path
2022-03-03 07:44:41 +00:00
}
2022-04-28 15:15:37 +00:00
// 计算文件Gcid
2022-03-03 07:44:41 +00:00
func getGcid(r io.Reader, size int64) (string, error) {
calcBlockSize := func(j int64) int64 {
2022-04-28 15:15:37 +00:00
if j >= 0 && j <= 0x8000000 {
return 0x40000
2022-03-03 07:44:41 +00:00
}
2022-04-28 15:15:37 +00:00
if j <= 0x8000000 || j > 0x10000000 {
if j <= 0x10000000 || j > 0x20000000 {
return 0x200000
2022-03-03 07:44:41 +00:00
}
2022-04-28 15:15:37 +00:00
return 0x100000
2022-03-03 07:44:41 +00:00
}
2022-04-28 15:15:37 +00:00
return 0x80000
2022-03-03 07:44:41 +00:00
}
hash1 := sha1.New()
hash2 := sha1.New()
2022-04-28 15:15:37 +00:00
readSize := calcBlockSize(size)
2022-03-03 07:44:41 +00:00
for {
hash2.Reset()
2022-04-28 15:15:37 +00:00
if n, err := io.CopyN(hash2, r, readSize); err != nil && n == 0 {
2022-03-03 07:44:41 +00:00
if err != io.EOF {
return "", err
}
break
}
hash1.Write(hash2.Sum(nil))
}
return hex.EncodeToString(hash1.Sum(nil)), nil
}