Skip to content

Instantly share code, notes, and snippets.

@baudehlo
Created May 24, 2011 17:37
Show Gist options
  • Save baudehlo/989217 to your computer and use it in GitHub Desktop.
Save baudehlo/989217 to your computer and use it in GitHub Desktop.
var fs = require('fs');
// Bind to receiving of data
exports.hook_data = function( next, connection, params ) {
var haraka = this,
uid = connection.transaction.uuid;
connection.transaction.notes.attachments = {};
// Make sure body gets parsed
connection.transaction.parse_body = 1;
// Start
function start( content_type, filename) {
haraka.loginfo("Saving Attachment: " + filename);
currentAttachment = '/tmp/' + uid + '-' + filename;
connection.transaction.notes.attachments[filename] = currentAttachment;
connection.transaction.notes.current_stream = fs.createWriteStream( currentAttachment );
}
// Accumulate attachment data
function data( buf ) {
if ( connection.transaction.notes.current_stream &&
connection.transaction.notes.current_stream.writable ) {
connection.transaction.notes.current_stream.write( buf );
}
}
// End
function end() {
haraka.loginfo("End of attachment");
if ( connection.transaction.notes.current_stream ) {
connection.transaction.notes.current_stream.on('close', function () {
delete connection.transaction.notes.current_stream;
});
connection.transaction.notes.current_stream.destroySoon();
}
}
// Bind attachment listeners
connection.transaction.attachment_hooks( start, data, end );
// Continue with email
next();
}
exports.hook_data_post = function (next, connection) {
if (connection.transaction.notes.current_stream) {
connection.transaction.notes.current_stream.on('close', function () {
next();
});
}
else {
next();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment