forked from pantsel/konga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv.js
125 lines (111 loc) · 4.8 KB
/
csv.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
var csv = require('csv-parser')
var fs = require('fs')
module.exports = {
enabled : true,
schema : {
"name": "CSV",
"value": "csv",
"hasFiles" : true,
"description": "Import Consumers from a .csv document",
"form_fields": {
"connection": {
"file" : {
"name" : "File",
"type" : "file",
"required": true,
"description" : "Select the .csv document containing the consumers"
},
"raw" : {
name : "raw",
"type" : "boolean",
"default" : false,
description : "Whether or not to decode to utf-8 strings (optional)."
},
"separator" : {
name : "separator",
"type" : "text",
description : "Specify optional cell separator. Defaults to <code>','</code>"
},
"quote" : {
name : "quote",
"type" : "text",
description : "Specify optional quote character. Defaults to <code>'\"'</code>"
},
"escape" : {
name : "escape",
"type" : "text",
description : "Specify optional escape character. Defaults to quote value"
},
"newline" : {
name : "newline",
"type" : "text",
description : "Specify a newline character. Defaults to <code>'\\n'</code>"
},
"strict" : {
name : "strict",
"type" : "boolean",
"default" : true,
description : "Require column length match headers length (optional)."
},
"headers" : {
name : "headers",
"type" : "text",
"required": true,
"description" : "Specify the headers of each .csv row as a comma separated string." +
" ex: <code>'id,name,email,updated_at,created_at...'</code>"
}
},
"consumer": {
"username": {
"name": "username column",
"type": "text",
"required": true,
"description": "The header of the cell that will be used as the consumer <code>username</code>."
},
"custom_id": {
"name": "custom_id field",
"type": "text",
"required": true,
"description": "The header of the cell that will be used as the consumer <code>custom_id</code>."
}
}
}
},
methods : {
loadConsumers : function(req,res) {
req.file('file').upload(function (err, uploadFiles) {
if(err) return res.negotiate(err);
if(!uploadFiles.length) return res.badRequest("No files uploaded")
var result = [];
fs.createReadStream(uploadFiles[0].fd)
.pipe(csv({
raw: req.body.raw || false, // do not decode to utf-8 strings
separator: req.body.separator || ',', // specify optional cell separator
quote: req.body.quote || '"', // specify optional quote character
escape: req.body.escape || '"', // specify optional escape character (defaults to quote value)
newline: req.body.newline || '\n', // specify a newline character
strict: req.body.strict || true, // require column length match headers length
headers: req.body.headers.split(",") // Specifing the headers
}))
.on('data', function (data) {
var consumer = {}
Object.keys(data).forEach(function(key){
if(key === req.body.username) {
consumer.username = data[key]
}
if(key === req.body.custom_id) {
consumer.custom_id = data[key]
}
})
if(Object.keys(data).length === 2)
result.push(consumer);
})
.on('end', function () {
if(!result.length)
return res.notFound("No consumers found");
return res.json(result)
})
});
}
}
}