Skip to content

Commit 88a6073

Browse files
author
Amir Tocker
committed
Use a new timestamp for each chunk in upload_large API
1 parent 11822fa commit 88a6073

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

lib-es5/uploader.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ exports.upload_chunked_stream = function upload_chunked_stream(callback) {
167167
var chunk_start = sent;
168168
sent += buffer.length;
169169
options.content_range = `bytes ${chunk_start}-${sent - 1}/${is_last ? sent : -1}`;
170+
params.timestamp = utils.timestamp();
170171
var finished_part = function finished_part(result) {
171172
if (result.error != null || is_last) {
172173
if (typeof callback === "function") {

lib/uploader.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ exports.upload_chunked_stream = function upload_chunked_stream(callback, options
120120
let chunk_start = sent;
121121
sent += buffer.length;
122122
options.content_range = `bytes ${chunk_start}-${sent - 1}/${(is_last ? sent : -1)}`;
123+
params.timestamp = utils.timestamp();
123124
let finished_part = function (result) {
124125
if ((result.error != null) || is_last) {
125126
if (typeof callback === "function") {

test/uploader_spec.coffee

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Q = require('q')
99
path = require('path');
1010
isFunction = require('lodash/isFunction')
1111
at = require('lodash/at')
12+
uniq = require('lodash/uniq')
1213
ClientRequest = require('_http_client').ClientRequest
1314
require('jsdom-global')()
1415

@@ -33,7 +34,10 @@ describe "uploader", ->
3334
config = cloudinary.config(true)
3435
if(!(config.api_key && config.api_secret))
3536
expect().fail("Missing key and secret. Please set CLOUDINARY_URL.")
36-
cloudinary.v2.api.delete_resources_by_tag(helper.TEST_TAG) unless cloudinary.config().keep_test_products
37+
Q.allSettled [
38+
cloudinary.v2.api.delete_resources_by_tag(helper.TEST_TAG) unless cloudinary.config().keep_test_products
39+
cloudinary.v2.api.delete_resources_by_tag(helper.TEST_TAG, resource_type: "video") unless cloudinary.config().keep_test_products
40+
]
3741

3842
beforeEach ->
3943
cloudinary.config(true)
@@ -387,18 +391,34 @@ describe "uploader", ->
387391
done()
388392
true
389393

390-
it "should support uploading large video files", (done) ->
394+
it "should support uploading large video files", () ->
391395
@timeout helper.TIMEOUT_LONG * 10
392-
fs.stat LARGE_VIDEO, (err, stat) ->
393-
return done(new Error err.message) if err?
394-
cloudinary.v2.uploader.upload_chunked LARGE_VIDEO, {resource_type: 'video', timeout: helper.TIMEOUT_LONG * 10, tags: UPLOAD_TAGS}, (error, result) ->
395-
return done(new Error error.message) if error?
396-
expect(result.bytes).to.eql(stat.size)
397-
expect(result.etag).to.eql("ff6c391d26be0837ee5229885b5bd571")
398-
cloudinary.v2.uploader.destroy result.public_id, ()->
399-
done()
400-
true
401-
true
396+
writeSpy = sinon.spy(ClientRequest.prototype, 'write')
397+
stat = fs.statSync( LARGE_VIDEO)
398+
expect(stat).to.be.ok()
399+
Q.denodeify(cloudinary.v2.uploader.upload_chunked)( LARGE_VIDEO, {chunk_size: 6000000, resource_type: 'video', timeout: helper.TIMEOUT_LONG * 10, tags: UPLOAD_TAGS})
400+
.then (result)->
401+
expect(result.bytes).to.eql(stat.size)
402+
expect(result.etag).to.eql("ff6c391d26be0837ee5229885b5bd571")
403+
timestamps = writeSpy.args.map((a)-> a[0].toString())
404+
.filter((p) -> p.match(/timestamp/))
405+
.map((p) -> p.match(/"timestamp"\s+(\d+)/)[1])
406+
expect(timestamps.length).to.be.greaterThan(1)
407+
expect(uniq(timestamps)).to.eql(timestamps)
408+
.finally ->
409+
writeSpy.restore()
410+
411+
it "should update timestamp for each chuck", ()->
412+
writeSpy = sinon.spy(ClientRequest.prototype, 'write')
413+
Q.denodeify(cloudinary.v2.uploader.upload_chunked)( LARGE_VIDEO, {chunk_size: 6000000, resource_type: 'video', timeout: helper.TIMEOUT_LONG * 10, tags: UPLOAD_TAGS})
414+
.then ->
415+
timestamps = writeSpy.args.map((a)-> a[0].toString())
416+
.filter((p) -> p.match(/timestamp/))
417+
.map((p) -> p.match(/"timestamp"\s+(\d+)/)[1])
418+
expect(timestamps.length).to.be.greaterThan(1)
419+
expect(uniq(timestamps)).to.eql(timestamps)
420+
.finally ->
421+
writeSpy.restore()
402422

403423
it "should support uploading based on a url", (done) ->
404424
@timeout helper.TIMEOUT_MEDIUM

0 commit comments

Comments
 (0)