dsh-learning-quirky-stuff 4.3.1 → 4.6.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/ARCHITECTURE.md +29 -2
- package/README.md +72 -4
- package/THIRD_PARTY_NOTICES.md +5 -0
- package/capture-server.js +4 -0
- package/client.js +2299 -1242
- package/codex-provider.js +3 -4
- package/core.js +3 -0
- package/docs/dependency-security.md +1 -1
- package/docs/install.md +5 -5
- package/docs/privacy.md +9 -1
- package/docs/user-guide.html +106 -23
- package/docs/validation.md +28 -0
- package/graph-data.js +2 -0
- package/learning.js +79 -14
- package/licenses/packages/pdfjs-dist@6.5.438.txt +181 -0
- package/model.js +1 -1
- package/package.json +11 -6
- package/pdf-assets/LICENSE +177 -0
- package/pdf-assets/cmaps/78-EUC-H.bcmap +0 -0
- package/pdf-assets/cmaps/78-EUC-V.bcmap +0 -0
- package/pdf-assets/cmaps/78-H.bcmap +0 -0
- package/pdf-assets/cmaps/78-RKSJ-H.bcmap +0 -0
- package/pdf-assets/cmaps/78-RKSJ-V.bcmap +0 -0
- package/pdf-assets/cmaps/78-V.bcmap +0 -0
- package/pdf-assets/cmaps/78ms-RKSJ-H.bcmap +0 -0
- package/pdf-assets/cmaps/78ms-RKSJ-V.bcmap +0 -0
- package/pdf-assets/cmaps/83pv-RKSJ-H.bcmap +0 -0
- package/pdf-assets/cmaps/90ms-RKSJ-H.bcmap +0 -0
- package/pdf-assets/cmaps/90ms-RKSJ-V.bcmap +0 -0
- package/pdf-assets/cmaps/90msp-RKSJ-H.bcmap +0 -0
- package/pdf-assets/cmaps/90msp-RKSJ-V.bcmap +0 -0
- package/pdf-assets/cmaps/90pv-RKSJ-H.bcmap +0 -0
- package/pdf-assets/cmaps/90pv-RKSJ-V.bcmap +0 -0
- package/pdf-assets/cmaps/Add-H.bcmap +0 -0
- package/pdf-assets/cmaps/Add-RKSJ-H.bcmap +0 -0
- package/pdf-assets/cmaps/Add-RKSJ-V.bcmap +0 -0
- package/pdf-assets/cmaps/Add-V.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-CNS1-0.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-CNS1-1.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-CNS1-2.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-CNS1-3.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-CNS1-4.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-CNS1-5.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-CNS1-6.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-CNS1-UCS2.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-GB1-0.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-GB1-1.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-GB1-2.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-GB1-3.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-GB1-4.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-GB1-5.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-GB1-UCS2.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-Japan1-0.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-Japan1-1.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-Japan1-2.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-Japan1-3.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-Japan1-4.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-Japan1-5.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-Japan1-6.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-Japan1-UCS2.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-Korea1-0.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-Korea1-1.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-Korea1-2.bcmap +0 -0
- package/pdf-assets/cmaps/Adobe-Korea1-UCS2.bcmap +0 -0
- package/pdf-assets/cmaps/B5-H.bcmap +0 -0
- package/pdf-assets/cmaps/B5-V.bcmap +0 -0
- package/pdf-assets/cmaps/B5pc-H.bcmap +0 -0
- package/pdf-assets/cmaps/B5pc-V.bcmap +0 -0
- package/pdf-assets/cmaps/CNS-EUC-H.bcmap +0 -0
- package/pdf-assets/cmaps/CNS-EUC-V.bcmap +0 -0
- package/pdf-assets/cmaps/CNS1-H.bcmap +0 -0
- package/pdf-assets/cmaps/CNS1-V.bcmap +0 -0
- package/pdf-assets/cmaps/CNS2-H.bcmap +0 -0
- package/pdf-assets/cmaps/CNS2-V.bcmap +3 -0
- package/pdf-assets/cmaps/ETHK-B5-H.bcmap +0 -0
- package/pdf-assets/cmaps/ETHK-B5-V.bcmap +0 -0
- package/pdf-assets/cmaps/ETen-B5-H.bcmap +0 -0
- package/pdf-assets/cmaps/ETen-B5-V.bcmap +0 -0
- package/pdf-assets/cmaps/ETenms-B5-H.bcmap +3 -0
- package/pdf-assets/cmaps/ETenms-B5-V.bcmap +0 -0
- package/pdf-assets/cmaps/EUC-H.bcmap +0 -0
- package/pdf-assets/cmaps/EUC-V.bcmap +0 -0
- package/pdf-assets/cmaps/Ext-H.bcmap +0 -0
- package/pdf-assets/cmaps/Ext-RKSJ-H.bcmap +0 -0
- package/pdf-assets/cmaps/Ext-RKSJ-V.bcmap +0 -0
- package/pdf-assets/cmaps/Ext-V.bcmap +0 -0
- package/pdf-assets/cmaps/GB-EUC-H.bcmap +0 -0
- package/pdf-assets/cmaps/GB-EUC-V.bcmap +0 -0
- package/pdf-assets/cmaps/GB-H.bcmap +4 -0
- package/pdf-assets/cmaps/GB-V.bcmap +0 -0
- package/pdf-assets/cmaps/GBK-EUC-H.bcmap +0 -0
- package/pdf-assets/cmaps/GBK-EUC-V.bcmap +0 -0
- package/pdf-assets/cmaps/GBK2K-H.bcmap +0 -0
- package/pdf-assets/cmaps/GBK2K-V.bcmap +0 -0
- package/pdf-assets/cmaps/GBKp-EUC-H.bcmap +0 -0
- package/pdf-assets/cmaps/GBKp-EUC-V.bcmap +0 -0
- package/pdf-assets/cmaps/GBT-EUC-H.bcmap +0 -0
- package/pdf-assets/cmaps/GBT-EUC-V.bcmap +0 -0
- package/pdf-assets/cmaps/GBT-H.bcmap +0 -0
- package/pdf-assets/cmaps/GBT-V.bcmap +0 -0
- package/pdf-assets/cmaps/GBTpc-EUC-H.bcmap +0 -0
- package/pdf-assets/cmaps/GBTpc-EUC-V.bcmap +0 -0
- package/pdf-assets/cmaps/GBpc-EUC-H.bcmap +0 -0
- package/pdf-assets/cmaps/GBpc-EUC-V.bcmap +0 -0
- package/pdf-assets/cmaps/H.bcmap +0 -0
- package/pdf-assets/cmaps/HKdla-B5-H.bcmap +0 -0
- package/pdf-assets/cmaps/HKdla-B5-V.bcmap +0 -0
- package/pdf-assets/cmaps/HKdlb-B5-H.bcmap +0 -0
- package/pdf-assets/cmaps/HKdlb-B5-V.bcmap +0 -0
- package/pdf-assets/cmaps/HKgccs-B5-H.bcmap +0 -0
- package/pdf-assets/cmaps/HKgccs-B5-V.bcmap +0 -0
- package/pdf-assets/cmaps/HKm314-B5-H.bcmap +0 -0
- package/pdf-assets/cmaps/HKm314-B5-V.bcmap +0 -0
- package/pdf-assets/cmaps/HKm471-B5-H.bcmap +0 -0
- package/pdf-assets/cmaps/HKm471-B5-V.bcmap +0 -0
- package/pdf-assets/cmaps/HKscs-B5-H.bcmap +0 -0
- package/pdf-assets/cmaps/HKscs-B5-V.bcmap +0 -0
- package/pdf-assets/cmaps/Hankaku.bcmap +0 -0
- package/pdf-assets/cmaps/Hiragana.bcmap +0 -0
- package/pdf-assets/cmaps/KSC-EUC-H.bcmap +0 -0
- package/pdf-assets/cmaps/KSC-EUC-V.bcmap +0 -0
- package/pdf-assets/cmaps/KSC-H.bcmap +0 -0
- package/pdf-assets/cmaps/KSC-Johab-H.bcmap +0 -0
- package/pdf-assets/cmaps/KSC-Johab-V.bcmap +0 -0
- package/pdf-assets/cmaps/KSC-V.bcmap +0 -0
- package/pdf-assets/cmaps/KSCms-UHC-H.bcmap +0 -0
- package/pdf-assets/cmaps/KSCms-UHC-HW-H.bcmap +0 -0
- package/pdf-assets/cmaps/KSCms-UHC-HW-V.bcmap +0 -0
- package/pdf-assets/cmaps/KSCms-UHC-V.bcmap +0 -0
- package/pdf-assets/cmaps/KSCpc-EUC-H.bcmap +0 -0
- package/pdf-assets/cmaps/KSCpc-EUC-V.bcmap +0 -0
- package/pdf-assets/cmaps/Katakana.bcmap +0 -0
- package/pdf-assets/cmaps/LICENSE +36 -0
- package/pdf-assets/cmaps/NWP-H.bcmap +0 -0
- package/pdf-assets/cmaps/NWP-V.bcmap +0 -0
- package/pdf-assets/cmaps/RKSJ-H.bcmap +0 -0
- package/pdf-assets/cmaps/RKSJ-V.bcmap +0 -0
- package/pdf-assets/cmaps/Roman.bcmap +0 -0
- package/pdf-assets/cmaps/UniCNS-UCS2-H.bcmap +0 -0
- package/pdf-assets/cmaps/UniCNS-UCS2-V.bcmap +0 -0
- package/pdf-assets/cmaps/UniCNS-UTF16-H.bcmap +0 -0
- package/pdf-assets/cmaps/UniCNS-UTF16-V.bcmap +0 -0
- package/pdf-assets/cmaps/UniCNS-UTF32-H.bcmap +0 -0
- package/pdf-assets/cmaps/UniCNS-UTF32-V.bcmap +0 -0
- package/pdf-assets/cmaps/UniCNS-UTF8-H.bcmap +0 -0
- package/pdf-assets/cmaps/UniCNS-UTF8-V.bcmap +0 -0
- package/pdf-assets/cmaps/UniGB-UCS2-H.bcmap +0 -0
- package/pdf-assets/cmaps/UniGB-UCS2-V.bcmap +0 -0
- package/pdf-assets/cmaps/UniGB-UTF16-H.bcmap +0 -0
- package/pdf-assets/cmaps/UniGB-UTF16-V.bcmap +0 -0
- package/pdf-assets/cmaps/UniGB-UTF32-H.bcmap +0 -0
- package/pdf-assets/cmaps/UniGB-UTF32-V.bcmap +0 -0
- package/pdf-assets/cmaps/UniGB-UTF8-H.bcmap +0 -0
- package/pdf-assets/cmaps/UniGB-UTF8-V.bcmap +0 -0
- package/pdf-assets/cmaps/UniJIS-UCS2-H.bcmap +0 -0
- package/pdf-assets/cmaps/UniJIS-UCS2-HW-H.bcmap +0 -0
- package/pdf-assets/cmaps/UniJIS-UCS2-HW-V.bcmap +0 -0
- package/pdf-assets/cmaps/UniJIS-UCS2-V.bcmap +0 -0
- package/pdf-assets/cmaps/UniJIS-UTF16-H.bcmap +0 -0
- package/pdf-assets/cmaps/UniJIS-UTF16-V.bcmap +0 -0
- package/pdf-assets/cmaps/UniJIS-UTF32-H.bcmap +0 -0
- package/pdf-assets/cmaps/UniJIS-UTF32-V.bcmap +0 -0
- package/pdf-assets/cmaps/UniJIS-UTF8-H.bcmap +0 -0
- package/pdf-assets/cmaps/UniJIS-UTF8-V.bcmap +0 -0
- package/pdf-assets/cmaps/UniJIS2004-UTF16-H.bcmap +0 -0
- package/pdf-assets/cmaps/UniJIS2004-UTF16-V.bcmap +0 -0
- package/pdf-assets/cmaps/UniJIS2004-UTF32-H.bcmap +0 -0
- package/pdf-assets/cmaps/UniJIS2004-UTF32-V.bcmap +0 -0
- package/pdf-assets/cmaps/UniJIS2004-UTF8-H.bcmap +0 -0
- package/pdf-assets/cmaps/UniJIS2004-UTF8-V.bcmap +0 -0
- package/pdf-assets/cmaps/UniJISPro-UCS2-HW-V.bcmap +0 -0
- package/pdf-assets/cmaps/UniJISPro-UCS2-V.bcmap +0 -0
- package/pdf-assets/cmaps/UniJISPro-UTF8-V.bcmap +0 -0
- package/pdf-assets/cmaps/UniJISX0213-UTF32-H.bcmap +0 -0
- package/pdf-assets/cmaps/UniJISX0213-UTF32-V.bcmap +0 -0
- package/pdf-assets/cmaps/UniJISX02132004-UTF32-H.bcmap +0 -0
- package/pdf-assets/cmaps/UniJISX02132004-UTF32-V.bcmap +0 -0
- package/pdf-assets/cmaps/UniKS-UCS2-H.bcmap +0 -0
- package/pdf-assets/cmaps/UniKS-UCS2-V.bcmap +0 -0
- package/pdf-assets/cmaps/UniKS-UTF16-H.bcmap +0 -0
- package/pdf-assets/cmaps/UniKS-UTF16-V.bcmap +0 -0
- package/pdf-assets/cmaps/UniKS-UTF32-H.bcmap +0 -0
- package/pdf-assets/cmaps/UniKS-UTF32-V.bcmap +0 -0
- package/pdf-assets/cmaps/UniKS-UTF8-H.bcmap +0 -0
- package/pdf-assets/cmaps/UniKS-UTF8-V.bcmap +0 -0
- package/pdf-assets/cmaps/V.bcmap +0 -0
- package/pdf-assets/cmaps/WP-Symbol.bcmap +0 -0
- package/pdf-assets/iccs/CGATS001Compat-v2-micro.icc +0 -0
- package/pdf-assets/iccs/LICENSE +116 -0
- package/pdf-assets/pdf.worker.min.mjs +27 -0
- package/pdf-assets/standard_fonts/FoxitDingbats.pfb +0 -0
- package/pdf-assets/standard_fonts/FoxitFixed.pfb +0 -0
- package/pdf-assets/standard_fonts/FoxitFixedBold.pfb +0 -0
- package/pdf-assets/standard_fonts/FoxitFixedBoldItalic.pfb +0 -0
- package/pdf-assets/standard_fonts/FoxitFixedItalic.pfb +0 -0
- package/pdf-assets/standard_fonts/FoxitSerif.pfb +0 -0
- package/pdf-assets/standard_fonts/FoxitSerifBold.pfb +0 -0
- package/pdf-assets/standard_fonts/FoxitSerifBoldItalic.pfb +0 -0
- package/pdf-assets/standard_fonts/FoxitSerifItalic.pfb +0 -0
- package/pdf-assets/standard_fonts/FoxitSymbol.pfb +0 -0
- package/pdf-assets/standard_fonts/LICENSE_FOXIT +27 -0
- package/pdf-assets/standard_fonts/LICENSE_LIBERATION +363 -0
- package/pdf-assets/standard_fonts/LiberationSans-Bold.ttf +0 -0
- package/pdf-assets/standard_fonts/LiberationSans-BoldItalic.ttf +0 -0
- package/pdf-assets/standard_fonts/LiberationSans-Italic.ttf +0 -0
- package/pdf-assets/standard_fonts/LiberationSans-Regular.ttf +0 -0
- package/pdf-assets/wasm/LICENSE_JBIG2 +196 -0
- package/pdf-assets/wasm/LICENSE_OPENJPEG +39 -0
- package/pdf-assets/wasm/LICENSE_PDFJS_JBIG2 +13 -0
- package/pdf-assets/wasm/LICENSE_PDFJS_OPENJPEG +22 -0
- package/pdf-assets/wasm/LICENSE_PDFJS_QCMS +19 -0
- package/pdf-assets/wasm/LICENSE_PDFJS_QUICKJS +21 -0
- package/pdf-assets/wasm/LICENSE_QCMS +21 -0
- package/pdf-assets/wasm/LICENSE_QUICKJS +22 -0
- package/pdf-assets/wasm/jbig2.wasm +0 -0
- package/pdf-assets/wasm/jbig2_nowasm_fallback.js +2 -0
- package/pdf-assets/wasm/openjpeg.wasm +0 -0
- package/pdf-assets/wasm/openjpeg_nowasm_fallback.js +2 -0
- package/pdf-assets/wasm/qcms_bg.wasm +0 -0
- package/pdf-assets/wasm/quickjs-eval.js +16 -0
- package/pdf-assets/wasm/quickjs-eval.wasm +0 -0
- package/server/infographic-sources.js +3 -0
- package/server/modules/canvas-comments.js +270 -0
- package/server/modules/pdf.js +606 -0
- package/server/runtime.js +25 -4
- package/server/service-actions.js +9 -1
- package/server/sources.js +26 -9
- package/server/summary/prompts.js +40 -0
- package/server/task-manager.js +2 -2
- package/shared/canvas-comments.js +59 -0
- package/shared/canvas-quotes.js +50 -0
- package/shared/features.js +14 -0
- package/shared/infographics.js +5 -5
- package/shared/pdf.js +75 -0
- package/shared/version.js +1 -1
- package/summary-prompt.js +12 -36
- package/summary-sources.js +13 -1
|
@@ -0,0 +1,606 @@
|
|
|
1
|
+
import { randomUUID, createHash } from 'node:crypto';
|
|
2
|
+
import { mkdir, readFile, writeFile, open, rename, rm, readdir, stat } from 'node:fs/promises';
|
|
3
|
+
import { join, resolve, dirname } from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { assertId, atomicWrite } from '../../core.js';
|
|
6
|
+
import { JsonRepository } from '../lib/storage.js';
|
|
7
|
+
import { requiredText, optionalText, digest, cancelled } from '../lib/values.js';
|
|
8
|
+
import {
|
|
9
|
+
PDF_CHUNK_BYTES,
|
|
10
|
+
PDF_MAX_BYTES,
|
|
11
|
+
validPdfRect,
|
|
12
|
+
normalizePdfText,
|
|
13
|
+
pdfReference,
|
|
14
|
+
pdfCommentElement,
|
|
15
|
+
pdfSelectionText,
|
|
16
|
+
} from '../../shared/pdf.js';
|
|
17
|
+
|
|
18
|
+
/** PDFs are immutable shared assets. Links live on notes; annotations belong to each note. */
|
|
19
|
+
export class PdfModule {
|
|
20
|
+
constructor(context) {
|
|
21
|
+
Object.assign(this, context);
|
|
22
|
+
this.id = 'pdf';
|
|
23
|
+
this.documents = new JsonRepository(this.store.root, 'pdf-documents');
|
|
24
|
+
this.notebooks = new JsonRepository(this.store.root, 'pdf-notebooks');
|
|
25
|
+
this.pages = new JsonRepository(this.store.root, 'pdf-pages');
|
|
26
|
+
this.uploads = new JsonRepository(this.store.root, 'pdf-uploads');
|
|
27
|
+
this.filesRoot = join(this.store.root, 'modules', 'pdf-files');
|
|
28
|
+
this.assetRoot = fileURLToPath(new URL('../../pdf-assets/', import.meta.url));
|
|
29
|
+
this.tail = Promise.resolve();
|
|
30
|
+
}
|
|
31
|
+
serial(fn) {
|
|
32
|
+
const result = this.tail.then(fn);
|
|
33
|
+
this.tail = result.catch(() => {});
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
async init() {
|
|
37
|
+
for (const repo of [this.documents, this.notebooks, this.pages, this.uploads]) await repo.init();
|
|
38
|
+
await mkdir(this.filesRoot, { recursive: true, mode: 0o700 });
|
|
39
|
+
for (const upload of await this.uploads.list())
|
|
40
|
+
if (Date.now() - Date.parse(upload.createdAt) > 86400000) await this.clearUpload(upload.id);
|
|
41
|
+
}
|
|
42
|
+
register(router) {
|
|
43
|
+
router.register('pdf.backupRead', (request) => this.backupRead(request));
|
|
44
|
+
for (const method of [
|
|
45
|
+
'list',
|
|
46
|
+
'catalog',
|
|
47
|
+
'beginUpload',
|
|
48
|
+
'uploadChunk',
|
|
49
|
+
'finishUpload',
|
|
50
|
+
'cancelUpload',
|
|
51
|
+
'read',
|
|
52
|
+
'info',
|
|
53
|
+
'indexPage',
|
|
54
|
+
'annotate',
|
|
55
|
+
'position',
|
|
56
|
+
'link',
|
|
57
|
+
'unlink',
|
|
58
|
+
'asset',
|
|
59
|
+
'recognize',
|
|
60
|
+
])
|
|
61
|
+
router.register('pdf.' + method, (request) => this[method](request), 'pdf');
|
|
62
|
+
}
|
|
63
|
+
async note(noteId) {
|
|
64
|
+
const note = await this.store.read(noteId);
|
|
65
|
+
if (note.deletedAt) throw new Error('请先恢复笔记');
|
|
66
|
+
return note;
|
|
67
|
+
}
|
|
68
|
+
async linked(noteId, documentId) {
|
|
69
|
+
const note = await this.note(noteId),
|
|
70
|
+
id = assertId(documentId);
|
|
71
|
+
if (!note.pdfDocuments?.some((d) => d.id === id)) throw new Error('这份 PDF 尚未关联到当前笔记');
|
|
72
|
+
const document = await this.documents.get(id);
|
|
73
|
+
return { note, document };
|
|
74
|
+
}
|
|
75
|
+
async list({ noteId }) {
|
|
76
|
+
const note = await this.note(noteId),
|
|
77
|
+
documents = [];
|
|
78
|
+
for (const link of note.pdfDocuments || [])
|
|
79
|
+
documents.push({ ...(await this.documents.get(link.id)), linkedAt: link.linkedAt });
|
|
80
|
+
return {
|
|
81
|
+
documents,
|
|
82
|
+
notebook: (await this.notebooks.get(noteId, { optional: true })) || {
|
|
83
|
+
id: noteId,
|
|
84
|
+
annotations: [],
|
|
85
|
+
positions: {},
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
async catalog({ noteId }) {
|
|
90
|
+
await this.note(noteId);
|
|
91
|
+
const { boards } = await this.store.all(),
|
|
92
|
+
live = boards.filter((b) => !b.deletedAt),
|
|
93
|
+
result = [];
|
|
94
|
+
for (const document of await this.documents.list()) {
|
|
95
|
+
const notes = live.filter((b) =>
|
|
96
|
+
[...(b.pdfDocuments || []).map((d) => d.id), ...(b.pdfHistory || [])].includes(document.id),
|
|
97
|
+
);
|
|
98
|
+
if (notes.length)
|
|
99
|
+
result.push({
|
|
100
|
+
...document,
|
|
101
|
+
notes: notes.map((b) => ({ id: b.id, title: b.title })),
|
|
102
|
+
linked: notes.some((b) => b.id === noteId && b.pdfDocuments?.some((d) => d.id === document.id)),
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
return result;
|
|
106
|
+
}
|
|
107
|
+
async addLink(noteId, document) {
|
|
108
|
+
return this.store.serial(async () => {
|
|
109
|
+
const note = await this.note(noteId);
|
|
110
|
+
if (!note.pdfDocuments?.some((d) => d.id === document.id)) {
|
|
111
|
+
note.pdfDocuments = [
|
|
112
|
+
...(note.pdfDocuments || []),
|
|
113
|
+
{ id: document.id, title: document.name, linkedAt: new Date().toISOString() },
|
|
114
|
+
];
|
|
115
|
+
note.pdfHistory = (note.pdfHistory || []).filter((id) => id !== document.id);
|
|
116
|
+
note.updatedAt = new Date().toISOString();
|
|
117
|
+
await atomicWrite(this.store.boardPath(noteId), note);
|
|
118
|
+
}
|
|
119
|
+
return this.store.meta(note);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
async link({ noteId, documentId }) {
|
|
123
|
+
return this.serial(async () => this.addLink(noteId, await this.documents.get(assertId(documentId))));
|
|
124
|
+
}
|
|
125
|
+
async unlink({ noteId, documentId }) {
|
|
126
|
+
await this.linked(noteId, documentId);
|
|
127
|
+
await this.tasks.cancelWhere(
|
|
128
|
+
(job) =>
|
|
129
|
+
job.references.noteIds?.includes(noteId) &&
|
|
130
|
+
job.request?.canvasKind === 'pdf' &&
|
|
131
|
+
job.request.documentId === documentId,
|
|
132
|
+
cancelled('PDF 已移除关联', 'SOURCE_REMOVED'),
|
|
133
|
+
);
|
|
134
|
+
return this.store.serial(async () => {
|
|
135
|
+
const note = await this.note(noteId);
|
|
136
|
+
note.pdfDocuments = (note.pdfDocuments || []).filter((d) => d.id !== documentId);
|
|
137
|
+
note.pdfHistory = [...new Set([...(note.pdfHistory || []), documentId])];
|
|
138
|
+
note.updatedAt = new Date().toISOString();
|
|
139
|
+
await atomicWrite(this.store.boardPath(noteId), note);
|
|
140
|
+
return this.store.meta(note);
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
async beginUpload({ noteId, name, size, requestId }) {
|
|
144
|
+
await this.note(noteId);
|
|
145
|
+
assertId(requestId);
|
|
146
|
+
name = requiredText(name, 'PDF 文件名', 240).split(/[\\/]/).at(-1);
|
|
147
|
+
if (!/\.pdf$/i.test(name)) throw new Error('请选择 PDF 文件');
|
|
148
|
+
if (!Number.isSafeInteger(size) || size < 8 || size > PDF_MAX_BYTES)
|
|
149
|
+
throw new Error('PDF 大小需在 8 字节至 128 MB 之间');
|
|
150
|
+
return this.serial(async () => {
|
|
151
|
+
const previous = await this.uploads.get(requestId, { optional: true });
|
|
152
|
+
if (previous) {
|
|
153
|
+
if (previous.noteId !== noteId || previous.name !== name || previous.size !== size)
|
|
154
|
+
throw new Error('上传请求与原文件不一致');
|
|
155
|
+
return { ...previous, chunkSize: PDF_CHUNK_BYTES };
|
|
156
|
+
}
|
|
157
|
+
const record = { id: requestId, noteId, name, size, received: [], createdAt: new Date().toISOString() };
|
|
158
|
+
await mkdir(join(this.uploads.root, requestId + '.parts'), { recursive: true, mode: 0o700 });
|
|
159
|
+
await this.uploads.put(record.id, record);
|
|
160
|
+
return { ...record, chunkSize: PDF_CHUNK_BYTES };
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
async uploadChunk({ noteId, uploadId, index, data }) {
|
|
164
|
+
return this.serial(async () => {
|
|
165
|
+
await this.note(noteId);
|
|
166
|
+
const upload = await this.uploads.get(assertId(uploadId));
|
|
167
|
+
if (upload.noteId !== noteId) throw new Error('上传不属于当前笔记');
|
|
168
|
+
const count = Math.ceil(upload.size / PDF_CHUNK_BYTES);
|
|
169
|
+
if (
|
|
170
|
+
!Number.isInteger(index) ||
|
|
171
|
+
index < 0 ||
|
|
172
|
+
index >= count ||
|
|
173
|
+
typeof data !== 'string' ||
|
|
174
|
+
data.length > PDF_CHUNK_BYTES * 1.4 ||
|
|
175
|
+
!/^[A-Za-z0-9+/]*={0,2}$/.test(data)
|
|
176
|
+
)
|
|
177
|
+
throw new Error('PDF 分块数据不正确');
|
|
178
|
+
const bytes = Buffer.from(data, 'base64'),
|
|
179
|
+
expected = Math.min(PDF_CHUNK_BYTES, upload.size - index * PDF_CHUNK_BYTES);
|
|
180
|
+
if (bytes.length !== expected) throw new Error('PDF 分块大小不正确');
|
|
181
|
+
const file = join(this.uploads.root, uploadId + '.parts', String(index));
|
|
182
|
+
if (upload.received.includes(index)) {
|
|
183
|
+
if (!(await readFile(file)).equals(bytes)) throw new Error('重试上传的内容不一致');
|
|
184
|
+
} else {
|
|
185
|
+
await writeFile(file, bytes, { mode: 0o600 });
|
|
186
|
+
upload.received.push(index);
|
|
187
|
+
await this.uploads.put(uploadId, upload);
|
|
188
|
+
}
|
|
189
|
+
return { received: upload.received.length, total: count };
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
async finishUpload({ noteId, uploadId }) {
|
|
193
|
+
return this.serial(async () => {
|
|
194
|
+
await this.note(noteId);
|
|
195
|
+
const upload = await this.uploads.get(assertId(uploadId));
|
|
196
|
+
if (upload.noteId !== noteId) throw new Error('上传不属于当前笔记');
|
|
197
|
+
if (upload.documentId) {
|
|
198
|
+
const document = await this.documents.get(upload.documentId);
|
|
199
|
+
await this.addLink(noteId, document);
|
|
200
|
+
return document;
|
|
201
|
+
}
|
|
202
|
+
const count = Math.ceil(upload.size / PDF_CHUNK_BYTES);
|
|
203
|
+
if (upload.received.length !== count) throw new Error('PDF 尚未上传完整,请重试');
|
|
204
|
+
const temp = join(this.uploads.root, uploadId + '.pdf'),
|
|
205
|
+
handle = await open(temp, 'w', 0o600),
|
|
206
|
+
hash = createHash('sha256');
|
|
207
|
+
try {
|
|
208
|
+
for (let i = 0; i < count; i++) {
|
|
209
|
+
const bytes = await readFile(join(this.uploads.root, uploadId + '.parts', String(i)));
|
|
210
|
+
if (i === 0 && !bytes.subarray(0, 1024).includes(Buffer.from('%PDF-')))
|
|
211
|
+
throw new Error('文件内容不是有效的 PDF');
|
|
212
|
+
hash.update(bytes);
|
|
213
|
+
await handle.writeFile(bytes);
|
|
214
|
+
}
|
|
215
|
+
} finally {
|
|
216
|
+
await handle.close();
|
|
217
|
+
}
|
|
218
|
+
const id = hash.digest('hex'),
|
|
219
|
+
document = await this.documents.update(
|
|
220
|
+
id,
|
|
221
|
+
(old) =>
|
|
222
|
+
old || {
|
|
223
|
+
id,
|
|
224
|
+
name: upload.name,
|
|
225
|
+
size: upload.size,
|
|
226
|
+
pageCount: null,
|
|
227
|
+
createdAt: new Date().toISOString(),
|
|
228
|
+
},
|
|
229
|
+
{ optional: true },
|
|
230
|
+
);
|
|
231
|
+
await rename(temp, join(this.filesRoot, id + '.pdf'));
|
|
232
|
+
await this.addLink(noteId, document);
|
|
233
|
+
await this.uploads.put(uploadId, { ...upload, documentId: id });
|
|
234
|
+
await rm(join(this.uploads.root, uploadId + '.parts'), { recursive: true, force: true });
|
|
235
|
+
return document;
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
async clearUpload(id) {
|
|
239
|
+
await rm(join(this.uploads.root, assertId(id) + '.parts'), { recursive: true, force: true });
|
|
240
|
+
await rm(join(this.uploads.root, id + '.pdf'), { force: true });
|
|
241
|
+
await this.uploads.remove(id);
|
|
242
|
+
}
|
|
243
|
+
async cancelUpload({ noteId, uploadId }) {
|
|
244
|
+
return this.serial(async () => {
|
|
245
|
+
const u = await this.uploads.get(assertId(uploadId), { optional: true });
|
|
246
|
+
if (u && u.noteId !== noteId) throw new Error('上传不属于当前笔记');
|
|
247
|
+
await this.clearUpload(uploadId);
|
|
248
|
+
return { cancelled: true };
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
async read({ noteId, documentId, offset = 0, length = PDF_CHUNK_BYTES }) {
|
|
252
|
+
const { document } = await this.linked(noteId, documentId);
|
|
253
|
+
return this.fileRange(document, offset, length);
|
|
254
|
+
}
|
|
255
|
+
async backupRead({ documentId, offset = 0, length = PDF_CHUNK_BYTES }) {
|
|
256
|
+
return this.fileRange(await this.documents.get(assertId(documentId)), offset, length);
|
|
257
|
+
}
|
|
258
|
+
async fileRange(document, offset, length) {
|
|
259
|
+
if (
|
|
260
|
+
!Number.isSafeInteger(offset) ||
|
|
261
|
+
offset < 0 ||
|
|
262
|
+
offset >= document.size ||
|
|
263
|
+
!Number.isSafeInteger(length) ||
|
|
264
|
+
length < 1 ||
|
|
265
|
+
length > 2 * PDF_CHUNK_BYTES
|
|
266
|
+
)
|
|
267
|
+
throw new Error('PDF 读取范围不正确');
|
|
268
|
+
const handle = await open(join(this.filesRoot, document.id + '.pdf'), 'r');
|
|
269
|
+
try {
|
|
270
|
+
const buffer = Buffer.alloc(Math.min(length, document.size - offset));
|
|
271
|
+
const { bytesRead } = await handle.read(buffer, 0, buffer.length, offset);
|
|
272
|
+
if (bytesRead !== buffer.length) throw new Error('PDF 原件读取不完整,请恢复备份或重新导入');
|
|
273
|
+
return { data: buffer.subarray(0, bytesRead).toString('base64'), size: document.size };
|
|
274
|
+
} finally {
|
|
275
|
+
await handle.close();
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
async info({ noteId, documentId, pageCount, title }) {
|
|
279
|
+
await this.linked(noteId, documentId);
|
|
280
|
+
if (!Number.isInteger(pageCount) || pageCount < 1 || pageCount > 10000) throw new Error('PDF 页数不支持');
|
|
281
|
+
return this.documents.update(documentId, (d) => {
|
|
282
|
+
if (d.pageCount && d.pageCount !== pageCount) throw new Error('PDF 页数与保存的文件不一致');
|
|
283
|
+
return { ...d, pageCount, title: optionalText(title, 500) };
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
async validPage(noteId, documentId, page) {
|
|
287
|
+
const value = await this.linked(noteId, documentId);
|
|
288
|
+
if (!Number.isInteger(page) || page < 1 || !value.document.pageCount || page > value.document.pageCount)
|
|
289
|
+
throw new Error('PDF 页码不正确');
|
|
290
|
+
return value;
|
|
291
|
+
}
|
|
292
|
+
indexPage(request) {
|
|
293
|
+
return this.serial(() => this.indexPageData(request));
|
|
294
|
+
}
|
|
295
|
+
async indexPageData({ noteId, documentId, page, text, label, view, items = [] }) {
|
|
296
|
+
await this.validPage(noteId, documentId, page);
|
|
297
|
+
if (typeof text !== 'string' || text.length > 200000 || !validPdfRect(view))
|
|
298
|
+
throw new Error('PDF 页面文字或尺寸不正确');
|
|
299
|
+
if (
|
|
300
|
+
!Array.isArray(items) ||
|
|
301
|
+
items.length > 20000 ||
|
|
302
|
+
items.some(
|
|
303
|
+
(item) => typeof item.text !== 'string' || item.text.length > 20000 || !validPdfRect(item.rect),
|
|
304
|
+
)
|
|
305
|
+
)
|
|
306
|
+
throw new Error('PDF 文字位置不正确');
|
|
307
|
+
const id = documentId + '_' + page;
|
|
308
|
+
const existing = await this.pages.get(id, { optional: true });
|
|
309
|
+
const value =
|
|
310
|
+
existing && (existing.items?.length || !items.length)
|
|
311
|
+
? existing
|
|
312
|
+
: await this.pages.update(
|
|
313
|
+
id,
|
|
314
|
+
(old) =>
|
|
315
|
+
old
|
|
316
|
+
? old.items?.length || !items.length
|
|
317
|
+
? old
|
|
318
|
+
: { ...old, items }
|
|
319
|
+
: {
|
|
320
|
+
id,
|
|
321
|
+
documentId,
|
|
322
|
+
page,
|
|
323
|
+
text,
|
|
324
|
+
label: optionalText(label, 60) || String(page),
|
|
325
|
+
view,
|
|
326
|
+
hash: digest(text),
|
|
327
|
+
items,
|
|
328
|
+
},
|
|
329
|
+
{ optional: true },
|
|
330
|
+
);
|
|
331
|
+
const document = await this.documents.get(documentId);
|
|
332
|
+
if (!document.indexedPages?.includes(page))
|
|
333
|
+
await this.documents.update(documentId, (d) => ({
|
|
334
|
+
...d,
|
|
335
|
+
indexedPages: [...new Set([...(d.indexedPages || []), page])].sort((a, b) => a - b),
|
|
336
|
+
}));
|
|
337
|
+
return value;
|
|
338
|
+
}
|
|
339
|
+
async annotate(request) {
|
|
340
|
+
const { noteId, documentId, page } = request,
|
|
341
|
+
{ document } = await this.validPage(noteId, documentId, page),
|
|
342
|
+
id = assertId(request.id);
|
|
343
|
+
const kind = request.kind,
|
|
344
|
+
rects = request.rects || [],
|
|
345
|
+
points = request.points || [],
|
|
346
|
+
quote = optionalText(request.quote, 30000),
|
|
347
|
+
note = optionalText(request.note, 6000);
|
|
348
|
+
if ((request.quote?.length || 0) > 30000 || (request.note?.length || 0) > 6000)
|
|
349
|
+
throw new Error('批注内容较长,请缩小选区或缩短备注');
|
|
350
|
+
if (
|
|
351
|
+
!['highlight', 'ink', 'reference'].includes(kind) ||
|
|
352
|
+
!Array.isArray(rects) ||
|
|
353
|
+
rects.length > 300 ||
|
|
354
|
+
!rects.every(validPdfRect) ||
|
|
355
|
+
!Array.isArray(points) ||
|
|
356
|
+
points.length > 10000 ||
|
|
357
|
+
points.some(
|
|
358
|
+
(p) => !Array.isArray(p) || p.length !== 2 || p.some((n) => !Number.isFinite(n) || Math.abs(n) > 1e6),
|
|
359
|
+
)
|
|
360
|
+
)
|
|
361
|
+
throw new Error('PDF 批注范围不正确');
|
|
362
|
+
if (kind === 'ink' ? points.length < 2 : !rects.length) throw new Error('请先选中原文或绘制笔迹');
|
|
363
|
+
const indexed = await this.pages.get(documentId + '_' + page, { optional: true });
|
|
364
|
+
if (!indexed) throw new Error('页面仍在载入,请稍后重试');
|
|
365
|
+
const view = indexed.view,
|
|
366
|
+
inside = (point) =>
|
|
367
|
+
point[0] >= view[0] - 2 &&
|
|
368
|
+
point[0] <= view[2] + 2 &&
|
|
369
|
+
point[1] >= view[1] - 2 &&
|
|
370
|
+
point[1] <= view[3] + 2;
|
|
371
|
+
if (rects.some((r) => !inside(r.slice(0, 2)) || !inside(r.slice(2))) || points.some((p) => !inside(p)))
|
|
372
|
+
throw new Error('批注超出页面范围');
|
|
373
|
+
const expectedText =
|
|
374
|
+
request.selectionType === 'region' ? pdfSelectionText(indexed.items || [], rects) : indexed.text;
|
|
375
|
+
if (
|
|
376
|
+
quote &&
|
|
377
|
+
request.origin !== 'ocr' &&
|
|
378
|
+
!normalizePdfText(expectedText).includes(normalizePdfText(quote))
|
|
379
|
+
)
|
|
380
|
+
throw new Error('选中文字与本页不一致,请重新选择;图表可使用框选');
|
|
381
|
+
const stamp = digest({ documentId, page, kind, rects, points, quote }),
|
|
382
|
+
requestHash = digest({ stamp, note, origin: request.origin || 'pdf', deleted: !!request.deleted });
|
|
383
|
+
const result = await this.notebooks.update(
|
|
384
|
+
noteId,
|
|
385
|
+
async (old) => {
|
|
386
|
+
await this.linked(noteId, documentId);
|
|
387
|
+
old ||= { id: noteId, annotations: [], positions: {} };
|
|
388
|
+
const prior = old.annotations.find((a) => a.id === id);
|
|
389
|
+
if (prior && prior.documentId !== documentId) throw new Error('批注不属于这份 PDF');
|
|
390
|
+
if (prior?.lastRequestId === request.requestId) {
|
|
391
|
+
if (prior.requestHash !== requestHash) throw new Error('重试批注内容不一致');
|
|
392
|
+
return old;
|
|
393
|
+
}
|
|
394
|
+
if (request.baseVersion !== (prior?.version || 0)) {
|
|
395
|
+
throw new Error('批注已在另一窗口更新,请刷新后再修改');
|
|
396
|
+
}
|
|
397
|
+
const annotation = {
|
|
398
|
+
id,
|
|
399
|
+
noteId,
|
|
400
|
+
documentId,
|
|
401
|
+
page,
|
|
402
|
+
pageLabel: indexed?.label || String(page),
|
|
403
|
+
kind,
|
|
404
|
+
rects,
|
|
405
|
+
points,
|
|
406
|
+
quote,
|
|
407
|
+
selectionType: request.selectionType === 'region' ? 'region' : 'text',
|
|
408
|
+
note,
|
|
409
|
+
requestHash,
|
|
410
|
+
origin: request.origin === 'ocr' ? 'ocr' : 'pdf',
|
|
411
|
+
sourceHash: stamp,
|
|
412
|
+
version: (prior?.version || 0) + 1,
|
|
413
|
+
lastRequestId: assertId(request.requestId),
|
|
414
|
+
createdAt: prior?.createdAt || new Date().toISOString(),
|
|
415
|
+
updatedAt: new Date().toISOString(),
|
|
416
|
+
deletedAt: request.deleted ? new Date().toISOString() : null,
|
|
417
|
+
};
|
|
418
|
+
return {
|
|
419
|
+
...old,
|
|
420
|
+
annotations: prior
|
|
421
|
+
? old.annotations.map((a) => (a.id === id ? annotation : a))
|
|
422
|
+
: [...old.annotations, annotation],
|
|
423
|
+
};
|
|
424
|
+
},
|
|
425
|
+
{ optional: true },
|
|
426
|
+
);
|
|
427
|
+
await this.store.metadata({ id: noteId });
|
|
428
|
+
return result.annotations.find((a) => a.id === id);
|
|
429
|
+
}
|
|
430
|
+
async position({ noteId, documentId, page }) {
|
|
431
|
+
await this.validPage(noteId, documentId, page);
|
|
432
|
+
await this.notebooks.update(
|
|
433
|
+
noteId,
|
|
434
|
+
(old) => ({
|
|
435
|
+
...old,
|
|
436
|
+
id: noteId,
|
|
437
|
+
annotations: old?.annotations || [],
|
|
438
|
+
positions: { ...old?.positions, [documentId]: page },
|
|
439
|
+
}),
|
|
440
|
+
{ optional: true },
|
|
441
|
+
);
|
|
442
|
+
return { page };
|
|
443
|
+
}
|
|
444
|
+
async asset({ name }) {
|
|
445
|
+
if (
|
|
446
|
+
typeof name !== 'string' ||
|
|
447
|
+
!/^(?:pdf\.worker\.min\.mjs|(?:cmaps|standard_fonts|wasm|iccs)\/[A-Za-z0-9_.-]+)$/.test(name)
|
|
448
|
+
)
|
|
449
|
+
throw new Error('PDF 阅读资源不存在');
|
|
450
|
+
const file = resolve(this.assetRoot, name);
|
|
451
|
+
if (!file.startsWith(resolve(this.assetRoot) + '/')) throw new Error('PDF 阅读资源不存在');
|
|
452
|
+
return { data: (await readFile(file)).toString('base64') };
|
|
453
|
+
}
|
|
454
|
+
async commentScene(noteId, documentId) {
|
|
455
|
+
const { document } = await this.linked(noteId, documentId),
|
|
456
|
+
book = await this.notebooks.get(noteId, { optional: true });
|
|
457
|
+
return {
|
|
458
|
+
elements: (book?.annotations || [])
|
|
459
|
+
.filter((a) => a.documentId === documentId)
|
|
460
|
+
.map((a) => pdfCommentElement(document, a)),
|
|
461
|
+
files: {},
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
async recognize({ noteId, documentId, page, image }) {
|
|
465
|
+
await this.validPage(noteId, documentId, page);
|
|
466
|
+
if (
|
|
467
|
+
!image ||
|
|
468
|
+
!['image/png', 'image/jpeg'].includes(image.mimeType) ||
|
|
469
|
+
typeof image.data !== 'string' ||
|
|
470
|
+
image.data.length > 12_000_000 ||
|
|
471
|
+
!/^[A-Za-z0-9+/=]+$/.test(image.data)
|
|
472
|
+
)
|
|
473
|
+
throw new Error('识别图像不正确');
|
|
474
|
+
return this.tasks.start({
|
|
475
|
+
feature: 'pdf',
|
|
476
|
+
key: randomUUID(),
|
|
477
|
+
title: '识别 PDF 第 ' + page + ' 页',
|
|
478
|
+
references: { noteIds: [noteId] },
|
|
479
|
+
request: { canvasKind: 'pdf', documentId },
|
|
480
|
+
run: async (job) => {
|
|
481
|
+
job.progress('正在本机识别选定区域');
|
|
482
|
+
const result = await this.native.read(Buffer.from(image.data, 'base64'), { signal: job.signal });
|
|
483
|
+
job.assertActive();
|
|
484
|
+
await this.linked(noteId, documentId);
|
|
485
|
+
return { text: result.lines.map((line) => line.text).join('\n'), documentId, page };
|
|
486
|
+
},
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
async records(noteId) {
|
|
490
|
+
const note = await this.store.read(noteId);
|
|
491
|
+
if (note.deletedAt) return [];
|
|
492
|
+
const docs = new Map((note.pdfDocuments || []).map((d) => [d.id, d])),
|
|
493
|
+
result = [];
|
|
494
|
+
if (!docs.size) return result;
|
|
495
|
+
const linkedPages = [];
|
|
496
|
+
for (const documentId of docs.keys())
|
|
497
|
+
for (const number of (await this.documents.get(documentId)).indexedPages || []) {
|
|
498
|
+
const page = await this.pages.get(documentId + '_' + number, { optional: true });
|
|
499
|
+
if (page) linkedPages.push(page);
|
|
500
|
+
}
|
|
501
|
+
for (const page of linkedPages)
|
|
502
|
+
if (docs.has(page.documentId) && page.text)
|
|
503
|
+
result.push({
|
|
504
|
+
id: noteId + '/pdf/' + page.id,
|
|
505
|
+
boardId: noteId,
|
|
506
|
+
materialId: null,
|
|
507
|
+
summaryId: null,
|
|
508
|
+
title: docs.get(page.documentId).title + ' · 第 ' + page.page + ' 页',
|
|
509
|
+
boardTitle: note.title,
|
|
510
|
+
kind: 'material',
|
|
511
|
+
text: page.text,
|
|
512
|
+
hash: page.hash,
|
|
513
|
+
url: null,
|
|
514
|
+
locator: {
|
|
515
|
+
elementIds: [],
|
|
516
|
+
page: page.page,
|
|
517
|
+
pdf: { documentId: page.documentId, page: page.page, title: docs.get(page.documentId).title },
|
|
518
|
+
},
|
|
519
|
+
});
|
|
520
|
+
const book = await this.notebooks.get(noteId, { optional: true });
|
|
521
|
+
for (const a of book?.annotations || [])
|
|
522
|
+
if (!a.deletedAt && a.origin === 'ocr' && a.quote && docs.has(a.documentId))
|
|
523
|
+
result.push({
|
|
524
|
+
id: noteId + '/pdf-ocr/' + a.id,
|
|
525
|
+
boardId: noteId,
|
|
526
|
+
materialId: null,
|
|
527
|
+
summaryId: null,
|
|
528
|
+
title: 'PDF 识别文字(已核对)',
|
|
529
|
+
boardTitle: note.title,
|
|
530
|
+
kind: 'material',
|
|
531
|
+
text: a.quote,
|
|
532
|
+
hash: digest(a.quote),
|
|
533
|
+
url: null,
|
|
534
|
+
locator: {
|
|
535
|
+
elementIds: [],
|
|
536
|
+
page: a.page,
|
|
537
|
+
pdf: pdfReference({ id: a.documentId, name: docs.get(a.documentId).title }, a),
|
|
538
|
+
},
|
|
539
|
+
});
|
|
540
|
+
for (const a of book?.annotations || [])
|
|
541
|
+
if (!a.deletedAt && a.note && docs.has(a.documentId))
|
|
542
|
+
result.push({
|
|
543
|
+
id: noteId + '/pdf-note/' + a.id,
|
|
544
|
+
boardId: noteId,
|
|
545
|
+
materialId: null,
|
|
546
|
+
summaryId: null,
|
|
547
|
+
title: '我的 PDF 批注',
|
|
548
|
+
boardTitle: note.title,
|
|
549
|
+
kind: 'annotation',
|
|
550
|
+
text: a.note,
|
|
551
|
+
hash: digest(a.note),
|
|
552
|
+
url: null,
|
|
553
|
+
locator: {
|
|
554
|
+
elementIds: [],
|
|
555
|
+
page: a.page,
|
|
556
|
+
pdf: {
|
|
557
|
+
documentId: a.documentId,
|
|
558
|
+
page: a.page,
|
|
559
|
+
rects: a.rects,
|
|
560
|
+
title: docs.get(a.documentId).title,
|
|
561
|
+
},
|
|
562
|
+
},
|
|
563
|
+
});
|
|
564
|
+
return result;
|
|
565
|
+
}
|
|
566
|
+
async duplicate(fromId, toId) {
|
|
567
|
+
const from = await this.store.read(fromId);
|
|
568
|
+
for (const link of from.pdfDocuments || []) await this.link({ noteId: toId, documentId: link.id });
|
|
569
|
+
const book = await this.notebooks.get(fromId, { optional: true });
|
|
570
|
+
if (book)
|
|
571
|
+
await this.notebooks.put(toId, {
|
|
572
|
+
...book,
|
|
573
|
+
id: toId,
|
|
574
|
+
annotations: book.annotations.map((a) => ({ ...a, noteId: toId })),
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
async purge(ids) {
|
|
578
|
+
return this.serial(() => this.purgeUnused(ids));
|
|
579
|
+
}
|
|
580
|
+
async purgeUnused(ids) {
|
|
581
|
+
for (const id of ids) await this.notebooks.remove(id);
|
|
582
|
+
for (const u of await this.uploads.list()) if (ids.includes(u.noteId)) await this.clearUpload(u.id);
|
|
583
|
+
const { boards, errors } = await this.store.all();
|
|
584
|
+
// An unreadable note may still own this shared original. Defer asset cleanup.
|
|
585
|
+
if (errors.length) return;
|
|
586
|
+
const used = new Set(
|
|
587
|
+
boards.flatMap((b) => [...(b.pdfDocuments || []).map((d) => d.id), ...(b.pdfHistory || [])]),
|
|
588
|
+
);
|
|
589
|
+
for (const doc of await this.documents.list())
|
|
590
|
+
if (!used.has(doc.id)) {
|
|
591
|
+
await this.documents.remove(doc.id);
|
|
592
|
+
await rm(join(this.filesRoot, doc.id + '.pdf'), { force: true });
|
|
593
|
+
for (const p of await this.pages.list()) if (p.documentId === doc.id) await this.pages.remove(p.id);
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
async backup({ separateFiles = false } = {}) {
|
|
597
|
+
const documents = [];
|
|
598
|
+
for (const doc of await this.documents.list())
|
|
599
|
+
documents.push(
|
|
600
|
+
separateFiles
|
|
601
|
+
? { ...doc, assetFile: 'pdfs/' + doc.id + '.pdf' }
|
|
602
|
+
: { ...doc, data: (await readFile(join(this.filesRoot, doc.id + '.pdf'))).toString('base64') },
|
|
603
|
+
);
|
|
604
|
+
return { documents, notebooks: await this.notebooks.list(), pages: await this.pages.list() };
|
|
605
|
+
}
|
|
606
|
+
}
|