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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. "io"
  19. "net/http"
  20. "time"
  21. "github.com/astaxie/beego"
  22. "github.com/casbin/casibase/casdoor"
  23. "github.com/casbin/casibase/util"
  24. "github.com/casdoor/casdoor-go-sdk/casdoorsdk"
  25. )
  26. type Object struct {
  27. Key string
  28. LastModified *time.Time
  29. Size int64
  30. }
  31. func ListObjects(provider string, prefix string) ([]*Object, error) {
  32. resources, err := casdoor.ListResources(provider, prefix)
  33. if err != nil {
  34. return nil, err
  35. }
  36. res := []*Object{}
  37. for _, resource := range resources {
  38. created, _ := time.Parse(time.RFC3339, resource.CreatedTime)
  39. res = append(res, &Object{
  40. Key: util.GetNameFromIdNoCheck(resource.Name),
  41. LastModified: &created,
  42. Size: int64(resource.FileSize),
  43. })
  44. }
  45. return res, nil
  46. }
  47. func GetObject(provider string, key string) (io.ReadCloser, error) {
  48. res, err := casdoor.GetResource(provider, key)
  49. if err != nil {
  50. return nil, err
  51. }
  52. response, err := http.Get(res.Url)
  53. if err != nil {
  54. return nil, err
  55. }
  56. return response.Body, nil
  57. }
  58. func PutObject(provider string, key string, fileBuffer *bytes.Buffer) error {
  59. _, _, err := casdoorsdk.UploadResource("Casibase", "Casibase", "Casibase",
  60. fmt.Sprintf("/casibase/%s", key), fileBuffer.Bytes())
  61. if err != nil {
  62. return err
  63. }
  64. return nil
  65. }
  66. func DeleteObject(provider string, key string) error {
  67. casdoorOrganization := beego.AppConfig.String("casdoorOrganization")
  68. _, err := casdoorsdk.DeleteResource(util.GetIdFromOwnerAndName(casdoorOrganization, fmt.Sprintf("/casibase/%s", key)))
  69. if err != nil {
  70. return err
  71. }
  72. return nil
  73. }