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