|
@@ -15,6 +15,8 @@ dayjs.extend(localizedFormat);
|
|
|
|
|
|
import { TTS_RESPONSE_SPLIT } from '$lib/types';
|
|
|
|
|
|
+import pdfWorkerUrl from 'pdfjs-dist/build/pdf.worker.mjs?url';
|
|
|
+
|
|
|
import { marked } from 'marked';
|
|
|
import markedExtension from '$lib/utils/marked/extension';
|
|
|
import markedKatexExtension from '$lib/utils/marked/katex-extension';
|
|
@@ -1440,7 +1442,18 @@ export const parseJsonValue = (value: string): any => {
|
|
|
return value;
|
|
|
};
|
|
|
|
|
|
-export const extractContentFromFile = async (file, pdfjsLib = null) => {
|
|
|
+async function ensurePDFjsLoaded() {
|
|
|
+ if (!window.pdfjsLib) {
|
|
|
+ const pdfjs = await import('pdfjs-dist');
|
|
|
+ pdfjs.GlobalWorkerOptions.workerSrc = pdfWorkerUrl;
|
|
|
+ if (!window.pdfjsLib) {
|
|
|
+ throw new Error('pdfjsLib is required for PDF extraction');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return window.pdfjsLib;
|
|
|
+}
|
|
|
+
|
|
|
+export const extractContentFromFile = async (file: File) => {
|
|
|
// Known text file extensions for extra fallback
|
|
|
const textExtensions = [
|
|
|
'.txt',
|
|
@@ -1457,31 +1470,28 @@ export const extractContentFromFile = async (file, pdfjsLib = null) => {
|
|
|
'.rtf'
|
|
|
];
|
|
|
|
|
|
- function getExtension(filename) {
|
|
|
+ function getExtension(filename: string) {
|
|
|
const dot = filename.lastIndexOf('.');
|
|
|
return dot === -1 ? '' : filename.substr(dot).toLowerCase();
|
|
|
}
|
|
|
|
|
|
// Uses pdfjs to extract text from PDF
|
|
|
- async function extractPdfText(file) {
|
|
|
- if (!pdfjsLib) {
|
|
|
- throw new Error('pdfjsLib is required for PDF extraction');
|
|
|
- }
|
|
|
-
|
|
|
+ async function extractPdfText(file: File) {
|
|
|
+ const pdfjsLib = await ensurePDFjsLoaded();
|
|
|
const arrayBuffer = await file.arrayBuffer();
|
|
|
const pdf = await pdfjsLib.getDocument({ data: arrayBuffer }).promise;
|
|
|
let allText = '';
|
|
|
for (let pageNum = 1; pageNum <= pdf.numPages; pageNum++) {
|
|
|
const page = await pdf.getPage(pageNum);
|
|
|
const content = await page.getTextContent();
|
|
|
- const strings = content.items.map((item) => item.str);
|
|
|
+ const strings = content.items.map((item: any) => item.str);
|
|
|
allText += strings.join(' ') + '\n';
|
|
|
}
|
|
|
return allText;
|
|
|
}
|
|
|
|
|
|
// Reads file as text using FileReader
|
|
|
- function readAsText(file) {
|
|
|
+ function readAsText(file: File) {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
const reader = new FileReader();
|
|
|
reader.onload = () => resolve(reader.result);
|