Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(preview): improved document preview for text-like files (#377)

Corentin Thomasset205c6cfd461f
diff --git a/.changeset/big-walls-tell.md b/.changeset/big-walls-tell.md
new file mode 100644
index 00000000..7c40ce79
--- /dev/null
+++ b/.changeset/big-walls-tell.md
@@ -0,0 +1,5 @@
+---
+"@papra/app-client": patch
+---
+
+Improve file preview for text-like files (.env, yaml, extension-less text files,...)
diff --git a/apps/papra-client/src/locales/de.yml b/apps/papra-client/src/locales/de.yml
index b78b03ce..ddc47e20 100644
--- a/apps/papra-client/src/locales/de.yml
+++ b/apps/papra-client/src/locales/de.yml
@@ -256,6 +256,9 @@ documents.deleted.deleted-at: Gelöscht
documents.deleted.restoring: Wiederherstellen...
documents.deleted.deleting: Löschen...
+documents.preview.unknown-file-type: Kein Vorschau verfügbar für diesen Dateityp
+documents.preview.binary-file: Dies scheint eine Binärdatei zu sein und kann nicht als Text angezeigt werden
+
trash.delete-all.button: Alles löschen
trash.delete-all.confirm.title: Alle Dokumente dauerhaft löschen?
trash.delete-all.confirm.description: Sind Sie sicher, dass Sie alle Dokumente aus dem Papierkorb dauerhaft löschen möchten? Diese Aktion kann nicht rückgängig gemacht werden.
diff --git a/apps/papra-client/src/locales/en.yml b/apps/papra-client/src/locales/en.yml
index 5a4ceb1a..a92f0ea0 100644
--- a/apps/papra-client/src/locales/en.yml
+++ b/apps/papra-client/src/locales/en.yml
@@ -256,6 +256,9 @@ documents.deleted.deleted-at: Deleted
documents.deleted.restoring: Restoring...
documents.deleted.deleting: Deleting...
+documents.preview.unknown-file-type: No preview available for this file type
+documents.preview.binary-file: This appears to be a binary file and cannot be displayed as text
+
trash.delete-all.button: Delete all
trash.delete-all.confirm.title: Permanently delete all documents?
trash.delete-all.confirm.description: Are you sure you want to permanently delete all documents from the trash? This action cannot be undone.
diff --git a/apps/papra-client/src/locales/fr.yml b/apps/papra-client/src/locales/fr.yml
index ba171410..a742917c 100644
--- a/apps/papra-client/src/locales/fr.yml
+++ b/apps/papra-client/src/locales/fr.yml
@@ -256,6 +256,9 @@ documents.deleted.deleted-at: Supprimé
documents.deleted.restoring: Restauration...
documents.deleted.deleting: Suppression...
+documents.preview.unknown-file-type: Aucun aperçu disponible pour ce type de fichier
+documents.preview.binary-file: Cela semble être un fichier binaire et ne peut pas être affiché en texte
+
trash.delete-all.button: Supprimer tous les documents
trash.delete-all.confirm.title: Supprimer définitivement tous les documents ?
trash.delete-all.confirm.description: Êtes-vous sûr de vouloir supprimer définitivement tous les documents de la corbeille ? Cette action est irréversible.
diff --git a/apps/papra-client/src/modules/documents/components/document-preview.component.tsx b/apps/papra-client/src/modules/documents/components/document-preview.component.tsx
index 78d03ead..f40060c3 100644
--- a/apps/papra-client/src/modules/documents/components/document-preview.component.tsx
+++ b/apps/papra-client/src/modules/documents/components/document-preview.component.tsx
@@ -2,13 +2,14 @@ import type { Component } from 'solid-js';
import type { Document } from '../documents.types';
import { useQuery } from '@tanstack/solid-query';
import { createResource, Match, Suspense, Switch } from 'solid-js';
+import { useI18n } from '@/modules/i18n/i18n.provider';
import { Card } from '@/modules/ui/components/card';
import { fetchDocumentFile } from '../documents.services';
import { PdfViewer } from './pdf-viewer.component';
const imageMimeType = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
const pdfMimeType = ['application/pdf'];
-const txtLikeMimeType = ['text/plain', 'text/markdown', 'text/csv', 'text/html'];
+const txtLikeMimeType = ['application/x-yaml', 'application/json', 'application/xml'];
function blobToString(blob: Blob): Promise<string> {
return new Promise((resolve, reject) => {
@@ -19,6 +20,83 @@ function blobToString(blob: Blob): Promise<string> {
});
}
+/**
+ * TODO: IA generated code, add some tests
+ * Detects if a blob can be safely displayed as text by checking for valid UTF-8 encoding
+ * and common text patterns (low ratio of control characters, presence of readable text)
+ */
+async function isBlobTextSafe(blob: Blob): Promise<boolean> {
+ try {
+ const text = await blobToString(blob);
+
+ // Check if the text contains mostly printable characters
+ const totalChars = text.length;
+ if (totalChars === 0) {
+ return true;
+ } // Empty files are considered text-safe
+
+ // Count control characters (excluding common whitespace and newlines)
+ // Use a simpler approach to avoid linter issues with Unicode escapes
+ let controlCharCount = 0;
+ for (let i = 0; i < text.length; i++) {
+ const charCode = text.charCodeAt(i);
+ // Check for control characters (0-31, 127-159) excluding common whitespace
+ if ((charCode >= 0 && charCode <= 31 && ![9, 10, 13, 12, 11].includes(charCode))
+ || (charCode >= 127 && charCode <= 159)) {
+ controlCharCount++;
+ }
+ }
+
+ // If more than 10% of characters are control characters, it's likely binary
+ const controlCharRatio = controlCharCount / totalChars;
+ if (controlCharRatio > 0.1) {
+ return false;
+ }
+
+ // Check for common binary file signatures in the first few bytes
+ const arrayBuffer = await blob.arrayBuffer();
+ const uint8Array = new Uint8Array(arrayBuffer);
+
+ // Common binary file signatures to check
+ const binarySignatures = [
+ [0xFF, 0xD8, 0xFF], // JPEG
+ [0x89, 0x50, 0x4E, 0x47], // PNG
+ [0x47, 0x49, 0x46], // GIF
+ [0x25, 0x50, 0x44, 0x46], // PDF
+ [0x50, 0x4B, 0x03, 0x04], // ZIP/DOCX/XLSX
+ [0x7F, 0x45, 0x4C, 0x46], // ELF executable
+ [0x4D, 0x5A], // Windows executable
+ ];
+
+ for (const signature of binarySignatures) {
+ if (uint8Array.length >= signature.length) {
+ const matches = signature.every((byte, index) => uint8Array[index] === byte);
+ if (matches) {
+ return false;
+ }
+ }
+ }
+
+ // Check if the text contains mostly ASCII printable characters
+ let asciiPrintableCount = 0;
+ for (let i = 0; i < text.length; i++) {
+ const charCode = text.charCodeAt(i);
+ // ASCII printable characters (32-126) excluding common whitespace
+ if (charCode >= 32 && charCode <= 126 && ![9, 10, 13, 12, 11].includes(charCode)) {
+ asciiPrintableCount++;
+ }
+ }
+
+ const asciiRatio = asciiPrintableCount / totalChars;
+
+ // If less than 70% are ASCII printable, it's likely binary
+ return asciiRatio > 0.7;
+ } catch {
+ // If we can't read as text, it's definitely not text-safe
+ return false;
+ }
+}
+
const TextFromBlob: Component<{ blob: Blob }> = (props) => {
const [txt] = createResource(() => blobToString(props.blob));
@@ -34,12 +112,25 @@ const TextFromBlob: Component<{ blob: Blob }> = (props) => {
export const DocumentPreview: Component<{ document: Document }> = (props) => {
const getIsImage = () => imageMimeType.includes(props.document.mimeType);
const getIsPdf = () => pdfMimeType.includes(props.document.mimeType);
+ const getIsTxtLike = () => txtLikeMimeType.includes(props.document.mimeType) || props.document.mimeType.startsWith('text/');
+ const { t } = useI18n();
const query = useQuery(() => ({
queryKey: ['organizations', props.document.organizationId, 'documents', props.document.id, 'file'],
queryFn: () => fetchDocumentFile({ documentId: props.document.id, organizationId: props.document.organizationId }),
}));
+ // Create a resource to check if octet-stream blob is text-safe
+ const [isOctetStreamTextSafe] = createResource(
+ () => query.data && props.document.mimeType === 'application/octet-stream' ? query.data : null,
+ async (blob) => {
+ if (!blob) {
+ return false;
+ }
+ return await isBlobTextSafe(blob);
+ },
+ );
+
return (
<Suspense>
<Switch>
@@ -48,12 +139,30 @@ export const DocumentPreview: Component<{ document: Document }> = (props) => {
<img src={URL.createObjectURL(query.data!)} class="w-full h-full object-contain" />
</div>
</Match>
+
<Match when={getIsPdf() && query.data}>
<PdfViewer url={URL.createObjectURL(query.data!)} />
</Match>
- <Match when={txtLikeMimeType.includes(props.document.mimeType) && query.data}>
+
+ <Match when={getIsTxtLike() && query.data}>
<TextFromBlob blob={query.data!} />
</Match>
+
+ <Match when={props.document.mimeType === 'application/octet-stream' && query.data && isOctetStreamTextSafe()}>
+ <TextFromBlob blob={query.data!} />
+ </Match>
+
+ <Match when={props.document.mimeType === 'application/octet-stream' && query.data && !isOctetStreamTextSafe()}>
+ <Card class="px-6 py-12 text-center text-sm text-muted-foreground">
+ <p>{t('documents.preview.binary-file')}</p>
+ </Card>
+ </Match>
+
+ <Match when={query.data}>
+ <Card class="px-6 py-12 text-center text-sm text-muted-foreground">
+ <p>{t('documents.preview.unknown-file-type')}</p>
+ </Card>
+ </Match>
</Switch>
</Suspense>
);
diff --git a/apps/papra-client/src/modules/i18n/locales.types.ts b/apps/papra-client/src/modules/i18n/locales.types.ts
index 2af64633..a041e737 100644
--- a/apps/papra-client/src/modules/i18n/locales.types.ts
+++ b/apps/papra-client/src/modules/i18n/locales.types.ts
@@ -229,6 +229,8 @@ export type LocaleKeys =
| 'documents.deleted.deleted-at'
| 'documents.deleted.restoring'
| 'documents.deleted.deleting'
+ | 'documents.preview.unknown-file-type'
+ | 'documents.preview.binary-file'
| 'trash.delete-all.button'
| 'trash.delete-all.confirm.title'
| 'trash.delete-all.confirm.description'