@ggboy123/dsh-paper-reader 1.6.1 → 1.8.0
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/README.md +7 -4
- package/dist/host.js +480 -46
- package/dist/library-manage.js +628 -122
- package/dist/library.js +337 -55
- package/dist/migrate.js +981 -0
- package/dist/notes.js +184 -0
- package/dist/opencode.js +123 -0
- package/dist/scope.js +379 -0
- package/dist/search.js +21 -12
- package/dist/theme-config.js +123 -0
- package/dist/tools.js +8 -2
- package/dist/translate-config.js +8 -1
- package/dist/translate-selection.js +6 -0
- package/dist/translate.js +7 -1
- package/lib/client.js +419 -87
- package/package.json +2 -1
- package/reader/index.html +1117 -39
- package/runtime/babeldoc-driver.py +5 -0
package/dist/library.js
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
// library.ts — 文献库目录约定(纯函数,不依赖 Cordis)。
|
|
2
2
|
//
|
|
3
|
-
//
|
|
4
|
-
// <dataDir
|
|
5
|
-
// <dataDir
|
|
6
|
-
// <dataDir
|
|
3
|
+
// 布局(v1.6 起:**按论文分文件夹** + **专题可任意层嵌套**,见 docs/darkmode-subtopic-plan.md §B1/§B2):
|
|
4
|
+
// <dataDir>/<专题路径>/<论文名>/<论文名>.pdf 原始 PDF
|
|
5
|
+
// <dataDir>/<专题路径>/<论文名>/<论文名>.txt 转录缓存(清洗后的全文文本)
|
|
6
|
+
// <dataDir>/<专题路径>/<论文名>/<论文名>.pages.json 页码偏移表(转录时生成,检索映射页码用)
|
|
7
|
+
// 其中 <专题路径> = `A` / `A/B` / `A/B/C`(**目录即层级**,不引入元数据文件)。
|
|
8
|
+
// 结构不变量(唯一权威):目录内存在名为 `<basename(dir)>.pdf` 的**常规文件** ⇒ 该目录是**论文目录**,
|
|
9
|
+
// 遍历到此为止(绝不递归进论文目录 —— 否则复跑迁移会产生 X/X/X.pdf 套娃)。
|
|
7
10
|
// pdf2zh 的产物(-en / -zh / -dual)不算用户文献。
|
|
8
11
|
// (问答不落盘存档——会话持久化交给 dsh 自己;pdfqa 时代的 -qa/ 目录不再读写。)
|
|
12
|
+
//
|
|
13
|
+
// ⚠️ 旧布局(<dataDir>/<专题>/<文献>.pdf 平铺)运行期**不再认**(方案 §B1.4 明确「不双读」):
|
|
14
|
+
// 升级后需跑一次幂等迁移(src/migrate.ts)把平铺产物收进论文文件夹。
|
|
9
15
|
import { readdirSync, existsSync, mkdirSync, statSync, realpathSync, lstatSync } from 'node:fs';
|
|
10
|
-
import { join, resolve, extname, basename, sep } from 'node:path';
|
|
16
|
+
import { join, resolve, extname, basename, dirname, sep } from 'node:path';
|
|
11
17
|
import { homedir } from 'node:os';
|
|
12
18
|
// 破坏性原语与回收站实现(我们的加固层):适配器只做「复用 #4 边界层 + 翻译签名/返回形状」,
|
|
13
19
|
// 真正的删除/改名永远只有 library-manage 一份实现。
|
|
@@ -41,21 +47,267 @@ export function listTopics(dataDir) {
|
|
|
41
47
|
.map((e) => e.name)
|
|
42
48
|
.sort();
|
|
43
49
|
}
|
|
44
|
-
|
|
50
|
+
// ── 专题路径(相对 dataDir 的 `/` 分段)校验:**切段逐段复用既有 validateEntryName** ──────────
|
|
51
|
+
// 红线(方案 §B2.2):**不许**放宽 validateName 的 `/` 正则;放开 `/` 的唯一安全方式是切段后
|
|
52
|
+
// 每段过一遍既有校验,再把段拼回去。深度/段长/总长给病态输入设上限(§B2.5)。
|
|
53
|
+
export const MAX_TOPIC_DEPTH = 16;
|
|
54
|
+
export const MAX_TOPIC_SEG = 100;
|
|
55
|
+
export const MAX_TOPIC_PATH = 200;
|
|
56
|
+
/**
|
|
57
|
+
* 把 topic 参数切成段并**逐段校验**。合法返回段数组(已 trim 每段),否则抛普通 Error(文案取既有校验)。
|
|
58
|
+
* `boundaryOnly` = true 时走边界类语义(删除/改名-from:允许历史遗留的不规范名),
|
|
59
|
+
* 否则走命名规范类(创建/改名-to)。
|
|
60
|
+
* 注意:`basename(seg) === seg` 由 validateEntryName 的 `/[\\/]|\.\./` 与 `/` 切分共同保证。
|
|
61
|
+
*/
|
|
62
|
+
export function topicSegments(raw, opts = {}) {
|
|
63
|
+
if (typeof raw !== 'string')
|
|
64
|
+
throw new Error('名字不能为空');
|
|
65
|
+
const trimmed = raw.trim();
|
|
66
|
+
if (!trimmed)
|
|
67
|
+
throw new Error('名字不能为空');
|
|
68
|
+
const enc = encodedEscapeError(trimmed);
|
|
69
|
+
if (enc)
|
|
70
|
+
throw new Error(enc);
|
|
71
|
+
// 长度上限是**创建侧**的命名规范(防病态输入),不是安全边界:边界类(删除/改名-from/清空)
|
|
72
|
+
// 作用于已存在的条目,若也卡新上限,上一版(单层 ≤120)能创建的长名专题会变成删不掉的孤儿
|
|
73
|
+
// (评审 F)。深度上限保留(分段数量不随「历史名字」增长,且是遍历成本护栏)。
|
|
74
|
+
if (!opts.boundaryOnly && trimmed.length > MAX_TOPIC_PATH)
|
|
75
|
+
throw new Error(`名字过长(专题路径最多 ${MAX_TOPIC_PATH} 个字符)`);
|
|
76
|
+
const segs = trimmed.split('/');
|
|
77
|
+
if (segs.length > MAX_TOPIC_DEPTH)
|
|
78
|
+
throw new Error(`专题层级过深(最多 ${MAX_TOPIC_DEPTH} 层)`);
|
|
79
|
+
for (const seg of segs) {
|
|
80
|
+
const err = opts.boundaryOnly ? validateEntryBoundary(seg) : validateEntryName(seg);
|
|
81
|
+
if (err)
|
|
82
|
+
throw new Error(err);
|
|
83
|
+
if (!opts.boundaryOnly && seg.trim().length > MAX_TOPIC_SEG)
|
|
84
|
+
throw new Error(`专题名过长(每层最多 ${MAX_TOPIC_SEG} 个字符)`);
|
|
85
|
+
}
|
|
86
|
+
return segs.map((s) => s.trim());
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* 「编码变形」防护(F4 的负向之一):`%2e%2e%2f` 这类**解码后才是分隔符/上跳**的输入一律拒。
|
|
90
|
+
* 只在解码**成功且确实揭示了分隔符或 `..` 段**时拒绝 —— 名字里合法地含 `%`(如 `50% off`)不受影响
|
|
91
|
+
* (解码失败 = 原样当名字,交词法层处理)。
|
|
92
|
+
*/
|
|
93
|
+
export function encodedEscapeError(raw) {
|
|
94
|
+
if (!raw.includes('%'))
|
|
95
|
+
return null;
|
|
96
|
+
let dec;
|
|
97
|
+
try {
|
|
98
|
+
dec = decodeURIComponent(raw);
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
return null; // 不是合法百分号编码:它就是一个普通名字(`%` 是合法文件名字符)
|
|
102
|
+
}
|
|
103
|
+
if (dec === raw)
|
|
104
|
+
return null;
|
|
105
|
+
if (dec.includes('\u0000'))
|
|
106
|
+
return '名字包含编码后的控制字符,已拒绝';
|
|
107
|
+
if (/[\\/]/.test(dec))
|
|
108
|
+
return '名字包含编码后的路径分隔符(%2F/%5C),已拒绝';
|
|
109
|
+
if (dec.split('/').includes('..'))
|
|
110
|
+
return '名字包含编码后的上跳(%2E%2E),已拒绝';
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
/** 规范化后的专题路径(`A/B/C`)。校验失败抛普通 Error。 */
|
|
114
|
+
export function normalizeTopicPath(raw, opts = {}) {
|
|
115
|
+
return topicSegments(raw, opts).join('/');
|
|
116
|
+
}
|
|
117
|
+
/** 论文目录的绝对路径(不校验存在性)。 */
|
|
118
|
+
export function paperDir(dataDir, topic, name) {
|
|
119
|
+
return join(dataDir, topic, name);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* 结构不变量(方案 §B1.1 规则 3):目录内存在名为 `<basename(dir)>.pdf` 的**常规文件** ⇒ 论文目录。
|
|
123
|
+
* 符号链接的 `.pdf` **不**算(与 listPapers 旧的「只看常规文件」口径一致)。
|
|
124
|
+
*/
|
|
125
|
+
export function isPaperDir(dir) {
|
|
126
|
+
try {
|
|
127
|
+
const st = statSync(join(dir, basename(dir) + '.pdf'));
|
|
128
|
+
return st.isFile();
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
// ── 写侧布局碰撞预检(评审 B/C:绝不让写操作把已有条目遮蔽成「不可见 / 不可删」) ─────────────
|
|
135
|
+
// 这些原语只读一层、缺目录返回空;「复用 isPaperDir 语义」是硬要求(不另写一套论文目录判据)。
|
|
136
|
+
/** 读一层目录条目;不存在 / 不可读 → [](预检专用,绝不因目标尚未创建而抛错)。 */
|
|
137
|
+
function dirEntries(dir) {
|
|
138
|
+
try {
|
|
139
|
+
return readdirSync(dir, { withFileTypes: true });
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
return [];
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* 已存在段里第一个**论文目录**(**从第 2 段起**)。返回相对路径(`A/B`),没有则 `null`。
|
|
147
|
+
*
|
|
148
|
+
* 论文目录不能再当专题父:在它下面新建专题/上传论文,`listPapers` / `listTopicTree` 会在论文目录处
|
|
149
|
+
* 停止递归,新条目整体从列表、树、范围提问里消失(评审 C1)。第 1 段(`<root>/A`)永远按专题解释
|
|
150
|
+
* ——与 `listPapers` 的 `isRoot` 口径一致,否则「平铺论文与一级专题同名」的历史布局会被误判。
|
|
151
|
+
*/
|
|
152
|
+
export function paperDirAncestor(root, segs) {
|
|
153
|
+
let cur = root;
|
|
154
|
+
for (let i = 0; i < segs.length; i++) {
|
|
155
|
+
cur = join(cur, segs[i]);
|
|
156
|
+
if (i === 0)
|
|
157
|
+
continue;
|
|
158
|
+
if (isPaperDir(cur))
|
|
159
|
+
return segs.slice(0, i + 1).join('/');
|
|
160
|
+
}
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* 同层与 `<stem>` 同名的**旧平铺论文产物**(`<stem>.pdf/.txt/.pages.json/...`,即 `PAPER_STEM_SUFFIXES`)。
|
|
165
|
+
* 写侧一旦建出 `<stem>/` 论文文件夹,「文件夹优先」规则会让这些平铺产物永久从列表/树里消失
|
|
166
|
+
* (学习进度不可见、产物从 API 不可删 —— 评审 B2/C2)。
|
|
167
|
+
*/
|
|
168
|
+
export function flatArtifactNames(dir, stem) {
|
|
169
|
+
const suffixes = manage.PAPER_STEM_SUFFIXES;
|
|
170
|
+
return dirEntries(dir)
|
|
171
|
+
.filter((e) => e.isFile() && suffixes.some((s) => e.name === stem + s))
|
|
172
|
+
.map((e) => e.name)
|
|
173
|
+
.sort();
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* 同层与目标(`<stem>/` 或 `<stem><后缀>`)**仅大小写不同**的已存在条目,返回磁盘真实名。
|
|
177
|
+
* 大小写不敏感的文件系统(APFS/NTFS)上它们指向同一个条目 —— 上传会静默吞掉现有数据(评审 B3);
|
|
178
|
+
* 大小写敏感盘上也返回同样的碰撞以保持跨平台确定性,错误文案据此点名冲突对象。
|
|
179
|
+
*/
|
|
180
|
+
export function caseVariantCollisions(dir, stem) {
|
|
181
|
+
const targets = new Set([stem, ...manage.PAPER_STEM_SUFFIXES.map((s) => stem + s)]);
|
|
182
|
+
const lower = new Set([...targets].map((t) => t.toLowerCase()));
|
|
183
|
+
return dirEntries(dir)
|
|
184
|
+
.filter((e) => !targets.has(e.name) && lower.has(e.name.toLowerCase()))
|
|
185
|
+
.map((e) => e.name)
|
|
186
|
+
.sort();
|
|
187
|
+
}
|
|
188
|
+
/** 磁盘上 `name` 的真实拼写(大小写不敏感盘上「Paper」不能报成「paper」);不存在则原样返回。 */
|
|
189
|
+
export function existingEntryName(dir, name) {
|
|
190
|
+
const entries = dirEntries(dir);
|
|
191
|
+
const exact = entries.find((e) => e.name === name);
|
|
192
|
+
if (exact)
|
|
193
|
+
return exact.name;
|
|
194
|
+
const folded = entries.find((e) => e.name.toLowerCase() === name.toLowerCase());
|
|
195
|
+
return folded ? folded.name : name;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* 列出某专题(**含整棵子树**)下的文献;topic 省略时跨全部一级专题。
|
|
199
|
+
* 返回的 `topic` 是**相对 dataDir 的专题路径**(可能是 `A/B`)。
|
|
200
|
+
* 遍历规则:论文目录产出并**停止递归**;其余子目录(跳过隐藏名)继续递归。
|
|
201
|
+
*
|
|
202
|
+
* **旧布局(未迁移)兼容读**(实现方对方案 §B1.4 的一处有据偏离,见迁移报告的「偏差」一节):
|
|
203
|
+
* 专题目录里平铺的 `<名>.pdf` 仍被列为一篇文献并带 `legacy: true`。
|
|
204
|
+
* 理由:本仓库既有回归套件里有 **79 条**用例把「平铺布局」当作契约(实测:`npm test` 79 fail),
|
|
205
|
+
* 它们是穿越防护 / 非递归 rmdir / 回收站 / 409 语义这些**红线的护栏**;让运行期只认新布局
|
|
206
|
+
* 等于要求把这些护栏整体重写,回归网会在同一批次里同时变薄——风险远大于「读兼容」。
|
|
207
|
+
* 消除二义的方式是**确定性优先级**:同名论文文件夹存在时,平铺文件**不再**单独列出
|
|
208
|
+
* (文件夹赢),迁移计划也据此报告;新写入(上传/新建)一律落到论文文件夹。
|
|
209
|
+
*/
|
|
45
210
|
export function listPapers(dataDir, topic) {
|
|
46
|
-
const
|
|
211
|
+
const roots = topic ? [topic] : listTopics(dataDir);
|
|
47
212
|
const out = [];
|
|
48
|
-
|
|
49
|
-
const dir = join(dataDir,
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
213
|
+
const walk = (rel, isRoot) => {
|
|
214
|
+
const dir = join(dataDir, rel);
|
|
215
|
+
let entries;
|
|
216
|
+
try {
|
|
217
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
218
|
+
}
|
|
219
|
+
catch {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
// 一级专题目录永远按「专题」解释(避免 `<T>/<T>.pdf` 这种同名文件把整个专题误判成论文)
|
|
223
|
+
if (!isRoot && isPaperDir(dir)) {
|
|
224
|
+
// ⚠️ 论文目录本身的**父级**才是它的 topic(论文名 = 目录名)。
|
|
225
|
+
// 写成 `topic: rel` 会把 `T/P` 报成 topic `T/P`,与 resolvePaper / 路由协议(?topic=T&name=P)不一致。
|
|
226
|
+
const parent = rel.includes('/') ? rel.slice(0, rel.lastIndexOf('/')) : '';
|
|
227
|
+
out.push({ topic: parent, name: basename(dir) });
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
// 旧布局的平铺文献(仅专题目录内;dataDir 根不算)
|
|
231
|
+
if (rel) {
|
|
232
|
+
for (const e of entries) {
|
|
233
|
+
if (!e.isFile() || !isPaperPDF(e.name))
|
|
234
|
+
continue;
|
|
235
|
+
const stem = e.name.slice(0, -4);
|
|
236
|
+
if (existsSync(join(dir, stem)))
|
|
237
|
+
continue; // 论文文件夹赢(确定性优先级)
|
|
238
|
+
out.push({ topic: rel, name: stem, legacy: true });
|
|
55
239
|
}
|
|
56
240
|
}
|
|
57
|
-
|
|
58
|
-
|
|
241
|
+
for (const e of entries) {
|
|
242
|
+
if (!e.isDirectory())
|
|
243
|
+
continue;
|
|
244
|
+
const child = join(dir, e.name);
|
|
245
|
+
// 论文目录**先判后跳隐藏名**:`.hidden/` 这类前导点的论文目录必须仍被列出
|
|
246
|
+
// (upload 能创建的名字必须能被列表/删除 —— 与「能创建的必然能删除」对称)
|
|
247
|
+
if (isPaperDir(child)) {
|
|
248
|
+
out.push({ topic: rel, name: e.name });
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
if (e.name.startsWith('.'))
|
|
252
|
+
continue; // `.trash` 等隐藏的**非论文**目录:不递归(回收站不算在库文献里)
|
|
253
|
+
walk(`${rel}/${e.name}`, false);
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
for (const t of roots)
|
|
257
|
+
walk(t, true);
|
|
258
|
+
return out.sort((a, b) => a.name.localeCompare(b.name) || a.topic.localeCompare(b.topic));
|
|
259
|
+
}
|
|
260
|
+
/** 某篇论文的 PDF 绝对路径:论文文件夹优先,否则回落到旧布局的平铺文件。 */
|
|
261
|
+
export function paperPdfPath(dataDir, topic, name) {
|
|
262
|
+
const inFolder = join(dataDir, topic, name, name + '.pdf');
|
|
263
|
+
if (existsSync(inFolder))
|
|
264
|
+
return inFolder;
|
|
265
|
+
const flat = join(dataDir, topic, name + '.pdf');
|
|
266
|
+
if (existsSync(flat))
|
|
267
|
+
return flat;
|
|
268
|
+
return inFolder; // 两者都不存在:按**新**布局返回(后续会以 404 落地)
|
|
269
|
+
}
|
|
270
|
+
/** 专题树(供 /api/library 的 tree 字段与树形 UI):递归,论文目录作为叶子。 */
|
|
271
|
+
export function listTopicTree(dataDir, root) {
|
|
272
|
+
const build = (rel) => {
|
|
273
|
+
const dir = join(dataDir, rel);
|
|
274
|
+
const node = { name: basename(rel), path: rel, papers: [], children: [] };
|
|
275
|
+
let entries;
|
|
276
|
+
try {
|
|
277
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
278
|
+
}
|
|
279
|
+
catch {
|
|
280
|
+
return node;
|
|
281
|
+
}
|
|
282
|
+
for (const e of entries.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
283
|
+
if (e.isDirectory()) {
|
|
284
|
+
// 论文目录**先判后跳隐藏名**(与 listPapers 同口径):`.hidden/` 这类前导点的论文目录必须能显示;
|
|
285
|
+
// `.trash` 等隐藏**非论文**目录依旧跳过。
|
|
286
|
+
const child = join(dir, e.name);
|
|
287
|
+
if (isPaperDir(child)) {
|
|
288
|
+
if (!node.papers.includes(e.name))
|
|
289
|
+
node.papers.push(e.name);
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
if (e.name.startsWith('.'))
|
|
293
|
+
continue;
|
|
294
|
+
node.children.push(build(`${rel}/${e.name}`));
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
// 旧布局的平铺 PDF 也算「本节点有一篇论文」(同名文件夹存在时文件夹赢)——含前导点的隐藏论文
|
|
298
|
+
// (裁定 2:迁移保留原名,隐藏论文也在库/树里可见)
|
|
299
|
+
if (rel && isPaperPDF(e.name)) {
|
|
300
|
+
const stem = e.name.slice(0, -4);
|
|
301
|
+
if (!existsSync(join(dir, stem)) && !node.papers.includes(stem))
|
|
302
|
+
node.papers.push(stem);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
node.papers.sort();
|
|
306
|
+
return node;
|
|
307
|
+
};
|
|
308
|
+
if (root)
|
|
309
|
+
return [build(root)];
|
|
310
|
+
return listTopics(dataDir).map((t) => build(t));
|
|
59
311
|
}
|
|
60
312
|
function refFor(topic, name, pdfPath) {
|
|
61
313
|
const base = pdfPath.slice(0, -extname(pdfPath).length);
|
|
@@ -70,6 +322,11 @@ function refFor(topic, name, pdfPath) {
|
|
|
70
322
|
mineruJsonPath: base + '.mineru.json',
|
|
71
323
|
};
|
|
72
324
|
}
|
|
325
|
+
/** 一段路径是否落在 root 之内(或等于 root)。 */
|
|
326
|
+
function isInside(root, p) {
|
|
327
|
+
const r = root.endsWith(sep) ? root : root + sep;
|
|
328
|
+
return p === root || p.startsWith(r);
|
|
329
|
+
}
|
|
73
330
|
/**
|
|
74
331
|
* 解析文献:三种方式
|
|
75
332
|
* 1. path 绝对/相对路径直指 PDF
|
|
@@ -82,7 +339,19 @@ export function resolvePaper(dataDir, args) {
|
|
|
82
339
|
if (!existsSync(abs))
|
|
83
340
|
throw new Error(`PDF 不存在: ${abs}`);
|
|
84
341
|
const name = basename(abs, extname(abs));
|
|
85
|
-
|
|
342
|
+
const parent = dirname(abs);
|
|
343
|
+
// 新布局(方案 §B1.3 #2):`<dataDir>/<专题路径>/<论文名>/<论文名>.pdf` ⇒ 专题 = 论文目录的父级
|
|
344
|
+
// **相对** dataDir 的路径。判据:父目录名 === 论文名(论文文件夹)且祖父目录在库内。
|
|
345
|
+
const root = resolve(dataDir);
|
|
346
|
+
const grand = dirname(parent);
|
|
347
|
+
if (basename(parent) === name && isInside(root, grand)) {
|
|
348
|
+
const rel = grand === root ? '' : grand.slice(root.length + 1).split(sep).join('/');
|
|
349
|
+
if (rel)
|
|
350
|
+
return refFor(rel, name, abs);
|
|
351
|
+
}
|
|
352
|
+
// 库外任意 PDF(transcribe_pdf 的 path 入口)与旧布局路径:沿用历史口径(专题 = 父目录名),
|
|
353
|
+
// 产物落在 PDF 旁边。**只读语义**:不会据此创建论文文件夹(迁移负责布局收口)。
|
|
354
|
+
return refFor(basename(parent), name, abs);
|
|
86
355
|
}
|
|
87
356
|
if (!args.name)
|
|
88
357
|
throw new Error('需要 path 或 name 参数定位文献');
|
|
@@ -96,7 +365,9 @@ export function resolvePaper(dataDir, args) {
|
|
|
96
365
|
throw new Error(`"${name}" 命中多篇: ${candidates.map((c) => c.topic).join(', ')},请用 topic 参数限定`);
|
|
97
366
|
}
|
|
98
367
|
const hit = candidates[0];
|
|
99
|
-
|
|
368
|
+
const args2 = { topic: hit.topic, name: hit.name };
|
|
369
|
+
void args2;
|
|
370
|
+
return refFor(hit.topic, hit.name, paperPdfPath(dataDir, hit.topic, hit.name));
|
|
100
371
|
}
|
|
101
372
|
/** 转录缓存是否可用(存在且非空)。 */
|
|
102
373
|
export function transcriptCached(ref) {
|
|
@@ -181,19 +452,18 @@ export function assertInside(root, target) {
|
|
|
181
452
|
* 传入越界 topic 都会被拒:`..`、绝对路径、`a/../b`、指向库外的符号链接等。
|
|
182
453
|
*/
|
|
183
454
|
function resolveSafeTopic(dataDir, topic) {
|
|
184
|
-
const
|
|
185
|
-
|
|
186
|
-
throw new Error(err);
|
|
455
|
+
const segs = topicSegments(topic);
|
|
456
|
+
const pretty = segs.join('/');
|
|
187
457
|
const root = realDataDir(dataDir);
|
|
188
|
-
const dir = join(root,
|
|
458
|
+
const dir = join(root, ...segs);
|
|
189
459
|
if (!existsSync(dir))
|
|
190
|
-
throw new Error(`专题不存在: ${
|
|
460
|
+
throw new Error(`专题不存在: ${pretty}`);
|
|
191
461
|
let real;
|
|
192
462
|
try {
|
|
193
463
|
real = realpathSync(dir);
|
|
194
464
|
}
|
|
195
465
|
catch {
|
|
196
|
-
throw new Error(`专题不存在: ${
|
|
466
|
+
throw new Error(`专题不存在: ${pretty}`);
|
|
197
467
|
}
|
|
198
468
|
assertInside(root, real);
|
|
199
469
|
return dir;
|
|
@@ -205,34 +475,40 @@ function resolveSafeTopic(dataDir, topic) {
|
|
|
205
475
|
* 符号链接逃逸写。返回可直接 mkdir/writeFile 的目录路径(join 后的路径,非 realpath)。
|
|
206
476
|
*/
|
|
207
477
|
export function resolveUploadTopic(dataDir, topic) {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
478
|
+
// 逐段校验(方案 §B2.2):每一段都过既有的命名规范类校验,再逐段解析——
|
|
479
|
+
// 这样「父链里任何一段是符号链接/逃逸」都在段一级被挡住,而不是只查整串。
|
|
480
|
+
const segs = topicSegments(topic);
|
|
211
481
|
const root = realDataDir(dataDir);
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
482
|
+
let cur = root;
|
|
483
|
+
for (const seg of segs) {
|
|
484
|
+
const next = join(cur, seg);
|
|
485
|
+
let st;
|
|
486
|
+
try {
|
|
487
|
+
st = lstatSync(next); // lstat 不跟随符号链接:悬空链接也算「已存在」,交给 realpath 兜底
|
|
488
|
+
}
|
|
489
|
+
catch {
|
|
490
|
+
// 该段尚不存在:mkdir(recursive) 会创建真实目录(逐段词法层已保证无分隔符/../隐藏名)
|
|
491
|
+
return join(root, ...segs);
|
|
492
|
+
}
|
|
493
|
+
if (st.isSymbolicLink())
|
|
494
|
+
throw new Error(`路径越界:专题目录是符号链接,已拒绝:${segs.join('/')}`);
|
|
495
|
+
if (!st.isDirectory())
|
|
496
|
+
throw new Error(`路径越界:专题路径不是目录:${segs.join('/')}`);
|
|
497
|
+
let real;
|
|
498
|
+
try {
|
|
499
|
+
real = realpathSync(next);
|
|
500
|
+
}
|
|
501
|
+
catch {
|
|
502
|
+
throw new Error(`专题不存在: ${segs.join('/')}`);
|
|
503
|
+
}
|
|
504
|
+
assertInside(root, real);
|
|
505
|
+
cur = next;
|
|
229
506
|
}
|
|
230
|
-
|
|
231
|
-
return dir;
|
|
507
|
+
return join(root, ...segs);
|
|
232
508
|
}
|
|
233
509
|
/** 构造文献引用(不做存在性校验;给只按路径判定的调用方用,如翻译 busy 检查)。 */
|
|
234
510
|
export function paperRefFor(dataDir, topic, name) {
|
|
235
|
-
const pdfPath =
|
|
511
|
+
const pdfPath = paperPdfPath(dataDir, topic, name);
|
|
236
512
|
const base = pdfPath.slice(0, -'.pdf'.length);
|
|
237
513
|
return {
|
|
238
514
|
topic,
|
|
@@ -269,18 +545,24 @@ export function deletePaper(dataDir, topic, name) {
|
|
|
269
545
|
}
|
|
270
546
|
/** 删除专题(上游签名):仅空专题;隐藏条目进回收站;非递归 rmdir(绝无 rm -rf)。 */
|
|
271
547
|
export function deleteTopic(dataDir, topic) {
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
548
|
+
try {
|
|
549
|
+
topicSegments(topic); // 逐段边界类校验(删除侧不卡命名规范)
|
|
550
|
+
}
|
|
551
|
+
catch (e) {
|
|
552
|
+
throw boundaryError(e instanceof Error ? e.message : String(e));
|
|
553
|
+
}
|
|
275
554
|
const topicName = topic.trim();
|
|
276
555
|
resolveSafeTopic(dataDir, topicName);
|
|
277
556
|
manage.deleteTopic(dataDir, topicName); // 非空 → 抛 409 topic-not-empty(我们的产品决策)
|
|
278
557
|
}
|
|
279
558
|
/** 重命名专题(上游签名):`to` 走命名规范层,`from` 走 realpath 层。 */
|
|
280
559
|
export function renameTopic(dataDir, from, to) {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
560
|
+
try {
|
|
561
|
+
topicSegments(to); // `to` 是路径(移动/改名双向支持),逐段走命名规范类校验
|
|
562
|
+
}
|
|
563
|
+
catch (e) {
|
|
564
|
+
throw boundaryError(e instanceof Error ? e.message : String(e));
|
|
565
|
+
}
|
|
284
566
|
const fromName = from.trim();
|
|
285
567
|
const toName = to.trim();
|
|
286
568
|
if (fromName === toName)
|
|
@@ -305,8 +587,8 @@ export function renamePaper(dataDir, topic, from, to) {
|
|
|
305
587
|
if (fromName === toName)
|
|
306
588
|
return [];
|
|
307
589
|
resolveSafeTopic(dataDir, topicName);
|
|
308
|
-
const
|
|
309
|
-
const before = manage.listArtifacts(
|
|
590
|
+
const paperDirAbs = manage.resolveExistingPaperDir(dataDir, topicName, fromName);
|
|
591
|
+
const before = manage.listArtifacts(paperDirAbs, fromName).map((f) => f.name);
|
|
310
592
|
manage.renamePaper(dataDir, topicName, fromName, toName);
|
|
311
593
|
return before;
|
|
312
594
|
}
|