Add attachment previews and attachments view
Bump version to 0.2.2 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -1169,6 +1169,43 @@ ipcMain.handle('mail:save-attachment', async (event, input) => {
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('mail:preview-attachment', async (_event, input) => {
|
||||
const account = await readAccount();
|
||||
if (!account) throw new Error('Add a mail account before previewing attachments.');
|
||||
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 simpleParser(item.source);
|
||||
const attachment = parsed.attachments?.find(file => (file.filename || 'attachment') === filename);
|
||||
if (!attachment) throw new Error('No attachment was found in this message.');
|
||||
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.' };
|
||||
} finally {
|
||||
lock.release();
|
||||
}
|
||||
} finally {
|
||||
if (client.usable) await client.logout().catch(() => {});
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('mail:open-external', async (event, rawUrl) => {
|
||||
let target;
|
||||
try {
|
||||
|
||||
@@ -18,6 +18,7 @@ contextBridge.exposeInMainWorld('daybreakMail', {
|
||||
onSyncUpdate: callback => ipcRenderer.on('mail:sync-update', (_event, update) => callback(update)),
|
||||
updateMessage: details => ipcRenderer.invoke('mail:update-message', details),
|
||||
setSnooze: details => ipcRenderer.invoke('mail:set-snooze', details),
|
||||
previewAttachment: details => ipcRenderer.invoke('mail:preview-attachment', details),
|
||||
saveAttachment: details => ipcRenderer.invoke('mail:save-attachment', details),
|
||||
openExternal: url => ipcRenderer.invoke('mail:open-external', url),
|
||||
send: message => ipcRenderer.invoke('mail:send', message),
|
||||
|
||||
Reference in New Issue
Block a user