This commit is contained in:
Martin
2024-09-19 21:51:50 +08:00
commit 39fc7df763
10 changed files with 2788 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
const simpleParser = require('mailparser').simpleParser;
const { uploadToS3 } = require('./s3Service');
async function parseEmail(stream) {
const parsed = await simpleParser(stream);
const attachments = parsed.attachments || [];
delete parsed.attachments;
const processedAttachments = await Promise.all(attachments.map(async att => {
const s3Url = await uploadToS3(att);
return {
filename: att.filename,
contentType: att.contentType,
size: att.size,
s3Url: s3Url,
skipped: s3Url === null
};
}));
parsed.attachmentInfo = processedAttachments.filter(att => !att.skipped);
parsed.skippedAttachments = processedAttachments.filter(att => att.skipped).map(att => ({
filename: att.filename,
size: att.size
}));
return parsed;
}
module.exports = { parseEmail };
+25
View File
@@ -0,0 +1,25 @@
const config = require('../config');
async function uploadToS3(attachment) {
if (attachment.size > config.MAX_FILE_SIZE) {
console.log(`Skipping large attachment: ${attachment.filename} (${attachment.size} bytes)`);
return null;
}
const params = {
Bucket: config.BUCKET_NAME,
Key: `${Date.now()}-${attachment.filename}`,
Body: attachment.content,
ContentType: attachment.contentType
};
try {
const result = await config.s3.upload(params).promise();
return result.Location;
} catch (error) {
console.error('S3 upload error:', error);
throw error;
}
}
module.exports = { uploadToS3 };
+8
View File
@@ -0,0 +1,8 @@
const axios = require('axios');
const config = require('../config');
function sendToWebhook(data) {
return axios.post(config.WEBHOOK_URL, data, { timeout: 5000 });
}
module.exports = { sendToWebhook };