48hr.email/infrastructure/web/routes/inbox.js

180 lines
4.8 KiB
JavaScript
Raw Normal View History

2023-11-01 11:48:19 +01:00
const express = require('express')
const router = new express.Router()
2024-09-09 07:10:30 +02:00
const {param} = require('express-validator')
2023-11-01 13:04:44 +01:00
const config = require('../../../application/config')
2024-09-09 07:10:30 +02:00
const sanitizeAddress = param('address').customSanitizer(
2023-11-01 11:48:19 +01:00
(value, {req}) => {
return req.params.address
.replace(/[^A-Za-z0-9_.+@-]/g, '') // Remove special characters
.toLowerCase()
}
)
router.get('^/:address([^@/]+@[^@/]+)', sanitizeAddress, (req, res, _next) => {
const mailProcessingService = req.app.get('mailProcessingService')
res.render('inbox', {
2024-09-17 20:44:42 +02:00
title: `${config.http.branding[0]} | ` + req.params.address,
2023-11-01 11:48:19 +01:00
address: req.params.address,
2023-11-01 13:21:01 +01:00
mailSummaries: mailProcessingService.getMailSummaries(req.params.address),
2024-09-17 20:44:42 +02:00
madeby: config.http.branding[1],
madebysite: config.http.branding[2]
2023-11-01 11:48:19 +01:00
})
})
router.get(
'^/:address/:uid([0-9]+$)',
sanitizeAddress,
async (req, res, next) => {
try {
const mailProcessingService = req.app.get('mailProcessingService')
const mail = await mailProcessingService.getOneFullMail(
req.params.address,
req.params.uid
)
2024-09-28 05:21:51 +02:00
if (mail) {
2023-11-01 11:48:19 +01:00
// Emails are immutable, cache if found
res.set('Cache-Control', 'private, max-age=600')
res.render('mail', {
2024-09-28 01:49:06 +02:00
title: mail.subject + " | " + req.params.address,
2023-11-01 11:48:19 +01:00
address: req.params.address,
2023-11-01 13:21:37 +01:00
mail,
2023-11-02 06:25:22 +01:00
uid: req.params.uid,
2024-09-17 20:44:42 +02:00
madeby: config.http.branding[1],
madebysite: config.http.branding[2]
2023-11-01 11:48:19 +01:00
})
} else {
2024-01-26 02:41:50 +01:00
res.render(
'error',
{
address: req.params.address,
message: 'This mail could not be found. It either does not exist or has been deleted from our servers!',
2024-09-17 20:44:42 +02:00
madeby: config.http.branding[1],
madebysite: config.http.branding[2],
2024-01-26 02:41:50 +01:00
}
)
2023-11-01 11:48:19 +01:00
}
} catch (error) {
console.error('error while fetching one email', error)
next(error)
}
}
)
2024-09-28 02:16:58 +02:00
router.get(
'^/:address/delete-all',
sanitizeAddress,
async (req, res, next) => {
try {
const mailProcessingService = req.app.get('mailProcessingService')
const mailSummaries = await mailProcessingService.getMailSummaries(req.params.address)
for (mail in mailSummaries) {
await mailProcessingService.deleteSpecificEmail(req.params.address, mailSummaries[mail].uid)
}
res.redirect(`/inbox/${req.params.address}`)
} catch (error) {
console.error('error while deleting email', error)
next(error)
}
}
)
2023-11-02 06:25:22 +01:00
router.get(
2024-09-28 01:49:06 +02:00
'^/:address/:uid/delete',
2023-11-02 06:25:22 +01:00
sanitizeAddress,
async (req, res, next) => {
try {
const mailProcessingService = req.app.get('mailProcessingService')
2023-11-02 06:27:28 +01:00
await mailProcessingService.deleteSpecificEmail(req.params.address, req.params.uid)
2023-11-03 06:11:17 +01:00
res.redirect(`/inbox/${req.params.address}`)
2023-11-02 06:25:22 +01:00
} catch (error) {
console.error('error while deleting email', error)
next(error)
}
}
)
2024-09-28 09:04:05 +02:00
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 {
2024-09-29 09:56:35 +02:00
res.set('Content-Disposition', `attachment; filename=${attachment.filename}`);
res.set('Content-Type', attachment.contentType);
res.send(attachment.content);
return;
2024-09-28 09:04:05 +02:00
} 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)
}
}
)
2024-09-28 01:49:06 +02:00
router.get(
'^/:address/:uid/raw',
sanitizeAddress,
async (req, res, next) => {
try {
const mailProcessingService = req.app.get('mailProcessingService')
2024-09-28 04:44:13 +02:00
mail = await mailProcessingService.getOneFullMail(
2024-09-28 01:49:06 +02:00
req.params.address,
2024-09-28 04:44:13 +02:00
req.params.uid,
true
2024-09-28 01:49:06 +02:00
)
2024-09-28 04:44:13 +02:00
mail = mail.replace(/(?:\r\n|\r|\n)/g, '<br>')
2024-09-28 05:21:51 +02:00
if (mail) {
2024-09-28 01:49:06 +02:00
// Emails are immutable, cache if found
res.set('Cache-Control', 'private, max-age=600')
res.render('raw', {
2024-09-28 04:44:13 +02:00
title: req.params.uid + " | raw | " + req.params.address,
2024-09-28 01:49:06 +02:00
mail
})
} else {
res.render(
'error',
{
address: req.params.address,
message: 'This mail 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],
}
)
}
} catch (error) {
console.error('error while fetching one email', error)
next(error)
}
}
)
2023-11-01 11:48:19 +01:00
module.exports = router