Skip to content

Commit

Permalink
support checkChunkUploadedByResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
dolymood committed Oct 18, 2017
1 parent 0f9baed commit 6baf0be
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
14 changes: 14 additions & 0 deletions src/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ utils.extend(File.prototype, {
if (this.error) {
return
}
this._updateUploadedChunks(message, chunk)
clearTimeout(this._progeressId)
this._progeressId = 0
var timeDiff = Date.now() - this._lastProgressCallback
Expand All @@ -199,6 +200,19 @@ utils.extend(File.prototype, {
}
},

_updateUploadedChunks: function (message, chunk) {
var checkChunkUploaded = this.uploader.opts.checkChunkUploadedByResponse
if (checkChunkUploaded) {
utils.each(this.chunks, function (_chunk) {
if (checkChunkUploaded.call(this, _chunk, message)) {
_chunk.xhr = chunk.xhr
}
_chunk.tested = true
}, this)
this._firstResponse = true
}
},

_error: function () {
this.error = this.allError = true
var parent = this.parent
Expand Down
22 changes: 17 additions & 5 deletions src/uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ Uploader.defaults = {
successStatuses: [200, 201, 202],
onDropStopPropagation: false,
initFileFn: null,
readFileFn: webAPIFileRead
readFileFn: webAPIFileRead,
checkChunkUploadedByResponse: null
}

Uploader.utils = utils
Expand Down Expand Up @@ -197,16 +198,22 @@ utils.extend(Uploader.prototype, {
uploadNextChunk: function (preventEvents) {
var found = false
var pendingStatus = Chunk.STATUS.PENDING
var checkChunkUploaded = this.uploader.opts.checkChunkUploadedByResponse
if (this.opts.prioritizeFirstAndLastChunk) {
utils.each(this.files, function (file) {
if (!file.paused && file.chunks.length &&
file.chunks[0].status() === pendingStatus) {
if (file.paused) {
return
}
if (checkChunkUploaded && !file._firstResponse && file.isUploading()) {
// waiting for current file's first chunk response
return
}
if (file.chunks.length && file.chunks[0].status() === pendingStatus) {
file.chunks[0].send()
found = true
return false
}
if (!file.paused && file.chunks.length > 1 &&
file.chunks[file.chunks.length - 1].status() === pendingStatus) {
if (file.chunks.length > 1 && file.chunks[file.chunks.length - 1].status() === pendingStatus) {
file.chunks[file.chunks.length - 1].send()
found = true
return false
Expand All @@ -220,6 +227,10 @@ utils.extend(Uploader.prototype, {
// Now, simply look for the next, best thing to upload
utils.each(this.files, function (file) {
if (!file.paused) {
if (checkChunkUploaded && !file._firstResponse && file.isUploading()) {
// waiting for current file's first chunk response
return
}
utils.each(file.chunks, function (chunk) {
if (chunk.status() === pendingStatus) {
chunk.send()
Expand Down Expand Up @@ -287,6 +298,7 @@ utils.extend(Uploader.prototype, {
}
}
})
return should
})
// if should is true then return uploading chunks's length
return should && num
Expand Down

0 comments on commit 6baf0be

Please sign in to comment.