forked from Robmbackus/mail-attachment-notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
190 lines (146 loc) · 4.86 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
var notifier = require('mail-notifier');
var async = require('async');
var fs = require('fs'),
csv = require('csv-stream'),
xlsx = require('xlsx-to-json'),
AdmZip = require('adm-zip'),
mime = require('mime'),
uuid = require('node-uuid'),
util = require('util'),
EventEmitter = require('events').EventEmitter;
function ProcessAttachments(filePath, fileExtension, callback) {
function processFile() {
switch (fileExtension) {
case 'csv':
csvToJson(filePath, function (err, result) {
callback(err, result);
})
break;
case 'xlsx':
xlsxToJson(filePath, function (err, result) {
callback(err, result);
})
break
case 'txt':
readText(filePath, function (err, result) {
callback(err, result);
})
break;
default:
readText(filePath, function (err, result) {
callback(err, result);
});
break
}
}
if (fileExtension == 'zip') {
unzipAttachment(filePath, function (newPath, newExtension) {
processFile(newPath)
});
} else {
processFile(filePath)
}
}
function unzipAttachment(filePath, callback) {
var newPath;
var zip = new AdmZip(filePath);
var zipEntries = zip.getEntries(); // an array of ZipEntry records
zipEntries.forEach(function (zipEntry) {
if (!zipEntry.isDirectory && zipEntry.name.indexOf('._') < 0) {
var contentType = mime.lookup(zipEntry.name);
var fileExtension = mime.extension(contentType);
var fileName = uuid.v1() + '.' + fileExtension;
var newPath = '/tmp/' + fileName;
fs.writeFile(newPath, zipEntry.getData(), function (err) {
callback(newPath, fileExtension);
})
}
});
}
function readText(filePath, callback) {
fs.readFile(filePath, 'utf8', function (err, data) {
if (err) throw err;
callback(false, [{data:data}]);
});
}
function csvToJson(filePath, callback) {
var dataList = [];
var csvStream = csv.createStream(options);
var options = {
// delimiter : '\t', // default is ,
endLine: '\r\n', // default is \n,
// columns : ['columnName1', 'columnName2'], // by default read the first line and use values found as columns
escapeChar: '"', // default is an empty string
enclosedChar: '"' // default is an empty string
}
fs.createReadStream(filePath, {autoClose: true})
.pipe(csvStream)
.on('close', function (f) {
callback(false, dataList)
})
.on('error', function (err) {
})
.on('data', function (data) {
dataList.push(data);
})
}
function xlsxToJson(filePath, callback) {
xlsx({
input: filePath,
output: null
}, function (err, data) {
if (err) {
callback(true, null)
} else {
//Array of data
callback(false, data)
}
});
}
var n;
function MailAttachment(opts) {
EventEmitter.call(this);
n = notifier(opts)
var self = this;
self.options = opts;
}
util.inherits(MailAttachment, EventEmitter);
MailAttachment.prototype.start = function(){
var self = this;
n.on('end', function() {
n.start();
});
n.on('mail', function (mail) {
if (mail.attachments) {
async.each(mail.attachments, function (attachment, callback) {
var filePath = (self.options.directory || "/tmp") + '/' + attachment.fileName;
fs.writeFile(filePath, attachment.content, function (err) {
if (err) {
} else {
var fileExtension = mime.extension(attachment.contentType);
ProcessAttachments(filePath, fileExtension, function (err, result) {
var event = {
date:mail.date,
from:mail.from,
to:mail.to,
subject:mail.subject,
fileName:attachment.fileName,
contentType:attachment.contentType,
extention:fileExtension,
path:filePath,
data:result
};
self.emit('attachment', event);
})
}
});
})
}
}).start()
}
MailAttachment.prototype.stop = function () {
n.stop();
}
module.exports = function (opts) {
return new MailAttachment(opts);
};