Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(documents): add document reprocessing (#1517)

Corentin Thomasset956684c53c62
diff --git a/.changeset/pink-rings-build.md b/.changeset/pink-rings-build.md
new file mode 100644
index 00000000..f6a02567
--- /dev/null
+++ b/.changeset/pink-rings-build.md
@@ -0,0 +1,5 @@
+---
+'@papra/app': patch
+---
+
+Added a button to reprocess a document in the documents content tab.
diff --git a/apps/papra-client/src/locales/en.dictionary.ts b/apps/papra-client/src/locales/en.dictionary.ts
index 3f64a3b8..322cbd0e 100644
--- a/apps/papra-client/src/locales/en.dictionary.ts
+++ b/apps/papra-client/src/locales/en.dictionary.ts
@@ -424,6 +424,18 @@ export const translations = {
'documents.actions.cancel': 'Cancel',
'documents.actions.save': 'Save',
'documents.actions.saving': 'Saving...',
+ 'documents.reprocess.action': 'Reprocess document',
+ 'documents.reprocess.confirm.title': 'Reprocess document?',
+ 'documents.reprocess.confirm.description':
+ 'The content of the document will be re-extracted and the tagging rules will be re-applied. Manual changes to the content will be lost. The operation is done in the background and may take a few minutes to complete.',
+ 'documents.reprocess.confirm.submit': 'Reprocess',
+ 'documents.reprocess.queued': 'Document reprocessing queued',
+ 'documents.reprocess.queued.description':
+ 'Processing runs in the background, it may take a few minutes to complete, refresh to see the updated content.',
+ 'documents.reprocess.error': 'Failed to queue document reprocessing. Please try again.',
+ 'documents.reprocess.rate-limited':
+ 'Your organization has reached the reprocessing limit. Please try again later.',
+ 'documents.reprocess.disabled': 'Document reprocessing is disabled on this server.',
'documents.content.alert':
'The content of the document is automatically extracted from the document on upload. It is only used for search and indexing purposes.',
'documents.content.empty-placeholder':
diff --git a/apps/papra-client/src/modules/config/config.ts b/apps/papra-client/src/modules/config/config.ts
index c1a103d7..79aa0f08 100644
--- a/apps/papra-client/src/modules/config/config.ts
+++ b/apps/papra-client/src/modules/config/config.ts
@@ -65,6 +65,8 @@ export const buildTimeConfig = {
},
},
documents: {
+ // Only offer reprocessing when the server advertises support (also keeps demo mode disabled).
+ isReprocessingEnabled: false as boolean,
deletedDocumentsRetentionDays: asNumber(
import.meta.env.VITE_DOCUMENTS_DELETED_DOCUMENTS_RETENTION_DAYS,
30,
diff --git a/apps/papra-client/src/modules/documents/components/document-content-edition-panel.component.tsx b/apps/papra-client/src/modules/documents/components/document-content-edition-panel.component.tsx
index d5d95a68..4fd69abc 100644
--- a/apps/papra-client/src/modules/documents/components/document-content-edition-panel.component.tsx
+++ b/apps/papra-client/src/modules/documents/components/document-content-edition-panel.component.tsx
@@ -1,6 +1,8 @@
import type { Component } from 'solid-js';
+import type { Document } from '../documents.types';
import { useMutation, useQueryClient } from '@tanstack/solid-query';
import { createSignal, Show } from 'solid-js';
+import { useConfig } from '@/modules/config/config.provider';
import { useI18n } from '@/modules/i18n/i18n.provider';
import { cn } from '@/modules/shared/style/cn';
import { Alert, AlertDescription } from '@/modules/ui/components/alert';
@@ -8,31 +10,30 @@ import { Button } from '@/modules/ui/components/button';
import { createToast } from '@/modules/ui/components/sonner';
import { TextArea } from '@/modules/ui/components/textarea';
import { TextFieldRoot } from '@/modules/ui/components/textfield';
+import { useReprocessDocument } from '../documents.composables';
import { updateDocument } from '../documents.services';
-export const DocumentContentEditionPanel: Component<{
- documentId: string;
- organizationId: string;
- content: string;
-}> = (props) => {
+export const DocumentContentEditionPanel: Component<{ document: Document }> = (props) => {
const { t } = useI18n();
+ const { config } = useConfig();
+ const { reprocess, getIsReprocessing } = useReprocessDocument();
const queryClient = useQueryClient();
const [isEditing, setIsEditing] = createSignal(false);
- const [getContent, setContent] = createSignal(props.content);
+ const [getContent, setContent] = createSignal(props.document.content);
const updateMutation = useMutation(() => ({
mutationFn: async ({ content }: { content: string }) =>
updateDocument({
- documentId: props.documentId,
- organizationId: props.organizationId,
+ documentId: props.document.id,
+ organizationId: props.document.organizationId,
content,
}),
onSuccess: () => {
createToast({ type: 'success', message: 'Document content updated' });
setIsEditing(false);
void queryClient.invalidateQueries({
- queryKey: ['organizations', props.organizationId, 'documents', props.documentId],
+ queryKey: ['organizations', props.document.organizationId, 'documents', props.document.id],
});
},
onError: () => {
@@ -41,12 +42,13 @@ export const DocumentContentEditionPanel: Component<{
}));
const handleEdit = () => {
+ setContent(props.document.content);
setIsEditing(true);
};
const handleCancel = () => {
setIsEditing(false);
- setContent(props.content);
+ setContent(props.document.content);
};
const handleSave = () => {
@@ -57,7 +59,7 @@ export const DocumentContentEditionPanel: Component<{
<div class="flex flex-col gap-2">
<TextFieldRoot>
<TextArea
- value={getContent()}
+ value={isEditing() ? getContent() : props.document.content}
onInput={(e) => setContent(e.currentTarget.value)}
class={cn('font-mono placeholder:italic max-h-500px', {
'bg-muted text-muted-foreground': !isEditing(),
@@ -68,11 +70,23 @@ export const DocumentContentEditionPanel: Component<{
autoResize
/>
</TextFieldRoot>
- <div class="flex justify-end gap-2">
+ <div class="flex flex-wrap justify-end gap-2">
+ <Show
+ when={config.documents.isReprocessingEnabled && !props.document.isDeleted && !isEditing()}
+ >
+ <Button
+ variant="outline"
+ onClick={async () => reprocess({ document: props.document })}
+ isLoading={getIsReprocessing()}
+ >
+ <div class="i-tabler-refresh size-4 mr-2" />
+ {t('documents.reprocess.action')}
+ </Button>
+ </Show>
<Show
when={isEditing()}
fallback={
- <Button variant="outline" onClick={handleEdit}>
+ <Button variant="outline" onClick={handleEdit} disabled={getIsReprocessing()}>
<div class="i-tabler-edit size-4 mr-2" />
{t('documents.actions.edit')}
</Button>
diff --git a/apps/papra-client/src/modules/documents/components/document-management-dropdown.component.tsx b/apps/papra-client/src/modules/documents/components/document-management-dropdown.component.tsx
index 9172e8fe..93081fd5 100644
--- a/apps/papra-client/src/modules/documents/components/document-management-dropdown.component.tsx
+++ b/apps/papra-client/src/modules/documents/components/document-management-dropdown.component.tsx
@@ -3,6 +3,7 @@ import type { Component } from 'solid-js';
import type { Document } from '../documents.types';
import { A } from '@solidjs/router';
import { Show } from 'solid-js';
+import { useConfig } from '@/modules/config/config.provider';
import { useShareDocumentDialog } from '@/modules/document-share-links/components/share-document-dialog.component';
import { useI18n } from '@/modules/i18n/i18n.provider';
import { Button } from '@/modules/ui/components/button';
@@ -16,11 +17,17 @@ import {
DropdownMenuTrigger,
} from '@/modules/ui/components/dropdown-menu';
import { getDocumentOpenWithApps } from '../document.models';
-import { useDeleteDocument, useDownloadDocument } from '../documents.composables';
+import {
+ useDeleteDocument,
+ useDownloadDocument,
+ useReprocessDocument,
+} from '../documents.composables';
import { DocumentOpenWithDropdownItems } from './open-with.component';
import { useRenameDocumentDialog } from './rename-document-button.component';
export const DocumentManagementDropdown: Component<{ document: Document }> = (props) => {
+ const { config } = useConfig();
+ const { reprocess, getIsReprocessing } = useReprocessDocument();
const { deleteDocument } = useDeleteDocument();
const { downloadDocument } = useDownloadDocument();
const { openRenameDialog } = useRenameDocumentDialog();
@@ -108,6 +115,17 @@ export const DocumentManagementDropdown: Component<{ document: Document }> = (pr
<span>{t('documents.management.rename')}</span>
</DropdownMenuItem>
+ <Show when={config.documents.isReprocessingEnabled && !props.document.isDeleted}>
+ <DropdownMenuItem
+ class="cursor-pointer"
+ disabled={getIsReprocessing()}
+ onClick={async () => reprocess({ document: props.document })}
+ >
+ <div class="i-tabler-refresh size-4 mr-2" />
+ <span>{t('documents.reprocess.action')}</span>
+ </DropdownMenuItem>
+ </Show>
+
<DropdownMenuItem class="cursor-pointer text-red" onClick={async () => deleteDoc()}>
<div class="i-tabler-trash size-4 mr-2" />
<span>{t('documents.management.delete')}</span>
diff --git a/apps/papra-client/src/modules/documents/documents.composables.tsx b/apps/papra-client/src/modules/documents/documents.composables.tsx
index 21f65c24..5006fa0e 100644
--- a/apps/papra-client/src/modules/documents/documents.composables.tsx
+++ b/apps/papra-client/src/modules/documents/documents.composables.tsx
@@ -2,6 +2,7 @@ import type { Document } from './documents.types';
import { createSignal } from 'solid-js';
import { useI18n } from '@/modules/i18n/i18n.provider';
import { downloadFile } from '@/modules/shared/files/download';
+import { isHttpErrorWithCode, isRateLimitError } from '@/modules/shared/http/http-errors';
import { useConfirmModal } from '../shared/confirm';
import { queryClient } from '../shared/query/query-client';
import { createToast } from '../ui/components/sonner';
@@ -9,6 +10,7 @@ import {
deleteDocument,
fetchDocument,
fetchDocumentFile,
+ reprocessDocument,
restoreDocument,
} from './documents.services';
@@ -107,6 +109,59 @@ export function useDeleteDocument() {
};
}
+export function useReprocessDocument() {
+ const { confirm } = useConfirmModal();
+ const { t } = useI18n();
+ const [getIsReprocessing, setIsReprocessing] = createSignal(false);
+
+ return {
+ getIsReprocessing,
+ reprocess: async ({ document }: { document: Document }) => {
+ if (getIsReprocessing() || document.isDeleted) {
+ return;
+ }
+
+ setIsReprocessing(true);
+
+ try {
+ const isConfirmed = await confirm({
+ title: t('documents.reprocess.confirm.title'),
+ message: t('documents.reprocess.confirm.description'),
+ confirmButton: { text: t('documents.reprocess.confirm.submit') },
+ cancelButton: { text: t('documents.actions.cancel') },
+ });
+
+ if (!isConfirmed) {
+ return;
+ }
+
+ await reprocessDocument({
+ documentId: document.id,
+ organizationId: document.organizationId,
+ });
+
+ createToast({
+ type: 'success',
+ message: t('documents.reprocess.queued'),
+ description: t('documents.reprocess.queued.description'),
+ });
+ } catch (error) {
+ let message = t('documents.reprocess.error');
+
+ if (isRateLimitError({ error })) {
+ message = t('documents.reprocess.rate-limited');
+ } else if (isHttpErrorWithCode({ error, code: 'document.reprocessing_disabled' })) {
+ message = t('documents.reprocess.disabled');
+ }
+
+ createToast({ type: 'error', message });
+ } finally {
+ setIsReprocessing(false);
+ }
+ },
+ };
+}
+
export function useRestoreDocument() {
const [getIsRestoring, setIsRestoring] = createSignal(false);
diff --git a/apps/papra-client/src/modules/documents/documents.services.ts b/apps/papra-client/src/modules/documents/documents.services.ts
index 5e97364c..5502a255 100644
--- a/apps/papra-client/src/modules/documents/documents.services.ts
+++ b/apps/papra-client/src/modules/documents/documents.services.ts
@@ -111,6 +111,20 @@ export async function restoreDocument({
});
}
+export async function reprocessDocument({
+ documentId,
+ organizationId,
+}: {
+ documentId: string;
+ organizationId: string;
+}) {
+ await apiClient<void>({
+ method: 'POST',
+ path: `/api/organizations/${organizationId}/documents/${documentId}/reprocess`,
+ retry: 0,
+ });
+}
+
export async function fetchDocument({
documentId,
organizationId,
diff --git a/apps/papra-client/src/modules/documents/pages/document.page.tsx b/apps/papra-client/src/modules/documents/pages/document.page.tsx
index 458eaec0..c44a20a4 100644
--- a/apps/papra-client/src/modules/documents/pages/document.page.tsx
+++ b/apps/papra-client/src/modules/documents/pages/document.page.tsx
@@ -561,11 +561,7 @@ export const DocumentPage: Component = () => {
</TabsContent>
<TabsContent value="content">
- <DocumentContentEditionPanel
- documentId={getDocument().id}
- organizationId={params.organizationId}
- content={getDocument().content}
- />
+ <DocumentContentEditionPanel document={getDocument()} />
</TabsContent>
<TabsContent value="activity">
<Show when={activityQuery.data?.pages}>
diff --git a/apps/papra-server/src/modules/config/config.models.test.ts b/apps/papra-server/src/modules/config/config.models.test.ts
index 17df153c..30ae77f9 100644
--- a/apps/papra-server/src/modules/config/config.models.test.ts
+++ b/apps/papra-server/src/modules/config/config.models.test.ts
@@ -13,6 +13,7 @@ describe('config models', () => {
- auth.showLegalLinksOnAuthPage Whether to show Papra legal links on the auth pages
- auth.providers.*.isEnabled Wether a oauth provider is enabled
- documents.deletedExpirationDelayInDays The delay in days before a deleted document is permanently deleted
+ - documents.isReprocessingEnabled Whether existing documents can be reprocessed
- intakeEmails.isEnabled Whether intake emails are enabled
- intakeEmails.address.canCustomizeUsername Whether intake email usernames can be customized
- intakeEmails.address.domains The domains available for intake email creation
@@ -40,6 +41,7 @@ describe('config models', () => {
},
documents: {
deletedDocumentsRetentionDays: 30,
+ isReprocessingEnabled: true,
},
intakeEmails: {
isEnabled: true,
@@ -88,6 +90,7 @@ describe('config models', () => {
},
documents: {
deletedDocumentsRetentionDays: 30,
+ isReprocessingEnabled: true,
},
intakeEmails: {
isEnabled: true,
diff --git a/apps/papra-server/src/modules/config/config.models.ts b/apps/papra-server/src/modules/config/config.models.ts
index 7020f44a..f459d64a 100644
--- a/apps/papra-server/src/modules/config/config.models.ts
+++ b/apps/papra-server/src/modules/config/config.models.ts
@@ -42,7 +42,10 @@ export function getPublicConfig({
) ?? [],
},
},
- documents: { deletedDocumentsRetentionDays: config.documents.deletedDocumentsRetentionDays },
+ documents: {
+ deletedDocumentsRetentionDays: config.documents.deletedDocumentsRetentionDays,
+ isReprocessingEnabled: config.documents.isReprocessingEnabled,
+ },
intakeEmails: {
isEnabled: config.intakeEmails.isEnabled,
address: {