Zeige E-Mail-Anlagen in der Leseansicht, damit der Inhalt nachvollziehbar bleibt.
Deploy Development / deploy (push) Successful in 29s
Deploy Development / deploy (push) Successful in 29s
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
f0c8981e22
commit
2664021e7f
5 files changed
+124
-18
No files matched your search
@@ -186,15 +186,18 @@ def download_mail_attachment(
|
||||
if index < 0 or index >= len(parsed.attachments):
|
||||
raise not_found()
|
||||
item = parsed.attachments[index]
|
||||
return _mail_bytes(item.data, item.media_type or "application/octet-stream", item.filename)
|
||||
return _mail_bytes(item.data, item.media_type or "application/octet-stream", item.filename, inline=True)
|
||||
|
||||
|
||||
def _mail_bytes(data: bytes, media_type: str, filename: str) -> Response:
|
||||
def _mail_bytes(data: bytes, media_type: str, filename: str, inline: bool = False) -> Response:
|
||||
disposition = content_disposition(filename)
|
||||
if inline:
|
||||
disposition = disposition.replace("attachment;", "inline;", 1)
|
||||
return Response(
|
||||
content=data,
|
||||
media_type=media_type,
|
||||
headers={
|
||||
"Content-Disposition": content_disposition(filename),
|
||||
"Content-Disposition": disposition,
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
"Cache-Control": "private, no-store",
|
||||
},
|
||||
|
||||
@@ -263,6 +263,8 @@ def test_an_adopted_message_loads_in_full_with_its_attachment(client, db, monkey
|
||||
attachment = _raw(ada, f"/api/v1/external-refs/{created['id']}/mail/attachments/0")
|
||||
assert attachment.status_code == 200
|
||||
assert attachment.content.startswith(b"%PDF")
|
||||
assert attachment.headers["content-type"].startswith("application/pdf")
|
||||
assert attachment.headers["content-disposition"].startswith("inline;")
|
||||
ada.request(
|
||||
"POST",
|
||||
"/api/v1/external-refs",
|
||||
|
||||
+8
-3
@@ -76,16 +76,21 @@ function mailQuery(context) {
|
||||
return params;
|
||||
}
|
||||
|
||||
export async function downloadMailPart(elementId, context, suffix, filename) {
|
||||
export async function fetchMailPart(elementId, context, suffix) {
|
||||
const response = await fetch(`/api/v1/external-refs/${elementId}/mail/${suffix}?${mailQuery(context)}`, {
|
||||
credentials: "include",
|
||||
headers: scopeHeaders(),
|
||||
});
|
||||
if (!response.ok) {
|
||||
const data = await response.json().catch(() => null);
|
||||
throw new Error(data?.detail || "Die vollständige E-Mail ist nicht verfügbar.");
|
||||
const fallback = suffix === "original" ? "Die vollständige E-Mail ist nicht verfügbar." : "Die Anlage ist nicht verfügbar.";
|
||||
throw new Error(data?.detail || fallback);
|
||||
}
|
||||
const blob = await response.blob();
|
||||
return response.blob();
|
||||
}
|
||||
|
||||
export async function downloadMailPart(elementId, context, suffix, filename) {
|
||||
const blob = await fetchMailPart(elementId, context, suffix);
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { documentToText, downloadMailPart, fetchFile, fetchPaperless, markDirty, textToDocument, api } from "../api.js";
|
||||
import { documentToText, downloadMailPart, fetchFile, fetchMailPart, fetchPaperless, markDirty, textToDocument, api } from "../api.js";
|
||||
import { ErrorNote } from "./widgets.jsx";
|
||||
import { dateCaption, DirectionChoice, DirectionColumn, directionWord, formatDate, mailFacts, MarkdownEditor, sourceLabel } from "./content.jsx";
|
||||
import { NavIcon } from "./icons.jsx";
|
||||
@@ -7,14 +7,22 @@ import workerSrc from "pdfjs-dist/build/pdf.worker.min.mjs?url";
|
||||
|
||||
const SCALES = [0.75, 1, 1.25, 1.5, 2];
|
||||
|
||||
function MailAttachments({ files, onOpen }) {
|
||||
function previewKind(type, filename) {
|
||||
const media = (type || "").toLowerCase();
|
||||
const name = (filename || "").toLowerCase();
|
||||
if (media.startsWith("image/") || /\.(png|jpe?g|gif|webp)$/.test(name)) return "image";
|
||||
if (media.includes("pdf") || name.endsWith(".pdf")) return "pdf";
|
||||
return "other";
|
||||
}
|
||||
|
||||
function MailAttachments({ files, current, onOpen }) {
|
||||
if (!files?.length) return <p className="muted">Keine Anhänge.</p>;
|
||||
return (
|
||||
<ul className="mail-files">
|
||||
{files.map((file) => (
|
||||
<li key={`${file.index ?? "name"}-${file.filename}`}>
|
||||
{onOpen && Number.isInteger(file.index) ? (
|
||||
<button type="button" className="quiet" onClick={() => onOpen(file)}>{file.filename}</button>
|
||||
<button type="button" className={file.index === current ? "quiet is-current" : "quiet"} aria-current={file.index === current ? "true" : undefined} onClick={() => onOpen(file)}>{file.filename}</button>
|
||||
) : file.filename}
|
||||
</li>
|
||||
))}
|
||||
@@ -82,7 +90,9 @@ export function ReadingPane({ binding, context, backLabel, onClose, onReload, ca
|
||||
const [notice, setNotice] = useState("");
|
||||
const [annotate, setAnnotate] = useState(false);
|
||||
const [letter, setLetter] = useState(null);
|
||||
const [shownIndex, setShownIndex] = useState(null);
|
||||
const canvasRef = useRef(null);
|
||||
const objectUrl = useRef("");
|
||||
const closeRef = useRef(null);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -103,6 +113,12 @@ export function ReadingPane({ binding, context, backLabel, onClose, onReload, ca
|
||||
setPages(0);
|
||||
setMedia("");
|
||||
setLetter(null);
|
||||
setShownIndex(null);
|
||||
if (objectUrl.current) {
|
||||
URL.revokeObjectURL(objectUrl.current);
|
||||
objectUrl.current = "";
|
||||
}
|
||||
setBlobUrl("");
|
||||
const mail = element.external?.source === "imap";
|
||||
setPhase(element.kind === "note" || mail ? "ready" : element.preview?.available ? "loading" : "missing");
|
||||
return undefined;
|
||||
@@ -158,6 +174,70 @@ export function ReadingPane({ binding, context, backLabel, onClose, onReload, ca
|
||||
};
|
||||
}, [element?.id, element?.kind, element?.external?.source, element?.preview?.available, context?.kind, context?.id]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!letter?.complete) return undefined;
|
||||
const files = letter.attachments || [];
|
||||
const viewable = files.findIndex((file) => previewKind(file.content_type, file.filename) !== "other");
|
||||
setShownIndex(viewable >= 0 ? files[viewable].index : (files[0] ? files[0].index : null));
|
||||
return undefined;
|
||||
}, [letter]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!element || element.external?.source !== "imap" || shownIndex === null) 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);
|
||||
if (kind === "image") {
|
||||
setMedia("image");
|
||||
setPhase("ready");
|
||||
return;
|
||||
}
|
||||
if (kind === "pdf") {
|
||||
setMedia("pdf");
|
||||
try {
|
||||
const bytes = await blob.arrayBuffer();
|
||||
const pdfjs = await import("pdfjs-dist");
|
||||
pdfjs.GlobalWorkerOptions.workerSrc = workerSrc;
|
||||
const doc = await pdfjs.getDocument({ data: bytes }).promise;
|
||||
if (!alive) {
|
||||
doc.destroy();
|
||||
return;
|
||||
}
|
||||
setPdf(doc);
|
||||
setPages(doc.numPages);
|
||||
setPhase("ready");
|
||||
} catch (err) {
|
||||
if (!alive) return;
|
||||
setMedia("pdf-frame");
|
||||
setPhase("ready");
|
||||
}
|
||||
return;
|
||||
}
|
||||
setMedia("other");
|
||||
setPhase("ready");
|
||||
}).catch((err) => {
|
||||
if (!alive) return;
|
||||
setPhase("ready");
|
||||
setMedia("");
|
||||
setError(err.message || "Die Anlage ist nicht verfügbar.");
|
||||
});
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, [element?.id, element?.external?.source, shownIndex, letter, context?.kind, context?.id]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!element || element.external?.source !== "imap") return undefined;
|
||||
let alive = true;
|
||||
@@ -360,6 +440,7 @@ export function ReadingPane({ binding, context, backLabel, onClose, onReload, ca
|
||||
const shownDirection = communication.direction || null;
|
||||
const isMail = element.external?.source === "imap";
|
||||
const mailDetail = letter || element.external?.detail || {};
|
||||
const shownFile = isMail ? (letter?.attachments || []).find((item) => item.index === shownIndex) || null : null;
|
||||
|
||||
return (
|
||||
<section className="reader-pane" aria-label="Leser">
|
||||
@@ -401,7 +482,20 @@ export function ReadingPane({ binding, context, backLabel, onClose, onReload, ca
|
||||
{mailDetail.message_cc ? <p><span>Kopie</span> {mailDetail.message_cc}</p> : null}
|
||||
{letter?.complete ? (
|
||||
<>
|
||||
<MailAttachments files={letter.attachments} onOpen={(file) => openMailPart(`attachments/${file.index}`, file.filename)} />
|
||||
<MailAttachments files={letter.attachments} current={shownIndex} onOpen={(file) => { setError(""); setShownIndex(file.index); }} />
|
||||
{shownFile ? <p className="mail-attachment__caption">Anlage: {shownFile.filename}</p> : null}
|
||||
{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} style={{ width: `${Math.round(scale * 100)}%` }} />
|
||||
) : null}
|
||||
{media === "pdf" && pdf ? <canvas ref={canvasRef} className="reader-canvas" /> : null}
|
||||
{media === "pdf-frame" && blobUrl ? <iframe className="reader-frame" title={shownFile?.filename || element.title} src={blobUrl} /> : null}
|
||||
{media === "other" && blobUrl ? (
|
||||
<div className="reader-pane__state">
|
||||
<p>Diese Anlage lässt sich hier nicht als Seite zeigen.</p>
|
||||
<a href={blobUrl} target="_blank" rel="noreferrer">Anlage öffnen</a>
|
||||
</div>
|
||||
) : null}
|
||||
{letter.text ? <pre className="mail-letter__body">{letter.text}</pre> : (
|
||||
<p className="muted">Diese Nachricht hat keinen lesbaren Text. Die vollständige E-Mail bleibt abrufbar.</p>
|
||||
)}
|
||||
@@ -428,25 +522,25 @@ export function ReadingPane({ binding, context, backLabel, onClose, onReload, ca
|
||||
/>
|
||||
<p className="muted">Keine Angabe bleibt leer. Versand- und Zugangsdatum ändern die Richtung nicht.</p>
|
||||
</form>
|
||||
{phase === "loading" ? <p className="reader-pane__state">Vorschau wird geladen …</p> : null}
|
||||
{phase === "missing" ? (
|
||||
{!isMail && phase === "loading" ? <p className="reader-pane__state">Vorschau wird geladen …</p> : null}
|
||||
{!isMail && phase === "missing" ? (
|
||||
<div className="reader-pane__state">
|
||||
<p>Für diesen Inhalt gibt es keine eingebaute Vorschau.</p>
|
||||
<button type="button" onClick={openOriginal}>Original öffnen</button>
|
||||
</div>
|
||||
) : null}
|
||||
{phase === "error" ? (
|
||||
{!isMail && phase === "error" ? (
|
||||
<div className="reader-pane__state">
|
||||
<p>Die Vorschau ist nicht verfügbar.</p>
|
||||
<button type="button" onClick={openOriginal}>Original öffnen</button>
|
||||
</div>
|
||||
) : null}
|
||||
{media === "image" && blobUrl ? (
|
||||
{!isMail && media === "image" && blobUrl ? (
|
||||
<img className="reader-image" alt={element.title} src={blobUrl} style={{ width: `${Math.round(scale * 100)}%` }} />
|
||||
) : null}
|
||||
{media === "pdf" && pdf ? <canvas ref={canvasRef} className="reader-canvas" /> : null}
|
||||
{media === "pdf-frame" && blobUrl ? <iframe className="reader-frame" title={element.title} src={blobUrl} /> : null}
|
||||
{media === "other" && blobUrl ? (
|
||||
{!isMail && media === "pdf" && pdf ? <canvas ref={canvasRef} className="reader-canvas" /> : null}
|
||||
{!isMail && media === "pdf-frame" && blobUrl ? <iframe className="reader-frame" title={element.title} src={blobUrl} /> : null}
|
||||
{!isMail && media === "other" && blobUrl ? (
|
||||
<div className="reader-pane__state">
|
||||
<p>Die Datei ist geladen, aber hier nicht als Seite darstellbar.</p>
|
||||
<a href={blobUrl} target="_blank" rel="noreferrer">Original öffnen</a>
|
||||
|
||||
@@ -714,7 +714,9 @@ button.hierarchy__row { width: 100%; min-height: 34px; padding: 6px 8px; border:
|
||||
.mail-preview p, .mail-letter p { margin: 0; overflow-wrap: anywhere; }
|
||||
.mail-preview p span, .mail-letter p span { display: inline-block; min-width: 3.2rem; color: var(--text3); font-size: 12px; font-weight: 700; }
|
||||
.mail-preview__party { font-weight: 700; }
|
||||
.mail-files { margin: 0; padding-left: 1.1rem; }
|
||||
.mail-files { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 8px; }
|
||||
.mail-files button.is-current { background: var(--accent-light); color: var(--text); }
|
||||
.mail-attachment__caption { margin: 12px 0 0; font-weight: 650; }
|
||||
.preview-row--linked { box-shadow: inset 3px 0 0 var(--accent); }
|
||||
.bezug { flex: 1 1 100%; display: grid; gap: 2px; min-width: 0; }
|
||||
.bezug__link, a.bezug__link, p.bezug__link {
|
||||
|
||||
Reference in new issue
Block a user