-
-
Save jkantr/a4962b28dc58a0e8be1108c5a3fd4d6e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
const { promisify } = require('util'); | |
const writeFileAsync = promisify(fs.writeFile); | |
const s3etm = require('s3-emails-to-mongo'); | |
// MAKE THIS PART DYNAMIC LATER | |
s3etm.configure({ | |
Bucket: 'zhillb-mail', | |
}); | |
module.exports = async (req, res, next) => { | |
try { | |
const newMail = await s3etm(); | |
// check if new mail has attachment(s), if so save them | |
await Promise.all(newMail.map((msg) => { | |
if (msg.attachments.length) { | |
return Promise.all(msg.attachments.map((attachment) => { | |
const file = attachment.content; | |
const cid = attachment.cid; | |
const filePath = path.join(__dirname, '..', 'data/uploads', cid); | |
return writeFileAsync(filePath, file).then(() => { | |
console.log('saved file: ' + filePath); | |
console.log('mime type: ', attachment.contentType); | |
}).catch((err) => { | |
console.log('err with file: ', err); | |
throw err; | |
}) | |
})); | |
} | |
})) | |
res.json(newMail); | |
} catch(err) { | |
next(err); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment