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.

storage.go 2.2 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 Object struct {
  22. Key string
  23. LastModified string
  24. Size int64
  25. Url string
  26. }
  27. func ListObjects(provider string, prefix string) ([]*Object, error) {
  28. if provider == "" {
  29. return nil, fmt.Errorf("storage provider is empty")
  30. }
  31. casdoorOrganization := beego.AppConfig.String("casdoorOrganization")
  32. casdoorApplication := beego.AppConfig.String("casdoorApplication")
  33. resources, err := casdoorsdk.GetResources(casdoorOrganization, casdoorApplication, "provider", provider, "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 PutObject(provider string, user string, parent string, key string, fileBuffer *bytes.Buffer) error {
  49. if provider == "" {
  50. return fmt.Errorf("storage provider is empty")
  51. }
  52. _, _, err := casdoorsdk.UploadResource(user, "Casibase", parent, fmt.Sprintf("Direct/%s/%s", provider, key), fileBuffer.Bytes())
  53. if err != nil {
  54. return err
  55. }
  56. return nil
  57. }
  58. func DeleteObject(provider string, key string) error {
  59. if provider == "" {
  60. return fmt.Errorf("storage provider is empty")
  61. }
  62. _, err := casdoorsdk.DeleteResource(fmt.Sprintf("Direct/%s/%s", provider, key))
  63. if err != nil {
  64. return err
  65. }
  66. return nil
  67. }