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_file_donwload_stream_encoding' into 'master'
Browse files Browse the repository at this point in the history
Fix file download stream encoding

See merge request !8
  • Loading branch information
hong19 committed Dec 15, 2017
2 parents c018923 + a23a60a commit bd84e5f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
28 changes: 23 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ async.series([
});
},

function(cb){
function (cb){
// and now we're going to download that file we just uploaded
// method 1 : use Buffer to concatenate each chunks
kloudless.files.contents({
"account_id": accountId,
"file_id": fileId
Expand All @@ -82,17 +83,34 @@ async.series([
}
var filecontents = '';
console.log("got the filestream:");
filestream.on('data', function(chunk) {
filestream.on('data', function (chunk) {
console.log("reading in data chunk...");
console.log(chunk);
filecontents += chunk;
filecontents = Buffer.concat([filecontents, chunk]);
});
filestream.on('end',function() {
filestream.on('end', function () {
console.log("finished reading file!");
console.log(filecontents);
fs.writeFile("download.jpg", filecontents, function (err) {
console.log('write file error:' + err);
});
cb();
});
});
},
function (cb) {
// and now we're going to download that file we just uploaded
// method 2 : pipe the filestream directly
kloudless.files.contents({
"account_id": accountId,
"file_id": fileId
}, function (err, filestream) {
if (err) {
return console.log("Files contents: " + err);
}
console.log("got the filestream:");
filestream.pipe(fs.createWriteStream('download_2.jpg'));
cb();
});
}
]);
```
Expand Down
1 change: 0 additions & 1 deletion lib/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ BaseResource.prototype = {
charEncoding = charEncoding || 'utf-8';

if (!parse && statusCodeType == '2') {
res.setEncoding('binary');
return callback(null, res, res);
} else {
res.setEncoding(charEncoding);
Expand Down
24 changes: 8 additions & 16 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
var kloudless = require('../lib/kloudless')(process.env.API_KEY || 'your-api-key-here')
, async = require('async')
, fs = require('fs')
, path = require('path');
, path = require('path')
, assert = require('assert');

if (process.env.API_HOST)
kloudless.setHost(process.env.API_HOST, process.env.API_PORT || 443);
Expand Down Expand Up @@ -154,22 +155,13 @@ async.waterfall([
if (err) {
return cb('Files contents: ' + err);
}
var filecontents = '';

console.log('got the filestream:');
filestream.on('data', function(chunk){
console.log('reading in data chunk...');
console.log(chunk);
filecontents += chunk;
});
filestream.on('end',function(){
console.log('finished reading file!');
if (filecontents === fileBuffer.toString()) {
console.log('files contents test pass');
return cb(null);
}
else {
return cb("File contents fail test: " + filecontents)
}
filestream.on('readable', function () {
var buffer = filestream.read();
assert.deepStrictEqual(buffer, fileBuffer);
console.log('files contents test pass');
return cb(null);
});
});
},
Expand Down

0 comments on commit bd84e5f

Please sign in to comment.