@xiaofanzhao/dsh-plugin-knowledge-base 0.0.0-stage → 0.2.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.
@@ -0,0 +1,217 @@
1
+ // ============================================================================
2
+ // 原版式预览:用宿主 officeKit(LibreOffice)把 Office 文档转成 PDF。
3
+ //
4
+ // · 转换结果落在库目录 files/<docId>.preview.pdf,源文件更新(mtime 变新)才重转
5
+ // · 同一文档的并发请求共用一次转换
6
+ // · officeKit 不存在时抛 code='unavailable',界面降级到"文字阅读"
7
+ // ============================================================================
8
+ import fs from 'node:fs';
9
+ import path from 'node:path';
10
+
11
+ export const OFFICE_EXTENSIONS = new Set(['doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx']);
12
+ export const IMAGE_EXTENSIONS = new Set(['png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp', 'svg']);
13
+
14
+ function unavailable(message) {
15
+ const error = new Error(message);
16
+ error.code = 'unavailable';
17
+ return error;
18
+ }
19
+
20
+ export function createPreviewer({ store, getOfficeKit, logger }) {
21
+ const jobs = new Map();
22
+
23
+ function pathOf(node) {
24
+ return path.join(store.filesDir, `${node.id}.preview.pdf`);
25
+ }
26
+
27
+ function available() {
28
+ try {
29
+ return typeof getOfficeKit?.()?.convert === 'function';
30
+ } catch {
31
+ return false;
32
+ }
33
+ }
34
+
35
+ /** 已生成且比源文件新,就不用再转。 */
36
+ function cached(node, sourcePath) {
37
+ const out = pathOf(node);
38
+ try {
39
+ return fs.existsSync(out) && fs.statSync(out).mtimeMs >= fs.statSync(sourcePath).mtimeMs ? out : '';
40
+ } catch {
41
+ return '';
42
+ }
43
+ }
44
+
45
+ async function ensure(node, { priority = 'foreground' } = {}) {
46
+ if (!OFFICE_EXTENSIONS.has(node.ext)) {
47
+ const error = new Error('这个格式不需要 Office 转换');
48
+ error.code = 'unsupported';
49
+ throw error;
50
+ }
51
+ const kit = getOfficeKit?.();
52
+ if (!kit || typeof kit.convert !== 'function') {
53
+ throw unavailable('这台机器没有可用的 Office 转换服务(officeKit),没法生成原版式预览');
54
+ }
55
+ const sourcePath = store.raw.q(`SELECT file_path AS filePath FROM nodes WHERE id = ?`).get(node.id)?.filePath;
56
+ if (!sourcePath || !fs.existsSync(sourcePath)) {
57
+ const error = new Error('原始文件不见了,无法生成预览');
58
+ error.code = 'gone';
59
+ throw error;
60
+ }
61
+ const hit = cached(node, sourcePath);
62
+ if (hit) return hit;
63
+
64
+ const running = jobs.get(node.id);
65
+ if (running) return running;
66
+
67
+ const job = (async () => {
68
+ const stat = fs.statSync(sourcePath);
69
+ const version = `${stat.size}-${Math.round(stat.mtimeMs)}`;
70
+ const result = await kit.convert({
71
+ extension: node.ext,
72
+ priority,
73
+ source: {
74
+ key: `${node.id}:${version}`,
75
+ version,
76
+ bytes: stat.size,
77
+ read: async () => ({ bytes: new Uint8Array(await fs.promises.readFile(sourcePath)), version }),
78
+ },
79
+ });
80
+ const pdf = result?.pdf ?? result;
81
+ if (!pdf || typeof pdf.length !== 'number' || !pdf.length) {
82
+ throw new Error('转换没有返回 PDF 内容');
83
+ }
84
+ const out = pathOf(node);
85
+ await fs.promises.writeFile(out, Buffer.from(pdf));
86
+ logger?.info?.(`[knowledge-base] preview generated for ${node.name} (${Math.round(pdf.length / 1024)} KB)`);
87
+ return out;
88
+ })().finally(() => jobs.delete(node.id));
89
+
90
+ jobs.set(node.id, job);
91
+ return job;
92
+ }
93
+
94
+ function invalidate(node) {
95
+ try { fs.rmSync(pathOf(node), { force: true }); } catch { /* ignore */ }
96
+ for (const suffix of ['.pages', '.thumbs']) {
97
+ try { fs.rmSync(path.join(store.filesDir, `${node.id}${suffix}`), { recursive: true, force: true }); } catch { /* ignore */ }
98
+ }
99
+ }
100
+
101
+ // ── 逐页图片:自己画阅读器,避免把浏览器 PDF 阅读器(带缩略图栏)塞进页面 ──
102
+ // 两套尺寸:正文 96 DPI(清晰),左侧缩略图 24 DPI(小、能一次铺满列表)
103
+ const PAGE_DPI = 96;
104
+ const THUMB_DPI = 24;
105
+ const MAX_PAGES = 200;
106
+
107
+ const setDirOf = (node, suffix) => path.join(store.filesDir, `${node.id}${suffix}`);
108
+ const PAGE_SET = { dpi: PAGE_DPI, suffix: '.pages', maxPages: MAX_PAGES };
109
+ const THUMB_SET = { dpi: THUMB_DPI, suffix: '.thumbs', maxPages: MAX_PAGES };
110
+
111
+ function readManifest(node, spec, pdfMtime) {
112
+ try {
113
+ const raw = JSON.parse(fs.readFileSync(path.join(setDirOf(node, spec.suffix), 'manifest.json'), 'utf8'));
114
+ if (raw && raw.dpi === spec.dpi && raw.pdfMtime === pdfMtime && Number.isInteger(raw.pages) && raw.pages > 0) return raw;
115
+ return null;
116
+ } catch {
117
+ return null;
118
+ }
119
+ }
120
+
121
+ const setJobs = new Map();
122
+
123
+ async function renderPageSet(node, spec) {
124
+ const pdfPath = await ensure(node); // 先保证有 PDF
125
+ const pdfMtime = Math.round(fs.statSync(pdfPath).mtimeMs);
126
+ const cachedManifest = readManifest(node, spec, pdfMtime);
127
+ if (cachedManifest) return cachedManifest;
128
+
129
+ const jobKey = `${node.id}:${spec.suffix}`;
130
+ const running = setJobs.get(jobKey);
131
+ if (running) return running;
132
+
133
+ const kit = getOfficeKit?.();
134
+ if (!kit || typeof kit.renderImages !== 'function') {
135
+ throw unavailable('这台机器没有可用的 Office 渲染服务,没法生成逐页预览');
136
+ }
137
+
138
+ const job = (async () => {
139
+ const stat = fs.statSync(pdfPath);
140
+ const version = `${stat.size}-${Math.round(stat.mtimeMs)}`;
141
+ const request = (extra = {}) => kit.renderImages({
142
+ extension: 'pdf',
143
+ dpi: spec.dpi,
144
+ maxImages: spec.maxPages,
145
+ // officeKit 的上限:每张图 ≤ 16777216 像素、边长 ≤ 4096
146
+ maxPixels: 16_000_000,
147
+ maxDimension: 4096,
148
+ maxBytes: 240 * 1024 * 1024,
149
+ source: {
150
+ key: `${node.id}:pdf:${version}`,
151
+ version,
152
+ bytes: stat.size,
153
+ read: async () => ({ bytes: new Uint8Array(await fs.promises.readFile(pdfPath)), version }),
154
+ },
155
+ ...extra,
156
+ });
157
+
158
+ let result;
159
+ let truncated = false;
160
+ try {
161
+ result = await request();
162
+ } catch (error) {
163
+ const message = error instanceof Error ? error.message : String(error);
164
+ // 页数超过上限:退化成只渲染前 maxPages 页
165
+ if (/maxPages/i.test(message)) {
166
+ result = await request({ pages: Array.from({ length: spec.maxPages }, (_, index) => index + 1) });
167
+ truncated = true;
168
+ } else {
169
+ throw error;
170
+ }
171
+ }
172
+ const images = result?.images ?? [];
173
+ if (!images.length) throw new Error('渲染没有返回任何页面');
174
+ const dir = setDirOf(node, spec.suffix);
175
+ fs.rmSync(dir, { recursive: true, force: true });
176
+ fs.mkdirSync(dir, { recursive: true });
177
+ images.forEach((image, index) => {
178
+ const page = Number(image.page) || index + 1;
179
+ fs.writeFileSync(path.join(dir, `p${page}.png`), Buffer.from(image.png));
180
+ });
181
+ const manifest = {
182
+ dpi: spec.dpi,
183
+ pages: images.length,
184
+ pageCount: Number(result.pageCount) || images.length,
185
+ truncated,
186
+ pdfMtime,
187
+ width: images[0]?.width ?? 0,
188
+ height: images[0]?.height ?? 0,
189
+ generatedAt: Date.now(),
190
+ };
191
+ fs.writeFileSync(path.join(dir, 'manifest.json'), JSON.stringify(manifest));
192
+ logger?.info?.(`[knowledge-base] rendered ${images.length} ${spec.suffix} pages for ${node.name}`
193
+ + `${truncated ? ` (truncated from ${manifest.pageCount})` : ''}`);
194
+ return manifest;
195
+ })().finally(() => setJobs.delete(jobKey));
196
+
197
+ setJobs.set(jobKey, job);
198
+ return job;
199
+ }
200
+
201
+ const ensurePages = (node) => renderPageSet(node, PAGE_SET);
202
+ const ensureThumbs = (node) => renderPageSet(node, THUMB_SET);
203
+ const pageFile = (node, page) => path.join(setDirOf(node, '.pages'), `p${Number(page)}.png`);
204
+ const thumbFile = (node, page) => path.join(setDirOf(node, '.thumbs'), `p${Number(page)}.png`);
205
+
206
+ return {
207
+ ensure,
208
+ ensurePages,
209
+ ensureThumbs,
210
+ pageFile,
211
+ thumbFile,
212
+ available,
213
+ invalidate,
214
+ pathOf,
215
+ supports: (ext) => OFFICE_EXTENSIONS.has(ext),
216
+ };
217
+ }