fix: corretta visualizzazione di caratteri strani all'interno del testo delle email.

This commit is contained in:
Gabriele Cimaschi committed 2026-09-10 22:20:33 +02:00
1 parent c1dc86d6cd
commit 6944b02263
2 files changed
+173 -5

No files matched your search

+28 -5
View File
@@ -2,6 +2,7 @@ const express = require('express');
const router = express.Router();
const pool = require('../db');
const { db, logAttivita } = require('../activityDb');
const { cleanAndDecodeText } = require('../utils/textDecoder');
// Helper to get local timestamp in YYYY-MM-DD HH:mm:ss format
function getLocalTimestamp() {
@@ -292,6 +293,12 @@ router.get('/', async (req, res) => {
}
}
result.rows.forEach(t => {
if (t.title) t.title = cleanAndDecodeText(t.title);
if (t.customer_first) t.customer_first = cleanAndDecodeText(t.customer_first);
if (t.customer_last) t.customer_last = cleanAndDecodeText(t.customer_last);
});
res.json({
tickets: result.rows,
total,
@@ -443,14 +450,17 @@ router.get('/:id', async (req, res) => {
[id]
);
// Fetch HTML body attachment content for articles if present (e.g. file-1, file-1.html, text/html)
// Fetch HTML body attachment content for articles if present (e.g. file-1, file-2, text/html, html attachments)
const htmlAttachmentsRes = await pool.query(
`SELECT id, article_id, filename, content_type, content
FROM article_data_mime_attachment
WHERE article_id IN (SELECT id FROM article WHERE ticket_id = $1)
AND (
filename LIKE 'file-%'
OR filename LIKE '%.html'
OR filename LIKE '%.htm'
OR content_type LIKE '%html%'
OR content_type LIKE '%text%'
OR disposition = 'inline'
)
ORDER BY id ASC`,
@@ -522,10 +532,14 @@ router.get('/:id', async (req, res) => {
return text;
};
if (ticket.title) ticket.title = cleanAndDecodeText(ticket.title);
if (ticket.customer_first) ticket.customer_first = cleanAndDecodeText(ticket.customer_first);
if (ticket.customer_last) ticket.customer_last = cleanAndDecodeText(ticket.customer_last);
const htmlBodyMap = {};
for (const row of htmlAttachmentsRes.rows) {
if (row.content) {
const contentStr = decodeTextContent(row.content, row.content_type);
const contentStr = cleanAndDecodeText(row.content, row.content_type);
if (contentStr && /<(div|p|br|table|html|body|span|img|a|style)\b/i.test(contentStr)) {
const fname = (row.filename || '').toLowerCase();
if (!htmlBodyMap[row.article_id] || fname.startsWith('file-1') || fname.startsWith('file-2')) {
@@ -536,12 +550,21 @@ router.get('/:id', async (req, res) => {
}
articlesResult.rows.forEach(a => {
// Ensure a.a_body and subject are properly decoded if legacy encoded
// Decode article subjects, bodies, and sender/recipient headers
if (a.a_body) {
a.a_body = decodeTextContent(a.a_body, a.a_content_type);
a.a_body = cleanAndDecodeText(a.a_body, a.a_content_type);
}
if (a.a_subject) {
a.a_subject = decodeTextContent(a.a_subject, 'text/plain; charset=utf-8');
a.a_subject = cleanAndDecodeText(a.a_subject);
}
if (a.a_from) {
a.a_from = cleanAndDecodeText(a.a_from);
}
if (a.a_to) {
a.a_to = cleanAndDecodeText(a.a_to);
}
if (a.a_cc) {
a.a_cc = cleanAndDecodeText(a.a_cc);
}
if (htmlBodyMap[a.article_id]) {