dsh-gallery 0.11.2 → 0.12.1

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/fsutil.js ADDED
@@ -0,0 +1,136 @@
1
+ /**
2
+ * 底层文件工具:路径越界、读文件头、图片尺寸、参数归一化。
3
+ *
4
+ * S1 拆分自 index.js(行为零变化)。
5
+ */
6
+
7
+ import { promises as fsp, readFileSync, statSync, createReadStream } from 'node:fs'
8
+
9
+ export function numberOr(value, fallback, min, max) {
10
+ const n = typeof value === 'number' ? value : Number(value)
11
+ if (!Number.isFinite(n)) return fallback
12
+ return Math.min(Math.max(Math.trunc(n), min), max)
13
+ }
14
+
15
+ export function arrayOfStrings(value, fallback) {
16
+ if (!Array.isArray(value)) return [...fallback]
17
+ const cleaned = value.filter((entry) => typeof entry === 'string' && entry.trim() !== '').map((entry) => entry.trim())
18
+ return cleaned.length > 0 ? cleaned : [...fallback]
19
+ }
20
+
21
+ /** Windows 路径比较统一小写;用于「必须在根目录内」的判定。 */
22
+
23
+ export function isInside(root, target) {
24
+ const a = root.toLowerCase().replace(/[\\/]+$/, '')
25
+ const b = target.toLowerCase().replace(/[\\/]+$/, '')
26
+ return b === a || b.startsWith(a + '\\') || b.startsWith(a + '/')
27
+ }
28
+
29
+ /** 只读图片头部若干字节,用于解析尺寸;失败返回 null。 */
30
+
31
+ export async function readHead(fullPath, byteLimit) {
32
+ let handle
33
+ try {
34
+ handle = await fsp.open(fullPath, 'r')
35
+ const size = (await handle.stat()).size
36
+ const want = Math.min(byteLimit, size)
37
+ const buffer = Buffer.alloc(want)
38
+ const { bytesRead } = await handle.read(buffer, 0, want, 0)
39
+ return buffer.subarray(0, bytesRead)
40
+ } catch {
41
+ return null
42
+ } finally {
43
+ if (handle !== undefined) await handle.close().catch(() => {})
44
+ }
45
+ }
46
+
47
+ /** PNG:IHDR 在固定偏移。 */
48
+
49
+ export function pngSize(buf) {
50
+ if (buf.length < 24) return null
51
+ if (buf[0] !== 0x89 || buf[1] !== 0x50 || buf[2] !== 0x4e || buf[3] !== 0x47) return null
52
+ const width = buf.readUInt32BE(16)
53
+ const height = buf.readUInt32BE(20)
54
+ return width > 0 && height > 0 ? { width, height } : null
55
+ }
56
+
57
+ /** JPEG:顺序找 SOF 段。 */
58
+
59
+ export function jpegSize(buf) {
60
+ if (buf.length < 4 || buf[0] !== 0xff || buf[1] !== 0xd8) return null
61
+ let p = 2
62
+ while (p + 9 < buf.length) {
63
+ if (buf[p] !== 0xff) {
64
+ p += 1
65
+ continue
66
+ }
67
+ let marker = buf[p + 1]
68
+ while (marker === 0xff && p + 1 < buf.length) {
69
+ p += 1
70
+ marker = buf[p + 1]
71
+ }
72
+ if (marker === 0xd8 || marker === 0x01 || (marker >= 0xd0 && marker <= 0xd7)) {
73
+ p += 2
74
+ continue
75
+ }
76
+ if (marker === 0xd9 || marker === 0xda) return null
77
+ const length = buf.readUInt16BE(p + 2)
78
+ if (length < 2) return null
79
+ const isSof = marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc
80
+ if (isSof) {
81
+ const height = buf.readUInt16BE(p + 5)
82
+ const width = buf.readUInt16BE(p + 7)
83
+ return width > 0 && height > 0 ? { width, height } : null
84
+ }
85
+ p += 2 + length
86
+ }
87
+ return null
88
+ }
89
+
90
+ /** WebP:RIFF 容器,VP8 / VP8L / VP8X 三种尺寸布局。 */
91
+
92
+ export function webpSize(buf) {
93
+ if (buf.length < 32) return null
94
+ if (buf.toString('ascii', 0, 4) !== 'RIFF' || buf.toString('ascii', 8, 12) !== 'WEBP') return null
95
+ const chunk = buf.toString('ascii', 12, 16)
96
+ if (chunk === 'VP8 ') {
97
+ const width = buf.readUInt16LE(26) & 0x3fff
98
+ const height = buf.readUInt16LE(28) & 0x3fff
99
+ return width > 0 && height > 0 ? { width, height } : null
100
+ }
101
+ if (chunk === 'VP8L') {
102
+ const b0 = buf[21]
103
+ const b1 = buf[22]
104
+ const b2 = buf[23]
105
+ const b3 = buf[24]
106
+ const width = 1 + (((b0 & 0xff) | ((b1 & 0x3f) << 8)) & 0x3fff)
107
+ const height = 1 + ((((b1 & 0xc0) >> 6) | (b2 << 2) | ((b3 & 0x0f) << 10)) & 0x3fff)
108
+ return width > 0 && height > 0 ? { width, height } : null
109
+ }
110
+ if (chunk === 'VP8X') {
111
+ const width = 1 + (buf[24] | (buf[25] << 8) | (buf[26] << 16))
112
+ const height = 1 + (buf[27] | (buf[28] << 8) | (buf[29] << 16))
113
+ return width > 0 && height > 0 ? { width, height } : null
114
+ }
115
+ return null
116
+ }
117
+
118
+ /** 按扩展名挑选解析器;返回 {width,height} 或 null。 */
119
+
120
+ export function measureImage(buf, ext) {
121
+ try {
122
+ if (buf === null) return null
123
+ if (ext === '.png') return pngSize(buf)
124
+ if (ext === '.jpg' || ext === '.jpeg') return jpegSize(buf)
125
+ if (ext === '.webp') return webpSize(buf)
126
+ return pngSize(buf) ?? jpegSize(buf) ?? webpSize(buf)
127
+ } catch {
128
+ return null
129
+ }
130
+ }
131
+
132
+ /**
133
+ * 校验用户想设的产出根目录:必须是绝对路径、必须存在、必须是目录。
134
+ * @param candidate - 用户输入的路径。
135
+ * @returns { ok, root?, reason? } —— 通过时 root 是规范化后的绝对路径。
136
+ */
package/host/groups.js ADDED
@@ -0,0 +1,92 @@
1
+ /**
2
+ * 按对话分组(scan 依赖它,所以必须在前)。
3
+ *
4
+ * S1 拆分自 index.js(行为零变化)。
5
+ */
6
+
7
+ export function buildGroups(summary, maxPerGroup = 120, currentSessionId = '') {
8
+ if (!Array.isArray(summary) || summary.length === 0) return []
9
+
10
+ const groups = new Map()
11
+ for (const item of summary) {
12
+ const key = item.sessionId ?? '__unassigned__'
13
+ if (!groups.has(key)) {
14
+ groups.set(key, {
15
+ sessionId: item.sessionId,
16
+ title: item.sessionId === null ? '' : (item.sessionTitle || item.sessionId.slice(0, 8)),
17
+ createdAt: item.sessionCreatedAt || 0,
18
+ files: [],
19
+ })
20
+ }
21
+ const g = groups.get(key)
22
+ g.files.push({
23
+ absPath: item.absPath,
24
+ relPath: item.relPath,
25
+ category: item.category,
26
+ mtime: item.mtime,
27
+ width: item.width ?? null,
28
+ height: item.height ?? null,
29
+ size: item.size ?? 0,
30
+ name: item.name ?? item.relPath.split('/').pop(),
31
+ // 客户端卡片要靠这两个字段判断渲染类型(v0.10.1)
32
+ extension: item.extension ?? ('.' + item.relPath.split('/').pop().split('.').pop().toLowerCase()),
33
+ ...(item.mediaType !== undefined ? { mediaType: item.mediaType } : {}),
34
+ })
35
+ if (item.sessionId === null && item.mtime > g.createdAt) g.createdAt = item.mtime
36
+ }
37
+
38
+ const out = []
39
+ for (const g of groups.values()) {
40
+ g.files.sort((a, b) => b.mtime - a.mtime)
41
+ const counts = { final: 0, input: 0, intermediate: 0, unknown: 0 }
42
+ for (const f of g.files) {
43
+ if (counts[f.category] !== undefined) counts[f.category] += 1
44
+ }
45
+ // 只回传前 maxPerGroup 条明细(客户端滚动到底再按需请求)
46
+ const returned = g.files.slice(0, maxPerGroup)
47
+ out.push({
48
+ sessionId: g.sessionId,
49
+ isHistory: g.sessionId === null,
50
+ // 当前打开的会话:客户端据此置顶并默认展开
51
+ isCurrent: currentSessionId !== '' && g.sessionId === currentSessionId,
52
+ title: g.title,
53
+ createdAt: g.createdAt,
54
+ fileCount: g.files.length,
55
+ returnedCount: returned.length,
56
+ truncated: g.files.length > returned.length,
57
+ counts,
58
+ files: returned,
59
+ latestMtime: g.files.length > 0 ? g.files[0].mtime : 0,
60
+ })
61
+ }
62
+
63
+ // 组排序:
64
+ // 1. 当前对话永远第一(按时间倒序会把它压到中间)
65
+ // 2. 其余对话按创建时间倒序
66
+ // 3. 「历史资产」永远最后
67
+ out.sort((a, b) => {
68
+ if (a.isCurrent !== b.isCurrent) return a.isCurrent ? -1 : 1
69
+ if (a.isHistory && !b.isHistory) return 1
70
+ if (b.isHistory && !a.isHistory) return -1
71
+ return b.createdAt - a.createdAt
72
+ })
73
+
74
+ return out
75
+ }
76
+
77
+ /**
78
+ * 把一个相对路径解析成根目录内的绝对路径;越界或不存在返回 null。
79
+ * 多目录支持:第一个 root 找不到就逐个试其余 root。
80
+ * @param roots - 扫描目录列表(或单个 root 字符串,兼容旧调用)。
81
+ */
82
+ /**
83
+ * 把「相对路径」解析成一个可读文件。
84
+ *
85
+ * @param roots - 允许的根目录。
86
+ * @param relPath - 相对 root 的路径(用 / 分隔)。
87
+ * @param opts - 配置。
88
+ * @param byteCap - 体积上限(字节)。**图片和媒体上限不同**:
89
+ * 图片用 maxImageBytes(默认 64MB),音视频用 maxMediaBytes(默认 200MB)。
90
+ * 不传时退回 maxImageBytes。以前这里固定用 maxImageBytes,
91
+ * 导致 127MB 的视频被判「不可读」→ 前端收到 404。
92
+ */