Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(ocr): extract text from EPUB files
Elian Dorance423ec43bbc
diff --git a/apps/server/src/services/ocr/processors/office_processor.integration.spec.ts b/apps/server/src/services/ocr/processors/office_processor.integration.spec.tsindex c1706f4b0e..2e6c09d349 100644--- a/apps/server/src/services/ocr/processors/office_processor.integration.spec.ts+++ b/apps/server/src/services/ocr/processors/office_processor.integration.spec.ts@@ -26,6 +26,7 @@ const ODT = 'application/vnd.oasis.opendocument.text';const ODS = 'application/vnd.oasis.opendocument.spreadsheet';const ODP = 'application/vnd.oasis.opendocument.presentation';const RTF = 'application/rtf';+const EPUB = 'application/epub+zip';const SAMPLES_DIR = join(__dirname, 'samples');@@ -53,10 +54,23 @@ const SPREADSHEET_PHRASES = ['Plată'];+// The EPUB sample is a different document again: Charlotte Perkins Gilman's+// public-domain short story "The Yellow Wallpaper" (Project Gutenberg #1952).+// These phrases span the front matter and the story body, proving the parser+// walked the spine's XHTML chapters, not just the cover page.+const EPUB_PHRASES = [+ 'The Yellow Wallpaper',+ 'Charlotte Perkins Gilman',+ 'John is a physician',+ 'It is a big, airy room',+ 'creeping women'+];+// Each sample exercises a distinct officeparser code path: DOCX -> WordParser,// XLSX -> ExcelParser, PPTX -> PowerPointParser, ODT/ODS/ODP -> OpenOfficeParser-// (text / spreadsheet / presentation element handling), RTF -> RtfParser (routed-// via an explicit fileType hint), with OOXML vs. ODF magic-byte detection on top.+// (text / spreadsheet / presentation element handling), RTF -> RtfParser and+// EPUB -> EpubParser (both routed via an explicit fileType hint), with OOXML vs.+// ODF vs. EPUB magic-byte detection on top.const SAMPLES = [{ label: 'DOCX', file: 'demo.docx', mimeType: DOCX, phrases: DOCUMENT_PHRASES },{ label: 'XLSX', file: 'demo.xlsx', mimeType: XLSX, phrases: SPREADSHEET_PHRASES },@@ -64,7 +78,8 @@ const SAMPLES = [{ label: 'ODT', file: 'demo.odt', mimeType: ODT, phrases: DOCUMENT_PHRASES },{ label: 'ODS', file: 'demo.ods', mimeType: ODS, phrases: SPREADSHEET_PHRASES },{ label: 'ODP', file: 'demo.odp', mimeType: ODP, phrases: DOCUMENT_PHRASES },- { label: 'RTF', file: 'demo.rtf', mimeType: RTF, phrases: DOCUMENT_PHRASES }+ { label: 'RTF', file: 'demo.rtf', mimeType: RTF, phrases: DOCUMENT_PHRASES },+ { label: 'EPUB', file: 'demo.epub', mimeType: EPUB, phrases: EPUB_PHRASES }];beforeEach(async () => {diff --git a/apps/server/src/services/ocr/processors/office_processor.spec.ts b/apps/server/src/services/ocr/processors/office_processor.spec.tsindex 3abd2fdf91..97954820d6 100644--- a/apps/server/src/services/ocr/processors/office_processor.spec.ts+++ b/apps/server/src/services/ocr/processors/office_processor.spec.ts@@ -23,6 +23,7 @@ let OfficeProcessor: typeof import('./office_processor.js').OfficeProcessor;const DOCX = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';const ODT = 'application/vnd.oasis.opendocument.text';const RTF = 'application/rtf';+const EPUB = 'application/epub+zip';beforeEach(async () => {vi.clearAllMocks();@@ -43,6 +44,7 @@ describe('OfficeProcessor', () => {expect(processor.canProcess(ODT)).toBe(true);expect(processor.canProcess(RTF)).toBe(true);expect(processor.canProcess('text/rtf')).toBe(true);+ expect(processor.canProcess(EPUB)).toBe(true);expect(processor.canProcess('application/pdf')).toBe(false);expect(processor.getSupportedMimeTypes()).toContain(DOCX);expect(processor.getProcessingType()).toBe('office');@@ -80,6 +82,21 @@ describe('OfficeProcessor', () => {});});+ it('passes an explicit fileType hint for EPUB, which shares the ZIP container with DOCX/ODT', async () => {+ const processor = new OfficeProcessor();+ mockParseOffice.mockResolvedValue({ toText: () => 'epub body' });++ const result = await processor.extractText(buffer, { mimeType: EPUB });++ expect(result.text).toBe('epub body');+ expect(mockParseOffice).toHaveBeenCalledWith(buffer, {+ outputErrorToConsole: false,+ newlineDelimiter: '\n',+ ignoreNotes: false,+ fileType: 'epub'+ });+ });+it('reports zero confidence and defaults language when no text is extracted', async () => {const processor = new OfficeProcessor();mockParseOffice.mockResolvedValue({ toText: () => ' ' });diff --git a/apps/server/src/services/ocr/processors/office_processor.ts b/apps/server/src/services/ocr/processors/office_processor.tsindex dcbc2807dc..c35809e7c4 100644--- a/apps/server/src/services/ocr/processors/office_processor.ts+++ b/apps/server/src/services/ocr/processors/office_processor.ts@@ -15,7 +15,9 @@ const SUPPORTED_MIME_TYPES = new Set(['application/vnd.oasis.opendocument.presentation',// Rich Text Format'application/rtf',- 'text/rtf'+ 'text/rtf',+ // E-book+ 'application/epub+zip']);const PARSER_CONFIG: OfficeParserConfig = {@@ -24,18 +26,23 @@ const PARSER_CONFIG: OfficeParserConfig = {ignoreNotes: false};-// officeparser auto-detects most formats from the buffer's magic bytes, but its-// RTF detection (via file-type) is unreliable — some valid RTF documents are not-// recognised and parsing then fails. For those MIME types we route to the correct-// parser explicitly with a fileType hint instead of relying on auto-detection.+// officeparser auto-detects most formats from the buffer's magic bytes, but for+// some MIME types we route to the correct parser explicitly with a fileType hint+// instead of relying on auto-detection:+// - RTF detection (via file-type) is unreliable — some valid RTF documents are+// not recognised and parsing then fails.+// - EPUB shares the ZIP container (PK magic bytes) with DOCX/ODT, so an explicit+// hint avoids any ambiguity in container-format detection.const MIME_TYPE_TO_FILE_TYPE: Record<string, SupportedFileType> = {'application/rtf': 'rtf',- 'text/rtf': 'rtf'+ 'text/rtf': 'rtf',+ 'application/epub+zip': 'epub'};/**- * Office document processor for extracting text from DOCX/XLSX/PPTX and ODT/ODS/ODP files.- * Uses officeparser's main API, which auto-detects the format from the buffer's magic bytes.+ * Office document processor for extracting text from DOCX/XLSX/PPTX, ODT/ODS/ODP,+ * RTF and EPUB files. Uses officeparser's main API, which auto-detects the format+ * from the buffer's magic bytes (with an explicit fileType hint for RTF and EPUB).*/export class OfficeProcessor extends FileProcessor {diff --git a/apps/server/src/services/ocr/processors/samples/demo.epub b/apps/server/src/services/ocr/processors/samples/demo.epubnew file mode 100644index 0000000000..11108dc35fBinary files /dev/null and b/apps/server/src/services/ocr/processors/samples/demo.epub differ