multipart.js 2.8 KB

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