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

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 controllers
  15. import (
  16. "encoding/json"
  17. "mime/multipart"
  18. "github.com/casbin/casibase/object"
  19. )
  20. func (c *ApiController) UpdateFile() {
  21. userName, ok := c.RequireSignedIn()
  22. if !ok {
  23. return
  24. }
  25. storeId := c.Input().Get("store")
  26. key := c.Input().Get("key")
  27. var file object.File
  28. err := json.Unmarshal(c.Ctx.Input.RequestBody, &file)
  29. if err != nil {
  30. c.ResponseError(err.Error())
  31. return
  32. }
  33. res := object.UpdateFile(storeId, key, &file)
  34. if res {
  35. addRecordForFile(c, userName, "Update", storeId, key, "", true)
  36. }
  37. c.ResponseOk(res)
  38. }
  39. func (c *ApiController) AddFile() {
  40. userName, ok := c.RequireSignedIn()
  41. if !ok {
  42. return
  43. }
  44. storeId := c.Input().Get("store")
  45. key := c.Input().Get("key")
  46. isLeaf := c.Input().Get("isLeaf") == "1"
  47. filename := c.Input().Get("filename")
  48. var file multipart.File
  49. if isLeaf {
  50. var err error
  51. file, _, err = c.GetFile("file")
  52. if err != nil {
  53. c.ResponseError(err.Error())
  54. return
  55. }
  56. defer file.Close()
  57. }
  58. res, bs, err := object.AddFile(storeId, userName, key, isLeaf, filename, file)
  59. if err != nil {
  60. c.ResponseError(err.Error())
  61. return
  62. }
  63. if res {
  64. addFileToCache(key, filename, bs)
  65. addRecordForFile(c, userName, "Add", storeId, key, filename, isLeaf)
  66. }
  67. c.ResponseOk(res)
  68. }
  69. func (c *ApiController) DeleteFile() {
  70. userName, ok := c.RequireSignedIn()
  71. if !ok {
  72. return
  73. }
  74. storeId := c.Input().Get("store")
  75. key := c.Input().Get("key")
  76. isLeaf := c.Input().Get("isLeaf") == "1"
  77. res, err := object.DeleteFile(storeId, key, isLeaf)
  78. if err != nil {
  79. c.ResponseError(err.Error())
  80. return
  81. }
  82. if res {
  83. addRecordForFile(c, userName, "Delete", storeId, key, "", isLeaf)
  84. }
  85. c.ResponseOk(res)
  86. }