Zeige PDF-Anlagen in der Mail-Ansicht, damit der Browser sie nicht unter einem zufälligen Namen speichert.
Deploy Development / deploy (push) Successful in 47s
Test Suite / pytest-backend (push) Successful in 6m16s
Test Suite / smoke-dev (push) Successful in 1s
Test Suite / frontend-build (push) Successful in 22s

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
LarsandCursor committed 2026-10-23 12:59:07 +02:00
1 parent d0f09830bb
commit cd239a3ab8
5 files changed
+164 -26

No files matched your search

+60 -13
View File
@@ -1,4 +1,3 @@
import { DocumentView } from "../pages/DocumentView.jsx";
import { useEffect, useRef, useState } from "react";
import { api, documentToText, downloadMailPart, fetchExternal, fetchMailPart, markDirty, textToDocument } from "../api.js";
import { MailAttachments, MailBody } from "../mail-view.jsx";
@@ -17,6 +16,18 @@ function previewKind(type, filename) {
return "other";
}
function isPdfBytes(buffer) {
const sample = new TextDecoder("iso-8859-1").decode(buffer.slice(0, 1024)).trim();
return sample.startsWith("%PDF");
}
function downloadName(file, pdf) {
const name = file?.filename || "anlage";
if (!pdf || name.toLowerCase().endsWith(".pdf")) return name;
const stem = name.toLowerCase().endsWith(".bin") ? name.slice(0, -4).trim() : name;
return `${stem || "anlage"}.pdf`;
}
export function NoteStage({ element, context, binding, onReload }) {
const editor = editors.note;
const [text, setText] = useState(() => documentToText(element.document));
@@ -162,19 +173,41 @@ export function MailStage({ element, context, binding, shownDirection }) {
}, [element.id, element.external?.source, context?.kind, context?.id]);
useEffect(() => {
if (shownIndex === null) return undefined;
if (shownIndex === null) {
setMedia("");
setPdf(null);
setPages(0);
setPhase("idle");
if (objectUrl.current) {
URL.revokeObjectURL(objectUrl.current);
objectUrl.current = "";
}
setBlobUrl("");
return undefined;
}
const file = (letter?.attachments || []).find((item) => item.index === shownIndex);
if (!file) return undefined;
let alive = true;
setPhase("loading");
setPdf(null);
setPages(0);
setPage(1);
setMedia("");
fetchMailPart(element.id, context, `attachments/${file.index}`).then(async (blob) => {
if (!alive) return;
if (objectUrl.current) URL.revokeObjectURL(objectUrl.current);
const url = URL.createObjectURL(blob);
objectUrl.current = url;
setBlobUrl(url);
const kind = previewKind(blob.type || file.content_type, file.filename);
const buffer = await blob.arrayBuffer();
if (!alive) return;
const declared = previewKind(blob.type || file.content_type, file.filename);
const kind = isPdfBytes(buffer) ? "pdf" : declared;
if (objectUrl.current) {
URL.revokeObjectURL(objectUrl.current);
objectUrl.current = "";
}
setBlobUrl("");
if (kind === "image") {
const url = URL.createObjectURL(new Blob([buffer], { type: blob.type || file.content_type || "application/octet-stream" }));
objectUrl.current = url;
setBlobUrl(url);
setMedia("image");
setPhase("ready");
return;
@@ -182,10 +215,9 @@ export function MailStage({ element, context, binding, shownDirection }) {
if (kind === "pdf") {
setMedia("pdf");
try {
const bytes = await blob.arrayBuffer();
const pdfjs = await import("pdfjs-dist/legacy/build/pdf.mjs");
pdfjs.GlobalWorkerOptions.workerSrc = workerSrc;
const doc = await pdfjs.getDocument({ data: bytes }).promise;
const doc = await pdfjs.getDocument({ data: new Uint8Array(buffer) }).promise;
if (!alive) {
doc.destroy();
return;
@@ -195,16 +227,17 @@ export function MailStage({ element, context, binding, shownDirection }) {
setPhase("ready");
} catch (err) {
if (!alive) return;
setMedia("pdf-frame");
setPdf(null);
setPhase("ready");
}
return;
}
setMedia("other");
setMedia("file");
setPhase("ready");
}).catch((err) => {
if (!alive) return;
setPhase("ready");
setMedia("");
setError(err.message || "Die Anlage ist nicht verfügbar.");
});
return () => {
@@ -243,6 +276,17 @@ export function MailStage({ element, context, binding, shownDirection }) {
}
}
async function saveAttachment() {
if (!shownFile) return;
setError("");
const namedPdf = media === "pdf" || (shownFile.content_type || "").toLowerCase().includes("pdf") || (shownFile.filename || "").toLowerCase().endsWith(".pdf");
try {
await downloadMailPart(element.id, context, `attachments/${shownFile.index}`, downloadName(shownFile, namedPdf));
} catch (err) {
setError(err.message);
}
}
return (
<article className="mail-letter">
<p className="mail-letter__way"><DirectionColumn direction={shownDirection} />{directionWord(shownDirection) ? <span>{directionWord(shownDirection)}</span> : null}</p>
@@ -262,8 +306,11 @@ export function MailStage({ element, context, binding, shownDirection }) {
{shownFile && phase === "loading" ? <p className="muted">Die Anlage wird geladen …</p> : null}
{media === "image" && blobUrl ? <img className="reader-image" alt={shownFile?.filename || element.title} src={blobUrl} /> : null}
{media === "pdf" && pdf ? <canvas ref={canvasRef} className="reader-canvas" /> : null}
{media === "pdf-frame" && blobUrl ? <DocumentView title={shownFile?.filename || element.title} url={blobUrl} /> : null}
{media === "other" && blobUrl && binding?.rights?.download ? <a href={blobUrl} target="_blank" rel="noreferrer">Anlage öffnen</a> : null}
{shownFile && phase === "ready" && binding?.rights?.download && (media === "file" || (media === "pdf" && !pdf)) ? (
<p className="muted">
<button type="button" onClick={saveAttachment}>{downloadName(shownFile, media === "pdf")} herunterladen</button>
</p>
) : null}
{pages > 1 ? (
<div className="reader-pages">
<button type="button" disabled={page <= 1} onClick={() => setPage((current) => Math.max(1, current - 1))}>Vorherige Seite</button>