Load retrieves data from a Notion directory and returns a list of schema.Document objects.
(ctx context.Context)
| 93 | |
| 94 | // Load retrieves data from a Notion directory and returns a list of schema.Document objects. |
| 95 | func (l *RecursiveDirectoryLoader) Load(ctx context.Context) ([]schema.Document, error) { |
| 96 | var docs []schema.Document |
| 97 | |
| 98 | err := filepath.WalkDir(l.root, func(path string, d fs.DirEntry, err error) error { |
| 99 | if err != nil { |
| 100 | return err |
| 101 | } |
| 102 | if d.IsDir() { |
| 103 | rel, _ := filepath.Rel(l.root, path) |
| 104 | depth := strings.Count(rel, string(os.PathSeparator)) |
| 105 | if depth >= l.maxDepth { |
| 106 | return fs.SkipDir |
| 107 | } |
| 108 | return nil |
| 109 | } |
| 110 | |
| 111 | ext := strings.ToLower(filepath.Ext(path)) |
| 112 | if len(l.allowExt) > 0 { |
| 113 | if _, ok := l.allowExt[ext]; !ok { |
| 114 | return nil |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | var fileDocs []schema.Document |
| 119 | if err := func() error { |
| 120 | f, err := os.Open(path) |
| 121 | if err != nil { |
| 122 | return nil |
| 123 | } |
| 124 | defer f.Close() |
| 125 | |
| 126 | loader, err := l.newLoader(f) |
| 127 | if err != nil { |
| 128 | return err |
| 129 | } |
| 130 | |
| 131 | fileDocs, err = loader.Load(ctx) |
| 132 | if err != nil { |
| 133 | return err |
| 134 | } |
| 135 | for i := range fileDocs { |
| 136 | if fileDocs[i].Metadata == nil { |
| 137 | fileDocs[i].Metadata = make(map[string]any) |
| 138 | } |
| 139 | fileDocs[i].Metadata["source"] = path |
| 140 | } |
| 141 | return nil |
| 142 | }(); err != nil { |
| 143 | log.Printf("skip %s: %v", path, err) |
| 144 | } else { |
| 145 | docs = append(docs, fileDocs...) |
| 146 | } |
| 147 | |
| 148 | return nil |
| 149 | }) |
| 150 | return docs, err |
| 151 | } |
| 152 |
no test coverage detected