Skip to content
This repository has been archived by the owner on Apr 5, 2019. It is now read-only.

Commit

Permalink
Merge branch 'fix_update_file_upload_format_to_support_v1_api' into '…
Browse files Browse the repository at this point in the history
…master'


DEV-802 Kloudless-node: update file.upload request format to API v1

See merge request !7
  • Loading branch information
hong19 committed Nov 24, 2017
2 parents dcb67a5 + 447caae commit 9cc1514
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 22 deletions.
35 changes: 28 additions & 7 deletions lib/methods/files.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var BaseMethod = require('./basemethod');
var fs = require('fs');
var Error = require('../error');
var utils = require('../utils');

var FilesMethods = {
upload: BaseMethod.extend({
Expand All @@ -13,15 +14,35 @@ var FilesMethods = {
'file'
],
dataFunction: function(data, cb) {
if (data['file'] instanceof fs.ReadStream || data['file'] instanceof Buffer) {
if (data['file']) {
data['metadata'] = JSON.stringify({'parent_id': data['parent_id'], 'name': data['name']});
cb(null, data);
} else {
cb(new Error.KloudlessError(
{message: "The 'file' parameter for files.upload()"+
" must be an instance of either Buffer or fs.ReadStream."}
));

if (data['file'] instanceof Buffer) {
cb(null, data);
} else if (data['file'] instanceof fs.ReadStream) {
//convert fs.ReadStream to Buffer
utils.streamToBuffer(data['file'])
.then(function (fileBuffer) {
data['file'] = fileBuffer;
cb(null, data);
})
.catch(function () {
cb(new Error.KloudlessError(
{
message: "Failed to convert ReadStream to Buffer."
}
));
});
} else {
cb(new Error.KloudlessError(
{
message: "The 'file' parameter for files.upload()" +
" must be an instance of either Buffer."
}
));
}
}

}
}),
get: BaseMethod.extend({
Expand Down
28 changes: 13 additions & 15 deletions lib/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,18 @@ BaseResource.prototype = {
user_headers = user_headers || {};
var headers;
var requestData = JSON.stringify(data || {});
var requestForm = new FormData();
var shouldPipeForm = false;

if (data['file'] && (data['file'] instanceof Buffer)) {
shouldPipeForm = true;
for (var dataName in data) {
var appendData = data[dataName];
if(dataName != "queryParams") {
if(dataName == "file" && data['name']) requestForm.append(dataName, appendData, {filename: data['name']});
else requestForm.append(dataName, appendData);
}
var shouldPipeFile = false;

if (data['file'] && (data['file'] instanceof Buffer || data['file'] instanceof fs.ReadStream)) {
shouldPipeFile = true;

headers = {
'Authorization': this._kloudless.getApiField('auth'),
'Accept': 'application/json',
'Content-Type': "application/octet-stream",
'X-Kloudless-Metadata': data['metadata']
// X-Kloudless-Metadata is json like '{"name": "hello.png", "parent_id": "fL2hp"}'
}
headers = requestForm.getHeaders();
headers['Authorization'] = this._kloudless.getApiField('auth');
} else {
headers = {
'Authorization': this._kloudless.getApiField('auth'),
Expand Down Expand Up @@ -166,8 +164,8 @@ BaseResource.prototype = {
req.on('response', self._responseHandler(req, parseResponse, callback));
req.on('error', self._errorHandler(req, callback));

if (shouldPipeForm) {
requestForm.pipe(req);
if (shouldPipeFile) {
req.write(data['file']);
} else {
req.write(requestData, 'utf8');
}
Expand Down
18 changes: 18 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,23 @@ var utils = module.exports = {
}
}
return Constructor;
},
/**
* convert fs.ReadStream to Buffer
*
* @param {fs.ReadStream} stream
* @returns {Promise}
*/
streamToBuffer: function (stream) {
return new Promise(function (resolve, reject) {
var buffers = [];
stream.on('error', reject);
stream.on('data', function (data) {
return buffers.push(data);
});
stream.on('end', function () {
return resolve(Buffer.concat(buffers));
});
})
}
};

0 comments on commit 9cc1514

Please sign in to comment.