add option to download attachments

pull/16/head
ClaraCrazy 2024-09-28 09:04:05 +02:00
parent 729dcf17ef
commit c3565b5b34
3 changed files with 64 additions and 9 deletions

View File

@ -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

View File

@ -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,

View File

@ -45,14 +45,9 @@
{% endif %}
{% if mail.attachments %}
<div class="mail_attachments" >
<p>Download your attachments: (SoonTM)
<p>
{% for attachment in mail.attachments %}
<a href="">📎 {{ attachment.filename }}</a>
{#
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
#}
<a href="/inbox/{{ address }}/{{ uid }}/{{ attachment.checksum }}"><u>📎 {{ attachment.filename }}</u></a>
{% endfor %}
</p>
</div>