Skip to content

Commit 0cbfdf7

Browse files
fix: upload_chunked_stream works properly with more than 2 chunks (#673)
* fix: upload_chunked_stream works properly with more than 2 chunks
1 parent a475d9f commit 0cbfdf7

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

lib/uploader.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,10 @@ class Chunkable extends Writable {
102102
this.emit('ready', this.buffer, false, (active) => {
103103
this.active = active;
104104
if (this.active) {
105-
this.buffer = data.slice(grab);
106-
done();
105+
// Start processing the remaining data
106+
const remaining = data.slice(grab);
107+
this.buffer = Buffer.alloc(0); // Reset the buffer
108+
this._write(remaining, encoding, done); // Process the remaining data
107109
}
108110
});
109111
}

test/.resources/big-image.jpg

15.9 MB
Loading

test/integration/api/uploader/uploader_spec.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const cloneDeep = require('lodash/cloneDeep');
1414
const assert = require('assert');
1515

1616
const IMAGE_FILE = helper.IMAGE_FILE;
17+
const LARGE_IMAGE_FILE = helper.LARGE_IMAGE_FILE;
1718
const LARGE_RAW_FILE = helper.LARGE_RAW_FILE;
1819
const LARGE_VIDEO = helper.LARGE_VIDEO;
1920
const EMPTY_IMAGE = helper.EMPTY_IMAGE;
@@ -1002,9 +1003,8 @@ describe("uploader", function () {
10021003
});
10031004
});
10041005
it("should successfully upload with pipes", function (done) {
1005-
var file_reader, upload;
10061006
this.timeout(TIMEOUT.LONG);
1007-
upload = cloudinary.v2.uploader.upload_stream({
1007+
const upload = cloudinary.v2.uploader.upload_stream({
10081008
tags: UPLOAD_TAGS
10091009
}, function (error, result) {
10101010
var expected_signature;
@@ -1017,8 +1017,19 @@ describe("uploader", function () {
10171017
expect(result.signature).to.eql(expected_signature);
10181018
done();
10191019
});
1020-
file_reader = fs.createReadStream(IMAGE_FILE);
1021-
file_reader.pipe(upload);
1020+
fs.createReadStream(IMAGE_FILE).pipe(upload);
1021+
});
1022+
it("should successfully upload in chunks with pipes", (done) => {
1023+
this.timeout(TIMEOUT.LONG);
1024+
const upload = cloudinary.v2.uploader.upload_chunked_stream({
1025+
chunk_size: 7000000,
1026+
timeout: TIMEOUT.LONG
1027+
}, (error, result) => {
1028+
assert.strictEqual(error, undefined);
1029+
assert.ok(result.public_id);
1030+
done();
1031+
});
1032+
fs.createReadStream(LARGE_IMAGE_FILE).pipe(upload);
10221033
});
10231034
it("should fail with http.Agent (non secure)", function () {
10241035
this.timeout(TIMEOUT.LONG);

test/spechelper.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ exports.libPath = libPath;
1919
const api_http = String(config().upload_prefix).startsWith('http:') ? http : https;
2020

2121
exports.IMAGE_FILE = "test/.resources/logo.png";
22+
exports.LARGE_IMAGE_FILE = "test/.resources/big-image.jpg";
2223
exports.LARGE_RAW_FILE = "test/.resources/TheCompleteWorksOfShakespeare.mobi";
2324
exports.LARGE_VIDEO = "test/.resources/CloudBookStudy-HD.mp4";
2425
exports.EMPTY_IMAGE = "test/.resources/empty.gif";

0 commit comments

Comments
 (0)