| 131 | } |
| 132 | |
| 133 | func listDirectory(ctx context.Context, widgetId string, dir string, maxFiles int) (<-chan DirEntryResult, error) { |
| 134 | key := widgetId + "|" + dir |
| 135 | if cached, ok := getCache(key); ok { |
| 136 | ch := make(chan DirEntryResult, ListDirChanSize) |
| 137 | go func() { |
| 138 | defer close(ch) |
| 139 | for _, r := range cached { |
| 140 | select { |
| 141 | case ch <- r: |
| 142 | case <-ctx.Done(): |
| 143 | return |
| 144 | } |
| 145 | } |
| 146 | }() |
| 147 | return ch, nil |
| 148 | } |
| 149 | |
| 150 | // Use singleflight to ensure only one listing operation occurs per key. |
| 151 | value, err, _ := group.Do(key, func() (interface{}, error) { |
| 152 | f, err := os.Open(dir) |
| 153 | if err != nil { |
| 154 | return nil, err |
| 155 | } |
| 156 | defer f.Close() |
| 157 | fi, err := f.Stat() |
| 158 | if err != nil { |
| 159 | return nil, err |
| 160 | } |
| 161 | if !fi.IsDir() { |
| 162 | return nil, fmt.Errorf("%s is not a directory", dir) |
| 163 | } |
| 164 | entries, err := f.ReadDir(maxFiles) |
| 165 | if err != nil { |
| 166 | return nil, err |
| 167 | } |
| 168 | var results []DirEntryResult |
| 169 | for _, entry := range entries { |
| 170 | results = append(results, DirEntryResult{Entry: entry}) |
| 171 | } |
| 172 | // Add parent directory (“..”) entry if not at the filesystem root. |
| 173 | if filepath.Dir(dir) != dir { |
| 174 | mockDir := &MockDirEntry{ |
| 175 | NameStr: "..", |
| 176 | IsDirVal: true, |
| 177 | FileMode: fs.ModeDir | 0755, |
| 178 | } |
| 179 | results = append(results, DirEntryResult{Entry: mockDir}) |
| 180 | } |
| 181 | return results, nil |
| 182 | }) |
| 183 | if err != nil { |
| 184 | return nil, err |
| 185 | } |
| 186 | results := value.([]DirEntryResult) |
| 187 | setCache(key, results) |
| 188 | |
| 189 | ch := make(chan DirEntryResult, ListDirChanSize) |
| 190 | go func() { |