file parse 优化
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 1m41s

This commit is contained in:
Song367 2026-03-12 17:46:29 +08:00
parent a1f8b1b50b
commit c357ea9b2a

View File

@ -1,5 +1,10 @@
import mammoth from 'mammoth';
import { getDocument } from 'pdfjs-dist';
import { getDocument, GlobalWorkerOptions } from 'pdfjs-dist';
import PdfWorker from 'pdfjs-dist/build/pdf.worker.mjs?worker';
if (typeof window !== 'undefined' && 'Worker' in window) {
GlobalWorkerOptions.workerPort = new PdfWorker();
}
const SUPPORTED_EXTENSIONS = ['docx', 'txt', 'pdf', 'md'] as const;
export type SupportedUploadExtension = typeof SUPPORTED_EXTENSIONS[number];
@ -19,7 +24,7 @@ function getExtension(fileName: string): SupportedUploadExtension | null {
async function parsePdf(file: File): Promise<string> {
const data = new Uint8Array(await file.arrayBuffer());
const pdf = await getDocument({ data, disableWorker: true as unknown as boolean }).promise;
const pdf = await getDocument({ data }).promise;
const pages: string[] = [];
for (let pageNumber = 1; pageNumber <= pdf.numPages; pageNumber += 1) {