From 44150a67811160d611b58480b1d05d6356605e26 Mon Sep 17 00:00:00 2001 From: Yang Luo Date: Sun, 8 Aug 2021 11:37:16 +0800 Subject: [PATCH] Refactor the storage provider interface. --- controllers/account.go | 6 +-- object/storage.go | 109 ++++------------------------------------- storage/aliyun.go | 31 ++++++++++++ storage/aws_s3.go | 34 +++++++++++++ storage/storage.go | 28 +++++++++++ 5 files changed, 105 insertions(+), 103 deletions(-) create mode 100644 storage/aliyun.go create mode 100644 storage/aws_s3.go create mode 100644 storage/storage.go diff --git a/controllers/account.go b/controllers/account.go index bb5bf888..bdc0ff9e 100644 --- a/controllers/account.go +++ b/controllers/account.go @@ -256,9 +256,9 @@ func (c *ApiController) UploadAvatar() { } dist, _ := base64.StdEncoding.DecodeString(avatarBase64[index+1:]) - msg := object.UploadAvatar(provider, user.GetId(), dist) - if msg != "" { - c.ResponseError(msg) + err := object.UploadAvatar(provider, user.GetId(), dist) + if err != nil { + c.ResponseError(err.Error()) return } diff --git a/object/storage.go b/object/storage.go index 316a5bf7..ea6abc12 100644 --- a/object/storage.go +++ b/object/storage.go @@ -17,113 +17,22 @@ package object import ( "bytes" "fmt" - - awss3 "github.com/aws/aws-sdk-go/service/s3" + "github.com/casbin/casdoor/storage" "github.com/casbin/casdoor/util" - "github.com/qor/oss" - "github.com/qor/oss/aliyun" - //"github.com/qor/oss/qiniu" - "github.com/qor/oss/s3" ) -func getAliyunClient(provider *Provider) oss.StorageInterface { - if util.IsStrsEmpty( - provider.Endpoint, - provider.ClientId, - provider.ClientSecret, - provider.Bucket) { - return nil +func UploadAvatar(provider *Provider, username string, avatar []byte) error { + storageProvider := storage.GetStorageProvider(provider.Type, provider.ClientId, provider.ClientSecret, provider.RegionId, provider.Bucket, provider.Endpoint) + if storageProvider == nil { + return fmt.Errorf("the provider type: %s is not supported", provider.Type) } - ret := aliyun.New(&aliyun.Config{ - AccessID: provider.ClientId, - AccessKey: provider.ClientSecret, - Bucket: provider.Bucket, - Endpoint: provider.Endpoint, - }) - - if len(provider.Domain) == 0 { - provider.Domain = ret.GetEndpoint() + if provider.Domain == "" { + provider.Domain = storageProvider.GetEndpoint() UpdateProvider(provider.GetId(), provider) } - return ret -} - -func getQiniuClient(provider *Provider) oss.StorageInterface { - fmt.Println("Casdoor does not support Qiniu now.") - return nil - // endpoint := section.Key("endpoint").String() - // accessId := section.Key("accessId").String() - // accessKey := section.Key("accessKey").String() - // domain = section.Key("domain").String() - // bucket := section.Key("bucket").String() - // region := section.Key("region").String() - // if accessId == "" || accessKey == "" || bucket == "" || endpoint == "" || region == "" { - // return "Config oss.conf wrong" - // } - // storage = qiniu.New(&qiniu.Config{ - // AccessID: accessId, - // AccessKey: accessKey, - // Bucket: bucket, - // Region: region, - // Endpoint: endpoint, - // }) - // return "" -} - -func getAwss3Client(provider *Provider) oss.StorageInterface { - if util.IsStrsEmpty( - provider.Endpoint, - provider.ClientId, - provider.ClientSecret, - provider.Bucket, - provider.RegionId) { - return nil - } - - ret := s3.New(&s3.Config{ - AccessID: provider.ClientId, - AccessKey: provider.ClientSecret, - Region: provider.RegionId, - Bucket: provider.Bucket, - Endpoint: provider.Endpoint, - ACL: awss3.BucketCannedACLPublicRead, - }) - - if len(provider.Domain) == 0 { - provider.Domain = ret.GetEndpoint() - UpdateProvider(provider.GetId(), provider) - } - return ret -} - -func getStorageClient(provider *Provider) oss.StorageInterface { - if provider == nil || provider.Category != "Storage" { - return nil - } - - switch provider.Type { - case "Aliyun OSS": - return getAliyunClient(provider) - case "Qiniu": - return getQiniuClient(provider) - case "AWS S3": - return getAwss3Client(provider) - } - - return nil -} - -func UploadAvatar(provider *Provider, username string, avatar []byte) string { - storage := getStorageClient(provider) - if storage == nil { - return fmt.Sprintf("Provider type: %s is not supported", provider.Type) - } path := fmt.Sprintf("%s/%s.png", util.UrlJoin(util.GetUrlPath(provider.Domain), "/avatar"), username) - _, err := storage.Put(path, bytes.NewReader(avatar)) - if err != nil { - return err.Error() - } - return "" + _, err := storageProvider.Put(path, bytes.NewReader(avatar)) + return err } diff --git a/storage/aliyun.go b/storage/aliyun.go new file mode 100644 index 00000000..b5dd78c1 --- /dev/null +++ b/storage/aliyun.go @@ -0,0 +1,31 @@ +// Copyright 2021 The casbin Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package storage + +import ( + "github.com/qor/oss" + "github.com/qor/oss/aliyun" +) + +func NewAliyunStorageProvider(clientId string, clientSecret string, region string, bucket string, endpoint string) oss.StorageInterface { + sp := aliyun.New(&aliyun.Config{ + AccessID: clientId, + AccessKey: clientSecret, + Bucket: bucket, + Endpoint: endpoint, + }) + + return sp +} diff --git a/storage/aws_s3.go b/storage/aws_s3.go new file mode 100644 index 00000000..deec7605 --- /dev/null +++ b/storage/aws_s3.go @@ -0,0 +1,34 @@ +// Copyright 2021 The casbin Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package storage + +import ( + awss3 "github.com/aws/aws-sdk-go/service/s3" + "github.com/qor/oss" + "github.com/qor/oss/s3" +) + +func NewAwsS3StorageProvider(clientId string, clientSecret string, region string, bucket string, endpoint string) oss.StorageInterface { + sp := s3.New(&s3.Config{ + AccessID: clientId, + AccessKey: clientSecret, + Region: region, + Bucket: bucket, + Endpoint: endpoint, + ACL: awss3.BucketCannedACLPublicRead, + }) + + return sp +} diff --git a/storage/storage.go b/storage/storage.go new file mode 100644 index 00000000..36a5cb3a --- /dev/null +++ b/storage/storage.go @@ -0,0 +1,28 @@ +// Copyright 2021 The casbin Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package storage + +import "github.com/qor/oss" + +func GetStorageProvider(providerType string, clientId string, clientSecret string, region string, bucket string, endpoint string) oss.StorageInterface { + switch providerType { + case "AWS S3": + return NewAwsS3StorageProvider(clientId, clientSecret, region, bucket, endpoint) + case "Aliyun OSS": + return NewAliyunStorageProvider(clientId, clientSecret, region, bucket, endpoint) + } + + return nil +}