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.6 kB

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