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.

store_provider.go 3.5 kB

2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. "fmt"
  17. "strings"
  18. "github.com/casbin/casibase/storage"
  19. )
  20. func (store *Store) createPathIfNotExisted(tokens []string, size int64, url string, lastModifiedTime string, isLeaf bool) {
  21. currentFile := store.FileTree
  22. for i, token := range tokens {
  23. if currentFile.Children == nil {
  24. currentFile.Children = []*File{}
  25. }
  26. if currentFile.ChildrenMap == nil {
  27. currentFile.ChildrenMap = map[string]*File{}
  28. }
  29. tmpFile, ok := currentFile.ChildrenMap[token]
  30. if ok {
  31. currentFile = tmpFile
  32. continue
  33. }
  34. isLeafTmp := false
  35. if i == len(tokens)-1 {
  36. isLeafTmp = isLeaf
  37. }
  38. key := strings.Join(tokens[:i+1], "/")
  39. newFile := &File{
  40. Key: key,
  41. Title: token,
  42. IsLeaf: isLeafTmp,
  43. Url: url,
  44. Children: []*File{},
  45. ChildrenMap: map[string]*File{},
  46. }
  47. if i == len(tokens)-1 {
  48. newFile.Size = size
  49. newFile.CreatedTime = lastModifiedTime
  50. if token == "_hidden.ini" {
  51. continue
  52. }
  53. } else if i == len(tokens)-2 {
  54. if tokens[len(tokens)-1] == "_hidden.ini" {
  55. newFile.CreatedTime = lastModifiedTime
  56. }
  57. }
  58. currentFile.Children = append(currentFile.Children, newFile)
  59. currentFile.ChildrenMap[token] = newFile
  60. currentFile = newFile
  61. }
  62. }
  63. func isObjectLeaf(object *storage.Object) bool {
  64. isLeaf := true
  65. if object.Key[len(object.Key)-1] == '/' {
  66. isLeaf = false
  67. }
  68. return isLeaf
  69. }
  70. func (store *Store) Populate() error {
  71. objects, err := storage.ListObjects(store.StorageProvider, "")
  72. if err != nil {
  73. return err
  74. }
  75. if store.FileTree == nil {
  76. store.FileTree = &File{
  77. Key: "/",
  78. Title: store.DisplayName,
  79. CreatedTime: store.CreatedTime,
  80. IsLeaf: false,
  81. Url: "",
  82. Children: []*File{},
  83. ChildrenMap: map[string]*File{},
  84. }
  85. }
  86. sortedObjects := []*storage.Object{}
  87. for _, object := range objects {
  88. if strings.HasSuffix(object.Key, "/_hidden.ini") {
  89. sortedObjects = append(sortedObjects, object)
  90. }
  91. }
  92. for _, object := range objects {
  93. if !strings.HasSuffix(object.Key, "/_hidden.ini") {
  94. sortedObjects = append(sortedObjects, object)
  95. }
  96. }
  97. for _, object := range sortedObjects {
  98. lastModifiedTime := object.LastModified
  99. isLeaf := isObjectLeaf(object)
  100. size := object.Size
  101. url := object.Url
  102. tokens := strings.Split(strings.Trim(object.Key, "/"), "/")
  103. store.createPathIfNotExisted(tokens, size, url, lastModifiedTime, isLeaf)
  104. // fmt.Printf("%s, %d, %v\n", object.Key, object.Size, object.LastModified)
  105. }
  106. return nil
  107. }
  108. func (store *Store) GetVideoData() ([]string, error) {
  109. objects, err := storage.ListObjects(store.StorageProvider, "2023/视频附件")
  110. if err != nil {
  111. return nil, err
  112. }
  113. res := []string{}
  114. for _, object := range objects {
  115. if strings.HasSuffix(object.Key, "/_hidden.ini") {
  116. continue
  117. }
  118. url := fmt.Sprintf("%s/%s", store.StorageProvider, object.Key)
  119. res = append(res, url)
  120. }
  121. return res, nil
  122. }