-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.js
65 lines (54 loc) · 1.56 KB
/
model.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
var CSV = require('a-csv');
var modm = require('modm');
// Modm model with settings
var model = modm('dms', {
host: 'localhost',
port: 27017,
server: {
pooSize: 5
},
db: {w: 1}
});
var schema = new modm.Schema({field: String});
// Validate uploaded file
exports.validateFile = function (file, callback) {
if (!file.csv || !file.csv.size) {
callback('Invalid file upload!');
} else if (file.csv.type != 'text/csv') {
callback('Invalid file type');
}
};
// Get first row of the uploaded file. It does not need header parameter to parse CSV.
exports.getFirstRow = function (filePath, options, callback) {
if (typeof opitons === 'function') {
callback = options;
options = {};
}
var absolutePath = M.config.APPLICATION_ROOT + M.config.app.id + '/' + filePath;
CSV.parse(absolutePath, options, function (err, row, next) {
if (err || !row) {
return callback(err || 'Could not read the first row from file: ' + filePath);
}
callback(null, row);
});
};
// Parse options selected by user
exports.getOptions = function (options, callback) {
if (!options || !options.charset || !options.separator) {
callback('Invalid options');
return;
}
// tmp object store options
var tmp = {
charset: options.charset
}
switch (options.separator) {
case 'semicolon':
tmp.delimiter = ';';
break;
case 'comma':
tmp.delimiter = ',';
break;
}
callback(null, tmp);
};