Anhänge (Unterlagen + Mailanhänge): PDF im neuen Tab öffnen statt Download

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-06 15:42:12 +02:00
co-authored by Claude Opus 4.8
parent d99c8885fc
commit 4003279453
2 changed files with 24 additions and 4 deletions
+19
View File
@@ -103,6 +103,21 @@ if (!fs.existsSync(emailAnhaengeDir)) {
fs.mkdirSync(emailAnhaengeDir, { recursive: true });
}
// Serve a stored file with `Content-Disposition: inline` so the browser opens it
// (PDF/image) in a tab instead of downloading. Falls back to the file extension
// when no MIME type is stored.
function serveInline(res, filePath, filename, mime) {
const type = mime || {
'.pdf': 'application/pdf', '.png': 'image/png', '.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg', '.gif': 'image/gif', '.webp': 'image/webp',
}[path.extname(filename || filePath).toLowerCase()];
if (type) res.type(type);
const safe = String(filename || 'datei').replace(/["\r\n]/g, '');
res.setHeader('Content-Disposition',
`inline; filename="${safe}"; filename*=UTF-8''${encodeURIComponent(safe)}`);
res.sendFile(filePath);
}
// --- E-mail display + reply helpers --------------------------------------
// A stored message is rendered as HTML when we have an HTML body (or the plain
// body is actually HTML markup — some senders put HTML in the text/plain part).
@@ -1475,6 +1490,8 @@ initializeDatabase().then(() => {
if (!a) return res.status(404).send('Anhang nicht gefunden');
const p = path.join(emailAnhaengeDir, a.pfad);
if (!fs.existsSync(p)) return res.status(404).send('Datei nicht gefunden');
// inline=1 opens viewable files (PDF/image) in the browser tab instead of forcing a download.
if (req.query.inline === '1') return serveInline(res, p, a.name || a.pfad, a.mime);
res.download(p, a.name || a.pfad);
} catch (error) {
console.error('Error downloading e-mail attachment:', error);
@@ -1943,6 +1960,8 @@ initializeDatabase().then(() => {
if (!anhang) return res.status(404).send('Anhang nicht gefunden');
const filePath = path.join(anhaengeDir, anhang.pfad);
if (!fs.existsSync(filePath)) return res.status(404).send('Datei nicht gefunden');
// inline=1 opens viewable files (PDF/image) in the browser tab instead of forcing a download.
if (req.query.inline === '1') return serveInline(res, filePath, anhang.dateiname, anhang.mime);
res.download(filePath, anhang.dateiname);
} catch (error) {
console.error('Error downloading attachment:', error);