Skip to content

Commit eb2ed8e

Browse files
authored
Merge pull request #277 from aayusharyan/patch-3
Add new functionality to calculate chunk size dynamically
2 parents 6aca5b2 + 92f0ee5 commit eb2ed8e

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Available configuration options are:
102102
* `target` The target URL for the multipart POST request. This can be a string or a function. If a
103103
function, it will be passed a FlowFile, a FlowChunk and isTest boolean (Default: `/`)
104104
* `singleFile` Enable single file upload. Once one file is uploaded, second file will overtake existing one, first one will be canceled. (Default: false)
105-
* `chunkSize` The size in bytes of each uploaded chunk of data. The last uploaded chunk will be at least this size and up to two the size, see [Issue #51](https://github.com/23/resumable.js/issues/51) for details and reasons. (Default: `1*1024*1024`)
105+
* `chunkSize` The size in bytes of each uploaded chunk of data. This can be a number or a function. If a function, it will be passed a FlowFile. The last uploaded chunk will be at least this size and up to two the size, see [Issue #51](https://github.com/23/resumable.js/issues/51) for details and reasons. (Default: `1*1024*1024`, 1MB)
106106
* `forceChunkSize` Force all chunks to be less or equal than chunkSize. Otherwise, the last chunk will be greater than or equal to `chunkSize`. (Default: `false`)
107107
* `simultaneousUploads` Number of simultaneous uploads (Default: `3`)
108108
* `fileParameterName` The name of the multipart POST parameter to use for the file chunk (Default: `file`)

src/flow.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Flow.js is a library providing multiple simultaneous, stable and
1313
* resumable uploads via the HTML5 File API.
1414
* @param [opts]
15-
* @param {number} [opts.chunkSize]
15+
* @param {number|Function} [opts.chunkSize]
1616
* @param {bool} [opts.forceChunkSize]
1717
* @param {number} [opts.simultaneousUploads]
1818
* @param {bool} [opts.singleFile]
@@ -750,6 +750,12 @@
750750
* @type {string}
751751
*/
752752
this.uniqueIdentifier = (uniqueIdentifier === undefined ? flowObj.generateUniqueIdentifier(file) : uniqueIdentifier);
753+
754+
/**
755+
* Size of Each Chunk
756+
* @type {number}
757+
*/
758+
this.chunkSize = 0;
753759

754760
/**
755761
* List of chunks
@@ -938,8 +944,9 @@
938944
// Rebuild stack of chunks from file
939945
this._prevProgress = 0;
940946
var round = this.flowObj.opts.forceChunkSize ? Math.ceil : Math.floor;
947+
this.chunkSize = evalOpts(this.flowObj.opts.chunkSize, this);
941948
var chunks = Math.max(
942-
round(this.size / this.flowObj.opts.chunkSize), 1
949+
round(this.size / this.chunkSize), 1
943950
);
944951
for (var offset = 0; offset < chunks; offset++) {
945952
this.chunks.push(
@@ -1153,7 +1160,7 @@
11531160
* Size of a chunk
11541161
* @type {number}
11551162
*/
1156-
this.chunkSize = this.flowObj.opts.chunkSize;
1163+
this.chunkSize = this.fileObj.chunkSize;
11571164

11581165
/**
11591166
* Chunk start byte in a file
@@ -1267,7 +1274,7 @@
12671274
getParams: function () {
12681275
return {
12691276
flowChunkNumber: this.offset + 1,
1270-
flowChunkSize: this.flowObj.opts.chunkSize,
1277+
flowChunkSize: this.chunkSize,
12711278
flowCurrentChunkSize: this.endByte - this.startByte,
12721279
flowTotalSize: this.fileObj.size,
12731280
flowIdentifier: this.fileObj.uniqueIdentifier,

0 commit comments

Comments
 (0)