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/CHANGELOG.md +233 -0
- package/README.md +43 -10
- package/client.js +179 -52
- package/host/classify.js +70 -0
- package/host/constants.js +133 -0
- package/host/fsutil.js +136 -0
- package/host/groups.js +92 -0
- package/host/http.js +785 -0
- package/host/media.js +122 -0
- package/host/protocol.js +25 -0
- package/host/scan.js +531 -0
- package/host/session-index.js +187 -0
- package/host/session.js +192 -0
- package/host/settings.js +232 -0
- package/index.js +23 -1813
- package/package.json +8 -2
- package/session-index.js +7 -187
package/host/http.js
ADDED
|
@@ -0,0 +1,785 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP 路由:媒体直链(webServer + 206 Range)与 op 路由(connection.fetch)。
|
|
3
|
+
*
|
|
4
|
+
* S1 拆分自 index.js 的 apply()(行为零变化)。
|
|
5
|
+
*
|
|
6
|
+
* 两条路由的差别(这决定 P0 级 bug):
|
|
7
|
+
* · 媒体路由走 ctx.webServer.register —— Node 原生 req/res,能返回 206
|
|
8
|
+
* · op 路由走 ctx.connection.fetch —— Fetch API,走 /api 网关(需认证)
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { promises as fsp } from 'node:fs'
|
|
12
|
+
import path from 'node:path'
|
|
13
|
+
|
|
14
|
+
import { ROUTE, MEDIA_ROUTE } from './constants.js'
|
|
15
|
+
import { isInside } from './fsutil.js'
|
|
16
|
+
import {
|
|
17
|
+
resolveOptions, readSettingsFile, readHiddenList,
|
|
18
|
+
validateRootCandidate, readSettingsPayload,
|
|
19
|
+
} from './settings.js'
|
|
20
|
+
import { scanGallery } from './scan.js'
|
|
21
|
+
import { resolveReadable, serveMedia, contentTypeFor } from './media.js'
|
|
22
|
+
|
|
23
|
+
/** 统一 JSON 响应。 */
|
|
24
|
+
function json(body, status) {
|
|
25
|
+
|
|
26
|
+
return new Response(JSON.stringify(body), {
|
|
27
|
+
|
|
28
|
+
status: status ?? 200,
|
|
29
|
+
|
|
30
|
+
headers: { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' },
|
|
31
|
+
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 注册媒体直链路由(Node 原生 req/res,支持 HTTP Range / 206)。
|
|
39
|
+
*/
|
|
40
|
+
export function registerMediaRoute(ctx, config) {
|
|
41
|
+
ctx.effect(
|
|
42
|
+
|
|
43
|
+
() => ctx.webServer.register({
|
|
44
|
+
|
|
45
|
+
kind: 'prefix',
|
|
46
|
+
|
|
47
|
+
path: MEDIA_ROUTE,
|
|
48
|
+
|
|
49
|
+
handler: async (req, res) => {
|
|
50
|
+
|
|
51
|
+
// ---- 防护 3:只读 ----
|
|
52
|
+
|
|
53
|
+
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
|
54
|
+
|
|
55
|
+
res.writeHead(405, { 'content-type': 'text/plain; charset=utf-8' })
|
|
56
|
+
|
|
57
|
+
res.end('method not allowed')
|
|
58
|
+
|
|
59
|
+
return
|
|
60
|
+
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// ---- 防护 2:拒绝跨站 ----
|
|
64
|
+
|
|
65
|
+
const site = req.headers['sec-fetch-site']
|
|
66
|
+
|
|
67
|
+
if (typeof site === 'string' && site === 'cross-site') {
|
|
68
|
+
|
|
69
|
+
res.writeHead(403, { 'content-type': 'text/plain; charset=utf-8' })
|
|
70
|
+
|
|
71
|
+
res.end('cross-site request refused')
|
|
72
|
+
|
|
73
|
+
return
|
|
74
|
+
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const origin = req.headers.origin
|
|
78
|
+
|
|
79
|
+
if (typeof origin === 'string' && origin !== '') {
|
|
80
|
+
|
|
81
|
+
let originHost = ''
|
|
82
|
+
|
|
83
|
+
try { originHost = new URL(origin).host } catch { originHost = '' }
|
|
84
|
+
|
|
85
|
+
const host = req.headers.host
|
|
86
|
+
|
|
87
|
+
if (originHost !== '' && typeof host === 'string' && originHost !== host) {
|
|
88
|
+
|
|
89
|
+
res.writeHead(403, { 'content-type': 'text/plain; charset=utf-8' })
|
|
90
|
+
|
|
91
|
+
res.end('origin mismatch')
|
|
92
|
+
|
|
93
|
+
return
|
|
94
|
+
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
try {
|
|
102
|
+
|
|
103
|
+
const url = new URL(req.url ?? '/', 'http://dsh.internal')
|
|
104
|
+
|
|
105
|
+
const relPath = url.searchParams.get('path') ?? ''
|
|
106
|
+
|
|
107
|
+
if (relPath === '') {
|
|
108
|
+
|
|
109
|
+
res.writeHead(400, { 'content-type': 'text/plain; charset=utf-8' })
|
|
110
|
+
|
|
111
|
+
res.end('missing path')
|
|
112
|
+
|
|
113
|
+
return
|
|
114
|
+
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const resolved = resolveOptions(config)
|
|
118
|
+
|
|
119
|
+
// ---- 防护 1:路径白名单 ----
|
|
120
|
+
|
|
121
|
+
const found = await resolveReadable(resolved.roots, relPath, resolved, resolved.maxMediaBytes)
|
|
122
|
+
|
|
123
|
+
if (found === null) {
|
|
124
|
+
|
|
125
|
+
res.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' })
|
|
126
|
+
|
|
127
|
+
res.end('not found')
|
|
128
|
+
|
|
129
|
+
return
|
|
130
|
+
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
let stat
|
|
134
|
+
|
|
135
|
+
try {
|
|
136
|
+
|
|
137
|
+
stat = await fsp.stat(found.absolute)
|
|
138
|
+
|
|
139
|
+
} catch (error) {
|
|
140
|
+
|
|
141
|
+
res.writeHead(500, { 'content-type': 'text/plain; charset=utf-8' })
|
|
142
|
+
|
|
143
|
+
res.end(String(error?.message ?? error))
|
|
144
|
+
|
|
145
|
+
return
|
|
146
|
+
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const type = contentTypeFor(path.extname(found.absolute).toLowerCase())
|
|
150
|
+
|
|
151
|
+
serveMedia(req, res, found.absolute, stat.size, type)
|
|
152
|
+
|
|
153
|
+
} catch (error) {
|
|
154
|
+
|
|
155
|
+
if (!res.headersSent) {
|
|
156
|
+
|
|
157
|
+
res.writeHead(500, { 'content-type': 'text/plain; charset=utf-8' })
|
|
158
|
+
|
|
159
|
+
res.end(String(error?.message ?? error))
|
|
160
|
+
|
|
161
|
+
} else {
|
|
162
|
+
|
|
163
|
+
res.destroy()
|
|
164
|
+
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
},
|
|
170
|
+
|
|
171
|
+
}),
|
|
172
|
+
|
|
173
|
+
'dsh-gallery: media range route',
|
|
174
|
+
|
|
175
|
+
)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* 注册 op 路由(POST + op 区分动作,走 /api 网关)。
|
|
180
|
+
*/
|
|
181
|
+
export function registerOpsRoute(ctx, config) {
|
|
182
|
+
ctx.effect(
|
|
183
|
+
|
|
184
|
+
() =>
|
|
185
|
+
|
|
186
|
+
ctx.connection.fetch.register({
|
|
187
|
+
|
|
188
|
+
path: ROUTE,
|
|
189
|
+
|
|
190
|
+
methods: ['POST'],
|
|
191
|
+
|
|
192
|
+
requestBody: 'buffered',
|
|
193
|
+
|
|
194
|
+
fetch: async (request) => {
|
|
195
|
+
|
|
196
|
+
let body
|
|
197
|
+
|
|
198
|
+
try {
|
|
199
|
+
|
|
200
|
+
body = await request.json()
|
|
201
|
+
|
|
202
|
+
} catch {
|
|
203
|
+
|
|
204
|
+
body = {}
|
|
205
|
+
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const op = typeof body?.op === 'string' ? body.op : ''
|
|
209
|
+
|
|
210
|
+
// 每次请求都重新解析配置:这样用户在面板里改 root 后无需重启即生效
|
|
211
|
+
|
|
212
|
+
const resolved = resolveOptions(config)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
if (op === 'list') {
|
|
217
|
+
|
|
218
|
+
try {
|
|
219
|
+
|
|
220
|
+
// 三种画廊:图片 / 文件 / 音视频(v0.10.1)
|
|
221
|
+
|
|
222
|
+
const kind = body?.kind === 'file' ? 'file'
|
|
223
|
+
|
|
224
|
+
: body?.kind === 'media' ? 'media'
|
|
225
|
+
|
|
226
|
+
: 'image'
|
|
227
|
+
|
|
228
|
+
const scope = body?.scope === 'session' ? 'session' : 'global'
|
|
229
|
+
|
|
230
|
+
const sessionId = typeof body?.sessionId === 'string' ? body.sessionId : ''
|
|
231
|
+
|
|
232
|
+
return json(await scanGallery(config, kind, scope, sessionId, body?.page))
|
|
233
|
+
|
|
234
|
+
} catch (error) {
|
|
235
|
+
|
|
236
|
+
return json({ ok: false, error: { code: 'scan-failed', message: String(error?.message ?? error) } }, 500)
|
|
237
|
+
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
// 读取当前设置(含 root 来源、是否存在)
|
|
245
|
+
|
|
246
|
+
if (op === 'settings') {
|
|
247
|
+
|
|
248
|
+
try {
|
|
249
|
+
|
|
250
|
+
return json({ ok: true, settings: await readSettingsPayload(config) })
|
|
251
|
+
|
|
252
|
+
} catch (error) {
|
|
253
|
+
|
|
254
|
+
return json({ ok: false, error: { code: 'settings-failed', message: String(error?.message ?? error) } }, 500)
|
|
255
|
+
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
// 修改产出根目录(单值,保留兼容):会覆盖 roots,只留这一个
|
|
263
|
+
|
|
264
|
+
if (op === 'setRoot') {
|
|
265
|
+
|
|
266
|
+
const candidate = body?.root
|
|
267
|
+
|
|
268
|
+
const check = await validateRootCandidate(candidate)
|
|
269
|
+
|
|
270
|
+
if (check.ok !== true) {
|
|
271
|
+
|
|
272
|
+
const messages = {
|
|
273
|
+
|
|
274
|
+
empty: '目录不能为空',
|
|
275
|
+
|
|
276
|
+
'not-absolute': '必须是绝对路径,例如 /Users/you/outputs 或 D:\\产出',
|
|
277
|
+
|
|
278
|
+
'not-directory': '该路径不是目录',
|
|
279
|
+
|
|
280
|
+
'not-found': '目录不存在',
|
|
281
|
+
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return json({
|
|
285
|
+
|
|
286
|
+
ok: false,
|
|
287
|
+
|
|
288
|
+
error: { code: check.reason, message: messages[check.reason] ?? '目录无效' },
|
|
289
|
+
|
|
290
|
+
}, 400)
|
|
291
|
+
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
try {
|
|
295
|
+
|
|
296
|
+
const opts = resolveOptions(config)
|
|
297
|
+
|
|
298
|
+
// 写 roots 数组,并把旧的单值 root 清掉,避免重复
|
|
299
|
+
|
|
300
|
+
const current = readSettingsFile(opts.settingsFile)
|
|
301
|
+
|
|
302
|
+
delete current.root
|
|
303
|
+
|
|
304
|
+
await fsp.mkdir(path.dirname(opts.settingsFile), { recursive: true })
|
|
305
|
+
|
|
306
|
+
await fsp.writeFile(
|
|
307
|
+
|
|
308
|
+
opts.settingsFile,
|
|
309
|
+
|
|
310
|
+
JSON.stringify({ ...current, roots: [check.root] }, null, 2),
|
|
311
|
+
|
|
312
|
+
'utf8',
|
|
313
|
+
|
|
314
|
+
)
|
|
315
|
+
|
|
316
|
+
return json({ ok: true, settings: await readSettingsPayload(config) })
|
|
317
|
+
|
|
318
|
+
} catch (error) {
|
|
319
|
+
|
|
320
|
+
return json({ ok: false, error: { code: 'write-failed', message: String(error?.message ?? error) } }, 500)
|
|
321
|
+
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
// 追加一个扫描目录(多目录支持)
|
|
329
|
+
|
|
330
|
+
if (op === 'addRoot') {
|
|
331
|
+
|
|
332
|
+
const check = await validateRootCandidate(body?.root)
|
|
333
|
+
|
|
334
|
+
if (check.ok !== true) {
|
|
335
|
+
|
|
336
|
+
const messages = {
|
|
337
|
+
|
|
338
|
+
empty: '目录不能为空',
|
|
339
|
+
|
|
340
|
+
'not-absolute': '必须是绝对路径',
|
|
341
|
+
|
|
342
|
+
'not-directory': '该路径不是目录',
|
|
343
|
+
|
|
344
|
+
'not-found': '目录不存在',
|
|
345
|
+
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
return json({ ok: false, error: { code: check.reason, message: messages[check.reason] ?? '目录无效' } }, 400)
|
|
349
|
+
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
try {
|
|
353
|
+
|
|
354
|
+
const opts = resolveOptions(config)
|
|
355
|
+
|
|
356
|
+
const current = readSettingsFile(opts.settingsFile)
|
|
357
|
+
|
|
358
|
+
const list = Array.isArray(current.roots) && current.roots.length > 0
|
|
359
|
+
|
|
360
|
+
? current.roots.slice()
|
|
361
|
+
|
|
362
|
+
: opts.roots.slice()
|
|
363
|
+
|
|
364
|
+
// 去重(大小写不敏感)
|
|
365
|
+
|
|
366
|
+
if (!list.some((r) => path.resolve(r).toLowerCase() === check.root.toLowerCase())) {
|
|
367
|
+
|
|
368
|
+
list.push(check.root)
|
|
369
|
+
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
delete current.root
|
|
373
|
+
|
|
374
|
+
await fsp.mkdir(path.dirname(opts.settingsFile), { recursive: true })
|
|
375
|
+
|
|
376
|
+
await fsp.writeFile(opts.settingsFile, JSON.stringify({ ...current, roots: list }, null, 2), 'utf8')
|
|
377
|
+
|
|
378
|
+
return json({ ok: true, settings: await readSettingsPayload(config) })
|
|
379
|
+
|
|
380
|
+
} catch (error) {
|
|
381
|
+
|
|
382
|
+
return json({ ok: false, error: { code: 'write-failed', message: String(error?.message ?? error) } }, 500)
|
|
383
|
+
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
// 移除一个扫描目录
|
|
391
|
+
|
|
392
|
+
if (op === 'removeRoot') {
|
|
393
|
+
|
|
394
|
+
const target = typeof body?.root === 'string' ? body.root.trim() : ''
|
|
395
|
+
|
|
396
|
+
if (target === '') {
|
|
397
|
+
|
|
398
|
+
return json({ ok: false, error: { code: 'empty', message: '缺少要移除的目录' } }, 400)
|
|
399
|
+
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
try {
|
|
403
|
+
|
|
404
|
+
const opts = resolveOptions(config)
|
|
405
|
+
|
|
406
|
+
const current = readSettingsFile(opts.settingsFile)
|
|
407
|
+
|
|
408
|
+
const list = (Array.isArray(current.roots) && current.roots.length > 0
|
|
409
|
+
|
|
410
|
+
? current.roots.slice()
|
|
411
|
+
|
|
412
|
+
: opts.roots.slice()
|
|
413
|
+
|
|
414
|
+
).filter((r) => path.resolve(r).toLowerCase() !== path.resolve(target).toLowerCase())
|
|
415
|
+
|
|
416
|
+
if (list.length === 0) {
|
|
417
|
+
|
|
418
|
+
return json({ ok: false, error: { code: 'last-root', message: '至少要保留一个扫描目录' } }, 400)
|
|
419
|
+
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
delete current.root
|
|
423
|
+
|
|
424
|
+
await fsp.mkdir(path.dirname(opts.settingsFile), { recursive: true })
|
|
425
|
+
|
|
426
|
+
await fsp.writeFile(opts.settingsFile, JSON.stringify({ ...current, roots: list }, null, 2), 'utf8')
|
|
427
|
+
|
|
428
|
+
return json({ ok: true, settings: await readSettingsPayload(config) })
|
|
429
|
+
|
|
430
|
+
} catch (error) {
|
|
431
|
+
|
|
432
|
+
return json({ ok: false, error: { code: 'write-failed', message: String(error?.message ?? error) } }, 500)
|
|
433
|
+
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
if (op === 'raw') {
|
|
441
|
+
|
|
442
|
+
// raw 要给播放器取整个文件,必须用媒体上限(默认 200MB)。
|
|
443
|
+
// 不传 byteCap 会回退到 maxImageBytes(64MB)—— 127MB 视频会被判「不可读」→ 404。
|
|
444
|
+
const found = await resolveReadable(resolved.roots, body?.path, resolved, resolved.maxMediaBytes)
|
|
445
|
+
|
|
446
|
+
if (found === null) return json({ ok: false, error: { code: 'not-readable', message: '文件不存在或不可读' } }, 404)
|
|
447
|
+
|
|
448
|
+
let bytes
|
|
449
|
+
|
|
450
|
+
try {
|
|
451
|
+
|
|
452
|
+
bytes = await fsp.readFile(found.absolute)
|
|
453
|
+
|
|
454
|
+
} catch (error) {
|
|
455
|
+
|
|
456
|
+
return json({ ok: false, error: { code: 'read-failed', message: String(error?.message ?? error) } }, 500)
|
|
457
|
+
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
return new Response(bytes, {
|
|
461
|
+
|
|
462
|
+
status: 200,
|
|
463
|
+
|
|
464
|
+
headers: {
|
|
465
|
+
|
|
466
|
+
'content-type': contentTypeFor(path.extname(found.absolute).toLowerCase()),
|
|
467
|
+
|
|
468
|
+
'content-length': String(bytes.byteLength),
|
|
469
|
+
|
|
470
|
+
'cache-control': 'no-store',
|
|
471
|
+
|
|
472
|
+
},
|
|
473
|
+
|
|
474
|
+
})
|
|
475
|
+
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
// 在文件管理器中定位该文件(仅 Windows)
|
|
481
|
+
|
|
482
|
+
if (op === 'reveal') {
|
|
483
|
+
|
|
484
|
+
// reveal 要能定位大文件,所以用媒体上限(这里取两者较大值)
|
|
485
|
+
|
|
486
|
+
const capForReveal = Math.max(resolved.maxImageBytes, resolved.maxMediaBytes)
|
|
487
|
+
|
|
488
|
+
const found = await resolveReadable(resolved.roots, body?.path, resolved, capForReveal)
|
|
489
|
+
|
|
490
|
+
if (found === null) {
|
|
491
|
+
|
|
492
|
+
return json({ ok: false, error: { code: 'not-readable', message: '文件不存在或不可读' } }, 404)
|
|
493
|
+
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// mode: 'select' 在文件夹里选中文件;'play' 尝试用默认程序打开(对媒体更直接)
|
|
497
|
+
|
|
498
|
+
const mode = body?.mode === 'play' ? 'play' : 'select'
|
|
499
|
+
|
|
500
|
+
try {
|
|
501
|
+
|
|
502
|
+
const { spawn } = await import('node:child_process')
|
|
503
|
+
|
|
504
|
+
let child
|
|
505
|
+
|
|
506
|
+
if (process.platform === 'win32') {
|
|
507
|
+
|
|
508
|
+
if (mode === 'play') {
|
|
509
|
+
|
|
510
|
+
// cmd start:让 shell 按文件关联打开。空字符串是窗口标题占位,
|
|
511
|
+
|
|
512
|
+
// 不占位的话中文路径会被当作窗口标题。
|
|
513
|
+
|
|
514
|
+
child = spawn('cmd.exe', ['/c', 'start', '', found.absolute], {
|
|
515
|
+
|
|
516
|
+
detached: true, stdio: 'ignore', windowsHide: true,
|
|
517
|
+
|
|
518
|
+
})
|
|
519
|
+
|
|
520
|
+
} else {
|
|
521
|
+
|
|
522
|
+
// explorer /select 在直接 spawn 时对含中文的路径会以退出码 1 失败(已实测)。
|
|
523
|
+
|
|
524
|
+
// 改走 cmd /c start "" explorer.exe /select,"<path>":让 cmd 负责引号与编码,
|
|
525
|
+
|
|
526
|
+
// 这是 Windows 上打开并选中中文路径文件的稳定写法。
|
|
527
|
+
|
|
528
|
+
child = spawn('cmd.exe', [
|
|
529
|
+
|
|
530
|
+
'/c', 'start', '',
|
|
531
|
+
|
|
532
|
+
'explorer.exe', `/select,"${found.absolute}"`,
|
|
533
|
+
|
|
534
|
+
], {
|
|
535
|
+
|
|
536
|
+
detached: true, stdio: 'ignore', windowsHide: true,
|
|
537
|
+
|
|
538
|
+
})
|
|
539
|
+
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
} else {
|
|
543
|
+
|
|
544
|
+
child = spawn('xdg-open', [mode === 'play' ? found.absolute : path.dirname(found.absolute)], {
|
|
545
|
+
|
|
546
|
+
detached: true, stdio: 'ignore',
|
|
547
|
+
|
|
548
|
+
})
|
|
549
|
+
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
// 不要 unref 立即丢:先观察 800ms 内的错误与退出码,异常时报给前端
|
|
553
|
+
|
|
554
|
+
const outcome = await new Promise((resolve) => {
|
|
555
|
+
|
|
556
|
+
let settled = false
|
|
557
|
+
|
|
558
|
+
const done = (payload) => {
|
|
559
|
+
|
|
560
|
+
if (settled) return
|
|
561
|
+
|
|
562
|
+
settled = true
|
|
563
|
+
|
|
564
|
+
resolve(payload)
|
|
565
|
+
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
child.on('error', (error) => done({
|
|
569
|
+
|
|
570
|
+
ok: false, code: 'spawn-error', message: String(error?.message ?? error),
|
|
571
|
+
|
|
572
|
+
}))
|
|
573
|
+
|
|
574
|
+
child.on('exit', (code, signal) => {
|
|
575
|
+
|
|
576
|
+
// explorer 成功时通常很快退出(它把活交给已有 explorer 进程)
|
|
577
|
+
|
|
578
|
+
if (mode === 'play') {
|
|
579
|
+
|
|
580
|
+
// cmd start 退出码 0 表示已交给关联程序
|
|
581
|
+
|
|
582
|
+
done(code === 0 || code === null
|
|
583
|
+
|
|
584
|
+
? { ok: true, exitCode: code, signal }
|
|
585
|
+
|
|
586
|
+
: { ok: false, code: 'exit-nonzero', message: `exit ${code}`, exitCode: code })
|
|
587
|
+
|
|
588
|
+
} else {
|
|
589
|
+
|
|
590
|
+
// 现在 select 也走 cmd start,退出码 0 表示命令已被接受
|
|
591
|
+
|
|
592
|
+
done(code === 0 || code === null
|
|
593
|
+
|
|
594
|
+
? { ok: true, exitCode: code, signal }
|
|
595
|
+
|
|
596
|
+
: { ok: false, code: 'select-exit-nonzero', message: `cmd start 退出码 ${code}`, exitCode: code })
|
|
597
|
+
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
})
|
|
601
|
+
|
|
602
|
+
setTimeout(() => done({ ok: true, note: 'assumed-ok-after-800ms' }), 800)
|
|
603
|
+
|
|
604
|
+
})
|
|
605
|
+
|
|
606
|
+
if (child !== undefined && typeof child.unref === 'function' && outcome.ok) {
|
|
607
|
+
|
|
608
|
+
try { child.unref() } catch {}
|
|
609
|
+
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
if (!outcome.ok) {
|
|
613
|
+
|
|
614
|
+
return json({ ok: false, error: { code: outcome.code, message: outcome.message, mode }, detail: outcome }, 500)
|
|
615
|
+
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
return json({ ok: true, mode, detail: outcome })
|
|
619
|
+
|
|
620
|
+
} catch (error) {
|
|
621
|
+
|
|
622
|
+
return json({
|
|
623
|
+
|
|
624
|
+
ok: false,
|
|
625
|
+
|
|
626
|
+
error: { code: 'reveal-failed', message: String(error?.message ?? error), mode },
|
|
627
|
+
|
|
628
|
+
}, 500)
|
|
629
|
+
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
// ---------- 隐藏 / 取消隐藏 ----------
|
|
637
|
+
|
|
638
|
+
// 隐藏只记路径,不动文件;列表扫描时过滤掉。
|
|
639
|
+
|
|
640
|
+
if (op === 'hide' || op === 'unhide') {
|
|
641
|
+
|
|
642
|
+
const rel = body?.path
|
|
643
|
+
|
|
644
|
+
if (typeof rel !== 'string' || rel === '') {
|
|
645
|
+
|
|
646
|
+
return json({ ok: false, error: { code: 'invalid-path', message: '缺少文件路径' } }, 400)
|
|
647
|
+
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
const normalized = rel.replace(/\\/g, '/')
|
|
651
|
+
|
|
652
|
+
if (normalized.split('/').some((s) => s === '..' || s === '')) {
|
|
653
|
+
|
|
654
|
+
return json({ ok: false, error: { code: 'invalid-path', message: '路径非法' } }, 400)
|
|
655
|
+
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
try {
|
|
659
|
+
|
|
660
|
+
const opts = resolveOptions(config)
|
|
661
|
+
|
|
662
|
+
const hidden = new Set(await readHiddenList(opts.settingsFile))
|
|
663
|
+
|
|
664
|
+
if (op === 'hide') hidden.add(normalized)
|
|
665
|
+
|
|
666
|
+
else hidden.delete(normalized)
|
|
667
|
+
|
|
668
|
+
await fsp.mkdir(path.dirname(opts.settingsFile), { recursive: true })
|
|
669
|
+
|
|
670
|
+
// 只改 hidden 字段,其余设置保留
|
|
671
|
+
|
|
672
|
+
const current = readSettingsFile(opts.settingsFile)
|
|
673
|
+
|
|
674
|
+
const next = { ...current, hidden: [...hidden] }
|
|
675
|
+
|
|
676
|
+
await fsp.writeFile(opts.settingsFile, JSON.stringify(next, null, 2), 'utf8')
|
|
677
|
+
|
|
678
|
+
return json({ ok: true, hidden: hidden.size })
|
|
679
|
+
|
|
680
|
+
} catch (error) {
|
|
681
|
+
|
|
682
|
+
return json({ ok: false, error: { code: 'hide-failed', message: String(error?.message ?? error) } }, 500)
|
|
683
|
+
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
|
|
690
|
+
// ---------- 全部恢复(清空隐藏列表)----------
|
|
691
|
+
|
|
692
|
+
if (op === 'clearHidden') {
|
|
693
|
+
|
|
694
|
+
try {
|
|
695
|
+
|
|
696
|
+
const opts = resolveOptions(config)
|
|
697
|
+
|
|
698
|
+
const current = readSettingsFile(opts.settingsFile)
|
|
699
|
+
|
|
700
|
+
const before = Array.isArray(current.hidden) ? current.hidden.length : 0
|
|
701
|
+
|
|
702
|
+
await fsp.mkdir(path.dirname(opts.settingsFile), { recursive: true })
|
|
703
|
+
|
|
704
|
+
await fsp.writeFile(
|
|
705
|
+
|
|
706
|
+
opts.settingsFile,
|
|
707
|
+
|
|
708
|
+
JSON.stringify({ ...current, hidden: [] }, null, 2),
|
|
709
|
+
|
|
710
|
+
'utf8',
|
|
711
|
+
|
|
712
|
+
)
|
|
713
|
+
|
|
714
|
+
return json({ ok: true, cleared: before })
|
|
715
|
+
|
|
716
|
+
} catch (error) {
|
|
717
|
+
|
|
718
|
+
return json({ ok: false, error: { code: 'clear-failed', message: String(error?.message ?? error) } }, 500)
|
|
719
|
+
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
// ---------- 删除文件(不可逆,前端必须二次确认)----------
|
|
727
|
+
|
|
728
|
+
if (op === 'delete') {
|
|
729
|
+
|
|
730
|
+
const found = await resolveReadable(resolved.roots, body?.path, resolved)
|
|
731
|
+
|
|
732
|
+
if (found === null) return json({ ok: false, error: { code: 'not-readable', message: '文件不存在或不可读' } }, 404)
|
|
733
|
+
|
|
734
|
+
// 双保险:前端未明确确认则拒绝执行
|
|
735
|
+
|
|
736
|
+
if (body?.confirm !== true) {
|
|
737
|
+
|
|
738
|
+
return json({ ok: false, error: { code: 'need-confirm', message: '需要确认后才能删除' } }, 400)
|
|
739
|
+
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
try {
|
|
743
|
+
|
|
744
|
+
await fsp.rm(found.absolute, { force: true })
|
|
745
|
+
|
|
746
|
+
// 删除后从隐藏列表里清掉,避免残留
|
|
747
|
+
|
|
748
|
+
const opts = resolveOptions(config)
|
|
749
|
+
|
|
750
|
+
const hidden = new Set(await readHiddenList(opts.settingsFile))
|
|
751
|
+
|
|
752
|
+
const rel = body?.path.replace(/\\/g, '/')
|
|
753
|
+
|
|
754
|
+
if (hidden.has(rel)) {
|
|
755
|
+
|
|
756
|
+
hidden.delete(rel)
|
|
757
|
+
|
|
758
|
+
const current = readSettingsFile(opts.settingsFile)
|
|
759
|
+
|
|
760
|
+
await fsp.writeFile(opts.settingsFile, JSON.stringify({ ...current, hidden: [...hidden] }, null, 2), 'utf8')
|
|
761
|
+
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
return json({ ok: true, deleted: found.absolute })
|
|
765
|
+
|
|
766
|
+
} catch (error) {
|
|
767
|
+
|
|
768
|
+
return json({ ok: false, error: { code: 'delete-failed', message: String(error?.message ?? error) } }, 500)
|
|
769
|
+
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
|
|
775
|
+
|
|
776
|
+
return json({ ok: false, error: { code: 'unknown-op', message: `未知操作:${op}` } }, 400)
|
|
777
|
+
|
|
778
|
+
},
|
|
779
|
+
|
|
780
|
+
}),
|
|
781
|
+
|
|
782
|
+
'dsh-gallery: api route',
|
|
783
|
+
|
|
784
|
+
)
|
|
785
|
+
}
|