Skip to content

Commit d7f1830

Browse files
committed
Merge branch 'develop'
2 parents 8d40a7b + 0ce84d2 commit d7f1830

File tree

3 files changed

+58
-23
lines changed

3 files changed

+58
-23
lines changed

Changes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.4.8 - 2019-12-05
2+
3+
- The filename of files within a winmail.dat are now cleaned up as well (surprisingly a lot of people are still sending those)
4+
15
# 1.4.7 - 2019-12-04
26

37
- Limiting parsing and converting from text to html to 4MB (we saw larger emails to hold up processing significantly)

index.js

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -694,19 +694,9 @@ function _storeAttachments(connection, plugin, attachments, mail_object, cb) {
694694
// Filename cleanup
695695
if (attachment.fileName !== 'attachment.txt' && attachment.fileName !== 'invite.ics') {
696696

697-
// Split filename by last dot
698-
var _fN = attachment.fileName.split(/\.(?=[^\.]+$)/);
699-
// Clean up filename that could potentially cause an issue
700-
var _fN_clean = _fN[0].replace(/[^A-Za-z0-9]/g, '_');
701-
// Finale Filename
702-
attachment.fileName = `${_fN_clean}.${_fN[1]}`;
703-
704-
// Split generated filename by last dot
705-
var _fNG = attachment.generatedFileName.split(/\.(?=[^\.]+$)/);
706-
// Clean up filename that could potentially cause an issue
707-
var _fNG_clean = _fNG[0].replace(/[^A-Za-z0-9]/g, '_');
708-
// Finale Filename
709-
attachment.generatedFileName = `${_fNG_clean}.${_fNG[1]}`;
697+
var _file_names = _cleanFileName(attachment.fileName, attachment.generatedFileName);
698+
attachment.fileName = _file_names.file_name;
699+
attachment.generatedFileName = _file_names.generated_file_name;
710700

711701
}
712702

@@ -763,7 +753,7 @@ function _storeAttachments(connection, plugin, attachments, mail_object, cb) {
763753
// use tnef to extract the file into the same directory
764754
var exec_command = `tnef ${attachment_full_path} -C ${attachment_directory}`;
765755

766-
plugin.lognotice('converting winmail.dat by calling :', exec_command);
756+
plugin.lognotice('WINMAIL: Converting :', exec_command);
767757

768758
// execute the tnef process to extract the real attachment
769759
var tnef_process = exec(exec_command, function (error, stdout, stderr) {
@@ -772,22 +762,42 @@ function _storeAttachments(connection, plugin, attachments, mail_object, cb) {
772762
// get the contents of the directory as all for the attachments
773763
fs.readdir(attachment_directory, function (error, contents) {
774764

775-
// loop over each file in the direoctory that is not winmail.dat and add it as an attachment
765+
// loop over each file in the directory that is not winmail.dat and add it as an attachment
776766
async.eachLimit(contents.filter((fn) => fn !== 'winmail.dat'), 3, function (file_name, each_callback) {
767+
768+
// Path to original file
769+
var _path_org = path.join(attachment_directory, file_name);
770+
// plugin.loginfo(`WINMAIL.DAT: PATH ORG: ${_path_org}`);
771+
772+
// Convert filename
773+
var _file_names = _cleanFileName(file_name, file_name);
774+
var _file_name_new = _file_names.file_name;
775+
// plugin.loginfo(`WINMAIL.DAT: NEW NAME: ${_file_name_new}`);
776+
777+
// Path to new file
778+
var _path_new = path.join(attachment_directory, _file_name_new);
779+
// plugin.loginfo(`WINMAIL.DAT: NEW PATH: ${_file_name_new}`);
780+
781+
// Convert the name on disk
782+
try {
783+
fs.moveSync(_path_org, _path_new, { overwrite: true });
784+
}
785+
catch(e) {}
786+
777787
// get the size of the file from the stats
778-
var attachment_file_path = path.join(attachment_directory, file_name);
779-
fs.stat(attachment_file_path, function (error, stats) {
788+
fs.stat(_path_new, function (error, stats) {
780789

781-
if (error) plugin.logerror('errror getting stats, error :', error);
790+
if (error) plugin.logerror('error getting stats, error :', error);
782791

783792
var attachment = {
784793
'length' : stats ? +stats.size : 0,
785-
'fileName' : file_name,
786-
'generatedFileName' : file_name,
794+
'fileName' : _file_name_new,
795+
'generatedFileName' : _file_name_new,
787796
'checksum' : attachment_checksum
788797
};
789-
790-
// plugin.lognotice('tnef extracted attachment : ', attachment);
798+
// plugin.loginfo(`WINMAIL.DAT: ATTACHMENT OBJECT: ${_file_name_new}`);
799+
// If we can store
800+
plugin.loginfo(`WINMAIL: Attachment ${_file_name_new} successfully stored locally`);
791801

792802
_attachments.push(attachment);
793803

@@ -889,3 +899,24 @@ function _checkInlineImages(plugin, email, callback) {
889899
// Return
890900
return callback(null, email);
891901
}
902+
903+
// Cleanup filename of attachment
904+
function _cleanFileName(file_name, generated_file_name) {
905+
906+
// Split filename by last dot
907+
var _fN = file_name.split(/\.(?=[^\.]+$)/);
908+
// Clean up filename that could potentially cause an issue
909+
var _fN_clean = _fN[0].replace(/[^A-Za-z0-9]/g, '_');
910+
911+
// Split generated filename by last dot
912+
var _fNG = generated_file_name.split(/\.(?=[^\.]+$)/);
913+
// Clean up filename that could potentially cause an issue
914+
var _fNG_clean = _fNG[0].replace(/[^A-Za-z0-9]/g, '_');
915+
916+
// Return
917+
return {
918+
'file_name' : `${_fN_clean}.${_fN[1]}`,
919+
'generated_file_name' : `${_fNG_clean}.${_fNG[1]}`
920+
};
921+
922+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "haraka-plugin-mongodb",
3-
"version": "1.4.7",
3+
"version": "1.4.8",
44
"description": "Haraka plugin that stores emails in MongoDb. Additionally, stores reports on delivery.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)