diff --git a/object/store_provider.go b/object/store_provider.go index fbf5173..5002106 100644 --- a/object/store_provider.go +++ b/object/store_provider.go @@ -19,7 +19,6 @@ import ( "strings" "time" - "github.com/aliyun/aliyun-oss-go-sdk/oss" "github.com/casbin/casibase/storage" ) @@ -72,7 +71,7 @@ func (store *Store) createPathIfNotExisted(tokens []string, size int64, lastModi } } -func isObjectLeaf(object *oss.ObjectProperties) bool { +func isObjectLeaf(object *storage.Object) bool { isLeaf := true if object.Key[len(object.Key)-1] == '/' { isLeaf = false @@ -97,7 +96,7 @@ func (store *Store) Populate() error { } } - sortedObjects := []oss.ObjectProperties{} + sortedObjects := []*storage.Object{} for _, object := range objects { if strings.HasSuffix(object.Key, "/_hidden.ini") { sortedObjects = append(sortedObjects, object) @@ -111,7 +110,7 @@ func (store *Store) Populate() error { for _, object := range sortedObjects { lastModifiedTime := object.LastModified.Local().Format(time.RFC3339) - isLeaf := isObjectLeaf(&object) + isLeaf := isObjectLeaf(object) size := object.Size tokens := strings.Split(strings.Trim(object.Key, "/"), "/") diff --git a/storage/casdoor.go b/storage/casdoor.go deleted file mode 100644 index 6b34bae..0000000 --- a/storage/casdoor.go +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright 2023 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 ( - "fmt" - "io" - "net/http" - "time" - - "github.com/astaxie/beego" - "github.com/casbin/casibase/casdoor" - "github.com/casbin/casibase/util" - "github.com/casdoor/casdoor-go-sdk/casdoorsdk" -) - -type casdoorClient struct { - Storage -} - -func NewCasdoorStorage() Storage { - return &casdoorClient{} -} - -func (s *casdoorClient) Get(key string) (io.ReadCloser, error) { - res, err := casdoor.GetResource(key) - if err != nil { - return nil, err - } - - response, err := http.Get(res.Url) - if err != nil { - return nil, err - } - - return response.Body, nil -} - -func (s *casdoorClient) Put(user, key string, bytes []byte) error { - _, _, err := casdoorsdk.UploadResource(user, "Casibase", "Casibase", - fmt.Sprintf("/resource/%s/%s/%s", - casdoor.Organization, casdoor.Application, key), - bytes) - if err != nil { - return err - } - return nil -} - -func (s *casdoorClient) Delete(key string) error { - _, err := casdoorsdk.DeleteResource(util.GetIdFromOwnerAndName(fmt.Sprintf("/resource/%s/%s/casibase", - beego.AppConfig.String("casdoorOrganization"), - beego.AppConfig.String("casdoorApplication")), key)) - if err != nil { - return err - } - return nil -} - -func (s *casdoorClient) List(prefix string) ([]*Object, error) { - res, err := casdoor.ListResources(prefix) - if err != nil { - return nil, err - } - - result := make([]*Object, 0) - for _, r := range res { - created, _ := time.Parse(time.RFC3339, r.CreatedTime) - result = append(result, &Object{ - Key: util.GetNameFromIdNoCheck(r.Name), - LastModified: &created, - Storage: s, - }) - } - - return result, nil -} diff --git a/storage/conf.go b/storage/conf.go deleted file mode 100644 index cd81610..0000000 --- a/storage/conf.go +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2023 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 - -var ( - endpoint = "" - clientId = "" - clientSecret = "" -) diff --git a/storage/storage.go b/storage/storage.go index 15b465e..e8ac793 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -15,28 +15,70 @@ package storage import ( + "bytes" + "fmt" "io" + "net/http" "time" -) -type Storage interface { - Get(key string) (io.ReadCloser, error) - Put(user, key string, bytes []byte) error - Delete(key string) error - List(prefix string) ([]*Object, error) -} + "github.com/astaxie/beego" + "github.com/casbin/casibase/casdoor" + "github.com/casbin/casibase/util" + "github.com/casdoor/casdoor-go-sdk/casdoorsdk" +) type Object struct { Key string LastModified *time.Time - Storage Storage + Size int64 } -func NewStorageProvider(provider string) Storage { - switch provider { - case "casdoor": - return NewCasdoorStorage() - default: - return nil +func ListObjects(bucketName string, prefix string) ([]*Object, error) { + resources, err := casdoor.ListResources(prefix) + if err != nil { + return nil, err } + + res := []*Object{} + for _, resource := range resources { + created, _ := time.Parse(time.RFC3339, resource.CreatedTime) + res = append(res, &Object{ + Key: util.GetNameFromIdNoCheck(resource.Name), + LastModified: &created, + }) + } + return res, nil +} + +func GetObject(bucketName string, key string) (io.ReadCloser, error) { + res, err := casdoor.GetResource(key) + if err != nil { + return nil, err + } + + response, err := http.Get(res.Url) + if err != nil { + return nil, err + } + + return response.Body, nil +} + +func PutObject(bucketName string, key string, fileBuffer *bytes.Buffer) error { + _, _, err := casdoorsdk.UploadResource("Casibase", "Casibase", "Casibase", + fmt.Sprintf("/resource/%s/%s/%s", casdoor.Organization, casdoor.Application, key), fileBuffer.Bytes()) + if err != nil { + return err + } + return nil +} + +func DeleteObject(bucketName string, key string) error { + _, err := casdoorsdk.DeleteResource(util.GetIdFromOwnerAndName(fmt.Sprintf("/resource/%s/%s/casibase", + beego.AppConfig.String("casdoorOrganization"), + beego.AppConfig.String("casdoorApplication")), key)) + if err != nil { + return err + } + return nil } diff --git a/storage/storage_test.go b/storage/storage_test.go index c03108d..0066e1e 100644 --- a/storage/storage_test.go +++ b/storage/storage_test.go @@ -18,67 +18,21 @@ package storage_test import ( - "io" "testing" "github.com/casbin/casibase/casdoor" - "github.com/casbin/casibase/controllers" + "github.com/casbin/casibase/object" "github.com/casbin/casibase/storage" ) func TestStorage(t *testing.T) { - _, err := storage.ListObjects("casibase", "") + object.InitConfig() + casdoor.InitCasdoorAdapter() + + objects, err := storage.ListObjects("casibase", "") if err != nil { panic(err) } -} - -func TestCasdoor(t *testing.T) { - controllers.InitAuthConfig() - casdoor.InitCasdoorAdapter() - s := storage.NewCasdoorStorage() - - // Test Put - err := s.Put("admin", "test", []byte("test")) - if err != nil { - t.Error(err) - } - - // Test List - objs, err := s.List("admin") - if err != nil { - t.Error(err) - } - - for _, obj := range objs { - t.Log(obj) - } - - // Test Get - in, err := s.Get("test") - if err != nil { - t.Error(err) - } - - bytes, err := io.ReadAll(in) - if err != nil { - t.Error(err) - } - - t.Log(string(bytes)) - - // Test Delete - err = s.Delete("test") - if err != nil { - t.Error(err) - } - - objs, err = s.List("test") - if err != nil { - t.Error(err) - } - - for _, obj := range objs { - t.Log(obj) - } + + println(objects) }