62 lines
3.0 KiB
JavaScript
62 lines
3.0 KiB
JavaScript
const fs = require('node:fs/promises');
|
|
|
|
function createAttachmentService({ imapClient, folderPath, parseForAttachments, writeFile = fs.writeFile }) {
|
|
async function loadAttachment(account, input, selectAttachment) {
|
|
const uid = Number(input?.uid);
|
|
const folder = String(input?.folder || 'Inbox');
|
|
const filename = String(input?.filename || 'attachment');
|
|
if (!Number.isInteger(uid) || uid < 1) throw new Error('That message could not be identified.');
|
|
|
|
const client = imapClient(account);
|
|
try {
|
|
await client.connect();
|
|
const mailboxPath = await folderPath(client, folder);
|
|
if (!mailboxPath) throw new Error('That mail folder could not be found.');
|
|
const lock = await client.getMailboxLock(mailboxPath);
|
|
try {
|
|
const item = await client.fetchOne(uid, { uid: true, source: true }, { uid: true });
|
|
if (!item?.source) throw new Error('The message is no longer available.');
|
|
const parsed = await parseForAttachments(item.source);
|
|
const attachment = selectAttachment(parsed.attachments || [], filename);
|
|
if (!attachment) throw new Error('No attachment was found in this message.');
|
|
return { attachment, filename };
|
|
} finally {
|
|
lock.release();
|
|
}
|
|
} finally {
|
|
if (client.usable) await client.logout().catch(() => {});
|
|
}
|
|
}
|
|
|
|
async function saveAttachment(account, input, showSaveDialog) {
|
|
const { attachment, filename } = await loadAttachment(account, input, (attachments, selectedName) =>
|
|
attachments.find(file => (file.filename || 'attachment') === selectedName) || attachments[0]);
|
|
const safeFilename = filename.replace(/[\\/:*?"<>|]/g, '_');
|
|
const result = await showSaveDialog({
|
|
defaultPath: (attachment.filename || safeFilename).replace(/[\\/:*?"<>|]/g, '_')
|
|
});
|
|
if (result.canceled || !result.filePath) return { canceled: true };
|
|
await writeFile(result.filePath, attachment.content);
|
|
return { saved: true };
|
|
}
|
|
|
|
async function previewAttachment(account, input) {
|
|
const { attachment } = await loadAttachment(account, input, (attachments, filename) =>
|
|
attachments.find(file => (file.filename || 'attachment') === filename));
|
|
const mimeType = String(attachment.contentType || '').toLowerCase().split(';')[0];
|
|
const size = attachment.content.length;
|
|
if (/^image\/(png|jpe?g|gif|webp|bmp|svg\+xml)$/.test(mimeType)) {
|
|
if (size > 5 * 1048576) return { kind: 'none', reason: 'This image is too large to preview. Download it instead.' };
|
|
return { kind: 'image', dataUrl: `data:${mimeType};base64,${attachment.content.toString('base64')}` };
|
|
}
|
|
if (mimeType.startsWith('text/') || ['application/json', 'application/xml'].includes(mimeType)) {
|
|
return { kind: 'text', text: attachment.content.subarray(0, 100000).toString('utf8'), truncated: size > 100000 };
|
|
}
|
|
return { kind: 'none', reason: 'No preview is available for this file type. Download it to open it.' };
|
|
}
|
|
|
|
return { previewAttachment, saveAttachment };
|
|
}
|
|
|
|
module.exports = { createAttachmentService };
|