lumigather 1.0.3 → 1.0.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lumigather",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "局域网 Mod 图片画廊:目录浏览、压缩包直出、序列帧合成 GIF",
5
5
  "bin": {
6
6
  "lumigather": "bin/lumigather.js"
package/server/app.js CHANGED
@@ -127,6 +127,16 @@ const browseRoutes = createRoute({
127
127
  "GET /images": (req, res, c) => browse.apiImages(c.query, res),
128
128
  "GET /dir": (req, res, c) => browse.apiDir(c.query, res),
129
129
  "GET /media": (req, res, c) => browse.apiMedia(c.query, res, req.headers),
130
+ // 扫描进度:只读内存里的进度表与闸门状态,**不触发任何扫描**
131
+ "GET /scan-progress": (req, res) => {
132
+ const queueStats = require("./lib/scan-queue").scanQueueStats();
133
+ jsonRes(res, 200, {
134
+ active: queueStats.active,
135
+ queued: queueStats.queued,
136
+ concurrency: queueStats.concurrency,
137
+ items: require("./lib/scan-progress").progressSnapshot(),
138
+ });
139
+ },
130
140
  });
131
141
 
132
142
  const archiveRoutes = createRoute({
@@ -215,6 +225,8 @@ createServer({
215
225
  "/api/images",
216
226
  "/api/dir",
217
227
  "/api/media",
228
+ // 扫描进度:只读计数与当前路径,不含条目内容;与 /api/index-cache 同性质
229
+ "/api/scan-progress",
218
230
  "/api/uploaded",
219
231
  // 上传:无 targetDir 时游客可传(写入项目 uploads/),带 targetDir 才要求登录;
220
232
  // 该判定由 routes/upload.js 的 requireAuth 负责,故此处须先放行框架登录门。
@@ -251,7 +251,11 @@ function createServer(opts) {
251
251
  const cfgPort = typeof config.get === "function"
252
252
  ? config.get("port")
253
253
  : (typeof config.readConfig === "function" ? (config.readConfig() || {}).port : null);
254
- const port = portOpt || cfgPort || process.env.PORT || DEFAULT_PORT;
254
+ // 端口优先级:环境变量 PORT(bin/start.sh 注入)> 调用方传参 > 配置文件/默认值。
255
+ // 原先把 cfgPort 放在 PORT 之前:配置文件里有默认 port 时,--port 与 PORT 永远被覆盖
256
+ // (实测 npx --port 8682 仍监听配置里的 8642)。命令行显式端口必须能盖过配置文件默认值。
257
+ const envPort = Number(process.env.PORT || 0);
258
+ const port = (Number.isInteger(envPort) && envPort > 0) ? envPort : (portOpt || cfgPort || DEFAULT_PORT);
255
259
 
256
260
  server.listen(port, () => {
257
261
  // 不传 host 时 Node 监听 ::/0.0.0.0(局域网可访问)。
@@ -126,7 +126,7 @@ const DEFAULT_BLACKLIST_DIRS = [
126
126
  "/proc", "/sys", "/dev", "/run", "/boot", "/etc", "/bin", "/sbin", "/lib", "/lib64",
127
127
  "/usr", "/var", "/tmp",
128
128
  // 群晖 NAS 元数据目录(系统/套件数据,不属于用户资源)
129
- "/volume1/@appstore", "/volume1/@appdata", "/volume1/@database", "/volume1/@tmp", "/volume1/docker",
129
+ "/volume1/@appstore", "/volume1/@appdata", "/volume1/@database", "/volume1/@tmp", "/volume1/docker", // dsh-skip-sensitive
130
130
  ];
131
131
 
132
132
  /** 解析「目录数组」字符串(分隔符 : 或 ,)为绝对路径数组 */
@@ -261,10 +261,39 @@ function resolveTitle(cfg) {
261
261
  }
262
262
 
263
263
  // 浏览器不可见密码哈希
264
+ // 公开接口绝不下发的字段名(凭据类)。新增配置项若含凭据,必须补到这里。
265
+ // 为什么用键名集合而不是逐个 if:这类字段散落在嵌套对象里(如 autoUpdate.githubToken),
266
+ // 写死单点剥离下次加配置又会漏 —— 用键名规则一次性覆盖。
267
+ const SECRET_CONFIG_KEYS = new Set([
268
+ "githubtoken", "token", "accesstoken", "secret", "password", "passwordhash",
269
+ "passwordsalt", "credential", "credentials", "privatekey", "apikey",
270
+ ]);
271
+
272
+ // 递归剥离凭据类字段(深度上限防止异常嵌套)。
273
+ function stripSecretKeys(value, depth = 0) {
274
+ if (depth > 3 || value === null || typeof value !== "object") return value;
275
+ if (Array.isArray(value)) return value.map((item) => stripSecretKeys(item, depth + 1));
276
+ const cleaned = {};
277
+ for (const [key, item] of Object.entries(value)) {
278
+ if (SECRET_CONFIG_KEYS.has(String(key).toLowerCase())) continue;
279
+ cleaned[key] = stripSecretKeys(item, depth + 1);
280
+ }
281
+ return cleaned;
282
+ }
283
+
264
284
  function publicConfig(cfg) {
265
- const { passwordHash, passwordSalt, ...rest } = cfg;
285
+ // 2026-11-19:原实现用 `...rest` 把整个 cfg 透传出去,只显式剥了 passwordHash/passwordSalt,
286
+ // 结果嵌套在 autoUpdate 里的 githubToken(明文 GitHub PAT)被匿名接口 /api/lumigather 原样下发 ——
287
+ // 等于任何能访问该端口的人都能拿到仓库写权限。改为统一按键名递归剥离。
288
+ const sanitized = stripSecretKeys(cfg);
289
+ const { passwordHash, passwordSalt, autoUpdate, ...rest } = sanitized;
290
+ const rawAutoUpdate = (cfg && cfg.autoUpdate) || {};
266
291
  return {
267
292
  ...rest,
293
+ // 前端需要知道「有没有配 token」才能显示状态,但只回布尔,绝不回值
294
+ autoUpdate: autoUpdate && typeof autoUpdate === "object"
295
+ ? { ...autoUpdate, hasGithubToken: !!rawAutoUpdate.githubToken }
296
+ : autoUpdate,
268
297
  title: resolveTitle(cfg), // 旧默认值 "Gallery" 视为未设置(只在读取时折算,不落盘)
269
298
  fsRoot: resolveFsRoot(cfg), // 向后兼容:单根(取第一个根)
270
299
  fsRoots: resolveFsRoots(cfg), // 多根:全部允许的根(前端可用于目录树/限制提示)
package/server/lib/gif.js CHANGED
@@ -129,7 +129,9 @@ function gifFromDir(absDir, durMs, res) {
129
129
  } catch (_) {
130
130
  /* 缓存目录已存在或不可建:后续写入会报错并返回 500 */
131
131
  }
132
- const frameMs = Number.isFinite(durMs) && durMs > 0 ? durMs : Math.round(2000 / Math.max(1, det.frames.length));
132
+ // Math.max(1, …):帧数超过 4001 时 2000/N 四舍五入会得到 0,而 _gifArgs 用它算 1000/frameMs
133
+ // 作 framerate —— 除以 0 会让 ffmpeg 报错。与 gifFromZip 保持一致。
134
+ const frameMs = Number.isFinite(durMs) && durMs > 0 ? durMs : Math.max(1, Math.round(2000 / Math.max(1, det.frames.length)));
133
135
  let mtime = 0;
134
136
  try {
135
137
  mtime = fs.statSync(absDir).mtimeMs;
@@ -221,13 +223,18 @@ function gifFromZip(zipPath, durMs, res, opts) {
221
223
  imageName: det.frames[0] && det.frames[0].name,
222
224
  iniText,
223
225
  });
226
+ // 帧间隔优先级:显式传入 > ini 的 $speed 换算 > 按 2 秒均分。
227
+ //
228
+ // 最后这档兜底是本次补的:原来这里「两者都无 → 400 拒绝」,但 frameMs 是**内容属性**,
229
+ // 客户端无从得知、也不该由它决定 —— 逼客户端传参等于把服务端的规则搬过去(本项目明令禁止)。
230
+ // 而且目录级合成(gifFromDir)从一开始就是「2 秒均分」兜底,压缩包这边没理由不同:
231
+ // 同一件事两处两种做法,调用方只能猜该按哪边。
232
+ //
233
+ // Math.max(1, …) 不能省:帧数超过 4001 时 2000/N 四舍五入会得到 0,
234
+ // 而 _gifArgs 用它算 1000/frameMs 作 framerate —— 除以 0 会让 ffmpeg 直接报错。
224
235
  const frameMs = Number.isFinite(durMs) && durMs > 0
225
236
  ? durMs
226
- : (speed ? frameMsFromIniSpeed(speed, fps) : 0);
227
- if (!frameMs) {
228
- jsonRes(res, 400, { ok: false, error: "无帧间隔来源:包名未带 @NNms,且包内 ini 无 $speed" });
229
- return;
230
- }
237
+ : (speed ? frameMsFromIniSpeed(speed, fps) : Math.max(1, Math.round(2000 / Math.max(1, det.frames.length))));
231
238
  try {
232
239
  fs.mkdirSync(CACHE_GIF_DIR, { recursive: true });
233
240
  } catch (_) {
@@ -0,0 +1,284 @@
1
+ // 图片像素尺寸读取 —— 可切换实现的适配层。
2
+ //
3
+ // 业务代码只调 readDimensions(),不直接 require 任何第三方包。换实现只改这个文件。
4
+ //
5
+ // 为什么做成适配层而不是直接装库:本项目当前是零依赖(package.json 里没有 dependencies)。
6
+ // 内置实现已经覆盖这个项目会遇到的格式(png/jpeg/gif/webp),所以默认不需要装任何东西;
7
+ // 将来遇到内置实现不支持的格式(avif/heic 等),`npm i image-size` 就会自动切换过来,
8
+ // 业务代码一行都不用动。
9
+ //
10
+ // 只读文件头(几十 KB),不整文件读:7 万张图 × 整文件读会把首扫拖成分钟级。
11
+ // 任何失败都返回 null,不抛异常 —— 一张坏图不能让整个目录扫描失败。
12
+
13
+ const fs = require("fs");
14
+
15
+ // 单次最多读这么多字节找头部信息。JPEG 的 SOF 段通常在前几 KB,
16
+ // 128KB 对「带大 EXIF/ICC 的图」也够用;超出仍找不到就返回 null(不猜)。
17
+ const HEADER_READ_BYTES = 128 * 1024;
18
+
19
+ // 只有这些扩展名才尝试读尺寸;bmp 暂不支持(返回 null),但保留在名单里以免将来漏改
20
+ const IMAGE_EXTENSIONS = new Set([".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp"]);
21
+
22
+ const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
23
+ const GIF_SIGNATURES = ["GIF87a", "GIF89a"];
24
+ const RIFF_SIGNATURE = "RIFF";
25
+ const WEBP_SIGNATURE = "WEBP";
26
+
27
+ // JPEG 的 SOF 标记:0xFFC0..0xFFCF,但这三个不是 SOF(分别是 DHT/JPG/DAC)
28
+ const JPEG_SOF_RANGE_START = 0xc0;
29
+ const JPEG_SOF_RANGE_END = 0xcf;
30
+ const JPEG_NON_SOF_MARKERS = new Set([0xc4, 0xc8, 0xcc]);
31
+
32
+ // ---- 适配层:优先用 image-size(若已安装),否则用内置实现 ----
33
+
34
+ /**
35
+ * 尝试解析第三方 image-size 包;装了就返回一个 readDimensions 形态的函数,否则 null。
36
+ *
37
+ * 兼容两代 API:
38
+ * v1: module.exports = function(buffer) -> {width, height, type}
39
+ * v2: module.exports = { imageSize(buffer) }
40
+ * 库读的是 Buffer(这里自己读文件),避免不同版本的路径/异步签名差异。
41
+ */
42
+ function resolveLibraryBackend() {
43
+ let module;
44
+ try {
45
+ module = require("image-size");
46
+ } catch (_) {
47
+ return null; // 没装:这是正常情况,不是错误
48
+ }
49
+ const readFromBuffer =
50
+ typeof module === "function"
51
+ ? module
52
+ : module && typeof module.imageSize === "function"
53
+ ? module.imageSize
54
+ : module && typeof module.default === "function"
55
+ ? module.default
56
+ : null;
57
+ if (!readFromBuffer) return null;
58
+ return (filePath) => {
59
+ const result = readFromBuffer(fs.readFileSync(filePath));
60
+ if (!result || !result.width || !result.height) return null;
61
+ return { width: Number(result.width), height: Number(result.height) };
62
+ };
63
+ }
64
+
65
+ const libraryBackend = resolveLibraryBackend();
66
+
67
+ /** 当前实际使用的实现:便于日志与自检("image-size" / "builtin")。 */
68
+ const backendName = libraryBackend ? "image-size" : "builtin";
69
+
70
+ /**
71
+ * 读图片像素尺寸;读不出来返回 null。
72
+ *
73
+ * @param {string} filePath 绝对路径
74
+ * @returns {{width:number,height:number}|null}
75
+ */
76
+ function readDimensions(filePath) {
77
+ const extension = extensionOf(filePath);
78
+ if (!IMAGE_EXTENSIONS.has(extension)) return null;
79
+
80
+ if (libraryBackend) {
81
+ try {
82
+ return libraryBackend(filePath);
83
+ } catch (_) {
84
+ // 库也认不出的格式:退回内置实现试一次(内置只认四种,认不出就 null)
85
+ return readDimensionsBuiltin(filePath);
86
+ }
87
+ }
88
+ return readDimensionsBuiltin(filePath);
89
+ }
90
+
91
+ /** 内置实现:按格式读头。任何异常都吞掉返回 null。 */
92
+ function readDimensionsBuiltin(filePath) {
93
+ let handle = null;
94
+ try {
95
+ handle = fs.openSync(filePath, "r");
96
+ const buffer = Buffer.allocUnsafe(HEADER_READ_BYTES);
97
+ const bytesRead = fs.readSync(handle, buffer, 0, HEADER_READ_BYTES, 0);
98
+ const header = buffer.subarray(0, bytesRead);
99
+
100
+ if (looksLikePng(header)) return readPng(header);
101
+ if (looksLikeGif(header)) return readGif(header);
102
+ if (looksLikeJpeg(header)) return readJpeg(header);
103
+ if (looksLikeWebp(header)) return readWebp(header);
104
+ return null;
105
+ } catch (_) {
106
+ return null; // 无权限/文件消失/读失败:当"没有尺寸"
107
+ } finally {
108
+ if (handle !== null) {
109
+ try {
110
+ fs.closeSync(handle);
111
+ } catch (_) {
112
+ /* 关不掉也不影响结果 */
113
+ }
114
+ }
115
+ }
116
+ }
117
+
118
+ function extensionOf(filePath) {
119
+ const dot = filePath.lastIndexOf(".");
120
+ return dot < 0 ? "" : filePath.slice(dot).toLowerCase();
121
+ }
122
+
123
+ // ---- PNG ----
124
+ // 8 字节签名 + 4 字节长度 + "IHDR" + 宽(4) + 高(4),都是大端 uint32
125
+ function looksLikePng(header) {
126
+ return header.length >= 24 && header.subarray(0, 8).equals(PNG_SIGNATURE);
127
+ }
128
+
129
+ function readPng(header) {
130
+ if (header.toString("latin1", 12, 16) !== "IHDR") return null;
131
+ return { width: header.readUInt32BE(16), height: header.readUInt32BE(20) };
132
+ }
133
+
134
+ // ---- GIF ----
135
+ // 6 字节签名 + 逻辑屏幕宽(2, 小端) + 高(2, 小端)
136
+ function looksLikeGif(header) {
137
+ if (header.length < 10) return false;
138
+ const signature = header.toString("latin1", 0, 6);
139
+ return GIF_SIGNATURES.includes(signature);
140
+ }
141
+
142
+ function readGif(header) {
143
+ return { width: header.readUInt16LE(6), height: header.readUInt16LE(8) };
144
+ }
145
+
146
+ // ---- JPEG ----
147
+ // 从 0xFFD8 之后扫段标记,找 SOFn;段内:精度(1) + 高(2, 大端) + 宽(2, 大端)
148
+ function looksLikeJpeg(header) {
149
+ return header.length >= 4 && header[0] === 0xff && header[1] === 0xd8;
150
+ }
151
+
152
+ function readJpeg(header) {
153
+ let offset = 2; // 跳过 SOI
154
+ while (offset + 9 < header.length) {
155
+ if (header[offset] !== 0xff) {
156
+ offset += 1; // 段间可能有填充字节,逐字节找下一个标记
157
+ continue;
158
+ }
159
+ const marker = header[offset + 1];
160
+ // 0xFF 后面跟 0xFF 是填充,跳过
161
+ if (marker === 0xff) {
162
+ offset += 1;
163
+ continue;
164
+ }
165
+ // 无长度的独立标记:SOI/EOI/RSTn/TEM
166
+ if (marker === 0xd8 || marker === 0xd9 || marker === 0x01 || (marker >= 0xd0 && marker <= 0xd7)) {
167
+ offset += 2;
168
+ continue;
169
+ }
170
+ const segmentLength = header.readUInt16BE(offset + 2);
171
+ if (segmentLength < 2) return null; // 段长度非法,别再往下猜
172
+ const isSof = marker >= JPEG_SOF_RANGE_START && marker <= JPEG_SOF_RANGE_END && !JPEG_NON_SOF_MARKERS.has(marker);
173
+ if (isSof) {
174
+ if (offset + 9 > header.length) return null;
175
+ return {
176
+ height: header.readUInt16BE(offset + 5),
177
+ width: header.readUInt16BE(offset + 7),
178
+ };
179
+ }
180
+ offset += 2 + segmentLength;
181
+ }
182
+ return null;
183
+ }
184
+
185
+ // ---- WebP ----
186
+ // RIFF(4) + 文件长度(4) + "WEBP"(4) + 子块类型(4) + ...
187
+ function looksLikeWebp(header) {
188
+ return (
189
+ header.length >= 16 &&
190
+ header.toString("latin1", 0, 4) === RIFF_SIGNATURE &&
191
+ header.toString("latin1", 8, 12) === WEBP_SIGNATURE
192
+ );
193
+ }
194
+
195
+ function readWebp(header) {
196
+ const chunkType = header.toString("latin1", 12, 16);
197
+ if (chunkType === "VP8 ") return readWebpLossy(header);
198
+ if (chunkType === "VP8L") return readWebpLossless(header);
199
+ if (chunkType === "VP8X") return readWebpExtended(header);
200
+ return null;
201
+ }
202
+
203
+ // 有损:子块头 8 字节 + 帧头 3 字节(起始码 9d 01 2a) 之后,宽高各 14 位(小端)
204
+ function readWebpLossy(header) {
205
+ if (header.length < 30) return null;
206
+ if (header[23] !== 0x9d || header[24] !== 0x01 || header[25] !== 0x2a) return null;
207
+ return {
208
+ width: header.readUInt16LE(26) & 0x3fff,
209
+ height: header.readUInt16LE(28) & 0x3fff,
210
+ };
211
+ }
212
+
213
+ // 无损:子块头 8 字节 + 签名 0x2f,然后 14 位宽-1、14 位高-1(按位读)
214
+ function readWebpLossless(header) {
215
+ if (header.length < 25) return null;
216
+ if (header[20] !== 0x2f) return null;
217
+ const bits = header.readUInt32LE(21);
218
+ return {
219
+ width: (bits & 0x3fff) + 1,
220
+ height: ((bits >>> 14) & 0x3fff) + 1,
221
+ };
222
+ }
223
+
224
+ // 扩展:子块头 8 字节 + 标志(4) + 画布宽(3, 小端, 存的是宽-1) + 高(3, 小端, 存的是高-1)
225
+ function readWebpExtended(header) {
226
+ if (header.length < 30) return null;
227
+ const width = header.readUIntLE(24, 3) + 1;
228
+ const height = header.readUIntLE(27, 3) + 1;
229
+ return { width, height };
230
+ }
231
+
232
+ /**
233
+ * readDimensions 的**异步版本**(扫描路径专用)。
234
+ *
235
+ * 同步版每次 open/read 都是阻塞 syscall;一个大目录上千张图时,这些同步调用会把事件循环
236
+ * 切得连 /api/status 都要排队。异步版只把"读"换成 fs.promises,**解析逻辑完全复用**同步版的
237
+ * 那几个 readPng/readGif/readJpeg/readWebp —— 两条路径不会各自漂移。
238
+ *
239
+ * 装了第三方库时仍走同步库(它只接受 Buffer),这一点在零依赖场景下无影响。
240
+ */
241
+ async function readDimensionsAsync(filePath) {
242
+ const extension = extensionOf(filePath);
243
+ if (!IMAGE_EXTENSIONS.has(extension)) return null;
244
+
245
+ if (libraryBackend) {
246
+ try {
247
+ return libraryBackend(filePath);
248
+ } catch (_) {
249
+ return readDimensionsBuiltinAsync(filePath);
250
+ }
251
+ }
252
+ return readDimensionsBuiltinAsync(filePath);
253
+ }
254
+
255
+ async function readDimensionsBuiltinAsync(filePath) {
256
+ let handle = null;
257
+ try {
258
+ handle = await fs.promises.open(filePath, "r");
259
+ const buffer = Buffer.allocUnsafe(HEADER_READ_BYTES);
260
+ const { bytesRead } = await handle.read(buffer, 0, HEADER_READ_BYTES, 0);
261
+ const header = buffer.subarray(0, bytesRead);
262
+
263
+ // 解析分支与同步版逐字相同(同一组函数),只是取字节的方式不同
264
+ if (looksLikePng(header)) return readPng(header);
265
+ if (looksLikeGif(header)) return readGif(header);
266
+ if (looksLikeJpeg(header)) return readJpeg(header);
267
+ if (looksLikeWebp(header)) return readWebp(header);
268
+ return null;
269
+ } catch (_) {
270
+ return null;
271
+ } finally {
272
+ if (handle !== null) {
273
+ try { await handle.close(); } catch (_) { /* 关不掉也不影响结果 */ }
274
+ }
275
+ }
276
+ }
277
+
278
+ module.exports = {
279
+ readDimensions,
280
+ readDimensionsAsync,
281
+ backendName,
282
+ // 单测要单独验证内置实现(即使装了库也测它),所以也导出内部入口
283
+ readDimensionsBuiltin,
284
+ };