1 | var readFile = require('fs').readFile,
|
---|
2 | basename = require('path').basename;
|
---|
3 |
|
---|
4 | exports.build = function(data, boundary, callback) {
|
---|
5 |
|
---|
6 | if (typeof data != 'object' || typeof data.pipe == 'function')
|
---|
7 | return callback(new Error('Multipart builder expects data as key/val object.'));
|
---|
8 |
|
---|
9 | var body = '',
|
---|
10 | object = flatten(data),
|
---|
11 | count = Object.keys(object).length;
|
---|
12 |
|
---|
13 | if (count === 0)
|
---|
14 | return callback(new Error('Empty multipart body. Invalid data.'))
|
---|
15 |
|
---|
16 | function done(err, section) {
|
---|
17 | if (err) return callback(err);
|
---|
18 | if (section) body += section;
|
---|
19 | --count || callback(null, body + '--' + boundary + '--');
|
---|
20 | };
|
---|
21 |
|
---|
22 | for (var key in object) {
|
---|
23 | var value = object[key];
|
---|
24 | if (value === null || typeof value == 'undefined') {
|
---|
25 | done();
|
---|
26 | } else if (Buffer.isBuffer(value)) {
|
---|
27 | var part = { buffer: value, content_type: 'application/octet-stream' };
|
---|
28 | generate_part(key, part, boundary, done);
|
---|
29 | } else {
|
---|
30 | var part = (value.buffer || value.file || value.content_type) ? value : { value: value };
|
---|
31 | generate_part(key, part, boundary, done);
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | }
|
---|
36 |
|
---|
37 | function generate_part(name, part, boundary, callback) {
|
---|
38 |
|
---|
39 | var return_part = '--' + boundary + '\r\n';
|
---|
40 | return_part += 'Content-Disposition: form-data; name="' + name + '"';
|
---|
41 |
|
---|
42 | function append(data, filename) {
|
---|
43 |
|
---|
44 | if (data) {
|
---|
45 | var binary = part.content_type.indexOf('text') == -1;
|
---|
46 | return_part += '; filename="' + encodeURIComponent(filename) + '"\r\n';
|
---|
47 | if (binary) return_part += 'Content-Transfer-Encoding: binary\r\n';
|
---|
48 | return_part += 'Content-Type: ' + part.content_type + '\r\n\r\n';
|
---|
49 | return_part += binary ? data.toString('binary') : data.toString('utf8');
|
---|
50 | }
|
---|
51 |
|
---|
52 | callback(null, return_part + '\r\n');
|
---|
53 | };
|
---|
54 |
|
---|
55 | if ((part.file || part.buffer) && part.content_type) {
|
---|
56 |
|
---|
57 | var filename = part.filename ? part.filename : part.file ? basename(part.file) : name;
|
---|
58 | if (part.buffer) return append(part.buffer, filename);
|
---|
59 |
|
---|
60 | readFile(part.file, function(err, data) {
|
---|
61 | if (err) return callback(err);
|
---|
62 | append(data, filename);
|
---|
63 | });
|
---|
64 |
|
---|
65 | } else {
|
---|
66 |
|
---|
67 | if (typeof part.value == 'object')
|
---|
68 | return callback(new Error('Object received for ' + name + ', expected string.'))
|
---|
69 |
|
---|
70 | if (part.content_type) {
|
---|
71 | return_part += '\r\n';
|
---|
72 | return_part += 'Content-Type: ' + part.content_type;
|
---|
73 | }
|
---|
74 |
|
---|
75 | return_part += '\r\n\r\n';
|
---|
76 | return_part += Buffer.from(String(part.value), 'utf8').toString('binary');
|
---|
77 | append();
|
---|
78 |
|
---|
79 | }
|
---|
80 |
|
---|
81 | }
|
---|
82 |
|
---|
83 | // flattens nested objects for multipart body
|
---|
84 | function flatten(object, into, prefix) {
|
---|
85 | into = into || {};
|
---|
86 |
|
---|
87 | for(var key in object) {
|
---|
88 | var prefix_key = prefix ? prefix + '[' + key + ']' : key;
|
---|
89 | var prop = object[key];
|
---|
90 |
|
---|
91 | if (prop && typeof prop === 'object' && !(prop.buffer || prop.file || prop.content_type))
|
---|
92 | flatten(prop, into, prefix_key)
|
---|
93 | else
|
---|
94 | into[prefix_key] = prop;
|
---|
95 | }
|
---|
96 |
|
---|
97 | return into;
|
---|
98 | }
|
---|