2023-04-03 10:51:11 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-07-10 10:07:26 +00:00
|
|
|
"strconv"
|
2023-04-03 10:51:11 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
|
|
|
"github.com/qiniu/go-sdk/v7/auth"
|
|
|
|
"github.com/qiniu/go-sdk/v7/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
type kodoClient struct {
|
2024-07-10 10:07:26 +00:00
|
|
|
bucket string
|
|
|
|
domain string
|
|
|
|
timeout string
|
|
|
|
auth *auth.Credentials
|
|
|
|
client *storage.BucketManager
|
2023-04-03 10:51:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewKodoClient(vars map[string]interface{}) (*kodoClient, error) {
|
2024-01-31 07:08:14 +00:00
|
|
|
accessKey := loadParamFromVars("accessKey", vars)
|
|
|
|
secretKey := loadParamFromVars("secretKey", vars)
|
|
|
|
bucket := loadParamFromVars("bucket", vars)
|
|
|
|
domain := loadParamFromVars("domain", vars)
|
2024-07-10 10:07:26 +00:00
|
|
|
timeout := loadParamFromVars("timeout", vars)
|
|
|
|
if timeout == "" {
|
|
|
|
timeout = "1"
|
|
|
|
}
|
2023-12-01 09:50:09 +00:00
|
|
|
conn := auth.New(accessKey, secretKey)
|
2023-04-03 10:51:11 +00:00
|
|
|
cfg := storage.Config{
|
|
|
|
UseHTTPS: false,
|
|
|
|
}
|
|
|
|
bucketManager := storage.NewBucketManager(conn, &cfg)
|
|
|
|
|
2024-07-10 10:07:26 +00:00
|
|
|
return &kodoClient{client: bucketManager, auth: conn, bucket: bucket, domain: domain, timeout: timeout}, nil
|
2023-04-03 10:51:11 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 09:50:09 +00:00
|
|
|
func (k kodoClient) ListBuckets() ([]interface{}, error) {
|
|
|
|
buckets, err := k.client.Buckets(true)
|
2023-04-03 10:51:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var datas []interface{}
|
|
|
|
for _, bucket := range buckets {
|
|
|
|
datas = append(datas, bucket)
|
|
|
|
}
|
|
|
|
return datas, nil
|
|
|
|
}
|
|
|
|
|
2023-12-01 09:50:09 +00:00
|
|
|
func (k kodoClient) Exist(path string) (bool, error) {
|
|
|
|
if _, err := k.client.Stat(k.bucket, path); err != nil {
|
2023-05-12 14:30:26 +00:00
|
|
|
return false, err
|
2023-04-03 10:51:11 +00:00
|
|
|
}
|
2023-05-12 14:30:26 +00:00
|
|
|
return true, nil
|
2023-04-03 10:51:11 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 09:50:09 +00:00
|
|
|
func (k kodoClient) Size(path string) (int64, error) {
|
|
|
|
file, err := k.client.Stat(k.bucket, path)
|
2023-04-03 10:51:11 +00:00
|
|
|
if err != nil {
|
2023-12-01 09:50:09 +00:00
|
|
|
return 0, err
|
2023-04-03 10:51:11 +00:00
|
|
|
}
|
2023-12-01 09:50:09 +00:00
|
|
|
return file.Fsize, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k kodoClient) Delete(path string) (bool, error) {
|
|
|
|
if err := k.client.Delete(k.bucket, path); err != nil {
|
2023-04-03 10:51:11 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2023-12-01 09:50:09 +00:00
|
|
|
func (k kodoClient) Upload(src, target string) (bool, error) {
|
2024-07-10 10:07:26 +00:00
|
|
|
|
|
|
|
int64Value, _ := strconv.ParseInt(k.timeout, 10, 64)
|
|
|
|
unixTimestamp := int64Value * 3600
|
|
|
|
|
2023-04-03 10:51:11 +00:00
|
|
|
putPolicy := storage.PutPolicy{
|
2024-07-10 10:07:26 +00:00
|
|
|
Scope: k.bucket,
|
|
|
|
Expires: uint64(unixTimestamp),
|
2023-04-03 10:51:11 +00:00
|
|
|
}
|
2023-12-01 09:50:09 +00:00
|
|
|
upToken := putPolicy.UploadToken(k.auth)
|
2023-04-03 10:51:11 +00:00
|
|
|
cfg := storage.Config{UseHTTPS: true, UseCdnDomains: false}
|
|
|
|
resumeUploader := storage.NewResumeUploaderV2(&cfg)
|
|
|
|
ret := storage.PutRet{}
|
|
|
|
putExtra := storage.RputV2Extra{}
|
|
|
|
if err := resumeUploader.PutFile(context.Background(), &ret, upToken, target, src, &putExtra); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2023-12-01 09:50:09 +00:00
|
|
|
func (k kodoClient) Download(src, target string) (bool, error) {
|
2023-04-03 10:51:11 +00:00
|
|
|
deadline := time.Now().Add(time.Second * 3600).Unix()
|
2023-12-01 09:50:09 +00:00
|
|
|
privateAccessURL := storage.MakePrivateURL(k.auth, k.domain, src, deadline)
|
2023-04-03 10:51:11 +00:00
|
|
|
|
|
|
|
fo := files.NewFileOp()
|
|
|
|
if err := fo.DownloadFile(privateAccessURL, target); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2023-12-01 09:50:09 +00:00
|
|
|
func (k kodoClient) ListObjects(prefix string) ([]string, error) {
|
2023-08-08 08:44:12 +00:00
|
|
|
var result []string
|
2023-04-03 10:51:11 +00:00
|
|
|
marker := ""
|
|
|
|
for {
|
2023-12-01 09:50:09 +00:00
|
|
|
entries, _, nextMarker, hashNext, err := k.client.ListFiles(k.bucket, prefix, "", marker, 1000)
|
2023-04-03 10:51:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, entry := range entries {
|
|
|
|
result = append(result, entry.Key)
|
|
|
|
}
|
|
|
|
if hashNext {
|
|
|
|
marker = nextMarker
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|