From c3565b5b3460c86dcb44ff7f1127e20c8fd6bcbf Mon Sep 17 00:00:00 2001 From: ClaraCrazy Date: Sat, 28 Sep 2024 09:04:05 +0200 Subject: [PATCH] add option to download attachments --- application/imap-service.js | 8 +++-- infrastructure/web/routes/inbox.js | 56 ++++++++++++++++++++++++++++++ infrastructure/web/views/mail.twig | 9 ++--- 3 files changed, 64 insertions(+), 9 deletions(-) diff --git a/application/imap-service.js b/application/imap-service.js index f23b19b..190ae2c 100644 --- a/application/imap-service.js +++ b/application/imap-service.js @@ -254,8 +254,12 @@ class ImapService extends EventEmitter { .map(addressObj => addressObj.address) const from = headerPart.from.flatMap(from => addressparser(from)) - - const subject = headerPart.subject[0] + let subject = "No Subject" + try { + subject = headerPart.subject[0] + } catch { + // Do nothing + } const date = headerPart.date[0] const {uid} = message.attributes diff --git a/infrastructure/web/routes/inbox.js b/infrastructure/web/routes/inbox.js index f6a58d5..90d09d5 100644 --- a/infrastructure/web/routes/inbox.js +++ b/infrastructure/web/routes/inbox.js @@ -95,6 +95,62 @@ router.get( } ) +router.get( + '^/:address/:uid/:checksum([a-f0-9]+$)', + sanitizeAddress, + async (req, res, next) => { + try { + const mailProcessingService = req.app.get('mailProcessingService') + const mail = await mailProcessingService.getOneFullMail( + req.params.address, + req.params.uid + ) + var index = mail.attachments.findIndex(attachment => attachment.checksum === req.params.checksum); + const attachment = mail.attachments[index]; + if (attachment) { + try { + if (attachment) { + res.set('Content-Disposition', `attachment; filename=${attachment.filename}`); + res.set('Content-Type', attachment.contentType); + res.send(attachment.content); + return; // Add this line to exit the function after sending the response + } else { + res.render( + 'error', + { + address: req.params.address, + message: 'This attachment could not be found. It either does not exist or has been deleted from our servers!', + madeby: config.http.branding[1], + madebysite: config.http.branding[2], + } + ); + return; // Add this line to exit the function after rendering the error page + } + } catch (error) { + console.error('error while fetching attachment', error); + next(error); + } + } else { + res.render( + 'error', + { + address: req.params.address, + message: 'This attachment could not be found. It either does not exist or has been deleted from our servers!', + madeby: config.http.branding[1], + madebysite: config.http.branding[2], + } + ) + } + res.redirect(`/inbox/${req.params.address}`) + } catch (error) { + console.error('error while deleting email', error) + next(error) + } + } +) + + + router.get( '^/:address/:uid/raw', sanitizeAddress, diff --git a/infrastructure/web/views/mail.twig b/infrastructure/web/views/mail.twig index e38269c..a858864 100644 --- a/infrastructure/web/views/mail.twig +++ b/infrastructure/web/views/mail.twig @@ -45,14 +45,9 @@ {% endif %} {% if mail.attachments %}
-

Download your attachments: (SoonTM) +

{% for attachment in mail.attachments %} - 📎 {{ attachment.filename }} - {# - Add actual button to download each file, inline. - Or even better. just an a href with filename. - very basic, like regular links look. maybe a paperclip icon - #} + 📎 {{ attachment.filename }} {% endfor %}