84 lines
3.0 KiB
JavaScript
84 lines
3.0 KiB
JavaScript
import { escapeHtml } from './content.js'
|
|
import { mailHtml, safeImageUrl } from './mail-html.js'
|
|
|
|
function raster(mime, content) {
|
|
if (mime === 'image/png')
|
|
return content.subarray(0, 8).equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]))
|
|
if (mime === 'image/jpeg') return content[0] === 255 && content[1] === 216 && content[2] === 255
|
|
if (mime === 'image/gif') return ['GIF87a', 'GIF89a'].includes(content.subarray(0, 6).toString())
|
|
if (mime === 'image/webp')
|
|
return (
|
|
content.subarray(0, 4).toString() === 'RIFF' && content.subarray(8, 12).toString() === 'WEBP'
|
|
)
|
|
return false
|
|
}
|
|
export function messageView(s, m) {
|
|
const original = s.get(
|
|
'SELECT html,source IS NOT NULL has_source FROM message_originals WHERE message_id=?',
|
|
m.id,
|
|
)
|
|
const source =
|
|
original?.html || s.get('SELECT html FROM message_html WHERE message_id=?', m.id)?.html
|
|
const markup = source ? Buffer.from(source, 'utf8') : null
|
|
const truncated = !!markup && markup.length > 2 * 1024 * 1024
|
|
const attachments = s.all(
|
|
"SELECT a.name,a.mime,a.content,i.content_id FROM attachments a LEFT JOIN attachment_inline i ON a.id=i.attachment_id WHERE a.message_id=? AND a.mime IN ('image/png','image/jpeg','image/gif','image/webp')",
|
|
m.id,
|
|
)
|
|
let bytes = 0,
|
|
blockedInline = 0,
|
|
remoteImages = 0
|
|
const cache = new Map()
|
|
const resolveImage = (value) => {
|
|
const raw = String(value || '').trim()
|
|
if (!raw) return null
|
|
if (cache.has(raw)) {
|
|
const cached = cache.get(raw)
|
|
if (cached.bytes && bytes + cached.bytes > 20 * 1024 * 1024) {
|
|
blockedInline++
|
|
return null
|
|
}
|
|
bytes += cached.bytes
|
|
return cached.url
|
|
}
|
|
let embeddedBytes = 0
|
|
let result = safeImageUrl(raw)
|
|
if (result?.startsWith('https:')) remoteImages++
|
|
if (!result && !/^(https?:|\/\/|data:)/i.test(raw)) {
|
|
let name = raw.replace(/^cid:/i, '').replace(/^<|>$/g, '')
|
|
try {
|
|
name = decodeURIComponent(name)
|
|
} catch {}
|
|
name = name.replace(/^<|>$/g, '')
|
|
if (!/^cid:/i.test(raw)) name = name.replace(/^\.\//, '')
|
|
const matching = attachments.filter((a) =>
|
|
/^cid:/i.test(raw) ? a.content_id === name : a.name === name,
|
|
)
|
|
const image = matching.length === 1 ? matching[0] : null
|
|
if (
|
|
image &&
|
|
raster(image.mime, image.content) &&
|
|
bytes + image.content.length <= 20 * 1024 * 1024
|
|
) {
|
|
bytes += image.content.length
|
|
embeddedBytes = image.content.length
|
|
result = `data:${image.mime};base64,${image.content.toString('base64')}`
|
|
} else blockedInline++
|
|
}
|
|
cache.set(raw, { url: result, bytes: embeddedBytes })
|
|
return result
|
|
}
|
|
const html = source
|
|
? mailHtml(markup.subarray(0, 2 * 1024 * 1024).toString('utf8'), { resolveImage })
|
|
: `<div style="white-space:pre-wrap">${escapeHtml(m.body).replace(/\n/g, '<br>')}</div>`
|
|
return {
|
|
html,
|
|
text: m.body,
|
|
hasSource: !!original?.has_source,
|
|
hasHtml: !!source,
|
|
truncated,
|
|
blockedInline,
|
|
remoteImages,
|
|
}
|
|
}
|