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

Commit bd84e5f

Browse files
committed
Merge branch 'fix_file_donwload_stream_encoding' into 'master'
Fix file download stream encoding See merge request !8
2 parents c018923 + a23a60a commit bd84e5f

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

README.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ async.series([
7171
});
7272
},
7373

74-
function(cb){
74+
function (cb){
7575
// and now we're going to download that file we just uploaded
76+
// method 1 : use Buffer to concatenate each chunks
7677
kloudless.files.contents({
7778
"account_id": accountId,
7879
"file_id": fileId
@@ -82,17 +83,34 @@ async.series([
8283
}
8384
var filecontents = '';
8485
console.log("got the filestream:");
85-
filestream.on('data', function(chunk) {
86+
filestream.on('data', function (chunk) {
8687
console.log("reading in data chunk...");
8788
console.log(chunk);
88-
filecontents += chunk;
89+
filecontents = Buffer.concat([filecontents, chunk]);
8990
});
90-
filestream.on('end',function() {
91+
filestream.on('end', function () {
9192
console.log("finished reading file!");
92-
console.log(filecontents);
93+
fs.writeFile("download.jpg", filecontents, function (err) {
94+
console.log('write file error:' + err);
95+
});
9396
cb();
9497
});
9598
});
99+
},
100+
function (cb) {
101+
// and now we're going to download that file we just uploaded
102+
// method 2 : pipe the filestream directly
103+
kloudless.files.contents({
104+
"account_id": accountId,
105+
"file_id": fileId
106+
}, function (err, filestream) {
107+
if (err) {
108+
return console.log("Files contents: " + err);
109+
}
110+
console.log("got the filestream:");
111+
filestream.pipe(fs.createWriteStream('download_2.jpg'));
112+
cb();
113+
});
96114
}
97115
]);
98116
```

lib/resources.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ BaseResource.prototype = {
4141
charEncoding = charEncoding || 'utf-8';
4242

4343
if (!parse && statusCodeType == '2') {
44-
res.setEncoding('binary');
4544
return callback(null, res, res);
4645
} else {
4746
res.setEncoding(charEncoding);

test/index.js

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
var kloudless = require('../lib/kloudless')(process.env.API_KEY || 'your-api-key-here')
44
, async = require('async')
55
, fs = require('fs')
6-
, path = require('path');
6+
, path = require('path')
7+
, assert = require('assert');
78

89
if (process.env.API_HOST)
910
kloudless.setHost(process.env.API_HOST, process.env.API_PORT || 443);
@@ -154,22 +155,13 @@ async.waterfall([
154155
if (err) {
155156
return cb('Files contents: ' + err);
156157
}
157-
var filecontents = '';
158+
158159
console.log('got the filestream:');
159-
filestream.on('data', function(chunk){
160-
console.log('reading in data chunk...');
161-
console.log(chunk);
162-
filecontents += chunk;
163-
});
164-
filestream.on('end',function(){
165-
console.log('finished reading file!');
166-
if (filecontents === fileBuffer.toString()) {
167-
console.log('files contents test pass');
168-
return cb(null);
169-
}
170-
else {
171-
return cb("File contents fail test: " + filecontents)
172-
}
160+
filestream.on('readable', function () {
161+
var buffer = filestream.read();
162+
assert.deepStrictEqual(buffer, fileBuffer);
163+
console.log('files contents test pass');
164+
return cb(null);
173165
});
174166
});
175167
},

0 commit comments

Comments
 (0)