Browse Source

Refactor the storage code

HEAD
Yang Luo 2 years ago
parent
commit
ec45d9e5eb
5 changed files with 64 additions and 179 deletions
  1. +3
    -4
      object/store_provider.go
  2. +0
    -89
      storage/casdoor.go
  3. +0
    -21
      storage/conf.go
  4. +56
    -14
      storage/storage.go
  5. +5
    -51
      storage/storage_test.go

+ 3
- 4
object/store_provider.go View File

@@ -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, "/"), "/")


+ 0
- 89
storage/casdoor.go View File

@@ -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
}

+ 0
- 21
storage/conf.go View File

@@ -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 = ""
)

+ 56
- 14
storage/storage.go View File

@@ -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 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 NewStorageProvider(provider string) Storage {
switch provider {
case "casdoor":
return NewCasdoorStorage()
default:
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
}

+ 5
- 51
storage/storage_test.go View File

@@ -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", "")
if err != nil {
panic(err)
}
}

func TestCasdoor(t *testing.T) {
controllers.InitAuthConfig()
object.InitConfig()
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")
objects, err := storage.ListObjects("casibase", "")
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)
panic(err)
}

for _, obj := range objs {
t.Log(obj)
}
println(objects)
}

Loading…
Cancel
Save