Skip to content

Commit

Permalink
Merge pull request #56 from srijs/fix-last-chunksize
Browse files Browse the repository at this point in the history
Fix the last chunk size dilemma.
  • Loading branch information
steffentchr committed Mar 18, 2013
2 parents 9d21185 + e6bd832 commit 3624185
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Available configuration options are:

* `target` The target URL for the multipart POST request (Default: `/`)
* `chunkSize` The size in bytes of each uploaded chunk of data (Default: `1*1024*1024`)
* `forceChunkSize` Force all chunks to be less or equal than chunkSize. Otherwise, the last chunk will be greater or equal chunkSize. (Default: `false`)
* `simultaneousUploads` Number of simultaneous uploads (Default: `3`)
* `fileParameterName` The name of the multipart POST parameter to use for the file chunk (Default: `file`)
* `query` Extra parameters to include in the multipart POST with data. This can be an object or a function. If a function, it will be passed a ResumableFile and a ResumableChunk object (Default: `{}`)
Expand Down
8 changes: 5 additions & 3 deletions resumable.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var Resumable = function(opts){
$.files = [];
$.defaults = {
chunkSize:1*1024*1024,
forceChunkSize:false,
simultaneousUploads:3,
fileParameterName:'file',
throttleProgressCallbacks:0.5,
Expand Down Expand Up @@ -218,7 +219,8 @@ var Resumable = function(opts){
// Rebuild stack of chunks from file
$.chunks = [];
$._prevProgress = 0;
for (var offset=0; offset<Math.max(Math.floor($.file.size/$.resumableObj.opts.chunkSize),1); offset++) {
var round = $.resumableObj.opts.forceChunkSize ? Math.ceil : Math.floor;
for (var offset=0; offset<Math.max(round($.file.size/$.resumableObj.opts.chunkSize),1); offset++) {
$.chunks.push(new ResumableChunk($.resumableObj, $, offset, chunkEvent));
}
}
Expand Down Expand Up @@ -257,8 +259,8 @@ var Resumable = function(opts){
// Computed properties
$.loaded = 0;
$.startByte = $.offset*$.resumableObj.opts.chunkSize;
$.endByte = ($.offset+1)*$.resumableObj.opts.chunkSize;
if ($.fileObjSize-$.endByte < $.resumableObj.opts.chunkSize) {
$.endByte = Math.min($.fileObjSize, ($.offset+1)*$.resumableObj.opts.chunkSize);
if ($.fileObjSize-$.endByte < $.resumableObj.opts.chunkSize && !$.resumableObj.opts.forceChunkSize) {
// The last chunk will be bigger than the chunk size, but less than 2*chunkSize
$.endByte = $.fileObjSize;
}
Expand Down

0 comments on commit 3624185

Please sign in to comment.