dsh-gallery 0.15.0 → 0.16.2
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 +71 -0
- package/README.md +35 -4
- package/client.js +1292 -21
- package/host/clipboard.js +211 -0
- package/host/derive.js +62 -0
- package/host/http.js +448 -1
- package/host/scan.js +33 -5
- package/host/session-index.js +34 -1
- package/host/trim.js +134 -0
- package/package.json +98 -91
package/host/http.js
CHANGED
|
@@ -11,14 +11,21 @@
|
|
|
11
11
|
import { promises as fsp } from 'node:fs'
|
|
12
12
|
import { createHash } from 'node:crypto'
|
|
13
13
|
import path from 'node:path'
|
|
14
|
+
import { spawn } from 'node:child_process'
|
|
14
15
|
|
|
15
16
|
import { ROUTE, MEDIA_ROUTE } from './constants.js'
|
|
16
17
|
import { isInside } from './fsutil.js'
|
|
18
|
+
import { cropMediaFile } from './trim.js'
|
|
17
19
|
import {
|
|
18
20
|
resolveOptions, readSettingsFile, readHiddenList,
|
|
19
21
|
validateRootCandidate, readSettingsPayload,
|
|
20
22
|
} from './settings.js'
|
|
21
|
-
import { scanGallery } from './scan.js'
|
|
23
|
+
import { scanGallery, resolveSessionScope, parentDirOf } from './scan.js'
|
|
24
|
+
import { locateSessionDir, readSessionLogText } from './session.js'
|
|
25
|
+
import { extractSessionTitleFast } from './session-index.js'
|
|
26
|
+
import { DEFAULT_SESSIONS_DIR } from './constants.js'
|
|
27
|
+
import { makeDerivedName, bumpName, pngBufferFrom, tmpPathFor } from './derive.js'
|
|
28
|
+
import { readClipboardFilePaths, writeClipboardFilePaths, internalClipboardPaths, pasteFilesIntoDir } from './clipboard.js'
|
|
22
29
|
|
|
23
30
|
/**
|
|
24
31
|
* 列表请求的短缓存 + 并发去重(加载提速,不改变返回内容与分组顺序)。
|
|
@@ -1341,6 +1348,446 @@ export function registerOpsRoute(ctx, config) {
|
|
|
1341
1348
|
|
|
1342
1349
|
|
|
1343
1350
|
|
|
1351
|
+
// ---- v0.16.0 粘贴 / 裁剪:把外部素材或派生资产落进当前对话目录 ----
|
|
1352
|
+
|
|
1353
|
+
/**
|
|
1354
|
+
* 按绝对路径找它所属的产出根(多根环境必备)。
|
|
1355
|
+
*
|
|
1356
|
+
* 以前一律取 `roots[0]` 算 relPath:粘贴目标在第二个根(G:/DSH任务文件)时,
|
|
1357
|
+
* 算出来的相对路径会变成 `../DSH任务文件/xxx`,登记与列表全都对不上。
|
|
1358
|
+
* 找不到返回空串,调用方按「无法归属」处理。
|
|
1359
|
+
*/
|
|
1360
|
+
function rootOfAbs(roots, abs) {
|
|
1361
|
+
const list = Array.isArray(roots) ? roots : []
|
|
1362
|
+
// 两侧都归一成平台路径再比:配置里常写正斜杠,落盘路径是反斜杠,
|
|
1363
|
+
// isInside 只做字符串前缀判断,不归一就会把同一条路径判成根外。
|
|
1364
|
+
const target = path.resolve(abs)
|
|
1365
|
+
for (const r of list) {
|
|
1366
|
+
if (typeof r === 'string' && r !== '' && isInside(path.resolve(r), target)) return r
|
|
1367
|
+
}
|
|
1368
|
+
return ''
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
/**
|
|
1372
|
+
* 由 sessionId 解析「这次要落进去的对话目录」(绝对路径),拿不到返回 null。
|
|
1373
|
+
*
|
|
1374
|
+
* v0.16.2 起调用方不一定传当前对话:全局视图里在别组的资产区粘贴/裁剪,
|
|
1375
|
+
* 前端会把**那个组**的 sessionId 传进来,这里就落到它的工作目录。
|
|
1376
|
+
*
|
|
1377
|
+
* 目录选择顺序(都很保守,宁可返回 null 也不凭空造目录):
|
|
1378
|
+
* ① `sessionDirsAbs` 里**已存在**的目录(带 root,跨产出根不会拼错);
|
|
1379
|
+
* ② 相对路径 + 本 root 拼出来的目录(旧行为,会话目录被清掉时靠 mkdir 恢复)。
|
|
1380
|
+
*/
|
|
1381
|
+
async function resolveSessionDir(config, sessionId) {
|
|
1382
|
+
if (typeof sessionId !== 'string' || sessionId === '') return null
|
|
1383
|
+
const opts = resolveOptions(config)
|
|
1384
|
+
let scope
|
|
1385
|
+
try { scope = await resolveSessionScope(config, sessionId, opts.roots) } catch { return null }
|
|
1386
|
+
if (!scope?.ok) return null
|
|
1387
|
+
const roots = (opts.roots ?? []).filter((r) => typeof r === 'string' && r !== '')
|
|
1388
|
+
if (roots.length === 0) return null
|
|
1389
|
+
const absDirs = (scope.sessionDirsAbs ?? [])
|
|
1390
|
+
.map((d) => String(d ?? ''))
|
|
1391
|
+
.filter((d) => d !== '')
|
|
1392
|
+
.map((d) => path.resolve(d))
|
|
1393
|
+
// 同上:正/反斜杠混用时 isInside 会误判,两侧都先归一
|
|
1394
|
+
.filter((d) => roots.some((r) => isInside(path.resolve(r), d)))
|
|
1395
|
+
for (const abs of absDirs) {
|
|
1396
|
+
try { if ((await fsp.stat(abs)).isDirectory()) return abs } catch { /* 目录不在,试下一个 */ }
|
|
1397
|
+
}
|
|
1398
|
+
const dirs = (scope.sessionDirs ?? []).map((d) => String(d)).filter((d) => d !== '')
|
|
1399
|
+
if (dirs.length === 0) return null
|
|
1400
|
+
const root = roots[0]
|
|
1401
|
+
const abs = path.resolve(root, dirs[0])
|
|
1402
|
+
if (!isInside(root, abs)) return null
|
|
1403
|
+
return abs
|
|
1404
|
+
}
|
|
1405
|
+
|
|
1406
|
+
/**
|
|
1407
|
+
* 读会话标题 / 创建时间(派生资产登记用)。
|
|
1408
|
+
*
|
|
1409
|
+
* 为什么 Host 自己读、不靠前端传:粘贴走 document 级监听、裁剪走
|
|
1410
|
+
* MediaPlayer 内部按钮,两处拿「当前对话组」的上下文方式不同,
|
|
1411
|
+
* 前端每处都传一遍容易漏;这里按 sessionId 统一兜底,
|
|
1412
|
+
* 前端传了就更优先(个别对话日志缺失时仍能正确归组)。
|
|
1413
|
+
*
|
|
1414
|
+
* 读不到就返回空标题 / 0,不抛错 —— 归组只靠 sessionId,
|
|
1415
|
+
* 标题缺失最多退化成 sessionId 前 8 位,绝不能因此让粘贴失败。
|
|
1416
|
+
*/
|
|
1417
|
+
async function sessionMetaOf(config, sessionId) {
|
|
1418
|
+
const meta = { title: '', createdAt: 0 }
|
|
1419
|
+
if (typeof sessionId !== 'string' || sessionId === '') return meta
|
|
1420
|
+
const sessionsDir = typeof config?.sessionsDir === 'string' && config.sessionsDir !== ''
|
|
1421
|
+
? path.resolve(config.sessionsDir)
|
|
1422
|
+
: DEFAULT_SESSIONS_DIR
|
|
1423
|
+
try {
|
|
1424
|
+
const dir = await locateSessionDir(sessionsDir, sessionId)
|
|
1425
|
+
if (dir === null) return meta
|
|
1426
|
+
try { meta.createdAt = (await fsp.stat(dir)).birthtimeMs || 0 } catch { /* 忽略 */ }
|
|
1427
|
+
// v4 会话日志是 zstd 分帧,readSessionLogText 逐帧解压 + 按 mtime 缓存。
|
|
1428
|
+
// 解压后实测 390 万字符,标题事件位置不固定(本次在第 9.8 万字符处),
|
|
1429
|
+
// 所以用不 split 全文的 extractSessionTitleFast,别在粘贴热路径上白烧内存。
|
|
1430
|
+
const text = await readSessionLogText(path.join(dir, 'session.v4.jsonl.zstd'))
|
|
1431
|
+
meta.title = extractSessionTitleFast(text)
|
|
1432
|
+
} catch { /* 会话目录不可读就当没有标题 */ }
|
|
1433
|
+
return meta
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1436
|
+
/**
|
|
1437
|
+
* 登记派生资产(粘贴 / 裁剪)到 frames.json,让它挂回产出它的对话。
|
|
1438
|
+
*
|
|
1439
|
+
* 🔴 不登记会发生什么(2026-11-13 老板实测):文件确实落盘了,但全局资产
|
|
1440
|
+
* 视图按「会话日志里出现过的路径」分组,日志里没有这条 → 掉进「未归类」
|
|
1441
|
+
* (5000+ 条的历史池底部),界面上等于「粘贴/裁剪没生成」。同一份登记
|
|
1442
|
+
* 同时解决「归到当前对话」和「自动标素材(input)」两件事。
|
|
1443
|
+
*
|
|
1444
|
+
* 登记失败不回滚文件:宁可归组不准,也不能让老板刚粘的素材凭空消失。
|
|
1445
|
+
*/
|
|
1446
|
+
async function registerDerivedAsset(opts, entry) {
|
|
1447
|
+
try {
|
|
1448
|
+
await registerFrame(opts.settingsFile, {
|
|
1449
|
+
relPath: entry.relPath,
|
|
1450
|
+
kind: entry.kind,
|
|
1451
|
+
// 粘贴进来的外部素材、裁剪出的片段一律算「素材」(input)
|
|
1452
|
+
category: 'input',
|
|
1453
|
+
sessionId: entry.sessionId,
|
|
1454
|
+
sessionTitle: entry.sessionTitle ?? '',
|
|
1455
|
+
sessionCreatedAt: entry.sessionCreatedAt ?? 0,
|
|
1456
|
+
sourceRelPath: entry.sourceRelPath ?? '',
|
|
1457
|
+
sourceName: entry.sourceName ?? '',
|
|
1458
|
+
savedAt: Date.now(),
|
|
1459
|
+
})
|
|
1460
|
+
return true
|
|
1461
|
+
} catch { return false }
|
|
1462
|
+
}
|
|
1463
|
+
|
|
1464
|
+
|
|
1465
|
+
/** 探测本机 ffmpeg;优先固定安装路径,其次 PATH;都没有返回 null。 */
|
|
1466
|
+
async function findFfmpeg() {
|
|
1467
|
+
const candidates = [
|
|
1468
|
+
'G:/Program Files/ffmpeg/bin/ffmpeg.exe',
|
|
1469
|
+
'G:/Program Files (x86)/ffmpeg/bin/ffmpeg.exe',
|
|
1470
|
+
]
|
|
1471
|
+
for (const c of candidates) {
|
|
1472
|
+
try {
|
|
1473
|
+
const abs = path.resolve(c)
|
|
1474
|
+
await fsp.access(abs, 0)
|
|
1475
|
+
return abs
|
|
1476
|
+
} catch { /* 换下一个 */ }
|
|
1477
|
+
}
|
|
1478
|
+
return 'ffmpeg'
|
|
1479
|
+
}
|
|
1480
|
+
|
|
1481
|
+
/** 跑一次 ffmpeg,收集 stderr 便于失败定位;非 0 退出码视为失败。 */
|
|
1482
|
+
function runFfmpeg(ffmpeg, args) {
|
|
1483
|
+
return new Promise((resolve, reject) => {
|
|
1484
|
+
let child
|
|
1485
|
+
try { child = spawn(ffmpeg, args, { windowsHide: true }) } catch (err) { reject(err); return }
|
|
1486
|
+
let stderr = ''
|
|
1487
|
+
child.stderr?.on('data', (d) => { stderr += String(d) })
|
|
1488
|
+
child.on('error', (err) => reject(err))
|
|
1489
|
+
child.on('close', (code) => {
|
|
1490
|
+
if (code === 0) resolve()
|
|
1491
|
+
else reject(new Error('ffmpeg exit ' + code + ': ' + stderr.slice(-800)))
|
|
1492
|
+
})
|
|
1493
|
+
})
|
|
1494
|
+
}
|
|
1495
|
+
|
|
1496
|
+
async function resolveSourceUnderRoots(opts, relPath) {
|
|
1497
|
+
for (const root of opts.roots) {
|
|
1498
|
+
if (typeof root !== 'string' || root === '') continue
|
|
1499
|
+
const abs = path.resolve(root, relPath)
|
|
1500
|
+
if (!isInside(root, abs)) continue
|
|
1501
|
+
try { const st = await fsp.stat(abs); if (st.isFile()) return { abs, ownerRoot: root } } catch { /* 换下一个 */ }
|
|
1502
|
+
}
|
|
1503
|
+
return null
|
|
1504
|
+
}
|
|
1505
|
+
|
|
1506
|
+
/** 在目标目录里找一个不重名的名字(重复时加 -n)。 */
|
|
1507
|
+
async function uniqueName(dir, base) {
|
|
1508
|
+
let name = base
|
|
1509
|
+
let abs = path.join(dir, name)
|
|
1510
|
+
let n = 2
|
|
1511
|
+
while (n <= 999) {
|
|
1512
|
+
let exists = true
|
|
1513
|
+
try { await fsp.access(abs) } catch { exists = false }
|
|
1514
|
+
if (!exists) break
|
|
1515
|
+
name = bumpName(base, n)
|
|
1516
|
+
abs = path.join(dir, name)
|
|
1517
|
+
n += 1
|
|
1518
|
+
}
|
|
1519
|
+
return { name, abs }
|
|
1520
|
+
}
|
|
1521
|
+
|
|
1522
|
+
// ---- 粘贴图片(剪贴板里的位图 / 截图,统一 PNG) ----
|
|
1523
|
+
if (op === 'pasteImage') {
|
|
1524
|
+
const opts = resolveOptions(config)
|
|
1525
|
+
const sessionId = String(body?.sessionId ?? '')
|
|
1526
|
+
const data = typeof body?.data === 'string' ? body.data : ''
|
|
1527
|
+
const ext = (typeof body?.ext === 'string' && body.ext !== '') ? body.ext : 'png'
|
|
1528
|
+
if (data === '') {
|
|
1529
|
+
return json({ ok: false, error: { code: 'bad-request', message: '缺少图片数据' } }, 400)
|
|
1530
|
+
}
|
|
1531
|
+
const buf = pngBufferFrom(data)
|
|
1532
|
+
if (buf === null) {
|
|
1533
|
+
return json({ ok: false, error: { code: 'bad-png', message: '图片数据为空、超过 24 MB 或非 PNG' } }, 400)
|
|
1534
|
+
}
|
|
1535
|
+
const dir = await resolveSessionDir(config, sessionId)
|
|
1536
|
+
if (dir === null) {
|
|
1537
|
+
return json({ ok: false, error: { code: 'no-session-dir', message: '无法确定当前对话目录' } }, 400)
|
|
1538
|
+
}
|
|
1539
|
+
const { name, abs } = await uniqueName(dir, makeDerivedName('粘贴', ext))
|
|
1540
|
+
const tmp = `${abs}.tmp-${process.pid}-${Date.now().toString(36)}`
|
|
1541
|
+
try {
|
|
1542
|
+
await fsp.mkdir(dir, { recursive: true })
|
|
1543
|
+
await fsp.writeFile(tmp, buf)
|
|
1544
|
+
await fsp.rename(tmp, abs)
|
|
1545
|
+
} catch (error) {
|
|
1546
|
+
try { await fsp.rm(tmp, { force: true }) } catch { /* 忽略 */ }
|
|
1547
|
+
return json({ ok: false, error: { code: 'paste-write-failed', message: String(error?.message ?? error) } }, 500)
|
|
1548
|
+
}
|
|
1549
|
+
// v0.16.2:按落盘位置找所属根,多根环境不能赌 roots[0]
|
|
1550
|
+
const root = rootOfAbs(opts.roots, abs)
|
|
1551
|
+
const relPath = root === '' ? '' : path.relative(root, abs).split(path.sep).join('/')
|
|
1552
|
+
// 登记归属,否则全局资产会把这张图扔进 5000+ 条的「未归类」池底部
|
|
1553
|
+
const meta = await sessionMetaOf(config, sessionId)
|
|
1554
|
+
const registered = await registerDerivedAsset(opts, {
|
|
1555
|
+
relPath, kind: 'paste', sessionId,
|
|
1556
|
+
sessionTitle: String(body?.sessionTitle ?? '') || meta.title,
|
|
1557
|
+
sessionCreatedAt: Number(body?.sessionCreatedAt) || meta.createdAt,
|
|
1558
|
+
})
|
|
1559
|
+
listCache.clear()
|
|
1560
|
+
return json({ ok: true, relPath, absPath: abs, name, registered })
|
|
1561
|
+
}
|
|
1562
|
+
|
|
1563
|
+
// ---- 粘贴文件(剪贴板里复制的文件,Electron 给真实路径) ----
|
|
1564
|
+
if (op === 'pasteFile') {
|
|
1565
|
+
const opts = resolveOptions(config)
|
|
1566
|
+
const sessionId = String(body?.sessionId ?? '')
|
|
1567
|
+
const src = String(body?.srcPath ?? '').replace(/\\/g, '/')
|
|
1568
|
+
const hint = typeof body?.name === 'string' && body.name !== '' ? body.name : null
|
|
1569
|
+
if (src === '') {
|
|
1570
|
+
return json({ ok: false, error: { code: 'bad-request', message: '缺少源文件路径' } }, 400)
|
|
1571
|
+
}
|
|
1572
|
+
const srcAbs = path.resolve(src)
|
|
1573
|
+
let srcStat
|
|
1574
|
+
try { srcStat = await fsp.stat(srcAbs) } catch {
|
|
1575
|
+
return json({ ok: false, error: { code: 'src-not-found', message: '源文件不存在' } }, 404)
|
|
1576
|
+
}
|
|
1577
|
+
if (!srcStat.isFile()) {
|
|
1578
|
+
return json({ ok: false, error: { code: 'bad-request', message: '源路径不是文件' } }, 400)
|
|
1579
|
+
}
|
|
1580
|
+
if (srcStat.size > 2 * 1024 * 1024 * 1024) {
|
|
1581
|
+
return json({ ok: false, error: { code: 'too-large', message: '文件超过 2 GB,粘贴被拒绝' } }, 400)
|
|
1582
|
+
}
|
|
1583
|
+
const dir = await resolveSessionDir(config, sessionId)
|
|
1584
|
+
if (dir === null) {
|
|
1585
|
+
return json({ ok: false, error: { code: 'no-session-dir', message: '无法确定当前对话目录' } }, 400)
|
|
1586
|
+
}
|
|
1587
|
+
const base = (hint ?? path.basename(srcAbs)).replace(/[\\/:*?"<>|\r\n\t]/g, '_').trim() || 'file'
|
|
1588
|
+
const { name, abs } = await uniqueName(dir, base)
|
|
1589
|
+
try {
|
|
1590
|
+
await fsp.mkdir(dir, { recursive: true })
|
|
1591
|
+
await fsp.copyFile(srcAbs, abs)
|
|
1592
|
+
// 🔴 与 pasteFiles 同口径:Windows CopyFile 保留源 mtime,画廊按时间窗过滤,
|
|
1593
|
+
// 不刷新会让刚粘进来的旧素材在 global 视图里「消失」。粘贴 = 现在进入本对话。
|
|
1594
|
+
try { const now = new Date(); await fsp.utimes(abs, now, now) } catch { /* 忽略 */ }
|
|
1595
|
+
} catch (error) {
|
|
1596
|
+
return json({ ok: false, error: { code: 'paste-write-failed', message: String(error?.message ?? error) } }, 500)
|
|
1597
|
+
}
|
|
1598
|
+
// v0.16.2:按落盘位置找所属根,多根环境不能赌 roots[0]
|
|
1599
|
+
const root = rootOfAbs(opts.roots, abs)
|
|
1600
|
+
const relPath = root === '' ? '' : path.relative(root, abs).split(path.sep).join('/')
|
|
1601
|
+
// 与 pasteImage 同口径:登记归属,否则全局资产看不到这条粘贴
|
|
1602
|
+
const meta = await sessionMetaOf(config, sessionId)
|
|
1603
|
+
const registered = await registerDerivedAsset(opts, {
|
|
1604
|
+
relPath, kind: 'paste', sessionId,
|
|
1605
|
+
sessionTitle: String(body?.sessionTitle ?? '') || meta.title,
|
|
1606
|
+
sessionCreatedAt: Number(body?.sessionCreatedAt) || meta.createdAt,
|
|
1607
|
+
})
|
|
1608
|
+
listCache.clear()
|
|
1609
|
+
return json({ ok: true, relPath, absPath: abs, name, registered })
|
|
1610
|
+
}
|
|
1611
|
+
|
|
1612
|
+
// ---- v0.16.0 复制资产到系统剪贴板(单文件右键 / 批量选择共用) ----
|
|
1613
|
+
// 只接收 roots 内的相对路径,由 Host 解析绝对路径,前端不拼路径、防穿越。
|
|
1614
|
+
if (op === 'copyAssets') {
|
|
1615
|
+
const opts = resolveOptions(config)
|
|
1616
|
+
const rels = Array.isArray(body?.paths) ? body.paths : []
|
|
1617
|
+
const absList = []
|
|
1618
|
+
const missed = []
|
|
1619
|
+
for (const rp of rels.slice(0, 200)) {
|
|
1620
|
+
const rel = String(rp ?? '').replace(/\\/g, '/')
|
|
1621
|
+
if (rel === '') continue
|
|
1622
|
+
const found = await resolveSourceUnderRoots(opts, rel)
|
|
1623
|
+
if (found === null) missed.push(rel)
|
|
1624
|
+
else absList.push(found.abs)
|
|
1625
|
+
}
|
|
1626
|
+
if (absList.length === 0) {
|
|
1627
|
+
return json({ ok: false, error: { code: 'copy-no-source', message: '选中的文件都不在扫描根目录内' } }, 400)
|
|
1628
|
+
}
|
|
1629
|
+
const result = await writeClipboardFilePaths(absList)
|
|
1630
|
+
return json({ ok: true, count: result.count, mode: result.mode, missed })
|
|
1631
|
+
}
|
|
1632
|
+
|
|
1633
|
+
// ---- v0.16.0 读系统剪贴板里的文件列表(音视频 / 任意文件粘贴用) ----
|
|
1634
|
+
// Chromium 的 paste 事件拿不到「资源管理器复制的文件」,只能从这里补。
|
|
1635
|
+
// 系统剪贴板没有文件列表时,退回插件内部剪贴板(复制→粘贴仍闭环)。
|
|
1636
|
+
if (op === 'clipboardFiles') {
|
|
1637
|
+
let paths = await readClipboardFilePaths()
|
|
1638
|
+
let mode = 'system'
|
|
1639
|
+
if (paths.length === 0) {
|
|
1640
|
+
paths = internalClipboardPaths()
|
|
1641
|
+
mode = 'internal'
|
|
1642
|
+
}
|
|
1643
|
+
const files = []
|
|
1644
|
+
for (const p of paths.slice(0, 200)) {
|
|
1645
|
+
const abs = path.resolve(String(p ?? '').replace(/\\/g, '/'))
|
|
1646
|
+
try {
|
|
1647
|
+
const st = await fsp.stat(abs)
|
|
1648
|
+
if (!st.isFile()) continue
|
|
1649
|
+
files.push({ path: abs, name: path.basename(abs), size: st.size })
|
|
1650
|
+
} catch { /* 剪贴板可能残留已删除的路径,跳过 */ }
|
|
1651
|
+
}
|
|
1652
|
+
return json({ ok: true, files, mode })
|
|
1653
|
+
}
|
|
1654
|
+
|
|
1655
|
+
// ---- v0.16.0 批量把外部文件复制进当前对话目录(任意类型,含音视频) ----
|
|
1656
|
+
// 与 pasteFile 同口径:2GB 上限、重名自增、原子目标不覆盖原件、自动标「素材」。
|
|
1657
|
+
if (op === 'pasteFiles') {
|
|
1658
|
+
const opts = resolveOptions(config)
|
|
1659
|
+
const sessionId = String(body?.sessionId ?? '')
|
|
1660
|
+
const srcs = Array.isArray(body?.srcPaths) ? body.srcPaths : []
|
|
1661
|
+
const dir = await resolveSessionDir(config, sessionId)
|
|
1662
|
+
if (dir === null) {
|
|
1663
|
+
return json({ ok: false, error: { code: 'no-session-dir', message: '无法确定当前对话目录' } }, 400)
|
|
1664
|
+
}
|
|
1665
|
+
// v0.16.2:粘贴目标可能是别组、也可能是另一个产出根,按目录找根
|
|
1666
|
+
const root = rootOfAbs(opts.roots, dir)
|
|
1667
|
+
const { files: okFiles, failed } = await pasteFilesIntoDir(srcs, dir, root)
|
|
1668
|
+
if (okFiles.length > 0) {
|
|
1669
|
+
// 每个落盘文件都要单独登记(批量粘贴可能一次进来十几个)
|
|
1670
|
+
const meta = await sessionMetaOf(config, sessionId)
|
|
1671
|
+
const sessionTitle = String(body?.sessionTitle ?? '') || meta.title
|
|
1672
|
+
const sessionCreatedAt = Number(body?.sessionCreatedAt) || meta.createdAt
|
|
1673
|
+
for (const f of okFiles) {
|
|
1674
|
+
f.registered = await registerDerivedAsset(opts, {
|
|
1675
|
+
relPath: f.relPath, kind: 'paste', sessionId, sessionTitle, sessionCreatedAt,
|
|
1676
|
+
})
|
|
1677
|
+
}
|
|
1678
|
+
listCache.clear()
|
|
1679
|
+
}
|
|
1680
|
+
if (okFiles.length === 0) {
|
|
1681
|
+
return json({
|
|
1682
|
+
ok: false,
|
|
1683
|
+
error: { code: 'paste-files-failed', message: failed[0]?.message ?? '没有可粘贴的文件' },
|
|
1684
|
+
files: [], failed,
|
|
1685
|
+
}, 500)
|
|
1686
|
+
}
|
|
1687
|
+
return json({ ok: true, files: okFiles, failed })
|
|
1688
|
+
}
|
|
1689
|
+
|
|
1690
|
+
// ---- 裁剪图片(前端已按框裁好 PNG,写源同目录) ----
|
|
1691
|
+
if (op === 'cropImage') {
|
|
1692
|
+
const opts = resolveOptions(config)
|
|
1693
|
+
const imageRel = String(body?.path ?? '').replace(/\\/g, '/')
|
|
1694
|
+
const data = typeof body?.data === 'string' ? body.data : ''
|
|
1695
|
+
if (imageRel === '' || data === '') {
|
|
1696
|
+
return json({ ok: false, error: { code: 'bad-request', message: '缺少 path 或 data' } }, 400)
|
|
1697
|
+
}
|
|
1698
|
+
const buf = pngBufferFrom(data)
|
|
1699
|
+
if (buf === null) {
|
|
1700
|
+
return json({ ok: false, error: { code: 'bad-png', message: '图片数据为空、超过 24 MB 或非 PNG' } }, 400)
|
|
1701
|
+
}
|
|
1702
|
+
const src = await resolveSourceUnderRoots(opts, imageRel)
|
|
1703
|
+
if (src === null) {
|
|
1704
|
+
return json({ ok: false, error: { code: 'image-not-found', message: '源图不在扫描目录内' } }, 404)
|
|
1705
|
+
}
|
|
1706
|
+
const dir = path.dirname(src.abs)
|
|
1707
|
+
const imageName = path.basename(src.abs)
|
|
1708
|
+
const { name, abs } = await uniqueName(dir, makeDerivedName('裁剪-' + imageName.replace(/\.[^.]+$/, ''), 'png'))
|
|
1709
|
+
const tmp = `${abs}.tmp-${process.pid}-${Date.now().toString(36)}`
|
|
1710
|
+
try {
|
|
1711
|
+
await fsp.mkdir(dir, { recursive: true })
|
|
1712
|
+
await fsp.writeFile(tmp, buf)
|
|
1713
|
+
await fsp.rename(tmp, abs)
|
|
1714
|
+
} catch (error) {
|
|
1715
|
+
try { await fsp.rm(tmp, { force: true }) } catch { /* 忽略 */ }
|
|
1716
|
+
return json({ ok: false, error: { code: 'crop-write-failed', message: String(error?.message ?? error) } }, 500)
|
|
1717
|
+
}
|
|
1718
|
+
const relPath = path.relative(src.ownerRoot, abs).split(path.sep).join('/')
|
|
1719
|
+
// 登记归属:裁剪产物同样不在会话日志里,不登记会掉进「未归类」
|
|
1720
|
+
const sessionId = String(body?.sessionId ?? '')
|
|
1721
|
+
const meta = await sessionMetaOf(config, sessionId)
|
|
1722
|
+
const registered = await registerDerivedAsset(opts, {
|
|
1723
|
+
relPath, kind: 'crop', sessionId,
|
|
1724
|
+
sessionTitle: String(body?.sessionTitle ?? '') || meta.title,
|
|
1725
|
+
sessionCreatedAt: Number(body?.sessionCreatedAt) || meta.createdAt,
|
|
1726
|
+
sourceRelPath: imageRel,
|
|
1727
|
+
sourceName: imageName,
|
|
1728
|
+
})
|
|
1729
|
+
listCache.clear()
|
|
1730
|
+
return json({ ok: true, relPath, absPath: abs, name, registered })
|
|
1731
|
+
}
|
|
1732
|
+
|
|
1733
|
+
// ---- 裁剪音视频(Host 调 ffmpeg 流拷贝切段,落源同目录) ----
|
|
1734
|
+
if (op === 'cropMedia') {
|
|
1735
|
+
const opts = resolveOptions(config)
|
|
1736
|
+
const mediaRel = String(body?.path ?? '').replace(/\\/g, '/')
|
|
1737
|
+
const start = Number(body?.start)
|
|
1738
|
+
const end = Number(body?.end)
|
|
1739
|
+
const kind = String(body?.kind ?? 'video')
|
|
1740
|
+
if (mediaRel === '' || !Number.isFinite(start) || !Number.isFinite(end)) {
|
|
1741
|
+
return json({ ok: false, error: { code: 'bad-request', message: '缺少 path 或起止时间' } }, 400)
|
|
1742
|
+
}
|
|
1743
|
+
if (start < 0 || end <= start) {
|
|
1744
|
+
return json({ ok: false, error: { code: 'bad-request', message: '起止时间不合法(结束须大于开始)' } }, 400)
|
|
1745
|
+
}
|
|
1746
|
+
const src = await resolveSourceUnderRoots(opts, mediaRel)
|
|
1747
|
+
if (src === null) {
|
|
1748
|
+
return json({ ok: false, error: { code: 'media-not-found', message: '源音视频不在扫描目录内' } }, 404)
|
|
1749
|
+
}
|
|
1750
|
+
const ffmpeg = await findFfmpeg()
|
|
1751
|
+
if (ffmpeg === null) {
|
|
1752
|
+
return json({ ok: false, error: { code: 'ffmpeg-missing', message: '未找到 ffmpeg,无法裁剪音视频' } }, 500)
|
|
1753
|
+
}
|
|
1754
|
+
const mediaName = path.basename(src.abs)
|
|
1755
|
+
const srcExt = path.extname(mediaName).replace(/^\./, '').toLowerCase()
|
|
1756
|
+
const outExt = srcExt || (kind === 'audio' ? 'mp3' : 'mp4')
|
|
1757
|
+
const { name, abs: outAbs } = await uniqueName(path.dirname(src.abs), makeDerivedName('裁剪-' + mediaName.replace(/\.[^.]+$/, ''), outExt))
|
|
1758
|
+
// 🔴 临时文件**必须保留扩展名**:ffmpeg 靠扩展名推断输出容器,
|
|
1759
|
+
// 写成 `xxx.mp4.tmp-123` 会报 "Unable to choose an output format" → HTTP 500。
|
|
1760
|
+
// 命名规则抽到 derive.tmpPathFor,测试直接打真函数,防止再次漂移。
|
|
1761
|
+
const tmpAbs = tmpPathFor(outAbs)
|
|
1762
|
+
let mode = 'copy'
|
|
1763
|
+
try {
|
|
1764
|
+
await fsp.mkdir(path.dirname(outAbs), { recursive: true })
|
|
1765
|
+
// 核心策略在 host/trim.js:先流拷贝,再用 ffprobe 校验时长,偏差 > 0.5s 才重编码。
|
|
1766
|
+
// 抽出去是为了能对真 ffmpeg 做集成测试(copy 只认关键帧这点必须实测才发现)。
|
|
1767
|
+
const result = await cropMediaFile({
|
|
1768
|
+
ffmpeg, src: src.abs, start, end, out: tmpAbs, outExt, kind,
|
|
1769
|
+
})
|
|
1770
|
+
mode = result.mode
|
|
1771
|
+
await fsp.rename(tmpAbs, outAbs)
|
|
1772
|
+
} catch (error) {
|
|
1773
|
+
try { await fsp.rm(tmpAbs, { force: true }) } catch { /* 忽略 */ }
|
|
1774
|
+
return json({ ok: false, error: { code: 'crop-failed', message: String(error?.message ?? error) } }, 500)
|
|
1775
|
+
}
|
|
1776
|
+
const relPath = path.relative(src.ownerRoot, outAbs).split(path.sep).join('/')
|
|
1777
|
+
// 与 cropImage 同口径:音视频片段也要挂回产出它的对话
|
|
1778
|
+
const sessionId = String(body?.sessionId ?? '')
|
|
1779
|
+
const meta = await sessionMetaOf(config, sessionId)
|
|
1780
|
+
const registered = await registerDerivedAsset(opts, {
|
|
1781
|
+
relPath, kind: 'crop', sessionId,
|
|
1782
|
+
sessionTitle: String(body?.sessionTitle ?? '') || meta.title,
|
|
1783
|
+
sessionCreatedAt: Number(body?.sessionCreatedAt) || meta.createdAt,
|
|
1784
|
+
sourceRelPath: mediaRel,
|
|
1785
|
+
sourceName: mediaName,
|
|
1786
|
+
})
|
|
1787
|
+
listCache.clear()
|
|
1788
|
+
return json({ ok: true, relPath, absPath: outAbs, name, start, end, mode, registered })
|
|
1789
|
+
}
|
|
1790
|
+
|
|
1344
1791
|
return json({ ok: false, error: { code: 'unknown-op', message: `未知操作:${op}` } }, 400)
|
|
1345
1792
|
|
|
1346
1793
|
},
|
package/host/scan.js
CHANGED
|
@@ -28,6 +28,21 @@ export function parentDirOf(relPath) {
|
|
|
28
28
|
/** v0.14.7:当前对话分类。日志/登记里没有的、同目录外部图片自动标「素材」(input)。 */
|
|
29
29
|
export function categoryForSessionAsset(autoCategory, externalAsset) {
|
|
30
30
|
return autoCategory ?? (externalAsset ? ASSET_CATEGORY.INPUT : null)
|
|
31
|
+
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 登记条目自带分类(v0.16.1)。
|
|
36
|
+
*
|
|
37
|
+
* frames.json 原本只登记截帧,截帧一律算「过程稿」(intermediate)。v0.16.1 起
|
|
38
|
+
* 粘贴 / 裁剪也登记——它们是从外部拿进来的素材或切出来的片段,必须算「素材」
|
|
39
|
+
* (input),否则界面上跟截帧混成一堆过程稿,老板分不清哪个能直接喂给工作流。
|
|
40
|
+
* 未知/缺省值仍回落到 intermediate,保持截帧原有行为不变。
|
|
41
|
+
*/
|
|
42
|
+
function frameCategoryOf(frame) {
|
|
43
|
+
const c = String(frame?.category ?? '')
|
|
44
|
+
if (c === ASSET_CATEGORY.INPUT || c === ASSET_CATEGORY.FINAL || c === ASSET_CATEGORY.UNKNOWN) return c
|
|
45
|
+
return ASSET_CATEGORY.INTERMEDIATE
|
|
31
46
|
}
|
|
32
47
|
|
|
33
48
|
export function extractAssetInfo(logText, sessionStartMs = 0) {
|
|
@@ -223,6 +238,14 @@ export async function resolveSessionScope(config, sessionId, allRoots) {
|
|
|
223
238
|
// v0.14.7:会话引用过的「精确目录」(相对 root、小写,空串表示 root 直属层)。
|
|
224
239
|
// 外部拷进同一目录、没被日志引用的图片,也要能在「当前对话」里看到并标为素材。
|
|
225
240
|
const sessionDirs = new Set()
|
|
241
|
+
/**
|
|
242
|
+
* v0.16.2:同一批目录的**绝对路径**(保留原始大小写)。
|
|
243
|
+
*
|
|
244
|
+
* 只留相对路径时,Host 拼目录只能赌「属于第一个 root」—— 本机有两个产出根
|
|
245
|
+
* (G:/DSH产出、G:/DSH任务文件),会话只用了第二个时就会拼到第一个根下,
|
|
246
|
+
* mkdir 造出一个空目录,粘贴的资产凭空消失。这里把根一起记下来。
|
|
247
|
+
*/
|
|
248
|
+
const sessionDirsAbs = new Set()
|
|
226
249
|
// relPath(小写) → 分类,供扫描时标注
|
|
227
250
|
const categoryByRel = new Map()
|
|
228
251
|
|
|
@@ -242,6 +265,7 @@ export async function resolveSessionScope(config, sessionId, allRoots) {
|
|
|
242
265
|
relPaths.push(relPath)
|
|
243
266
|
categoryByRel.set(relPath.toLowerCase(), info.category)
|
|
244
267
|
sessionDirs.add(parentDirOf(relPath))
|
|
268
|
+
sessionDirsAbs.add(path.dirname(absolute))
|
|
245
269
|
// 修法 B:有子目录时取第一层;没有子目录(文件直接在 root 下)时,
|
|
246
270
|
// 用空字符串占位,表示「只在根目录层匹配」,不再直接丢弃
|
|
247
271
|
if (segs.length >= 2) taskDirs.add(segs[0])
|
|
@@ -255,6 +279,7 @@ export async function resolveSessionScope(config, sessionId, allRoots) {
|
|
|
255
279
|
relPaths,
|
|
256
280
|
categoryByRel,
|
|
257
281
|
sessionDirs: [...sessionDirs],
|
|
282
|
+
sessionDirsAbs: [...sessionDirsAbs],
|
|
258
283
|
subSessions: subLogs.length,
|
|
259
284
|
}
|
|
260
285
|
}
|
|
@@ -312,11 +337,11 @@ export async function scanGallery(config, kind = 'image', scope = 'global', sess
|
|
|
312
337
|
sessionDirSet = new Set((scopeInfo.sessionDirs ?? []).map((d) => String(d).toLowerCase()))
|
|
313
338
|
// 会话日志缺失时 resolveSessionScope 可能不带分类表,补一个空的
|
|
314
339
|
if (!(scopeInfo.categoryByRel instanceof Map)) scopeInfo.categoryByRel = new Map()
|
|
315
|
-
//
|
|
340
|
+
// 本对话派生出来的资产(截帧 / 粘贴 / 裁剪):让它通过范围过滤,并给一个明确标签
|
|
316
341
|
for (const [rel, f] of frameByRel) {
|
|
317
342
|
if (String(f.sessionId ?? '') === sessionId) {
|
|
318
343
|
sessionRelSet.add(rel)
|
|
319
|
-
scopeInfo.categoryByRel?.set(rel,
|
|
344
|
+
scopeInfo.categoryByRel?.set(rel, frameCategoryOf(f))
|
|
320
345
|
sessionDirSet.add(parentDirOf(rel))
|
|
321
346
|
}
|
|
322
347
|
}
|
|
@@ -517,7 +542,9 @@ export async function scanGallery(config, kind = 'image', scope = 'global', sess
|
|
|
517
542
|
overlayRelPath: String(frame.overlayRelPath ?? ''),
|
|
518
543
|
fillColor: String(frame.fillColor ?? ''),
|
|
519
544
|
}
|
|
520
|
-
} else if (frame !== undefined) {
|
|
545
|
+
} else if (frame !== undefined && (frame.kind === undefined || frame.kind === 'frame')) {
|
|
546
|
+
// 只有「视频截帧」才带来源视频/时间码。粘贴 / 裁剪(v0.16.1 起也登记)
|
|
547
|
+
// 没有取自哪一帧的概念,误设 frameOf 会让卡片显示「取自《…》」的假来源。
|
|
521
548
|
item.frameOf = {
|
|
522
549
|
videoName: String(frame.videoName ?? ''),
|
|
523
550
|
videoRelPath: String(frame.videoRelPath ?? ''),
|
|
@@ -525,7 +552,7 @@ export async function scanGallery(config, kind = 'image', scope = 'global', sess
|
|
|
525
552
|
frameMs: Number(frame.frameMs) || 0,
|
|
526
553
|
}
|
|
527
554
|
}
|
|
528
|
-
//
|
|
555
|
+
// 登记不在会话日志里:ownerByAbs 查不到时,用登记里的会话信息补一个虚拟归属。
|
|
529
556
|
// 日志里已有归属的文件(例如视频本身)一律沿用原归属,不受影响。
|
|
530
557
|
const frameSid = String(frame?.sessionId ?? '')
|
|
531
558
|
const virtualOwner = frame !== undefined && frameSid !== ''
|
|
@@ -533,7 +560,8 @@ export async function scanGallery(config, kind = 'image', scope = 'global', sess
|
|
|
533
560
|
sessionId: frameSid,
|
|
534
561
|
title: String(frame.sessionTitle ?? '') || sessionIndex?.get(frameSid)?.title || '',
|
|
535
562
|
createdAt: Number(frame.sessionCreatedAt) || sessionIndex?.get(frameSid)?.createdAt || 0,
|
|
536
|
-
category
|
|
563
|
+
// 截帧 = 过程稿;粘贴 / 裁剪 = 素材(登记里带着 category,缺省回落过程稿)
|
|
564
|
+
category: frameCategoryOf(frame),
|
|
537
565
|
}
|
|
538
566
|
: undefined
|
|
539
567
|
const owner = ownerByAbs.get(abs) ?? virtualOwner
|
package/host/session-index.js
CHANGED
|
@@ -77,7 +77,7 @@ async function listSessionDirs(sessionsDir) {
|
|
|
77
77
|
* @param logText - 解压后的日志文本。
|
|
78
78
|
* @returns 标题字符串;没有则空串。
|
|
79
79
|
*/
|
|
80
|
-
function extractSessionTitle(logText) {
|
|
80
|
+
export function extractSessionTitle(logText) {
|
|
81
81
|
if (typeof logText !== 'string' || logText === '') return ''
|
|
82
82
|
// 取最后一个 title(用户可能改过标题)
|
|
83
83
|
let title = ''
|
|
@@ -92,6 +92,39 @@ function extractSessionTitle(logText) {
|
|
|
92
92
|
return title
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
+
/**
|
|
96
|
+
* 同上的省内存版:不 split 全文,只按 `session/title` 命中位置切片。
|
|
97
|
+
*
|
|
98
|
+
* 为什么要它:单个会话日志解压后实测 390 万字符(约 4 MB 字符串),
|
|
99
|
+
* `split('\n')` 会造出十几万个小字符串,在「粘贴一次就读一次标题」这种
|
|
100
|
+
* 热路径上纯属白烧内存。标题事件的位置也不固定(实测出现在第 98121 字符,
|
|
101
|
+
* 既不在开头也不在末尾),所以不能靠截头尾。
|
|
102
|
+
*
|
|
103
|
+
* 语义与 extractSessionTitle 一致:取最后一次出现的标题。
|
|
104
|
+
*/
|
|
105
|
+
export function extractSessionTitleFast(logText) {
|
|
106
|
+
if (typeof logText !== 'string' || logText === '') return ''
|
|
107
|
+
const MARK = 'session/title'
|
|
108
|
+
let title = ''
|
|
109
|
+
let pos = 0
|
|
110
|
+
while (pos < logText.length) {
|
|
111
|
+
const hit = logText.indexOf(MARK, pos)
|
|
112
|
+
if (hit < 0) break
|
|
113
|
+
const lineStart = logText.lastIndexOf('\n', hit) + 1
|
|
114
|
+
let lineEnd = logText.indexOf('\n', hit)
|
|
115
|
+
if (lineEnd < 0) lineEnd = logText.length
|
|
116
|
+
pos = lineEnd + 1
|
|
117
|
+
// 一整个会话序列化成一行时该行可能极大,直接跳过,避免解析几 MB 的单行
|
|
118
|
+
if (lineEnd - lineStart > 8192) continue
|
|
119
|
+
let o
|
|
120
|
+
try { o = JSON.parse(logText.slice(lineStart, lineEnd)) } catch { continue }
|
|
121
|
+
if (o?.type !== 'session/title') continue
|
|
122
|
+
const t = o?.data?.title
|
|
123
|
+
if (typeof t === 'string' && t.trim() !== '') title = t.trim()
|
|
124
|
+
}
|
|
125
|
+
return title
|
|
126
|
+
}
|
|
127
|
+
|
|
95
128
|
/**
|
|
96
129
|
* 从会话日志首行读元信息(创建时间、cwd、父会话)。
|
|
97
130
|
* @param logText - 解压后的日志文本。
|