From a40ab36838a962472888ec555ba06e849d08fe81 Mon Sep 17 00:00:00 2001 From: Mateo Date: Fri, 4 Dec 2026 17:48:33 +0100 Subject: [PATCH] feat(web): document viewers for PDF, Word, Excel, Markdown, CSV, code and logs --- .../ClientApp/apps/web/package.json | 8 + .../web/src/features/chat/chatModel.test.ts | 13 + .../apps/web/src/features/chat/chatModel.ts | 13 +- .../apps/web/src/features/files/entries.ts | 6 +- .../features/player/documentFormats.test.ts | 72 + .../src/features/player/documentFormats.ts | 121 + .../apps/web/src/features/player/public.ts | 7 +- .../features/player/viewer/DocumentViews.tsx | 46 +- .../features/player/viewer/MediaViewer.tsx | 13 +- .../player/viewer/documents/CodeBody.tsx | 13 + .../player/viewer/documents/CsvTable.tsx | 8 + .../player/viewer/documents/DataTable.tsx | 60 + .../player/viewer/documents/DocumentFrame.tsx | 76 + .../player/viewer/documents/DocumentView.tsx | 38 + .../player/viewer/documents/DocxView.tsx | 78 + .../player/viewer/documents/MarkdownBody.tsx | 39 + .../player/viewer/documents/PdfView.tsx | 219 ++ .../player/viewer/documents/SheetView.tsx | 51 + .../documents/TextDocumentView.test.tsx | 78 + .../viewer/documents/TextDocumentView.tsx | 112 + .../player/viewer/documents/documents.css | 161 ++ .../viewer/documents/fetchDocument.test.ts | 110 + .../player/viewer/documents/fetchDocument.ts | 154 ++ .../player/viewer/documents/highlight.ts | 56 + .../viewer/documents/sanitizeLinks.test.ts | 29 + .../player/viewer/documents/sanitizeLinks.ts | 20 + .../player/viewer/documents/tableData.test.ts | 46 + .../player/viewer/documents/tableData.ts | 23 + .../apps/web/src/features/search/fileMedia.ts | 9 +- .../ClientApp/apps/web/src/i18n/es/player.ts | 24 +- .../ClientApp/apps/web/src/i18n/gl/player.ts | 24 +- .../ClientApp/apps/web/vite.config.ts | 13 +- .../ClientApp/package-lock.json | 2011 ++++++++++++++++- .../packages/file-manager/src/core/media.ts | 2 +- 34 files changed, 3644 insertions(+), 109 deletions(-) create mode 100644 TelegramDownloader/ClientApp/apps/web/src/features/player/documentFormats.test.ts create mode 100644 TelegramDownloader/ClientApp/apps/web/src/features/player/documentFormats.ts create mode 100644 TelegramDownloader/ClientApp/apps/web/src/features/player/viewer/documents/CodeBody.tsx create mode 100644 TelegramDownloader/ClientApp/apps/web/src/features/player/viewer/documents/CsvTable.tsx create mode 100644 TelegramDownloader/ClientApp/apps/web/src/features/player/viewer/documents/DataTable.tsx create mode 100644 TelegramDownloader/ClientApp/apps/web/src/features/player/viewer/documents/DocumentFrame.tsx create mode 100644 TelegramDownloader/ClientApp/apps/web/src/features/player/viewer/documents/DocumentView.tsx create mode 100644 TelegramDownloader/ClientApp/apps/web/src/features/player/viewer/documents/DocxView.tsx create mode 100644 TelegramDownloader/ClientApp/apps/web/src/features/player/viewer/documents/MarkdownBody.tsx create mode 100644 TelegramDownloader/ClientApp/apps/web/src/features/player/viewer/documents/PdfView.tsx create mode 100644 TelegramDownloader/ClientApp/apps/web/src/features/player/viewer/documents/SheetView.tsx create mode 100644 TelegramDownloader/ClientApp/apps/web/src/features/player/viewer/documents/TextDocumentView.test.tsx create mode 100644 TelegramDownloader/ClientApp/apps/web/src/features/player/viewer/documents/TextDocumentView.tsx create mode 100644 TelegramDownloader/ClientApp/apps/web/src/features/player/viewer/documents/documents.css create mode 100644 TelegramDownloader/ClientApp/apps/web/src/features/player/viewer/documents/fetchDocument.test.ts create mode 100644 TelegramDownloader/ClientApp/apps/web/src/features/player/viewer/documents/fetchDocument.ts create mode 100644 TelegramDownloader/ClientApp/apps/web/src/features/player/viewer/documents/highlight.ts create mode 100644 TelegramDownloader/ClientApp/apps/web/src/features/player/viewer/documents/sanitizeLinks.test.ts create mode 100644 TelegramDownloader/ClientApp/apps/web/src/features/player/viewer/documents/sanitizeLinks.ts create mode 100644 TelegramDownloader/ClientApp/apps/web/src/features/player/viewer/documents/tableData.test.ts create mode 100644 TelegramDownloader/ClientApp/apps/web/src/features/player/viewer/documents/tableData.ts diff --git a/TelegramDownloader/ClientApp/apps/web/package.json b/TelegramDownloader/ClientApp/apps/web/package.json index f32c8c8..1e0a961 100644 --- a/TelegramDownloader/ClientApp/apps/web/package.json +++ b/TelegramDownloader/ClientApp/apps/web/package.json @@ -16,17 +16,25 @@ "@tfm/api-client": "*", "@tfm/file-manager": "*", "@tfm/ui": "*", + "docx-preview": "^0.4.1", + "highlight.js": "^11.12.0", "lucide-react": "^0.562.0", "music-metadata": "^11.16.1", + "papaparse": "^5.7.1", + "pdfjs-dist": "^6.5.438", "radix-ui": "^1.8.1", "react": "^19.2.0", "react-dom": "^19.2.0", + "react-markdown": "^10.1.0", "react-router": "^7.11.0", + "read-excel-file": "^9.3.20", + "remark-gfm": "^4.0.1", "uqr": "^0.1.3", "zustand": "^5.0.9" }, "devDependencies": { "@tailwindcss/vite": "^4.1.18", + "@types/papaparse": "^5.5.2", "@vitejs/plugin-react": "^5.1.1", "sharp": "^0.35.5", "tailwindcss": "^4.1.18", diff --git a/TelegramDownloader/ClientApp/apps/web/src/features/chat/chatModel.test.ts b/TelegramDownloader/ClientApp/apps/web/src/features/chat/chatModel.test.ts index 87c8bb2..8554f4a 100644 --- a/TelegramDownloader/ClientApp/apps/web/src/features/chat/chatModel.test.ts +++ b/TelegramDownloader/ClientApp/apps/web/src/features/chat/chatModel.test.ts @@ -146,6 +146,19 @@ describe('messageMedia', () => { }); }); + it('opens Word, Excel, Markdown and log attachments in the document viewer', () => { + const doc = (id: number, fileName: string) => messageMedia('5', msg(id, at, { hasMedia: true, mediaType: 'document', fileName }), url, 'x'); + expect(doc(13, 'Report.docx')).toMatchObject({ kind: 'document', name: 'Report.docx' }); + expect(doc(14, 'Budget.xlsx')).toMatchObject({ kind: 'document' }); + expect(doc(15, 'README.md')).toMatchObject({ kind: 'text' }); + expect(doc(16, 'server.log')).toMatchObject({ kind: 'text' }); + }); + + it('has no viewer for the old binary Office formats', () => { + expect(messageMedia('5', msg(17, at, { hasMedia: true, mediaType: 'document', fileName: 'Old.doc' }), url, 'x')).toBeNull(); + expect(messageMedia('5', msg(18, at, { hasMedia: true, mediaType: 'document', fileName: 'Old.xls' }), url, 'x')).toBeNull(); + }); + it('leaves other files and text messages alone', () => { expect(messageMedia('5', msg(11, at, { hasMedia: true, mediaType: 'document', fileName: 'backup.zip' }), url, 'x')).toBeNull(); expect(messageMedia('5', msg(12, at), url, 'x')).toBeNull(); diff --git a/TelegramDownloader/ClientApp/apps/web/src/features/chat/chatModel.ts b/TelegramDownloader/ClientApp/apps/web/src/features/chat/chatModel.ts index 4a81d08..6de31b6 100644 --- a/TelegramDownloader/ClientApp/apps/web/src/features/chat/chatModel.ts +++ b/TelegramDownloader/ClientApp/apps/web/src/features/chat/chatModel.ts @@ -1,4 +1,5 @@ import type { ApiChatMessageDto, ApiChatWindowDto, ChatMessageKind } from '@tfm/api-client'; +import { documentViewerKind } from '@/features/player/public'; /** How long the search in the chat waits after the last key (Enter does not wait). */ export const CHAT_SEARCH_DELAY_MS = 1200; @@ -169,15 +170,15 @@ export function fileTypeLabel(m: Pick = { @@ -102,9 +102,7 @@ export function viewerKind(entry: FileEntry): ViewerKind | null { if (kind === 'image') return 'image'; if (kind === 'video') return 'video'; if (kind === 'audio') return 'audio'; - if (entry.extension === 'pdf') return 'pdf'; - if (kind === 'text') return 'text'; - return null; + return documentViewerKind(entry.name) ?? (kind === 'text' ? 'text' : null); } /** An entry as a viewer item (inline URL, optional transcoding URL). */ diff --git a/TelegramDownloader/ClientApp/apps/web/src/features/player/documentFormats.test.ts b/TelegramDownloader/ClientApp/apps/web/src/features/player/documentFormats.test.ts new file mode 100644 index 0000000..66ccd42 --- /dev/null +++ b/TelegramDownloader/ClientApp/apps/web/src/features/player/documentFormats.test.ts @@ -0,0 +1,72 @@ +import { describe, expect, it } from 'vitest'; +import { codeLanguageOf, documentFormatOf, documentViewerKind, extensionOfName } from './documentFormats'; + +describe('documentFormatOf', () => { + it('knows the documents by extension, in any case', () => { + expect(documentFormatOf('Manual.PDF')).toBe('pdf'); + expect(documentFormatOf('README.md')).toBe('markdown'); + expect(documentFormatOf('notes.markdown')).toBe('markdown'); + expect(documentFormatOf('data.csv')).toBe('csv'); + expect(documentFormatOf('data.tsv')).toBe('tsv'); + expect(documentFormatOf('Budget.xlsx')).toBe('xlsx'); + expect(documentFormatOf('Report.docx')).toBe('docx'); + }); + + it('opens logs, subtitles and notes as plain text', () => { + expect(documentFormatOf('server.log')).toBe('text'); + expect(documentFormatOf('movie.srt')).toBe('text'); + expect(documentFormatOf('release.nfo')).toBe('text'); + expect(documentFormatOf('todo.txt')).toBe('text'); + }); + + it('opens source and config files as code', () => { + expect(documentFormatOf('appsettings.json')).toBe('code'); + expect(documentFormatOf('docker-compose.yml')).toBe('code'); + expect(documentFormatOf('script.py')).toBe('code'); + }); + + it('has no viewer for the old Office formats nor for archives', () => { + expect(documentFormatOf('Old.doc')).toBeNull(); + expect(documentFormatOf('Old.xls')).toBeNull(); + expect(documentFormatOf('Slides.ppt')).toBeNull(); + expect(documentFormatOf('backup.zip')).toBeNull(); + }); + + it('falls back to the MIME type of files without an extension', () => { + expect(documentFormatOf('attachment', 'application/pdf')).toBe('pdf'); + expect(documentFormatOf('attachment', 'text/plain; charset=utf-8')).toBe('text'); + expect(documentFormatOf(null, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')).toBe('docx'); + }); + + it('trusts the extension over the MIME type', () => { + expect(documentFormatOf('notes.md', 'text/plain')).toBe('markdown'); + }); +}); + +describe('documentViewerKind', () => { + it('is pdf, document (Word and Excel) or text', () => { + expect(documentViewerKind('a.pdf')).toBe('pdf'); + expect(documentViewerKind('a.docx')).toBe('document'); + expect(documentViewerKind('a.xlsx')).toBe('document'); + expect(documentViewerKind('a.md')).toBe('text'); + expect(documentViewerKind('a.csv')).toBe('text'); + expect(documentViewerKind('a.log')).toBe('text'); + expect(documentViewerKind('a.rar')).toBeNull(); + }); +}); + +describe('codeLanguageOf', () => { + it('gives the highlighting language of code files only', () => { + expect(codeLanguageOf('Program.cs')).toBe('csharp'); + expect(codeLanguageOf('config.toml')).toBe('ini'); + expect(codeLanguageOf('server.log')).toBeUndefined(); + }); +}); + +describe('extensionOfName', () => { + it('reads the last extension of the file name, not of a folder', () => { + expect(extensionOfName('archive.tar.gz')).toBe('gz'); + expect(extensionOfName('/var/my.folder/README')).toBe(''); + expect(extensionOfName('.gitignore')).toBe('gitignore'); + }); +}); diff --git a/TelegramDownloader/ClientApp/apps/web/src/features/player/documentFormats.ts b/TelegramDownloader/ClientApp/apps/web/src/features/player/documentFormats.ts new file mode 100644 index 0000000..64f387c --- /dev/null +++ b/TelegramDownloader/ClientApp/apps/web/src/features/player/documentFormats.ts @@ -0,0 +1,121 @@ +/** + * Which document viewer opens a file, from its name (and MIME type when + * there is no extension). Pure and small: used by every place that opens the + * viewer; the viewers themselves are lazy chunks. + */ +export type DocumentFormat = 'pdf' | 'markdown' | 'csv' | 'tsv' | 'xlsx' | 'docx' | 'code' | 'text'; + +/** Highlight.js language of each source/config extension. */ +const CODE_LANGUAGES: Record = { + json: 'json', + jsonc: 'json', + xml: 'xml', + html: 'xml', + htm: 'xml', + xhtml: 'xml', + plist: 'xml', + yml: 'yaml', + yaml: 'yaml', + toml: 'ini', + ini: 'ini', + cfg: 'ini', + conf: 'ini', + properties: 'properties', + css: 'css', + scss: 'scss', + js: 'javascript', + mjs: 'javascript', + cjs: 'javascript', + jsx: 'javascript', + ts: 'typescript', + tsx: 'typescript', + py: 'python', + sh: 'bash', + bash: 'bash', + zsh: 'bash', + ps1: 'powershell', + bat: 'dos', + cmd: 'dos', + sql: 'sql', + cs: 'csharp', + java: 'java', + kt: 'kotlin', + kts: 'kotlin', + go: 'go', + rs: 'rust', + c: 'c', + h: 'c', + cpp: 'cpp', + cc: 'cpp', + hpp: 'cpp', + php: 'php', + rb: 'ruby', + lua: 'lua', + diff: 'diff', + patch: 'diff' +}; + +/** Plain text: shown as is (logs, subtitles, notes...). */ +const PLAIN_TEXT = new Set(['txt', 'text', 'log', 'out', 'nfo', 'srt', 'vtt', 'ass', 'lrc', 'cue', 'env', 'csproj', 'sln', 'gitignore']); + +const BY_EXTENSION: Record = { + pdf: 'pdf', + md: 'markdown', + markdown: 'markdown', + csv: 'csv', + tsv: 'tsv', + xlsx: 'xlsx', + xlsm: 'xlsx', + docx: 'docx' +}; + +const BY_MIME: Record = { + 'application/pdf': 'pdf', + 'text/markdown': 'markdown', + 'text/csv': 'csv', + 'text/tab-separated-values': 'tsv', + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'xlsx', + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'docx', + 'application/json': 'code', + 'application/xml': 'code', + 'text/xml': 'code' +}; + +/** Lower-case extension of a file name, without the dot ('' when none). */ +export function extensionOfName(name: string | undefined | null): string { + if (!name) return ''; + const slash = Math.max(name.lastIndexOf('/'), name.lastIndexOf('\\')); + const base = name.slice(slash + 1); + const dot = base.lastIndexOf('.'); + // ".gitignore" has no extension but is named after one + if (dot === 0) return base.slice(1).toLowerCase(); + return dot > 0 ? base.slice(dot + 1).toLowerCase() : ''; +} + +/** The viewer of a file, or null when there is none (old Office formats, archives...). */ +export function documentFormatOf(name: string | undefined | null, mimeType?: string | null): DocumentFormat | null { + const ext = extensionOfName(name); + if (ext) { + if (BY_EXTENSION[ext]) return BY_EXTENSION[ext]; + if (CODE_LANGUAGES[ext]) return 'code'; + if (PLAIN_TEXT.has(ext)) return 'text'; + } + const mime = (mimeType ?? '').toLowerCase().split(';')[0].trim(); + if (BY_MIME[mime]) return BY_MIME[mime]; + if (!ext && mime.startsWith('text/')) return 'text'; + return null; +} + +/** Highlight.js language for a `code` file (undefined: no highlighting). */ +export function codeLanguageOf(name: string | undefined | null): string | undefined { + return CODE_LANGUAGES[extensionOfName(name)]; +} + +/** The viewer kind a document opens with (`document`: Word and Excel). */ +export function documentViewerKind(name: string | undefined | null, mimeType?: string | null): 'pdf' | 'text' | 'document' | null { + const format = documentFormatOf(name, mimeType); + if (!format) return null; + if (format === 'pdf') return 'pdf'; + if (format === 'xlsx' || format === 'docx') return 'document'; + return 'text'; +} diff --git a/TelegramDownloader/ClientApp/apps/web/src/features/player/public.ts b/TelegramDownloader/ClientApp/apps/web/src/features/player/public.ts index 2e8e981..37c5cbb 100644 --- a/TelegramDownloader/ClientApp/apps/web/src/features/player/public.ts +++ b/TelegramDownloader/ClientApp/apps/web/src/features/player/public.ts @@ -8,6 +8,8 @@ */ import { useViewer } from './viewerStore'; +export { codeLanguageOf, documentFormatOf, documentViewerKind, type DocumentFormat } from './documentFormats'; + /** A playable audio item. URLs must be playable by an