You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

casdoor.go 2.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2023 The casbin Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package storage
  15. import (
  16. "bytes"
  17. "fmt"
  18. "github.com/astaxie/beego"
  19. "github.com/casdoor/casdoor-go-sdk/casdoorsdk"
  20. )
  21. type CasdoorProvider struct {
  22. providerName string
  23. }
  24. func NewCasdoorProvider(providerName string) (*CasdoorProvider, error) {
  25. if providerName == "" {
  26. return nil, fmt.Errorf("storage provider name: [%s] doesn't exist", providerName)
  27. }
  28. return &CasdoorProvider{providerName: providerName}, nil
  29. }
  30. func (p *CasdoorProvider) ListObjects(prefix string) ([]*Object, error) {
  31. casdoorOrganization := beego.AppConfig.String("casdoorOrganization")
  32. casdoorApplication := beego.AppConfig.String("casdoorApplication")
  33. resources, err := casdoorsdk.GetResources(casdoorOrganization, casdoorApplication, "provider", p.providerName, "Direct", prefix)
  34. if err != nil {
  35. return nil, err
  36. }
  37. res := []*Object{}
  38. for _, resource := range resources {
  39. res = append(res, &Object{
  40. Key: resource.Name,
  41. LastModified: resource.CreatedTime,
  42. Size: int64(resource.FileSize),
  43. Url: resource.Url,
  44. })
  45. }
  46. return res, nil
  47. }
  48. func (p *CasdoorProvider) PutObject(user string, parent string, key string, fileBuffer *bytes.Buffer) error {
  49. _, _, err := casdoorsdk.UploadResource(user, "Casibase", parent, fmt.Sprintf("Direct/%s/%s", p.providerName, key), fileBuffer.Bytes())
  50. if err != nil {
  51. return err
  52. }
  53. return nil
  54. }
  55. func (p *CasdoorProvider) DeleteObject(key string) error {
  56. _, err := casdoorsdk.DeleteResource(fmt.Sprintf("Direct/%s/%s", p.providerName, key))
  57. if err != nil {
  58. return err
  59. }
  60. return nil
  61. }