Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(documents): add ai based ocr support (#1283)

Corentin Thomasset62ae3a066a0d
diff --git a/.changeset/cool-baboons-argue.md b/.changeset/cool-baboons-argue.md
new file mode 100644
index 00000000..410c9fef
--- /dev/null
+++ b/.changeset/cool-baboons-argue.md
@@ -0,0 +1,5 @@
+---
+'@papra/app': patch
+---
+
+Add support for AI based OCR integration for document content extraction, with fallback and configurable strategies for different document types.
diff --git a/apps/papra-server/src/modules/ai/adapters/ai-adapters.types.ts b/apps/papra-server/src/modules/ai/adapters/ai-adapters.types.ts
index 293fde05..8e883670 100644
--- a/apps/papra-server/src/modules/ai/adapters/ai-adapters.types.ts
+++ b/apps/papra-server/src/modules/ai/adapters/ai-adapters.types.ts
@@ -1,9 +1,11 @@
import type { AnyTextAdapter } from '@tanstack/ai';
import type { Config } from '../../config/config.types';
+export type AiTextAdapter = AnyTextAdapter;
+
export type AiAdapter = {
name: string;
- getTextAdapter: (args: { modelName: string }) => AnyTextAdapter;
+ getTextAdapter: (args: { modelName: string }) => AiTextAdapter;
};
export type AiAdapterFactory = (args: { config: Config }) => AiAdapter;
diff --git a/apps/papra-server/src/modules/ai/adapters/ai-adapters.usecases.ts b/apps/papra-server/src/modules/ai/adapters/ai-adapters.usecases.ts
index 01b62f56..6746dc1b 100644
--- a/apps/papra-server/src/modules/ai/adapters/ai-adapters.usecases.ts
+++ b/apps/papra-server/src/modules/ai/adapters/ai-adapters.usecases.ts
@@ -1,19 +1,17 @@
-import type { AnyTextAdapter } from '@tanstack/ai';
import type { Config } from '../../config/config.types';
import { parseModelId } from '../ai.schemas.models';
import { modelAdapterFactories } from './ai-adapters.registry';
import type { ModelAdapterNames } from './ai-adapters.registry';
import { createError } from '../../shared/errors/errors';
+import type { AiAdapter, AiTextAdapter } from './ai-adapters.types';
-export function resolveTextAdapter({
- modelId,
+export function resolveAdapter({
+ adapterId,
config,
}: {
- modelId: string;
+ adapterId?: string;
config: Config;
-}): AnyTextAdapter {
- const { modelName, adapterId } = parseModelId(modelId);
-
+}): AiAdapter {
const adapterName = adapterId ?? config.ai.defaultAdapterName;
const adapterFactory = modelAdapterFactories[adapterName as ModelAdapterNames];
@@ -29,5 +27,19 @@ export function resolveTextAdapter({
const adapter = adapterFactory({ config });
+ return adapter;
+}
+
+export function resolveTextAdapter({
+ modelId,
+ config,
+}: {
+ modelId: string;
+ config: Config;
+}): AiTextAdapter {
+ const { modelName, adapterId } = parseModelId(modelId);
+
+ const adapter = resolveAdapter({ adapterId, config });
+
return adapter.getTextAdapter({ modelName });
}
diff --git a/apps/papra-server/src/modules/ai/adapters/mistral/mistral.ai-adapters.config.ts b/apps/papra-server/src/modules/ai/adapters/mistral/mistral.ai-adapters.config.ts
index a880d894..859d6878 100644
--- a/apps/papra-server/src/modules/ai/adapters/mistral/mistral.ai-adapters.config.ts
+++ b/apps/papra-server/src/modules/ai/adapters/mistral/mistral.ai-adapters.config.ts
@@ -14,7 +14,7 @@ export const mistralAdapterConfig = {
doc: 'API key for the Mistral API',
schema: v.string(),
env: 'MISTRAL_API_KEY',
- default: 'mistral-api-key',
+ default: '',
showInDocumentation: false,
},
} as const satisfies AppConfigDefinition;
diff --git a/apps/papra-server/src/modules/config/config.ts b/apps/papra-server/src/modules/config/config.ts
index e56bf2bd..312c24f7 100644
--- a/apps/papra-server/src/modules/config/config.ts
+++ b/apps/papra-server/src/modules/config/config.ts
@@ -40,6 +40,7 @@ import { getCommitInfo } from './config.usecases';
import { planEntitlementsConfig } from '../plan-entitlements/plan-entitlements.config';
import { aiConfig } from '../ai/ai.config';
import { autoTaggingConfig } from '../auto-tagging/auto-tagging.config';
+import { documentContentExtractionConfig } from '../documents/content-extraction/content-extraction.config';
export const configDefinition = {
env: {
@@ -159,6 +160,7 @@ export const configDefinition = {
database: databaseConfig,
documents: documentsConfig,
documentsStorage: documentStorageConfig,
+ documentContentExtraction: documentContentExtractionConfig,
documentSearch: documentSearchConfig,
auth: authConfig,
ingestionFolder: ingestionFolderConfig,
diff --git a/apps/papra-server/src/modules/documents/content-extraction/content-extraction-strategies/content-extraction-strategies.types.ts b/apps/papra-server/src/modules/documents/content-extraction/content-extraction-strategies/content-extraction-strategies.types.ts
new file mode 100644
index 00000000..bd12a1ca
--- /dev/null
+++ b/apps/papra-server/src/modules/documents/content-extraction/content-extraction-strategies/content-extraction-strategies.types.ts
@@ -0,0 +1,8 @@
+export type ContentExtractionStrategy = {
+ name: string;
+ canExtractTextFromDocument: (args: { file: File }) => Promise<boolean>;
+ extractTextFromDocument: (args: {
+ file: File;
+ ocrLanguages?: string[];
+ }) => Promise<{ text: string; extractionContext?: Record<string, unknown> }>;
+};
diff --git a/apps/papra-server/src/modules/documents/content-extraction/content-extraction-strategies/content-extraction.strategies.ts b/apps/papra-server/src/modules/documents/content-extraction/content-extraction-strategies/content-extraction.strategies.ts
new file mode 100644
index 00000000..3a19f6de
--- /dev/null
+++ b/apps/papra-server/src/modules/documents/content-extraction/content-extraction-strategies/content-extraction.strategies.ts
@@ -0,0 +1,17 @@
+import { buildLectureContentExtractionStrategy } from './lecture/lecture.content-extraction-strategy';
+import { buildMistralOcrContentExtractionStrategy } from './mistral-ocr/mistral-ocr.content-extraction-strategy';
+
+export const strategiesRegistry = {
+ 'internal': buildLectureContentExtractionStrategy,
+ 'mistral-ocr': buildMistralOcrContentExtractionStrategy,
+};
+
+export function getContentExtractionStrategy({ strategyName }: { strategyName: string }) {
+ const strategyBuilder = strategiesRegistry[strategyName as keyof typeof strategiesRegistry];
+
+ if (!strategyBuilder) {
+ throw new Error(`Content extraction strategy "${strategyName}" is not registered.`);
+ }
+
+ return strategyBuilder;
+}
diff --git a/apps/papra-server/src/modules/documents/content-extraction/content-extraction-strategies/lecture/lecture.content-extraction-strategy.ts b/apps/papra-server/src/modules/documents/content-extraction/content-extraction-strategies/lecture/lecture.content-extraction-strategy.ts
new file mode 100644
index 00000000..bf7df8d3
--- /dev/null
+++ b/apps/papra-server/src/modules/documents/content-extraction/content-extraction-strategies/lecture/lecture.content-extraction-strategy.ts
@@ -0,0 +1,20 @@
+import type { ContentExtractionStrategy } from '../content-extraction-strategies.types';
+import { extractTextFromFile } from '@papra/lecture';
+
+export function buildLectureContentExtractionStrategy(): ContentExtractionStrategy {
+ return {
+ name: 'internal',
+ canExtractTextFromDocument: async () => true,
+ extractTextFromDocument: async ({ file, ocrLanguages }) => {
+ const { textContent, extractorType } = await extractTextFromFile({
+ file,
+ config: { tesseract: { languages: ocrLanguages } },
+ });
+
+ return {
+ text: textContent ?? '',
+ extractionContext: { extractorType },
+ };
+ },
+ };
+}
diff --git a/apps/papra-server/src/modules/documents/content-extraction/content-extraction-strategies/mistral-ocr/mistral-ocr.content-extraction-strategy.config.ts b/apps/papra-server/src/modules/documents/content-extraction/content-extraction-strategies/mistral-ocr/mistral-ocr.content-extraction-strategy.config.ts
new file mode 100644
index 00000000..a1c75fe2
--- /dev/null
+++ b/apps/papra-server/src/modules/documents/content-extraction/content-extraction-strategies/mistral-ocr/mistral-ocr.content-extraction-strategy.config.ts
@@ -0,0 +1,28 @@
+import * as v from 'valibot';
+import type { AppConfigDefinition } from '../../../../config/config.types';
+
+export const mistralOcrConfig = {
+ modelName: {
+ doc: 'The name of the Mistral OCR model to use for text extraction.',
+ env: 'MISTRAL_OCR_MODEL_NAME',
+ schema: v.string(),
+ default: 'mistral-ocr-latest',
+ },
+ mimeTypesAllowList: {
+ doc: 'The list of mime types that the Mistral OCR strategy will be used for. If the document mime type is not in this list, the strategy will be skipped. Comma separated list. Supports wildcards, e.g. "image/*" matches all image mime types, and "*" matches all formats. Prefix an entry with "!" to negate it, e.g. "*,!image/png" allows everything except PNG. Negations always take precedence over allows, even more specific ones (e.g. "image/png,!image/*" rejects PNG).',
+ env: 'MISTRAL_OCR_MIME_TYPES_ALLOW_LIST',
+ schema: v.pipe(
+ v.string(),
+ v.transform(
+ (value) =>
+ new Set(
+ value
+ .split(',')
+ .map((s) => s.trim().toLowerCase())
+ .filter(Boolean),
+ ),
+ ),
+ ),
+ default: 'application/pdf,image/*',
+ },
+} as const satisfies AppConfigDefinition;
diff --git a/apps/papra-server/src/modules/documents/content-extraction/content-extraction-strategies/mistral-ocr/mistral-ocr.content-extraction-strategy.models.test.ts b/apps/papra-server/src/modules/documents/content-extraction/content-extraction-strategies/mistral-ocr/mistral-ocr.content-extraction-strategy.models.test.ts
new file mode 100644
index 00000000..dd458c8f
--- /dev/null
+++ b/apps/papra-server/src/modules/documents/content-extraction/content-extraction-strategies/mistral-ocr/mistral-ocr.content-extraction-strategy.models.test.ts
@@ -0,0 +1,44 @@
+import { describe, expect, test } from 'vitest';
+import { IN_BYTES } from '../../../../shared/units';
+import { isMistralOcrAbleToExtractTextFromDocument } from './mistral-ocr.content-extraction-strategy.models';
+
+describe('mistral-ocr.models', () => {
+ describe('isMistralOcrAbleToExtractTextFromDocument', () => {
+ test('mistral ocrs can extract text allowed mime types and files below 50MB', () => {
+ expect(
+ isMistralOcrAbleToExtractTextFromDocument({
+ file: { type: 'application/pdf', size: 49 * IN_BYTES.MEGABYTE },
+ mimeTypesAllowList: new Set(['application/pdf', 'image/*']),
+ }),
+ ).to.eql(true);
+
+ expect(
+ isMistralOcrAbleToExtractTextFromDocument({
+ file: { type: 'application/pdf', size: 51 * IN_BYTES.MEGABYTE },
+ mimeTypesAllowList: new Set(['application/pdf', 'image/*']),
+ }),
+ ).to.eql(false);
+
+ expect(
+ isMistralOcrAbleToExtractTextFromDocument({
+ file: { type: 'image/png', size: 49 * IN_BYTES.MEGABYTE },
+ mimeTypesAllowList: new Set(['application/pdf', 'image/*']),
+ }),
+ ).to.eql(true);
+
+ expect(
+ isMistralOcrAbleToExtractTextFromDocument({
+ file: { type: 'image/png', size: 51 * IN_BYTES.MEGABYTE },
+ mimeTypesAllowList: new Set(['application/pdf', 'image/*']),
+ }),
+ ).to.eql(false);
+
+ expect(
+ isMistralOcrAbleToExtractTextFromDocument({
+ file: { type: 'image/jpeg', size: 49 * IN_BYTES.MEGABYTE },
+ mimeTypesAllowList: new Set(['application/pdf', 'image/*']),
+ }),
+ ).to.eql(true);
+ });
+ });
+});
diff --git a/apps/papra-server/src/modules/documents/content-extraction/content-extraction-strategies/mistral-ocr/mistral-ocr.content-extraction-strategy.models.ts b/apps/papra-server/src/modules/documents/content-extraction/content-extraction-strategies/mistral-ocr/mistral-ocr.content-extraction-strategy.models.ts
new file mode 100644
index 00000000..e3ff40c9
--- /dev/null
+++ b/apps/papra-server/src/modules/documents/content-extraction/content-extraction-strategies/mistral-ocr/mistral-ocr.content-extraction-strategy.models.ts
@@ -0,0 +1,37 @@
+import { fileToBase64DataUrl } from '../../../../shared/files/files';
+import { isMimeTypeAllowed } from '../../../../shared/mime-types/mime-types.models';
+import { IN_BYTES } from '../../../../shared/units';
+
+export async function buildDocumentInfo({ file }: { file: File }) {
+ const isImage = file.type.startsWith('image/');
+ const dataUrl = await fileToBase64DataUrl(file);
+
+ if (isImage) {
+ return {
+ type: 'image_url',
+ image_url: dataUrl,
+ };
+ }
+
+ return {
+ type: 'document_url',
+ document_url: dataUrl,
+ };
+}
+
+export function isMistralOcrAbleToExtractTextFromDocument({
+ file,
+ mimeTypesAllowList,
+}: {
+ file: { type: string; size: number };
+ mimeTypesAllowList: Set<string>;
+}) {
+ const isFormatSupported = isMimeTypeAllowed({
+ mimeType: file.type,
+ allowList: mimeTypesAllowList,
+ });
+
+ const isBelowSizeLimit = file.size < 50 * IN_BYTES.MEGABYTE;
+
+ return isBelowSizeLimit && isFormatSupported;
+}
diff --git a/apps/papra-server/src/modules/documents/content-extraction/content-extraction-strategies/mistral-ocr/mistral-ocr.content-extraction-strategy.ts b/apps/papra-server/src/modules/documents/content-extraction/content-extraction-strategies/mistral-ocr/mistral-ocr.content-extraction-strategy.ts
new file mode 100644
index 00000000..1804dea6
--- /dev/null
+++ b/apps/papra-server/src/modules/documents/content-extraction/content-extraction-strategies/mistral-ocr/mistral-ocr.content-extraction-strategy.ts
@@ -0,0 +1,35 @@
+import type { Config } from '../../../../config/config.types';
+import type { ContentExtractionStrategy } from '../content-extraction-strategies.types';
+import { extractTextWithMistralOcr } from './mistral-ocr.content-extraction-strategy.usecases';
+import { isMistralOcrAbleToExtractTextFromDocument } from './mistral-ocr.content-extraction-strategy.models';
+import { isNilOrEmptyString } from '../../../../shared/utils';
+
+export function buildMistralOcrContentExtractionStrategy({
+ config,
+}: {
+ config: Config;
+}): ContentExtractionStrategy {
+ const { modelName, mimeTypesAllowList } = config.documentContentExtraction.strategy.mistralOcr;
+ const { baseUrl, apiKey } = config.ai.adapters.mistral;
+
+ return {
+ name: 'mistral-ocr',
+ canExtractTextFromDocument: async ({ file }) => {
+ if (isNilOrEmptyString(apiKey)) {
+ return false;
+ }
+
+ return isMistralOcrAbleToExtractTextFromDocument({ file, mimeTypesAllowList });
+ },
+ extractTextFromDocument: async ({ file }) => {
+ const { text, processedPagesCount } = await extractTextWithMistralOcr({
+ file,
+ modelName,
+ baseUrl,
+ apiKey,
+ });
+
+ return { text, extractionContext: { processedPagesCount } };
+ },
+ };
+}
diff --git a/apps/papra-server/src/modules/documents/content-extraction/content-extraction-strategies/mistral-ocr/mistral-ocr.content-extraction-strategy.usecases.ts b/apps/papra-server/src/modules/documents/content-extraction/content-extraction-strategies/mistral-ocr/mistral-ocr.content-extraction-strategy.usecases.ts
new file mode 100644
index 00000000..f0a66afa
--- /dev/null
+++ b/apps/papra-server/src/modules/documents/content-extraction/content-extraction-strategies/mistral-ocr/mistral-ocr.content-extraction-strategy.usecases.ts
@@ -0,0 +1,55 @@
+import { ofetch } from 'ofetch';
+import * as v from 'valibot';
+import { buildDocumentInfo } from './mistral-ocr.content-extraction-strategy.models';
+import { IN_MS } from '../../../../shared/units';
+
+const ocrResponseSchema = v.object({
+ pages: v.array(
+ v.object({
+ markdown: v.string(),
+ }),
+ ),
+ usage_info: v.object({
+ pages_processed: v.pipe(v.number(), v.safeInteger()),
+ }),
+});
+
+export async function extractTextWithMistralOcr({
+ file,
+ modelName,
+ baseUrl,
+ apiKey,
+}: {
+ file: File;
+
+ modelName: string;
+ baseUrl: string;
+ apiKey: string;
+}) {
+ const url = `${baseUrl.replace(/\/$/, '')}/ocr`;
+
+ const response = await ofetch<unknown>(url, {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ 'Authorization': `Bearer ${apiKey}`,
+ },
+ timeout: 30 * IN_MS.SECOND,
+ body: JSON.stringify({
+ model: modelName,
+ include_image_base64: false,
+ include_blocks: false,
+ table_format: 'markdown',
+ document: await buildDocumentInfo({ file }),
+ }),
+ });
+
+ const parsedResponse = v.parse(ocrResponseSchema, response);
+
+ const text = parsedResponse.pages.map((page) => page.markdown).join('\n\n');
+
+ return {
+ text,
+ processedPagesCount: parsedResponse.usage_info.pages_processed,
+ };
+}
diff --git a/apps/papra-server/src/modules/documents/content-extraction/content-extraction.config.ts b/apps/papra-server/src/modules/documents/content-extraction/content-extraction.config.ts
new file mode 100644
index 00000000..fad984a3
--- /dev/null
+++ b/apps/papra-server/src/modules/documents/content-extraction/content-extraction.config.ts
@@ -0,0 +1,25 @@
+import type { AppConfigDefinition } from '../../config/config.types';
+import * as v from 'valibot';
+import { mistralOcrConfig } from './content-extraction-strategies/mistral-ocr/mistral-ocr.content-extraction-strategy.config';
+
+export const documentContentExtractionConfig = {
+ extractionStrategies: {
+ doc: [
+ 'Content extraction strategy, it can be a single strategy name like `internal`, or a comma-separated list of strategy names, in order of preference, like `mistral-ocr,internal`. The first strategy that can extract text from the document will be used, and if a strategy fails processing a document, the next one will try. Available strategies are:',
+ '- `internal`: Uses the internal `lecture` library to extract text from documents, which support all common document formats and uses Tesseract for OCR. This strategy is always available, great to use as a fallback when other strategies fail.',
+ '- `mistral-ocr`: Uses the Mistral OCR API to extract text from documents. This strategy requires a valid Mistral API key.',
+ ].join('\n'),
+ schema: v.pipe(
+ v.string(),
+ v.transform((value) => value.split(',').map((s) => s.trim())),
+ v.array(v.picklist(['internal', 'mistral-ocr'])),
+ v.minLength(1, 'At least one content extraction strategy must be specified.'),
+ ),
+ env: 'CONTENT_EXTRACTION_STRATEGY',
+ default: 'internal',
+ showInDocumentation: false,
+ },
+ strategy: {
+ mistralOcr: mistralOcrConfig,
+ },
+} as const satisfies AppConfigDefinition;
diff --git a/apps/papra-server/src/modules/documents/content-extraction/content-extraction.errors.ts b/apps/papra-server/src/modules/documents/content-extraction/content-extraction.errors.ts
new file mode 100644
index 00000000..1afb21b6
--- /dev/null
+++ b/apps/papra-server/src/modules/documents/content-extraction/content-extraction.errors.ts
@@ -0,0 +1,8 @@
+import { createErrorFactory } from '../../shared/errors/errors';
+
+export const createNoStrategyAbleToExtractTextError = createErrorFactory({
+ code: 'content-extraction.noStrategyAbleToExtractText',
+ message: 'No strategy was able to extract text from the document',
+ statusCode: 500,
+ isInternal: true,
+});
diff --git a/apps/papra-server/src/modules/documents/content-extraction/content-extraction.usecases.ts b/apps/papra-server/src/modules/documents/content-extraction/content-extraction.usecases.ts
new file mode 100644
index 00000000..75fe977e
--- /dev/null
+++ b/apps/papra-server/src/modules/documents/content-extraction/content-extraction.usecases.ts
@@ -0,0 +1,79 @@
+import { createLogger } from '../../shared/logger/logger';
+import type { Logger } from '../../shared/logger/logger';
+import type { Config } from '../../config/config.types';
+import { getContentExtractionStrategy } from './content-extraction-strategies/content-extraction.strategies';
+import { castError } from '@corentinth/chisels';
+import { createNoStrategyAbleToExtractTextError } from './content-extraction.errors';
+
+export type ExtractDocumentTextUsecase = (args: { file: File }) => Promise<{ text: string }>;
+
+export function buildExtractDocumentTextUsecase({
+ ocrLanguages,
+ config,
+ logger = createLogger({ namespace: 'extract-document-text-usecase' }),
+}: {
+ ocrLanguages?: string[];
+ config: Config;
+ logger?: Logger;
+}) {
+ return async ({ file }: { file: File }) => {
+ const { extractionStrategies } = config.documentContentExtraction;
+ const errors: Error[] = [];
+
+ for (const strategyName of extractionStrategies) {
+ const strategyFactory = getContentExtractionStrategy({ strategyName });
+ const strategy = strategyFactory({ config });
+
+ try {
+ const canExtract = await strategy.canExtractTextFromDocument({ file });
+
+ if (!canExtract) {
+ logger.debug(`Strategy ${strategyName} cannot extract text from document, skipping.`);
+ continue;
+ }
+
+ logger.debug(`Using strategy ${strategyName} to extract text from document`);
+
+ const startingTime = Date.now();
+ const { text, extractionContext } = await strategy.extractTextFromDocument({
+ file,
+ ocrLanguages,
+ });
+ const durationMs = Date.now() - startingTime;
+
+ logger.info(
+ {
+ durationMs,
+ strategyName,
+ fileSize: file.size,
+ fileType: file.type,
+ ...extractionContext,
+ },
+ 'Extracted text from document',
+ );
+
+ return { text };
+ } catch (error) {
+ logger.error(
+ {
+ strategyName,
+ fileSize: file.size,
+ fileType: file.type,
+ error,
+ },
+ 'Error extracting text from document',
+ );
+ errors.push(castError(error));
+ }
+ }
+
+ if (errors.length === 0) {
+ throw createNoStrategyAbleToExtractTextError();
+ }
+
+ throw new AggregateError(
+ errors,
+ 'All content extraction strategies failed to extract text from document',
+ );
+ };
+}
diff --git a/apps/papra-server/src/modules/documents/documents.services.ts b/apps/papra-server/src/modules/documents/documents.services.ts
index ee63fee9..89cbfd0c 100644
--- a/apps/papra-server/src/modules/documents/documents.services.ts
+++ b/apps/papra-server/src/modules/documents/documents.services.ts
@@ -1,6 +1,3 @@
-import type { Logger } from '@crowlog/logger';
-import { extractTextFromFile } from '@papra/lecture';
-import { createLogger } from '../shared/logger/logger';
import { getStreamSha256Hash } from '../shared/streams/stream-hash';
export async function getFileHash({ fileStream }: { fileStream: ReadableStream<Uint8Array> }) {
@@ -8,34 +5,3 @@ export async function getFileHash({ fileStream }: { fileStream: ReadableStream<U
return { hash };
}
-
-export async function extractDocumentText({
- file,
- ocrLanguages,
- logger = createLogger({ namespace: 'documents:services' }),
-}: {
- file: File;
- ocrLanguages?: string[];
- logger?: Logger;
-}) {
- const startTime = Date.now();
- const { textContent, error, extractorName, extractorType } = await extractTextFromFile({
- file,
- config: { tesseract: { languages: ocrLanguages } },
- logger,
- });
- const durationMs = Date.now() - startTime;
-
- if (error) {
- logger.error(
- { error, extractorName, extractorType, durationMs },
- 'Error while extracting text from document',
- );
- } else {
- logger.info({ extractorName, extractorType, durationMs }, 'Text extracted from document');
- }
-
- return {
- text: textContent ?? '',
- };
-}
diff --git a/apps/papra-server/src/modules/documents/documents.usecases.test.ts b/apps/papra-server/src/modules/documents/documents.usecases.test.ts
index 8d88f0c2..cd1d82e7 100644
--- a/apps/papra-server/src/modules/documents/documents.usecases.test.ts
+++ b/apps/papra-server/src/modules/documents/documents.usecases.test.ts
@@ -712,6 +712,9 @@ describe('documents usecases', () => {
taggingRulesRepository,
tagsRepository,
eventServices: createTestEventServices(),
+ extractDocumentText: async ({ file }) => ({
+ text: await collectReadableStreamToString({ stream: file.stream() }),
+ }),
});
const documentRecords = await db.select().from(documentsTable);
@@ -772,6 +775,9 @@ describe('documents usecases', () => {
taggingRulesRepository,
tagsRepository,
eventServices,
+ extractDocumentText: async ({ file }) => ({
+ text: await collectReadableStreamToString({ stream: file.stream() }),
+ }),
});
expect(eventServices.getEmittedEvents().map(({ eventName }) => eventName)).to.eql([
diff --git a/apps/papra-server/src/modules/documents/documents.usecases.ts b/apps/papra-server/src/modules/documents/documents.usecases.ts
index a3d315d3..08b636dc 100644
--- a/apps/papra-server/src/modules/documents/documents.usecases.ts
+++ b/apps/papra-server/src/modules/documents/documents.usecases.ts
@@ -48,8 +48,8 @@ import {
generateDocumentId as generateDocumentIdImpl,
} from './documents.models';
import { createDocumentsRepository } from './documents.repository';
-import { extractDocumentText } from './documents.services';
import { createStorageKey } from './storage/document-storage.usecases';
+import type { ExtractDocumentTextUsecase } from './content-extraction/content-extraction.usecases';
type DocumentStorageContext = {
storageKey: string;
@@ -578,19 +578,19 @@ export async function extractAndSaveDocumentFileContent({
organizationId,
documentsRepository,
documentsStorageService,
- ocrLanguages,
taggingRulesRepository,
tagsRepository,
eventServices,
+ extractDocumentText,
}: {
documentId: string;
- ocrLanguages?: string[];
organizationId: string;
documentsRepository: DocumentsRepository;
documentsStorageService: DocumentStorageService;
taggingRulesRepository: TaggingRulesRepository;
tagsRepository: TagsRepository;
eventServices: EventServices;
+ extractDocumentText: ExtractDocumentTextUsecase;
}) {
const { document } = await documentsRepository.getDocumentById({ documentId, organizationId });
@@ -611,7 +611,7 @@ export async function extractAndSaveDocumentFileContent({
mimeType: document.mimeType,
});
- const { text } = await extractDocumentText({ file, ocrLanguages });
+ const { text } = await extractDocumentText({ file });
const { document: updatedDocument } = await updateDocument({
documentId,
diff --git a/apps/papra-server/src/modules/documents/tasks/extract-document-file-content.task.ts b/apps/papra-server/src/modules/documents/tasks/extract-document-file-content.task.ts
index 8a2d12c3..cd820300 100644
--- a/apps/papra-server/src/modules/documents/tasks/extract-document-file-content.task.ts
+++ b/apps/papra-server/src/modules/documents/tasks/extract-document-file-content.task.ts
@@ -7,6 +7,7 @@ import { createTagsRepository } from '../../tags/tags.repository';
import { createDocumentsRepository } from '../documents.repository';
import { extractAndSaveDocumentFileContent } from '../documents.usecases';
import type { Config } from '../../config/config.types';
+import { buildExtractDocumentTextUsecase } from '../content-extraction/content-extraction.usecases';
export async function registerExtractDocumentFileContentTask({
taskServices,
@@ -37,15 +38,20 @@ export async function registerExtractDocumentFileContentTask({
ocrLanguages: string[];
};
+ const extractDocumentText = buildExtractDocumentTextUsecase({
+ ocrLanguages,
+ config,
+ });
+
await extractAndSaveDocumentFileContent({
documentId,
organizationId,
- ocrLanguages,
documentsRepository,
documentsStorageService,
taggingRulesRepository,
tagsRepository,
eventServices,
+ extractDocumentText,
});
if (!config.ai.isEnabled || !config.autoTagging.isEnabled) {
diff --git a/apps/papra-server/src/modules/shared/files/files.test.ts b/apps/papra-server/src/modules/shared/files/files.test.ts
new file mode 100644
index 00000000..fe96771e
--- /dev/null
+++ b/apps/papra-server/src/modules/shared/files/files.test.ts
@@ -0,0 +1,12 @@
+import { describe, expect, test } from 'vitest';
+import { fileToBase64DataUrl } from './files';
+
+describe('files', () => {
+ describe('fileToBase64DataUrl', () => {
+ test('builds a base64 data url from a file', async () => {
+ const file = new File(['lorem ipsum'], 'text-file.txt', { type: 'text/plain' });
+
+ expect(await fileToBase64DataUrl(file)).to.eql('data:text/plain;base64,bG9yZW0gaXBzdW0=');
+ });
+ });
+});
diff --git a/apps/papra-server/src/modules/shared/files/files.ts b/apps/papra-server/src/modules/shared/files/files.ts
new file mode 100644
index 00000000..9cea6929
--- /dev/null
+++ b/apps/papra-server/src/modules/shared/files/files.ts
@@ -0,0 +1,5 @@
+export async function fileToBase64DataUrl(file: File): Promise<string> {
+ const arrayBuffer = await file.arrayBuffer();
+ const base64String = Buffer.from(arrayBuffer).toString('base64');
+ return `data:${file.type};base64,${base64String}`;
+}
diff --git a/apps/papra-server/src/modules/shared/mime-types/mime-types.models.test.ts b/apps/papra-server/src/modules/shared/mime-types/mime-types.models.test.ts
new file mode 100644
index 00000000..9764c957
--- /dev/null
+++ b/apps/papra-server/src/modules/shared/mime-types/mime-types.models.test.ts
@@ -0,0 +1,183 @@
+import { describe, expect, test } from 'vitest';
+import { isMimeTypeAllowed } from './mime-types.models';
+
+describe('mime-types.models', () => {
+ describe('isMimeTypeAllowed', () => {
+ test('a mime type is allowed if it is in the list of supported mime types', () => {
+ expect(
+ isMimeTypeAllowed({
+ mimeType: 'application/pdf',
+ allowList: new Set(['application/pdf', 'image/png']),
+ }),
+ ).to.eql(true);
+
+ expect(
+ isMimeTypeAllowed({
+ mimeType: 'text/plain',
+ allowList: new Set(['application/pdf', 'image/png']),
+ }),
+ ).to.eql(false);
+ });
+
+ test('a single wildcard allow all mime type', () => {
+ expect(
+ isMimeTypeAllowed({
+ mimeType: 'application/pdf',
+ allowList: new Set(['*']),
+ }),
+ ).to.eql(true);
+
+ expect(
+ isMimeTypeAllowed({
+ mimeType: 'application/pdf',
+ allowList: new Set(['*', 'image/png']),
+ }),
+ ).to.eql(true);
+ });
+
+ test('a wildcard in the type part of the mime type allows all mime types of that type', () => {
+ expect(
+ isMimeTypeAllowed({
+ mimeType: 'application/pdf',
+ allowList: new Set(['application/*']),
+ }),
+ ).to.eql(true);
+
+ expect(
+ isMimeTypeAllowed({
+ mimeType: 'image/png',
+ allowList: new Set(['application/*']),
+ }),
+ ).to.eql(false);
+ });
+
+ test('an exclamation mark in front of a mime type negates it', () => {
+ expect(
+ isMimeTypeAllowed({
+ mimeType: 'application/pdf',
+ allowList: new Set(['!application/pdf']),
+ }),
+ ).to.eql(false);
+
+ expect(
+ isMimeTypeAllowed({
+ mimeType: 'image/png',
+ allowList: new Set(['!application/pdf']),
+ }),
+ ).to.eql(false);
+
+ expect(
+ isMimeTypeAllowed({
+ mimeType: 'image/png',
+ allowList: new Set(['image/png', '!application/pdf']),
+ }),
+ ).to.eql(true);
+ });
+
+ test('negations works with wildcards', () => {
+ expect(
+ isMimeTypeAllowed({
+ mimeType: 'application/pdf',
+ allowList: new Set(['!application/*']),
+ }),
+ ).to.eql(false);
+
+ expect(
+ isMimeTypeAllowed({
+ mimeType: 'image/png',
+ allowList: new Set(['image/png', '!application/*']),
+ }),
+ ).to.eql(true);
+
+ expect(
+ isMimeTypeAllowed({
+ mimeType: 'image/png',
+ allowList: new Set(['*', '!image/*']),
+ }),
+ ).to.eql(false);
+
+ expect(
+ isMimeTypeAllowed({
+ mimeType: 'application/pdf',
+ allowList: new Set(['*', '!image/*']),
+ }),
+ ).to.eql(true);
+
+ expect(
+ isMimeTypeAllowed({
+ mimeType: 'image/png',
+ allowList: new Set(['*', '!image/png']),
+ }),
+ ).to.eql(false);
+
+ expect(
+ isMimeTypeAllowed({
+ mimeType: 'image/jpeg',
+ allowList: new Set(['*', '!image/png']),
+ }),
+ ).to.eql(true);
+ });
+
+ test('a negation takes precedence over a positive match', () => {
+ expect(
+ isMimeTypeAllowed({
+ mimeType: 'image/png',
+ allowList: new Set(['image/png', '!image/png']),
+ }),
+ ).to.eql(false);
+ });
+
+ test('a mimetype can have parameters', () => {
+ expect(
+ isMimeTypeAllowed({
+ mimeType: 'application/pdf; charset=utf-8',
+ allowList: new Set(['application/pdf']),
+ }),
+ ).to.eql(true);
+
+ expect(
+ isMimeTypeAllowed({
+ mimeType: 'application/pdf; charset=utf-8',
+ allowList: new Set(['*', '!application/pdf']),
+ }),
+ ).to.eql(false);
+ });
+
+ test('invalid mimetype is not allowed', () => {
+ expect(
+ isMimeTypeAllowed({
+ mimeType: 'application',
+ allowList: new Set(['*']),
+ }),
+ ).to.eql(false);
+
+ expect(
+ isMimeTypeAllowed({
+ mimeType: 'application/',
+ allowList: new Set(['*']),
+ }),
+ ).to.eql(false);
+
+ expect(
+ isMimeTypeAllowed({
+ mimeType: '/pdf',
+ allowList: new Set(['*']),
+ }),
+ ).to.eql(false);
+
+ expect(
+ isMimeTypeAllowed({
+ mimeType: 'application/pdf/extra',
+ allowList: new Set(['*']),
+ }),
+ ).to.eql(false);
+
+ expect(
+ isMimeTypeAllowed({
+ mimeType: '',
+ allowList: new Set(['*']),
+ }),
+ ).to.eql(false);
+ });
+ });
+});
diff --git a/apps/papra-server/src/modules/shared/mime-types/mime-types.models.ts b/apps/papra-server/src/modules/shared/mime-types/mime-types.models.ts
index b75bbef4..3eb20c15 100644
--- a/apps/papra-server/src/modules/shared/mime-types/mime-types.models.ts
+++ b/apps/papra-server/src/modules/shared/mime-types/mime-types.models.ts
@@ -1,8 +1,38 @@
import mime from 'mime-types';
import { MIME_TYPES } from './mime-types.constants';
+import { isNilOrEmptyString } from '../utils';
export function getMimeTypeFromFileName(fileName: string): string {
const mimeType = mime.lookup(fileName);
return mimeType === false ? MIME_TYPES.OCTET_STREAM : mimeType;
}
+
+export function isMimeTypeAllowed({
+ mimeType,
+ allowList,
+}: {
+ mimeType: string;
+ allowList: Set<string>;
+}): boolean {
+ const mimeTypeWithoutParameters = mimeType.split(';')[0]!.trim();
+ const [type, subtype, ...rest] = mimeTypeWithoutParameters.split('/');
+
+ if (isNilOrEmptyString(type) || isNilOrEmptyString(subtype) || rest.length > 0) {
+ return false;
+ }
+
+ const group = `${type}/*`;
+ const negatedMimeType = `!${mimeTypeWithoutParameters}`;
+ const negatedGroup = `!${group}`;
+
+ if (allowList.has(negatedMimeType) || allowList.has(negatedGroup)) {
+ return false;
+ }
+
+ if (allowList.has(mimeTypeWithoutParameters) || allowList.has(group) || allowList.has('*')) {
+ return true;
+ }
+
+ return false;
+}