Skip to content

Commit 750571c

Browse files
committed
#140: fix
1 parent 96559e7 commit 750571c

6 files changed

+96
-42
lines changed

dist/FileAPI.html5.js

+31-13
Original file line numberDiff line numberDiff line change
@@ -2044,7 +2044,12 @@
20442044
fn(err);
20452045
}
20462046
else {
2047-
this._apply(image, fn);
2047+
try {
2048+
this._apply(image, fn);
2049+
} catch (err){
2050+
api.log('[err] FileAPI.Image.fn._apply:', err);
2051+
fn(err);
2052+
}
20482053
}
20492054
});
20502055
},
@@ -2829,7 +2834,7 @@
28292834
if ( options._chunked ) {
28302835
// chunked upload
28312836
if( xhr.upload ){
2832-
xhr.upload.addEventListener('progress', function (/**Event*/evt){
2837+
xhr.upload.addEventListener('progress', api.throttle(function (/**Event*/evt){
28332838
if (!data.retry) {
28342839
// show progress only for correct chunk uploads
28352840
options.progress({
@@ -2839,7 +2844,7 @@
28392844
, totalSize: data.size
28402845
}, _this, options);
28412846
}
2842-
}, false);
2847+
}, 100), false);
28432848
}
28442849

28452850
xhr.onreadystatechange = function (){
@@ -2874,6 +2879,9 @@
28742879
data.end = lkb;
28752880
} else {
28762881
data.end = data.start - 1;
2882+
if (416 == xhr.status) {
2883+
data.end = data.end - options.chunkSize;
2884+
}
28772885
}
28782886

28792887
setTimeout(function () {
@@ -2905,24 +2913,34 @@
29052913
}, 0);
29062914
}
29072915
}
2916+
29082917
xhr = null;
29092918
}
29102919
};
29112920

29122921
data.start = data.end + 1;
2913-
data.end = Math.max(Math.min(data.start + options.chunkSize, data.size ) - 1, data.start);
2922+
data.end = Math.max(Math.min(data.start + options.chunkSize, data.size) - 1, data.start);
29142923

2915-
var slice;
2916-
(slice = 'slice') in data.file || (slice = 'mozSlice') in data.file || (slice = 'webkitSlice') in data.file;
2924+
// Retrieve a slice of file
2925+
var
2926+
file = data.file
2927+
, slice = (file.slice || file.mozSlice || file.webkitSlice)(data.start, data.end + 1)
2928+
;
2929+
2930+
if( data.size && !slice.size ){
2931+
setTimeout(function (){
2932+
_this.end(-1);
2933+
});
2934+
} else {
2935+
xhr.setRequestHeader("Content-Range", "bytes " + data.start + "-" + data.end + "/" + data.size);
2936+
xhr.setRequestHeader("Content-Disposition", 'attachment; filename=' + encodeURIComponent(data.name));
2937+
xhr.setRequestHeader("Content-Type", data.type || "application/octet-stream");
2938+
2939+
xhr.send(slice);
2940+
}
29172941

2918-
xhr.setRequestHeader("Content-Range", "bytes " + data.start + "-" + data.end + "/" + data.size);
2919-
xhr.setRequestHeader("Content-Disposition", 'attachment; filename=' + encodeURIComponent(data.name));
2920-
xhr.setRequestHeader("Content-Type", data.type || "application/octet-stream");
2921-
2922-
slice = data.file[slice](data.start, data.end + 1);
2923-
29242942
xhr.send(slice);
2925-
slice = null;
2943+
file = slice = null;
29262944
} else {
29272945
// single piece upload
29282946
if( xhr.upload ){

dist/FileAPI.html5.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/FileAPI.js

+31-13
Original file line numberDiff line numberDiff line change
@@ -2044,7 +2044,12 @@
20442044
fn(err);
20452045
}
20462046
else {
2047-
this._apply(image, fn);
2047+
try {
2048+
this._apply(image, fn);
2049+
} catch (err){
2050+
api.log('[err] FileAPI.Image.fn._apply:', err);
2051+
fn(err);
2052+
}
20482053
}
20492054
});
20502055
},
@@ -2829,7 +2834,7 @@
28292834
if ( options._chunked ) {
28302835
// chunked upload
28312836
if( xhr.upload ){
2832-
xhr.upload.addEventListener('progress', function (/**Event*/evt){
2837+
xhr.upload.addEventListener('progress', api.throttle(function (/**Event*/evt){
28332838
if (!data.retry) {
28342839
// show progress only for correct chunk uploads
28352840
options.progress({
@@ -2839,7 +2844,7 @@
28392844
, totalSize: data.size
28402845
}, _this, options);
28412846
}
2842-
}, false);
2847+
}, 100), false);
28432848
}
28442849

28452850
xhr.onreadystatechange = function (){
@@ -2874,6 +2879,9 @@
28742879
data.end = lkb;
28752880
} else {
28762881
data.end = data.start - 1;
2882+
if (416 == xhr.status) {
2883+
data.end = data.end - options.chunkSize;
2884+
}
28772885
}
28782886

28792887
setTimeout(function () {
@@ -2905,24 +2913,34 @@
29052913
}, 0);
29062914
}
29072915
}
2916+
29082917
xhr = null;
29092918
}
29102919
};
29112920

29122921
data.start = data.end + 1;
2913-
data.end = Math.max(Math.min(data.start + options.chunkSize, data.size ) - 1, data.start);
2922+
data.end = Math.max(Math.min(data.start + options.chunkSize, data.size) - 1, data.start);
29142923

2915-
var slice;
2916-
(slice = 'slice') in data.file || (slice = 'mozSlice') in data.file || (slice = 'webkitSlice') in data.file;
2924+
// Retrieve a slice of file
2925+
var
2926+
file = data.file
2927+
, slice = (file.slice || file.mozSlice || file.webkitSlice)(data.start, data.end + 1)
2928+
;
2929+
2930+
if( data.size && !slice.size ){
2931+
setTimeout(function (){
2932+
_this.end(-1);
2933+
});
2934+
} else {
2935+
xhr.setRequestHeader("Content-Range", "bytes " + data.start + "-" + data.end + "/" + data.size);
2936+
xhr.setRequestHeader("Content-Disposition", 'attachment; filename=' + encodeURIComponent(data.name));
2937+
xhr.setRequestHeader("Content-Type", data.type || "application/octet-stream");
2938+
2939+
xhr.send(slice);
2940+
}
29172941

2918-
xhr.setRequestHeader("Content-Range", "bytes " + data.start + "-" + data.end + "/" + data.size);
2919-
xhr.setRequestHeader("Content-Disposition", 'attachment; filename=' + encodeURIComponent(data.name));
2920-
xhr.setRequestHeader("Content-Type", data.type || "application/octet-stream");
2921-
2922-
slice = data.file[slice](data.start, data.end + 1);
2923-
29242942
xhr.send(slice);
2925-
slice = null;
2943+
file = slice = null;
29262944
} else {
29272945
// single piece upload
29282946
if( xhr.upload ){

dist/FileAPI.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/FileAPI.Image.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,12 @@
278278
fn(err);
279279
}
280280
else {
281-
this._apply(image, fn);
281+
try {
282+
this._apply(image, fn);
283+
} catch (err){
284+
api.log('[err] FileAPI.Image.fn._apply:', err);
285+
fn(err);
286+
}
282287
}
283288
});
284289
},

lib/FileAPI.XHR.js

+25-12
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@
205205
if ( options._chunked ) {
206206
// chunked upload
207207
if( xhr.upload ){
208-
xhr.upload.addEventListener('progress', function (/**Event*/evt){
208+
xhr.upload.addEventListener('progress', api.throttle(function (/**Event*/evt){
209209
if (!data.retry) {
210210
// show progress only for correct chunk uploads
211211
options.progress({
@@ -215,7 +215,7 @@
215215
, totalSize: data.size
216216
}, _this, options);
217217
}
218-
}, false);
218+
}, 100), false);
219219
}
220220

221221
xhr.onreadystatechange = function (){
@@ -250,6 +250,9 @@
250250
data.end = lkb;
251251
} else {
252252
data.end = data.start - 1;
253+
if (416 == xhr.status) {
254+
data.end = data.end - options.chunkSize;
255+
}
253256
}
254257

255258
setTimeout(function () {
@@ -281,24 +284,34 @@
281284
}, 0);
282285
}
283286
}
287+
284288
xhr = null;
285289
}
286290
};
287291

288292
data.start = data.end + 1;
289-
data.end = Math.max(Math.min(data.start + options.chunkSize, data.size ) - 1, data.start);
293+
data.end = Math.max(Math.min(data.start + options.chunkSize, data.size) - 1, data.start);
290294

291-
var slice;
292-
(slice = 'slice') in data.file || (slice = 'mozSlice') in data.file || (slice = 'webkitSlice') in data.file;
295+
// Retrieve a slice of file
296+
var
297+
file = data.file
298+
, slice = (file.slice || file.mozSlice || file.webkitSlice)(data.start, data.end + 1)
299+
;
300+
301+
if( data.size && !slice.size ){
302+
setTimeout(function (){
303+
_this.end(-1);
304+
});
305+
} else {
306+
xhr.setRequestHeader("Content-Range", "bytes " + data.start + "-" + data.end + "/" + data.size);
307+
xhr.setRequestHeader("Content-Disposition", 'attachment; filename=' + encodeURIComponent(data.name));
308+
xhr.setRequestHeader("Content-Type", data.type || "application/octet-stream");
309+
310+
xhr.send(slice);
311+
}
293312

294-
xhr.setRequestHeader("Content-Range", "bytes " + data.start + "-" + data.end + "/" + data.size);
295-
xhr.setRequestHeader("Content-Disposition", 'attachment; filename=' + encodeURIComponent(data.name));
296-
xhr.setRequestHeader("Content-Type", data.type || "application/octet-stream");
297-
298-
slice = data.file[slice](data.start, data.end + 1);
299-
300313
xhr.send(slice);
301-
slice = null;
314+
file = slice = null;
302315
} else {
303316
// single piece upload
304317
if( xhr.upload ){

0 commit comments

Comments
 (0)