mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
Every PDF upload logged "Document search indexing failed: TypeError: textDocument.destroy is not a function" and left that PDF's contents unsearchable. pdfjs-dist's getDocument() returns a PDFDocumentLoadingTask; destroy() lives THERE. Its .promise resolves to a PDFDocumentProxy, which has no destroy() of its own - calling it on that object throws. The loading task is now kept and destroyed instead. Also silences "Ensure that the standardFontDataUrl API parameter is provided" on every PDF: pdfjs-dist ships its own standard-font metrics and CJK character maps as plain files in the installed package: point standardFontDataUrl/cMapUrl at them via the same runtimeResolve trick already used for the worker file, instead of falling back to an approximation on every single PDF indexed. Verified end-to-end against a real uploaded PDF, not just source patterns. Thanks to xet7 !
200 lines
9.2 KiB
JavaScript
200 lines
9.2 KiB
JavaScript
import crypto from 'crypto';
|
|
import path from 'path';
|
|
import { createRequire } from 'module';
|
|
import { Mongo } from 'meteor/mongo';
|
|
import { boundedStreamBuffer, gifCacheKey } from './imageGif';
|
|
|
|
export const DOCUMENT_MAX_BYTES = 32 * 1024 * 1024;
|
|
export const DOCUMENT_MAX_PAGES = 200;
|
|
export const DOCUMENT_MAX_TEXT = 2 * 1024 * 1024;
|
|
const inProgress = new Map();
|
|
// Search index only: one row per attachment holding the extracted plain
|
|
// text used by searchAttachmentDocumentText. The viewer itself (DOCX/XLSX/
|
|
// PPTX via office-open-xml-viewer, PDF via the native <embed>) no longer
|
|
// reads this collection - it exists purely so attachment contents are
|
|
// searchable from the card search box.
|
|
export const DocumentPreviews = new Mongo.Collection('documentPreviews');
|
|
|
|
function runtimeRequire(name) {
|
|
return createRequire(path.join(process.cwd(), 'package.json'))(name);
|
|
}
|
|
|
|
function runtimeResolve(name) {
|
|
return createRequire(path.join(process.cwd(), 'package.json')).resolve(name);
|
|
}
|
|
|
|
// pdfjs-dist's Node "fake worker" fallback locates pdf.worker.mjs relative to
|
|
// import.meta.url of the module that imported it. Under the bundled server
|
|
// (_build/main-dev, _build/main-prod) that URL points into the bundle, not
|
|
// into node_modules, so the fallback looks for the worker beside the bundled
|
|
// server.cjs and fails: "Cannot find module
|
|
// '.../_build/main-dev/pdf.worker.mjs'" - which used to surface as a bare 415
|
|
// on every PDF text-index attempt. Point GlobalWorkerOptions at the real
|
|
// on-disk file via Node's own module resolution (the same runtimeRequire
|
|
// trick already used for fflate) so pdfjs never falls back to guessing.
|
|
let pdfWorkerConfigured = false;
|
|
function configurePdfWorker(pdfjs) {
|
|
if (pdfWorkerConfigured) return;
|
|
pdfWorkerConfigured = true;
|
|
try {
|
|
pdfjs.GlobalWorkerOptions.workerSrc = runtimeResolve('pdfjs-dist/legacy/build/pdf.worker.mjs');
|
|
} catch (error) {
|
|
console.error('Could not resolve pdf.worker.mjs, PDF search indexing will fail:', error);
|
|
}
|
|
}
|
|
|
|
function decodeXml(value) {
|
|
return String(value || '').replace(/<[^>]+>/g, ' ').replace(/</g, '<')
|
|
.replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, "'")
|
|
.replace(/&/g, '&').replace(/\s+/g, ' ').trim();
|
|
}
|
|
|
|
function plainSearchText(value) {
|
|
return String(value || '').normalize('NFKC').replace(/[ |