Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
"mime-types": "3.0.2",
"multer": "2.1.1",
"normalize-strings": "1.1.1",
"officeparser": "6.0.7",
"officeparser": "6.1.0",
"rand-token": "1.0.1",
"safe-compare": "1.1.4",
"sanitize-filename": "1.6.4",
Expand Down
35 changes: 14 additions & 21 deletions apps/server/src/services/ocr/processors/office_processor.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
import { parseExcel } from 'officeparser/dist/parsers/ExcelParser.js';
import { parseOpenOffice } from 'officeparser/dist/parsers/OpenOfficeParser.js';
import { parsePowerPoint } from 'officeparser/dist/parsers/PowerPointParser.js';
import { parseWord } from 'officeparser/dist/parsers/WordParser.js';
import type { OfficeParserConfig } from 'officeparser/dist/types.js';
import { OfficeParser, type OfficeParserConfig } from 'officeparser';

import log from '../../log.js';
import { OCRProcessingOptions, OCRResult } from '../ocr_service.js';
import { FileProcessor } from './file_processor.js';

type Parser = (buffer: Buffer, config: OfficeParserConfig) => Promise<{ toText(): string }>;

const PARSER_BY_MIME: Record<string, Parser> = {
const SUPPORTED_MIME_TYPES = new Set([
// Office Open XML
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': parseWord,
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': parseExcel,
'application/vnd.openxmlformats-officedocument.presentationml.presentation': parsePowerPoint,
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
// OpenDocument
'application/vnd.oasis.opendocument.text': parseOpenOffice,
'application/vnd.oasis.opendocument.spreadsheet': parseOpenOffice,
'application/vnd.oasis.opendocument.presentation': parseOpenOffice
};
'application/vnd.oasis.opendocument.text',
'application/vnd.oasis.opendocument.spreadsheet',
'application/vnd.oasis.opendocument.presentation'
]);

const PARSER_CONFIG: OfficeParserConfig = {
outputErrorToConsole: false,
Expand All @@ -30,28 +24,27 @@ const PARSER_CONFIG: OfficeParserConfig = {

/**
* Office document processor for extracting text from DOCX/XLSX/PPTX and ODT/ODS/ODP files.
* Uses individual parsers from officeparser v6 to avoid pulling in pdfjs-dist.
* Uses officeparser's main API, which auto-detects the format from the buffer's magic bytes.
*/
export class OfficeProcessor extends FileProcessor {

canProcess(mimeType: string): boolean {
return mimeType in PARSER_BY_MIME;
return SUPPORTED_MIME_TYPES.has(mimeType);
}

getSupportedMimeTypes(): string[] {
return Object.keys(PARSER_BY_MIME);
return [...SUPPORTED_MIME_TYPES];
}

async extractText(buffer: Buffer, options: OCRProcessingOptions = {}): Promise<OCRResult> {
const mimeType = options.mimeType;
if (!mimeType || !(mimeType in PARSER_BY_MIME)) {
if (!mimeType || !SUPPORTED_MIME_TYPES.has(mimeType)) {
throw new Error(`Unsupported MIME type for Office processor: ${mimeType}`);
}

log.info(`Starting Office document text extraction for ${mimeType}...`);

const parse = PARSER_BY_MIME[mimeType];
const ast = await parse(buffer, PARSER_CONFIG);
const ast = await OfficeParser.parseOffice(buffer, PARSER_CONFIG);
const trimmed = ast.toText().trim();

return {
Expand Down
42 changes: 33 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading