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 1.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package object
  2. import (
  3. "strings"
  4. "time"
  5. "github.com/casbin/casbase/storage"
  6. "github.com/casbin/casbase/util"
  7. )
  8. func (store *Store) createPathIfNotExisted(tokens []string, lastModifiedTime string, lastIsLeaf bool) {
  9. currentFile := store.FileTree
  10. if currentFile == nil {
  11. currentFile = &File{
  12. Key: "/",
  13. Title: "",
  14. ModifiedTime: util.GetCurrentTime(),
  15. IsLeaf: false,
  16. Children: []*File{},
  17. ChildrenMap: map[string]*File{},
  18. }
  19. store.FileTree = currentFile
  20. }
  21. for i, token := range tokens {
  22. if currentFile.Children == nil {
  23. currentFile.Children = []*File{}
  24. }
  25. if currentFile.ChildrenMap == nil {
  26. currentFile.ChildrenMap = map[string]*File{}
  27. }
  28. tmpFile, ok := currentFile.ChildrenMap[token]
  29. if ok {
  30. currentFile = tmpFile
  31. continue
  32. }
  33. isLeaf := false
  34. if i == len(tokens)-1 {
  35. isLeaf = lastIsLeaf
  36. }
  37. key := strings.Join(tokens[:i+1], "/")
  38. newFile := &File{
  39. Key: key,
  40. Title: token,
  41. IsLeaf: isLeaf,
  42. Children: []*File{},
  43. ChildrenMap: map[string]*File{},
  44. }
  45. if i == len(tokens)-1 {
  46. newFile.ModifiedTime = lastModifiedTime
  47. }
  48. currentFile.Children = append(currentFile.Children, newFile)
  49. currentFile.ChildrenMap[token] = newFile
  50. currentFile = newFile
  51. }
  52. }
  53. func (store *Store) Populate() {
  54. objects := storage.ListObjects(store.Bucket)
  55. for _, object := range objects {
  56. lastModifiedTime := object.LastModified.Local().Format(time.RFC3339)
  57. lastIsLeaf := true
  58. if object.Key[len(object.Key)-1] == '/' {
  59. lastIsLeaf = false
  60. }
  61. tokens := strings.Split(strings.Trim(object.Key, "/"), "/")
  62. store.createPathIfNotExisted(tokens, lastModifiedTime, lastIsLeaf)
  63. //fmt.Printf("%s, %d, %v\n", object.Key, object.Size, object.LastModified)
  64. }
  65. }

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

Contributors (1)