Created
January 31, 2018 16:38
-
-
Save zacharyhill/2ac6e31975896b1e52bb61aa12948fe1 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 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 | |
newMail.forEach((msg) => { | |
try { | |
if (msg.attachments.length) { | |
msg.attachments.forEach(async (attachment) => { | |
const file = attachment.content; | |
const cid = attachment.cid; | |
const filePath = path.join(__dirname, '..', 'data/uploads', cid); | |
fs.writeFile(filePath, file, (err, data) => { | |
if (err) { | |
console.log('err with file: ', err); | |
} else { | |
console.log('saved file: ' + filePath); | |
console.log('mime type: ', attachment.contentType); | |
} | |
}); | |
}); | |
} | |
} catch(err) { | |
next(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
this should work equal as well, and should be pretty zippy ;)