* Best-effort peek: first few direct child folders + files for a folder. * Uses existing getFolderList.php + getFileList.php. * * Returns: { items: Array<{type,name}>, truncated: boolean }
(folder, sourceId = '')
| 3260 | * Returns: { items: Array<{type,name}>, truncated: boolean } |
| 3261 | */ |
| 3262 | async function fetchFolderPeek(folder, sourceId = '') { |
| 3263 | if (!folder) return null; |
| 3264 | if (isSlowRemoteSource(sourceId)) return null; |
| 3265 | |
| 3266 | const key = folderCacheKey(folder, sourceId); |
| 3267 | if (_folderPeekCache.has(key)) { |
| 3268 | return _folderPeekCache.get(key); |
| 3269 | } |
| 3270 | |
| 3271 | const sourceParam = sourceId ? `&sourceId=${encodeURIComponent(sourceId)}` : ''; |
| 3272 | const p = (async () => { |
| 3273 | try { |
| 3274 | // 1) Files in this folder |
| 3275 | let files = []; |
| 3276 | try { |
| 3277 | const res = await fetch( |
| 3278 | withBase(`/api/file/getFileList.php?folder=${encodeURIComponent(folder)}&recursive=0${sourceParam}&t=${Date.now()}`), |
| 3279 | { credentials: "include" } |
| 3280 | ); |
| 3281 | const raw = await safeJson(res); |
| 3282 | if (Array.isArray(raw.files)) { |
| 3283 | files = raw.files; |
| 3284 | } else if (raw.files && typeof raw.files === "object") { |
| 3285 | files = Object.entries(raw.files).map(([name, meta]) => ({ |
| 3286 | ...(meta || {}), |
| 3287 | name |
| 3288 | })); |
| 3289 | } |
| 3290 | } catch (e) { |
| 3291 | // ignore file errors; we can still show folders |
| 3292 | } |
| 3293 | |
| 3294 | // 2) Direct subfolders |
| 3295 | let subfolderNames = []; |
| 3296 | try { |
| 3297 | const res2 = await fetch( |
| 3298 | withBase(`/api/folder/getFolderList.php?folder=${encodeURIComponent(folder)}&counts=0${sourceParam}`), |
| 3299 | { credentials: "include" } |
| 3300 | ); |
| 3301 | const raw2 = await safeJson(res2); |
| 3302 | |
| 3303 | if (Array.isArray(raw2)) { |
| 3304 | const allPaths = raw2.map(item => item.folder ?? item); |
| 3305 | const depth = folder === "root" ? 1 : folder.split("/").length + 1; |
| 3306 | |
| 3307 | subfolderNames = allPaths |
| 3308 | .filter(p => { |
| 3309 | if (folder === "root") return p.indexOf("/") === -1; |
| 3310 | if (!p.startsWith(folder + "/")) return false; |
| 3311 | return p.split("/").length === depth; |
| 3312 | }) |
| 3313 | .map(p => p.split("/").pop() || p); |
| 3314 | } |
| 3315 | } catch (e) { |
| 3316 | // ignore folder errors |
| 3317 | } |
| 3318 | |
| 3319 | const items = []; |
no test coverage detected