Created
June 18, 2012 18:17
-
-
Save baudehlo/2949796 to your computer and use it in GitHub Desktop.
Haraka Plugin to save attachments to disk
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
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; | |
var id = 1; | |
// Start | |
function start( content_type, filename) { | |
haraka.loginfo("Saving Attachment: " + filename); | |
var currentAttachment = '/tmp/' + uid + '-' + (filename || (id++ + '.txt')); | |
connection.transaction.notes.attachments[filename] = currentAttachment; | |
connection.transaction.notes.current_stream = fs.createWriteStream( currentAttachment ); | |
} | |
// Accumulate attachment data | |
function data( buf ) { | |
// haraka.logdebug("write data"); | |
if ( connection.transaction.notes.current_stream && | |
connection.transaction.notes.current_stream.writable ) { | |
// haraka.loginfo("Saving " + buf.length + " bytes"); | |
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