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.

file.go 2.4 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 object
  15. import (
  16. "bytes"
  17. "fmt"
  18. "io"
  19. "mime/multipart"
  20. "strings"
  21. "github.com/casbin/casibase/storage"
  22. )
  23. func UpdateFile(storeId string, key string, file *File) bool {
  24. return true
  25. }
  26. func AddFile(storeId string, key string, isLeaf bool, filename string, file multipart.File) (bool, []byte, error) {
  27. store, err := GetStore(storeId)
  28. if err != nil {
  29. return false, nil, err
  30. }
  31. if store == nil {
  32. return false, nil, nil
  33. }
  34. var objectKey string
  35. var fileBuffer *bytes.Buffer
  36. if isLeaf {
  37. objectKey = fmt.Sprintf("%s/%s", key, filename)
  38. objectKey = strings.TrimLeft(objectKey, "/")
  39. fileBuffer = bytes.NewBuffer(nil)
  40. _, err = io.Copy(fileBuffer, file)
  41. if err != nil {
  42. return false, nil, err
  43. }
  44. bs := fileBuffer.Bytes()
  45. err = storage.PutObject(store.Bucket, objectKey, fileBuffer)
  46. if err != nil {
  47. return false, nil, err
  48. }
  49. return true, bs, nil
  50. } else {
  51. objectKey = fmt.Sprintf("%s/%s/_hidden.ini", key, filename)
  52. objectKey = strings.TrimLeft(objectKey, "/")
  53. fileBuffer = bytes.NewBuffer(nil)
  54. bs := fileBuffer.Bytes()
  55. err = storage.PutObject(store.Bucket, objectKey, fileBuffer)
  56. if err != nil {
  57. return false, nil, err
  58. }
  59. return true, bs, nil
  60. }
  61. }
  62. func DeleteFile(storeId string, key string, isLeaf bool) (bool, error) {
  63. store, err := GetStore(storeId)
  64. if err != nil {
  65. return false, err
  66. }
  67. if store == nil {
  68. return false, nil
  69. }
  70. if isLeaf {
  71. err = storage.DeleteObject(store.Bucket, key)
  72. if err != nil {
  73. return false, err
  74. }
  75. } else {
  76. objects, err := storage.ListObjects(store.Bucket, key)
  77. if err != nil {
  78. return false, err
  79. }
  80. for _, object := range objects {
  81. err = storage.DeleteObject(store.Bucket, object.Key)
  82. if err != nil {
  83. return false, err
  84. }
  85. }
  86. }
  87. return true, nil
  88. }