dsh-folia 4.0.6 → 4.0.8
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/lib/boot/index.js +42 -6
- package/lib/routes/boot.js +9 -3
- package/lib/routes/inventory.js +6 -3
- package/lib/routes/modules.js +6 -1
- package/lib/routes/site-thumb.js +4 -0
- package/package.json +1 -1
package/lib/boot/index.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* 3. **任何异常都必须放行**。注入的脚本顶层 try/catch,超时自毁,绝不把用户挡在门外。
|
|
10
10
|
*/
|
|
11
11
|
import { createHash } from 'node:crypto';
|
|
12
|
-
import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
12
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
13
13
|
import { homedir } from 'node:os';
|
|
14
14
|
import { extname, join } from 'node:path';
|
|
15
15
|
|
|
@@ -96,8 +96,42 @@ export function clipMetaFile(id) {
|
|
|
96
96
|
if (!String(id || '')) return null;
|
|
97
97
|
return join(bootDir(), clipStem(id) + '.json');
|
|
98
98
|
}
|
|
99
|
+
/** 旧命名规则下的路径:`<id>.mp4`(id 当年被限制成 ASCII 安全字符,所以能直接用)。 */
|
|
100
|
+
function legacyClipFile(id) {
|
|
101
|
+
const raw = String(id || '');
|
|
102
|
+
if (!/^[a-z0-9][a-z0-9._-]*$/i.test(raw)) return null;
|
|
103
|
+
return join(bootDir(), raw + '.mp4');
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 找出这个 id 实际对应的片段文件。
|
|
108
|
+
*
|
|
109
|
+
* 为什么需要"找"而不是直接算:4.0.2 把磁盘命名从 `<id>.mp4` 改成了
|
|
110
|
+
* `<id>-<hash>.mp4`(为了支持中文 slug),于是**升级前下载的片段**还叫旧名字。
|
|
111
|
+
* 列表是按文件名枚举的,所以旧文件看得见;可点「使用」时按新名字找不到 ⇒ `not-installed`。
|
|
112
|
+
* 这里按"新名字 → 旧名字"的顺序找,找到旧名字就**顺手迁移**成新名字(幂等)。
|
|
113
|
+
*/
|
|
114
|
+
export function resolveClipFile(id) {
|
|
115
|
+
const next = clipFile(id);
|
|
116
|
+
if (next && existsSync(next) && statSync(next).size > 0) return next;
|
|
117
|
+
const legacy = legacyClipFile(id);
|
|
118
|
+
if (legacy && existsSync(legacy) && statSync(legacy).size > 0) {
|
|
119
|
+
try {
|
|
120
|
+
if (next && !existsSync(next)) writeFileSync(next, readFileSync(legacy));
|
|
121
|
+
try { renameSync(legacy, next); } catch { /* 跨盘/占用:复制已经够用,旧文件留着无害 */ }
|
|
122
|
+
try { rmSync(legacy); } catch { /* ignore */ }
|
|
123
|
+
const metaPath = clipMetaFile(id);
|
|
124
|
+
if (metaPath && !existsSync(metaPath)) {
|
|
125
|
+
writeFileSync(metaPath, JSON.stringify({ id, migratedFrom: 'legacy' }, null, 2) + '\n');
|
|
126
|
+
}
|
|
127
|
+
} catch { /* 迁移失败也不影响这次使用 */ }
|
|
128
|
+
return next || legacy;
|
|
129
|
+
}
|
|
130
|
+
return next;
|
|
131
|
+
}
|
|
132
|
+
|
|
99
133
|
export function clipExists(id) {
|
|
100
|
-
const p =
|
|
134
|
+
const p = resolveClipFile(id);
|
|
101
135
|
return Boolean(p && existsSync(p) && statSync(p).size > 0);
|
|
102
136
|
}
|
|
103
137
|
|
|
@@ -128,11 +162,13 @@ export function listLocalClips() {
|
|
|
128
162
|
}
|
|
129
163
|
|
|
130
164
|
export function removeClip(id) {
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
|
|
165
|
+
const paths = [clipFile(id), legacyClipFile(id)].filter(Boolean);
|
|
166
|
+
let removed = false;
|
|
167
|
+
for (const p of paths) {
|
|
168
|
+
try { if (existsSync(p)) { rmSync(p); removed = true; } } catch { /* 继续尝试下一个 */ }
|
|
169
|
+
}
|
|
134
170
|
try { const m = clipMetaFile(id); if (m && existsSync(m)) rmSync(m); } catch { /* ignore */ }
|
|
135
|
-
return true;
|
|
171
|
+
return removed || true;
|
|
136
172
|
}
|
|
137
173
|
|
|
138
174
|
/* ── 注入的启动屏幕脚本 ─────────────────────────────────────────────────────
|
package/lib/routes/boot.js
CHANGED
|
@@ -14,7 +14,7 @@ import { join } from 'node:path';
|
|
|
14
14
|
import { sendJson } from '../json-response.js';
|
|
15
15
|
import { bodyReader } from '../http-body.js';
|
|
16
16
|
import { resolveModules } from './modules.js';
|
|
17
|
-
import { clipExists, clipFile, clipMetaFile, listLocalClips, readBoot, removeClip, writeBoot } from '../boot/index.js';
|
|
17
|
+
import { clipExists, clipFile, clipMetaFile, listLocalClips, readBoot, removeClip, resolveClipFile, writeBoot } from '../boot/index.js';
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* 站点基址。开机动画的候选**从站点分类取**(`?category=boot-animation`),
|
|
@@ -122,7 +122,7 @@ async function fetchClipBytes(url, chunks = 4) {
|
|
|
122
122
|
* MP4/MOV ISO BMFF —— 偏移 4..8 是 `ftyp`
|
|
123
123
|
* WebM EBML —— 开头 4 字节是 1A 45 DF A3
|
|
124
124
|
*/
|
|
125
|
-
|
|
125
|
+
function sniffVideo(buf) {
|
|
126
126
|
if (!buf || buf.length < 1024) return null;
|
|
127
127
|
if (buf.length >= 12 && buf.toString('latin1', 4, 8) === 'ftyp') {
|
|
128
128
|
const brand = buf.toString('latin1', 8, 12);
|
|
@@ -134,6 +134,7 @@ export function sniffVideo(buf) {
|
|
|
134
134
|
return null;
|
|
135
135
|
}
|
|
136
136
|
|
|
137
|
+
export { sniffVideo };
|
|
137
138
|
/** 读请求体里的 `{ id }`。 */
|
|
138
139
|
async function readIdBody(req) {
|
|
139
140
|
try {
|
|
@@ -237,6 +238,10 @@ async function fetchCandidatesUncached() {
|
|
|
237
238
|
}
|
|
238
239
|
}
|
|
239
240
|
|
|
241
|
+
/** 注入契约(ADR-0013 D12):本族从 `c` 消费的**必需**键。
|
|
242
|
+
* 判据 test/verify-inject-contracts.mjs 会把它与调用点逐键对账 —— 少一个就红,
|
|
243
|
+
* 所以新增依赖(哪怕只是 `log`)也必须同步写在这里。 */
|
|
244
|
+
export const DEPS = ['disposers','base','log'];
|
|
240
245
|
export function registerBootRoutes(webServer, c) {
|
|
241
246
|
const { disposers, base: BASE, log } = c;
|
|
242
247
|
|
|
@@ -397,7 +402,8 @@ export function registerBootRoutes(webServer, c) {
|
|
|
397
402
|
handler: async (req, res) => {
|
|
398
403
|
const boot = readBoot();
|
|
399
404
|
const id = boot.clipId;
|
|
400
|
-
|
|
405
|
+
// 旧命名的片段(升级前下载的)也要能播 —— 解析器会顺手迁移成新名字。
|
|
406
|
+
const file = resolveClipFile(id);
|
|
401
407
|
if (!file || !existsSync(file)) { res.statusCode = 404; res.end('no clip'); return; }
|
|
402
408
|
const size = statSync(file).size;
|
|
403
409
|
const range = String((req.headers && req.headers.range) || '');
|
package/lib/routes/inventory.js
CHANGED
|
@@ -232,8 +232,10 @@ function foliaTypeOfEntry(it) {
|
|
|
232
232
|
|
|
233
233
|
/** 条目的分类 slug(没有就返回空串)。 */
|
|
234
234
|
function foliaCategoryOf(it) {
|
|
235
|
-
|
|
236
|
-
|
|
235
|
+
// ⚠️ 形参**不要**叫 c:test/verify-inject-contracts.mjs 会把 c.<键> 读成"从注入面消费了
|
|
236
|
+
// 该键",这里一个分类对象曾经让对账表凭空多出 slug。名字取得像注入面就会被当成注入面。
|
|
237
|
+
const category = it && it.category;
|
|
238
|
+
return String((category && category.slug) || '').trim().toLowerCase();
|
|
237
239
|
}
|
|
238
240
|
|
|
239
241
|
/**
|
|
@@ -465,10 +467,11 @@ async function foliaRefresh(force = false) {
|
|
|
465
467
|
|
|
466
468
|
// 启动预热:插件一装好就在后台抓一次站点清单,这样**重启后的第一次打开面板**也不冷。
|
|
467
469
|
// 有意不 await、失败静默 —— 预热只是优化,绝不能影响启动。
|
|
468
|
-
|
|
470
|
+
function warmInventorySiteCache() {
|
|
469
471
|
try { foliaRemote(true).catch(() => {}); } catch { /* ignore */ }
|
|
470
472
|
}
|
|
471
473
|
|
|
474
|
+
export { warmInventorySiteCache };
|
|
472
475
|
export function registerInventoryRoutes(webServer, c) {
|
|
473
476
|
const { disposers, base: BASE, mediaOriginApi, buildInventory, componentsProbe } = c;
|
|
474
477
|
|
package/lib/routes/modules.js
CHANGED
|
@@ -170,10 +170,15 @@ export async function resolveModules(force = false) {
|
|
|
170
170
|
|
|
171
171
|
// 启动预热:模块索引很小(~1.6KB),但第一跳要走站点代理;提前拉一次,
|
|
172
172
|
// 开机动画页签第一次打开就不用等这一跳。
|
|
173
|
-
|
|
173
|
+
function warmModulesCache() {
|
|
174
174
|
try { resolveModules(true).catch(() => {}); } catch { /* ignore */ }
|
|
175
175
|
}
|
|
176
176
|
|
|
177
|
+
/** 注入契约(ADR-0013 D12):本族从 `c` 消费的**必需**键。
|
|
178
|
+
* 判据 test/verify-inject-contracts.mjs 会把它与调用点逐键对账 —— 少一个就红,
|
|
179
|
+
* 所以新增依赖(哪怕只是 `log`)也必须同步写在这里。 */
|
|
180
|
+
export const DEPS = ['disposers','base'];
|
|
181
|
+
export { warmModulesCache };
|
|
177
182
|
export function registerModulesRoutes(webServer, c) {
|
|
178
183
|
const { disposers, base: BASE } = c;
|
|
179
184
|
|
package/lib/routes/site-thumb.js
CHANGED
|
@@ -55,6 +55,10 @@ function allowed(url) {
|
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
+
/** 注入契约(ADR-0013 D12):本族从 `c` 消费的**必需**键。
|
|
59
|
+
* 判据 test/verify-inject-contracts.mjs 会把它与调用点逐键对账 —— 少一个就红,
|
|
60
|
+
* 所以新增依赖(哪怕只是 `log`)也必须同步写在这里。 */
|
|
61
|
+
export const DEPS = ['disposers','base','ensureImagePreview','log'];
|
|
58
62
|
export function registerSiteThumbRoutes(webServer, c) {
|
|
59
63
|
const { disposers, base: BASE, ensureImagePreview, log } = c;
|
|
60
64
|
|