@@ -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 - Z a - z 0 - 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 - Z a - z 0 - 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 - Z a - z 0 - 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 - Z a - z 0 - 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+ }
0 commit comments