From 42828d0a48f528653bac6ee9a91105bc6d12d5f5 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Wed, 31 Jan 2024 15:08:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=80=82=E9=85=8D=E4=B8=96=E7=BA=AA?= =?UTF-8?q?=E4=BA=92=E8=81=94=E7=89=88=E6=9C=AC=20OneDrive=20(#3763)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/utils/cloud_storage/client/cos.go | 10 ++++---- backend/utils/cloud_storage/client/helper.go | 2 +- backend/utils/cloud_storage/client/kodo.go | 8 +++---- backend/utils/cloud_storage/client/local.go | 2 +- backend/utils/cloud_storage/client/minio.go | 8 +++---- .../utils/cloud_storage/client/onedrive.go | 23 +++++++++++++------ backend/utils/cloud_storage/client/oss.go | 10 ++++---- backend/utils/cloud_storage/client/s3.go | 12 +++++----- backend/utils/cloud_storage/client/sftp.go | 10 ++++---- backend/utils/cloud_storage/client/webdav.go | 10 ++++---- 10 files changed, 52 insertions(+), 43 deletions(-) diff --git a/backend/utils/cloud_storage/client/cos.go b/backend/utils/cloud_storage/client/cos.go index 450cb1a16..0184c8ed4 100644 --- a/backend/utils/cloud_storage/client/cos.go +++ b/backend/utils/cloud_storage/client/cos.go @@ -17,11 +17,11 @@ type cosClient struct { } func NewCosClient(vars map[string]interface{}) (*cosClient, error) { - region := loadParamFromVars("region", true, vars) - accessKey := loadParamFromVars("accessKey", true, vars) - secretKey := loadParamFromVars("secretKey", true, vars) - bucket := loadParamFromVars("bucket", true, vars) - scType := loadParamFromVars("scType", true, vars) + region := loadParamFromVars("region", vars) + accessKey := loadParamFromVars("accessKey", vars) + secretKey := loadParamFromVars("secretKey", vars) + bucket := loadParamFromVars("bucket", vars) + scType := loadParamFromVars("scType", vars) if len(scType) == 0 { scType = "Standard" } diff --git a/backend/utils/cloud_storage/client/helper.go b/backend/utils/cloud_storage/client/helper.go index 60723d01d..31575d4b1 100644 --- a/backend/utils/cloud_storage/client/helper.go +++ b/backend/utils/cloud_storage/client/helper.go @@ -6,7 +6,7 @@ import ( "github.com/1Panel-dev/1Panel/backend/global" ) -func loadParamFromVars(key string, isString bool, vars map[string]interface{}) string { +func loadParamFromVars(key string, vars map[string]interface{}) string { if _, ok := vars[key]; !ok { if key != "bucket" { global.LOG.Errorf("load param %s from vars failed, err: not exist!", key) diff --git a/backend/utils/cloud_storage/client/kodo.go b/backend/utils/cloud_storage/client/kodo.go index 80a4fb64e..488f72b77 100644 --- a/backend/utils/cloud_storage/client/kodo.go +++ b/backend/utils/cloud_storage/client/kodo.go @@ -17,10 +17,10 @@ type kodoClient struct { } func NewKodoClient(vars map[string]interface{}) (*kodoClient, error) { - accessKey := loadParamFromVars("accessKey", true, vars) - secretKey := loadParamFromVars("secretKey", true, vars) - bucket := loadParamFromVars("bucket", true, vars) - domain := loadParamFromVars("domain", true, vars) + accessKey := loadParamFromVars("accessKey", vars) + secretKey := loadParamFromVars("secretKey", vars) + bucket := loadParamFromVars("bucket", vars) + domain := loadParamFromVars("domain", vars) conn := auth.New(accessKey, secretKey) cfg := storage.Config{ diff --git a/backend/utils/cloud_storage/client/local.go b/backend/utils/cloud_storage/client/local.go index 40e8a49be..74e1228ca 100644 --- a/backend/utils/cloud_storage/client/local.go +++ b/backend/utils/cloud_storage/client/local.go @@ -14,7 +14,7 @@ type localClient struct { } func NewLocalClient(vars map[string]interface{}) (*localClient, error) { - dir := loadParamFromVars("dir", true, vars) + dir := loadParamFromVars("dir", vars) return &localClient{dir: dir}, nil } diff --git a/backend/utils/cloud_storage/client/minio.go b/backend/utils/cloud_storage/client/minio.go index c52ae3ffb..2002e1aca 100644 --- a/backend/utils/cloud_storage/client/minio.go +++ b/backend/utils/cloud_storage/client/minio.go @@ -19,10 +19,10 @@ type minIoClient struct { } func NewMinIoClient(vars map[string]interface{}) (*minIoClient, error) { - endpoint := loadParamFromVars("endpoint", true, vars) - accessKeyID := loadParamFromVars("accessKey", true, vars) - secretAccessKey := loadParamFromVars("secretKey", true, vars) - bucket := loadParamFromVars("bucket", true, vars) + endpoint := loadParamFromVars("endpoint", vars) + accessKeyID := loadParamFromVars("accessKey", vars) + secretAccessKey := loadParamFromVars("secretKey", vars) + bucket := loadParamFromVars("bucket", vars) ssl := strings.Split(endpoint, ":")[0] if len(ssl) == 0 || (ssl != "https" && ssl != "http") { return nil, constant.ErrInvalidParams diff --git a/backend/utils/cloud_storage/client/onedrive.go b/backend/utils/cloud_storage/client/onedrive.go index 65733976b..3f37407d2 100644 --- a/backend/utils/cloud_storage/client/onedrive.go +++ b/backend/utils/cloud_storage/client/onedrive.go @@ -25,7 +25,8 @@ type oneDriveClient struct { } func NewOneDriveClient(vars map[string]interface{}) (*oneDriveClient, error) { - token := loadParamFromVars("accessToken", true, vars) + token := loadParamFromVars("accessToken", vars) + isCN := loadParamFromVars("isCN", vars) ctx := context.Background() ts := oauth2.StaticTokenSource( &oauth2.Token{AccessToken: token}, @@ -33,6 +34,9 @@ func NewOneDriveClient(vars map[string]interface{}) (*oneDriveClient, error) { tc := oauth2.NewClient(ctx, ts) client := odsdk.NewClient(tc) + if isCN == "true" { + client.BaseURL, _ = url.Parse("https://microsoftgraph.chinacloudapi.cn/v1.0/") + } return &oneDriveClient{client: *client}, nil } @@ -192,18 +196,23 @@ func (o *oneDriveClient) loadIDByPath(path string) (string, error) { func RefreshToken(grantType string, varMap map[string]interface{}) (string, string, error) { data := url.Values{} - data.Set("client_id", loadParamFromVars("client_id", true, varMap)) - data.Set("client_secret", loadParamFromVars("client_secret", true, varMap)) + isCN := loadParamFromVars("isCN", varMap) + data.Set("client_id", loadParamFromVars("client_id", varMap)) + data.Set("client_secret", loadParamFromVars("client_secret", varMap)) if grantType == "refresh_token" { data.Set("grant_type", "refresh_token") - data.Set("refresh_token", loadParamFromVars("refresh_token", true, varMap)) + data.Set("refresh_token", loadParamFromVars("refresh_token", varMap)) } else { data.Set("grant_type", "authorization_code") - data.Set("code", loadParamFromVars("code", true, varMap)) + data.Set("code", loadParamFromVars("code", varMap)) } - data.Set("redirect_uri", loadParamFromVars("redirect_uri", true, varMap)) + data.Set("redirect_uri", loadParamFromVars("redirect_uri", varMap)) client := &http.Client{} - req, err := http.NewRequest("POST", "https://login.microsoftonline.com/common/oauth2/v2.0/token", strings.NewReader(data.Encode())) + url := "https://login.microsoftonline.com/common/oauth2/v2.0/token" + if isCN == "true" { + url = "https://login.chinacloudapi.cn/common/oauth2/v2.0/token" + } + req, err := http.NewRequest("POST", url, strings.NewReader(data.Encode())) if err != nil { return "", "", fmt.Errorf("new http post client for access token failed, err: %v", err) } diff --git a/backend/utils/cloud_storage/client/oss.go b/backend/utils/cloud_storage/client/oss.go index 635039140..e4fed15d7 100644 --- a/backend/utils/cloud_storage/client/oss.go +++ b/backend/utils/cloud_storage/client/oss.go @@ -13,11 +13,11 @@ type ossClient struct { } func NewOssClient(vars map[string]interface{}) (*ossClient, error) { - endpoint := loadParamFromVars("endpoint", true, vars) - accessKey := loadParamFromVars("accessKey", true, vars) - secretKey := loadParamFromVars("secretKey", true, vars) - bucketStr := loadParamFromVars("bucket", true, vars) - scType := loadParamFromVars("scType", true, vars) + endpoint := loadParamFromVars("endpoint", vars) + accessKey := loadParamFromVars("accessKey", vars) + secretKey := loadParamFromVars("secretKey", vars) + bucketStr := loadParamFromVars("bucket", vars) + scType := loadParamFromVars("scType", vars) if len(scType) == 0 { scType = "Standard" } diff --git a/backend/utils/cloud_storage/client/s3.go b/backend/utils/cloud_storage/client/s3.go index 0a4ee58d3..9fc7ebadd 100644 --- a/backend/utils/cloud_storage/client/s3.go +++ b/backend/utils/cloud_storage/client/s3.go @@ -18,12 +18,12 @@ type s3Client struct { } func NewS3Client(vars map[string]interface{}) (*s3Client, error) { - accessKey := loadParamFromVars("accessKey", true, vars) - secretKey := loadParamFromVars("secretKey", true, vars) - endpoint := loadParamFromVars("endpoint", true, vars) - region := loadParamFromVars("region", true, vars) - bucket := loadParamFromVars("bucket", true, vars) - scType := loadParamFromVars("scType", true, vars) + accessKey := loadParamFromVars("accessKey", vars) + secretKey := loadParamFromVars("secretKey", vars) + endpoint := loadParamFromVars("endpoint", vars) + region := loadParamFromVars("region", vars) + bucket := loadParamFromVars("bucket", vars) + scType := loadParamFromVars("scType", vars) if len(scType) == 0 { scType = "Standard" } diff --git a/backend/utils/cloud_storage/client/sftp.go b/backend/utils/cloud_storage/client/sftp.go index a05b29243..17e828e1a 100644 --- a/backend/utils/cloud_storage/client/sftp.go +++ b/backend/utils/cloud_storage/client/sftp.go @@ -19,11 +19,11 @@ type sftpClient struct { } func NewSftpClient(vars map[string]interface{}) (*sftpClient, error) { - address := loadParamFromVars("address", true, vars) - port := loadParamFromVars("port", false, vars) - password := loadParamFromVars("password", true, vars) - username := loadParamFromVars("username", true, vars) - bucket := loadParamFromVars("bucket", true, vars) + address := loadParamFromVars("address", vars) + port := loadParamFromVars("port", vars) + password := loadParamFromVars("password", vars) + username := loadParamFromVars("username", vars) + bucket := loadParamFromVars("bucket", vars) auth := []ssh.AuthMethod{ssh.Password(password)} clientConfig := &ssh.ClientConfig{ diff --git a/backend/utils/cloud_storage/client/webdav.go b/backend/utils/cloud_storage/client/webdav.go index 66d6ccfec..78f4ac633 100644 --- a/backend/utils/cloud_storage/client/webdav.go +++ b/backend/utils/cloud_storage/client/webdav.go @@ -17,11 +17,11 @@ type webDAVClient struct { } func NewWebDAVClient(vars map[string]interface{}) (*webDAVClient, error) { - address := loadParamFromVars("address", true, vars) - port := loadParamFromVars("port", true, vars) - password := loadParamFromVars("password", true, vars) - username := loadParamFromVars("username", true, vars) - bucket := loadParamFromVars("bucket", true, vars) + address := loadParamFromVars("address", vars) + port := loadParamFromVars("port", vars) + password := loadParamFromVars("password", vars) + username := loadParamFromVars("username", vars) + bucket := loadParamFromVars("bucket", vars) url := fmt.Sprintf("%s:%s", address, port) if len(port) == 0 {