Skip to content

Commit 4d272c2

Browse files
committed
Add RESTBuilder.file().
1 parent ae1a096 commit 4d272c2

2 files changed

Lines changed: 91 additions & 14 deletions

File tree

builders.js

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3544,6 +3544,7 @@ function async_wait(arr, onItem, onCallback, index) {
35443544
}
35453545

35463546
function RESTBuilder(url) {
3547+
35473548
this.$url = url;
35483549
this.$headers = { 'User-Agent': 'Total.js/v' + F.version_header, Accept: 'application/json, text/plain, text/plain, text/xml' };
35493550
this.$method = 'get';
@@ -3552,6 +3553,7 @@ function RESTBuilder(url) {
35523553
this.$schema;
35533554
this.$length = 0;
35543555
this.$transform = transforms['restbuilder_default'];
3556+
this.$files = null;
35553557

35563558
// this.$flags;
35573559
// this.$data = {};
@@ -3597,6 +3599,15 @@ RESTBuilder.prototype.url = function(url) {
35973599
return this;
35983600
};
35993601

3602+
RESTBuilder.prototype.file = function(name, filename, buffer) {
3603+
var obj = { name: name, filename: filename, buffer: buffer };
3604+
if (this.$files)
3605+
this.$files.push(obj);
3606+
else
3607+
this.$files = [obj];
3608+
return this;
3609+
};
3610+
36003611
RESTBuilder.prototype.maketransform = function(obj, data) {
36013612
if (this.$transform) {
36023613
var fn = transforms['restbuilder'][this.$transform];
@@ -3820,12 +3831,26 @@ RESTBuilder.prototype.stream = function(callback) {
38203831
return U.download(self.$url, flags, self.$data, callback, self.$cookies, self.$headers, undefined, self.$timeout);
38213832
};
38223833

3834+
RESTBuilder.prototype.file = function(name, filename) {
3835+
var self = this;
3836+
var obj = { name: name, filename: filename };
3837+
if (self.$files)
3838+
self.$files.push(obj);
3839+
else
3840+
self.$files = [obj];
3841+
return self;
3842+
};
3843+
38233844
RESTBuilder.prototype.exec = function(callback) {
38243845

38253846
if (!callback)
38263847
callback = NOOP;
38273848

38283849
var self = this;
3850+
3851+
if (self.$files && self.$method === 'get')
3852+
self.$method = 'post';
3853+
38293854
var flags = self.$flags ? self.$flags : [self.$method];
38303855
var key;
38313856

@@ -3835,14 +3860,19 @@ RESTBuilder.prototype.exec = function(callback) {
38353860
self.$length && flags.push('<' + self.$length);
38363861
self.$redirect === false && flags.push('noredirect');
38373862

3838-
switch (self.$type) {
3839-
case 1:
3840-
flags.push('json');
3841-
break;
3842-
case 3:
3843-
flags.push('xml');
3844-
break;
3863+
if (self.$files) {
3864+
flags.push('upload');
3865+
} else {
3866+
switch (self.$type) {
3867+
case 1:
3868+
flags.push('json');
3869+
break;
3870+
case 3:
3871+
flags.push('xml');
3872+
break;
3873+
}
38453874
}
3875+
38463876
self.$flags = flags;
38473877
}
38483878

@@ -3892,7 +3922,7 @@ RESTBuilder.prototype.exec = function(callback) {
38923922
callback(err, self.maketransform(output.value, output), output);
38933923
output.cache = true;
38943924

3895-
}, self.$cookies, self.$headers, undefined, self.$timeout);
3925+
}, self.$cookies, self.$headers, undefined, self.$timeout, self.$files);
38963926
};
38973927

38983928
function exec_removelisteners(evt) {
@@ -3996,6 +4026,7 @@ exports.UrlBuilder = UrlBuilder;
39964026
exports.TransformBuilder = TransformBuilder;
39974027
exports.SchemaOptions = SchemaOptions;
39984028
global.RESTBuilder = RESTBuilder;
4029+
global.RESTBuilderResponse = RESTBuilderResponse;
39994030
global.ErrorBuilder = ErrorBuilder;
40004031
global.TransformBuilder = TransformBuilder;
40014032
global.Pagination = Pagination;

utils.js

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ const ALPHA_INDEX = { '&lt': '<', '&gt': '>', '&quot': '"', '&apos': '\'', '&amp
7272
const EMPTYARRAY = [];
7373
const EMPTYOBJECT = [];
7474
const NODEVERSION = parseFloat(process.version.toString().replace('v', '').replace(/\./g, ''));
75+
const STREAMPIPE = { end: false };
7576

7677
Object.freeze(EMPTYARRAY);
7778
Object.freeze(EMPTYOBJECT);
@@ -411,7 +412,7 @@ exports.keywords = function(content, forSearch, alternative, max_count, max_leng
411412
* @param {Number} timeout Request timeout.
412413
* return {Boolean}
413414
*/
414-
exports.request = function(url, flags, data, callback, cookies, headers, encoding, timeout) {
415+
exports.request = function(url, flags, data, callback, cookies, headers, encoding, timeout, files) {
415416

416417
// No data (data is optional argument)
417418
if (typeof(data) === 'function') {
@@ -492,7 +493,11 @@ exports.request = function(url, flags, data, callback, cookies, headers, encodin
492493
break;
493494

494495
case 'upload':
495-
headers['Content-Type'] = 'multipart/form-data';
496+
type = 4;
497+
options.upload = true;
498+
options.files = files || EMPTYARRAY;
499+
options.boundary = '----totaljs' + Math.random().toString(16).substring(2);
500+
headers['Content-Type'] = 'multipart/form-data; boundary=' + options.boundary;
496501
break;
497502

498503
case 'post':
@@ -514,7 +519,7 @@ exports.request = function(url, flags, data, callback, cookies, headers, encodin
514519
else
515520
method = 'GET';
516521

517-
if (type !== 3) {
522+
if (type < 3) {
518523
if (typeof(data) !== 'string')
519524
data = type === 1 ? JSON.stringify(data) : Qs.stringify(data);
520525
else if (data[0] === '?')
@@ -526,10 +531,11 @@ exports.request = function(url, flags, data, callback, cookies, headers, encodin
526531
}
527532
}
528533

529-
if (data) {
534+
if (data && type !== 4) {
530535
options.data = data instanceof Buffer ? data : exports.createBuffer(data, ENCODING);
531536
headers['Content-Length'] = options.data.length;
532-
}
537+
} else
538+
options.data = data;
533539

534540
if (cookies) {
535541
var builder = '';
@@ -584,9 +590,49 @@ function request_call(uri, options) {
584590
});
585591

586592
req.on('response', (response) => response.req = req);
587-
req.end(options.data);
593+
594+
if (options.upload) {
595+
options.first = true;
596+
options.files.wait(function(file, next) {
597+
// next();
598+
request_writefile(req, options, file, next);
599+
}, function() {
600+
601+
var keys = Object.keys(options.data);
602+
for (var i = 0, length = keys.length; i < length; i++) {
603+
var value = options.data[keys[i]];
604+
if (value != null) {
605+
req.write((options.first ? '' : NEWLINE) + '--' + options.boundary + NEWLINE + 'Content-Disposition: form-data; name="' + keys[i] + '"' + NEWLINE + NEWLINE + encodeURIComponent(value.toString()));
606+
if (options.first)
607+
options.first = false;
608+
}
609+
}
610+
611+
req.end(NEWLINE + '--' + options.boundary + '--');
612+
});
613+
} else
614+
req.end(options.data);
588615
}
589616

617+
function request_writefile(req, options, file, next) {
618+
619+
req.write((options.first ? '' : NEWLINE) + '--' + options.boundary + NEWLINE + 'Content-Disposition: form-data; name="' + file.name + '"; filename="' + exports.getName(file.filename) + '"' + NEWLINE + 'Content-Type: ' + exports.getContentType(exports.getExtension(file.filename)) + NEWLINE + NEWLINE);
620+
621+
if (options.first)
622+
options.first = false;
623+
624+
// Is Buffer
625+
if (file.buffer) {
626+
req.write(file.buffer);
627+
next();
628+
} else {
629+
var stream = Fs.createReadStream(file.filename);
630+
stream.once('close', next);
631+
stream.pipe(req, STREAMPIPE);
632+
}
633+
}
634+
635+
590636
function request_response(res, uri, options) {
591637

592638
res._buffer = null;

0 commit comments

Comments
 (0)