dsh-gallery 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/host/scan.js CHANGED
@@ -43,6 +43,35 @@ export function isFreshSince(stat, sessionStartMs) {
43
43
  return newest >= since
44
44
  }
45
45
 
46
+ /**
47
+ * 2026-11-16 第一档:限流并发执行。
48
+ * scan 阶段每个文件都要读一次 stat,串行时五千多个文件要 7 秒左右;
49
+ * 这里用固定数量的「工人」同时干,结果按下标回填,顺序与串行完全一致。
50
+ */
51
+ export async function mapPool(items, limit, fn) {
52
+ const list = Array.isArray(items) ? items : []
53
+ const out = new Array(list.length)
54
+ if (list.length === 0) return out
55
+ const width = Math.max(1, Math.min(Math.trunc(Number(limit) || 1), list.length))
56
+ let cursor = 0
57
+ const workers = []
58
+ for (let w = 0; w < width; w += 1) {
59
+ workers.push((async () => {
60
+ for (;;) {
61
+ const i = cursor
62
+ cursor += 1
63
+ if (i >= list.length) return
64
+ out[i] = await fn(list[i], i)
65
+ }
66
+ })())
67
+ }
68
+ await Promise.all(workers)
69
+ return out
70
+ }
71
+
72
+ /** 并发读 stat 的路数:本地盘 32 路收益已接近上限,再高只增加抢占。 */
73
+ export const SCAN_CONCURRENCY = 32
74
+
46
75
  export function categoryForSessionAsset(autoCategory, externalAsset) {
47
76
  return autoCategory ?? (externalAsset ? ASSET_CATEGORY.INPUT : null)
48
77
 
@@ -449,6 +478,8 @@ export async function scanGallery(config, kind = 'image', scope = 'global', sess
449
478
  const hiddenSet = new Set(await readHiddenList(opts.settingsFile))
450
479
 
451
480
  const matches = []
481
+ // 2026-11-16 第一档:两阶段扫描的候选路径暂存(先收集、再并发 stat)
482
+ const candidates = []
452
483
  let directories = 0
453
484
  let entries = 0
454
485
  let capped = false
@@ -509,48 +540,66 @@ export async function scanGallery(config, kind = 'image', scope = 'global', sess
509
540
  const relPath = path.relative(rootDir, fullPath).split(path.sep).join('/')
510
541
  // 当前对话:精确路径白名单优先;不在白名单但落在「本对话用过的目录」里,
511
542
  // 视作外部放进来的素材放行(v0.14.7)。其它目录一律不进当前对话。
512
- let externalAsset = false
513
543
  let looseDirHit = false
514
544
  if (sessionRelSet !== null && !sessionRelSet.has(relPath.toLowerCase())) {
515
545
  if (sessionDirSet === null || !sessionDirSet.has(parentDirOf(relPath))) continue
516
546
  looseDirHit = true
517
547
  }
518
- let stat
519
- try {
520
- stat = await fsp.stat(fullPath)
521
- } catch {
522
- continue
523
- }
524
- // 2026-11-15:同目录放宽只收本对话期间新出现 / 改动的图;
525
- // 否则只读查一次图,就会把该目录里几百张历史图全算进这个新对话。
526
- if (looseDirHit && !isFreshSince(stat, sessionStartMs)) continue
527
- if (looseDirHit) externalAsset = true
528
- if (stat.size <= 0 || stat.mtimeMs < cutoff) continue
529
- // 音视频:超过体积上限的直接跳过(视频可能几百 MB,列出来点开就卡)
530
- if (isMedia && stat.size > opts.maxMediaBytes) continue
531
548
  // 被隐藏的直接跳过(隐藏只影响列表展示,文件仍在磁盘上)
532
549
  if (hiddenSet.has(relPath)) continue
533
- matches.push({
550
+ // 第一阶段只登记路径,stat 留到全部收集完再统一并发读(第一档性能优化)
551
+ candidates.push({
552
+ fullPath,
534
553
  relPath,
535
- relKey: `${opts.roots.indexOf(rootDir)}:${relPath}`,
554
+ entryName,
536
555
  rootDir,
537
556
  rootIndex: opts.roots.indexOf(rootDir),
538
- name: entryName,
539
- size: stat.size,
540
- mtime: stat.mtimeMs,
541
- // 手工拷贝旧文件时 mtime 仍是老的,但创建时间是新的;
542
- // 两个视图的同目录放宽判据都看它,不能只给 mtime 造成一边有一边没有。
543
- birthtimeMs: stat.birthtimeMs,
544
557
  extension: ext,
545
- // v0.14.7:外部素材标记,扫描阶段据此给出「素材」标签
546
- ...(externalAsset ? { externalAsset: true } : {}),
547
- // 音视频区分「视频 / 音频」,客户端据此选 <video> 还是 <audio>
548
- ...(isMedia ? { mediaType: VIDEO_EXTS.includes(ext) ? 'video' : 'audio' } : {}),
558
+ looseDirHit,
549
559
  })
550
560
  }
551
561
  }
552
562
  }
553
563
 
564
+ // 2026-11-16 第一档:第二阶段限流并发读 stat。
565
+ // 全库五千多个文件逐个 await 要 7 秒左右,几十路并发能压到 1 秒内;
566
+ // 过滤条件(时间窗口 / 体积 / 隐藏 / 同目录新鲜度)与串行时完全一致,
567
+ // 只是把「等待」叠起来做,不改变任何一条判定。
568
+ const statted = await mapPool(candidates, SCAN_CONCURRENCY, async (c) => {
569
+ let stat
570
+ try {
571
+ stat = await fsp.stat(c.fullPath)
572
+ } catch {
573
+ return null
574
+ }
575
+ // 2026-11-15:同目录放宽只收本对话期间新出现 / 改动的图;
576
+ // 否则只读查一次图,就会把该目录里几百张历史图全算进这个新对话。
577
+ if (c.looseDirHit && !isFreshSince(stat, sessionStartMs)) return null
578
+ if (stat.size <= 0 || stat.mtimeMs < cutoff) return null
579
+ // 音视频:超过体积上限的直接跳过(视频可能几百 MB,列出来点开就卡)
580
+ if (isMedia && stat.size > opts.maxMediaBytes) return null
581
+ return {
582
+ relPath: c.relPath,
583
+ relKey: `${c.rootIndex}:${c.relPath}`,
584
+ rootDir: c.rootDir,
585
+ rootIndex: c.rootIndex,
586
+ name: c.entryName,
587
+ size: stat.size,
588
+ mtime: stat.mtimeMs,
589
+ // 手工拷贝旧文件时 mtime 仍是老的,但创建时间是新的;
590
+ // 两个视图的同目录放宽判据都看它,不能只给 mtime 造成一边有一边没有。
591
+ birthtimeMs: stat.birthtimeMs,
592
+ extension: c.extension,
593
+ // v0.14.7:外部素材标记,扫描阶段据此给出「素材」标签
594
+ ...(c.looseDirHit ? { externalAsset: true } : {}),
595
+ // 音视频区分「视频 / 音频」,客户端据此选 <video> 还是 <audio>
596
+ ...(isMedia ? { mediaType: VIDEO_EXTS.includes(c.extension) ? 'video' : 'audio' } : {}),
597
+ }
598
+ })
599
+ for (const item of statted) {
600
+ if (item !== null) matches.push(item)
601
+ }
602
+
554
603
  const rootMissing = !anyRootExists
555
604
 
556
605
  // v0.14.2:手动标签覆写优先于机器预判的产出状态。