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 2.8 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
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package object
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  7. "github.com/casbin/casibase/storage"
  8. )
  9. func (store *Store) createPathIfNotExisted(tokens []string, size int64, lastModifiedTime string, isLeaf bool) {
  10. currentFile := store.FileTree
  11. for i, token := range tokens {
  12. if currentFile.Children == nil {
  13. currentFile.Children = []*File{}
  14. }
  15. if currentFile.ChildrenMap == nil {
  16. currentFile.ChildrenMap = map[string]*File{}
  17. }
  18. tmpFile, ok := currentFile.ChildrenMap[token]
  19. if ok {
  20. currentFile = tmpFile
  21. continue
  22. }
  23. isLeafTmp := false
  24. if i == len(tokens)-1 {
  25. isLeafTmp = isLeaf
  26. }
  27. key := strings.Join(tokens[:i+1], "/")
  28. newFile := &File{
  29. Key: key,
  30. Title: token,
  31. IsLeaf: isLeafTmp,
  32. Children: []*File{},
  33. ChildrenMap: map[string]*File{},
  34. }
  35. if i == len(tokens)-1 {
  36. newFile.Size = size
  37. newFile.CreatedTime = lastModifiedTime
  38. if token == "_hidden.ini" {
  39. continue
  40. }
  41. } else if i == len(tokens)-2 {
  42. if tokens[len(tokens)-1] == "_hidden.ini" {
  43. newFile.CreatedTime = lastModifiedTime
  44. }
  45. }
  46. currentFile.Children = append(currentFile.Children, newFile)
  47. currentFile.ChildrenMap[token] = newFile
  48. currentFile = newFile
  49. }
  50. }
  51. func isObjectLeaf(object *oss.ObjectProperties) bool {
  52. isLeaf := true
  53. if object.Key[len(object.Key)-1] == '/' {
  54. isLeaf = false
  55. }
  56. return isLeaf
  57. }
  58. func (store *Store) Populate() {
  59. objects := storage.ListObjects(store.Bucket, "")
  60. if store.FileTree == nil {
  61. store.FileTree = &File{
  62. Key: "/",
  63. Title: store.DisplayName,
  64. CreatedTime: store.CreatedTime,
  65. IsLeaf: false,
  66. Children: []*File{},
  67. ChildrenMap: map[string]*File{},
  68. }
  69. }
  70. sortedObjects := []oss.ObjectProperties{}
  71. for _, object := range objects {
  72. if strings.HasSuffix(object.Key, "/_hidden.ini") {
  73. sortedObjects = append(sortedObjects, object)
  74. }
  75. }
  76. for _, object := range objects {
  77. if !strings.HasSuffix(object.Key, "/_hidden.ini") {
  78. sortedObjects = append(sortedObjects, object)
  79. }
  80. }
  81. for _, object := range sortedObjects {
  82. lastModifiedTime := object.LastModified.Local().Format(time.RFC3339)
  83. isLeaf := isObjectLeaf(&object)
  84. size := object.Size
  85. tokens := strings.Split(strings.Trim(object.Key, "/"), "/")
  86. store.createPathIfNotExisted(tokens, size, lastModifiedTime, isLeaf)
  87. //fmt.Printf("%s, %d, %v\n", object.Key, object.Size, object.LastModified)
  88. }
  89. }
  90. func (store *Store) GetVideoData() []string {
  91. objects := storage.ListObjects(store.Bucket, "2023/视频附件")
  92. res := []string{}
  93. for _, object := range objects {
  94. if strings.HasSuffix(object.Key, "/_hidden.ini") {
  95. continue
  96. }
  97. url := fmt.Sprintf("%s/%s", store.Domain, object.Key)
  98. res = append(res, url)
  99. }
  100. return res
  101. }

基于Casbin的开源AI领域知识库平台

Contributors (1)