Commit cd0b61fe authored by Henning Leutz's avatar Henning Leutz 🥋

refactor: uploaded uses fetch() instead of XMLHttpRequest

parent 39a66958
......@@ -94,8 +94,6 @@ define('controls/upload/File', [
return;
}
var self = this;
this.$is_paused = false;
this.$file_size = this.$File.size;
this.$chunk_size = (1024 * 256); // 256kb
......@@ -105,10 +103,10 @@ define('controls/upload/File', [
this.$execute = true; // false if no execute of the update routine
this.$result = null;
this.$error = false;
this.$errors = 0;
this.$ContextMenu = null;
this.$slice_method = 'slice';
this.$__updateRanges = null;
this.$ContextMenu = null;
this.$slice_method = 'slice';
if ('mozSlice' in this.$File) {
this.$slice_method = 'mozSlice';
......@@ -118,46 +116,6 @@ define('controls/upload/File', [
}
}
this.$Request = new XMLHttpRequest();
this.$Request.onload = function () {
if (self.$__updateRanges) {
self.$__updateRanges();
self.$__updateRanges = null;
}
self.upload();
};
this.$errors = 0;
// on error -> ProgressEvent
this.$Request.onerror = function () {
this.pause();
// test in view seconds again
if (this.$errors === 0) {
setTimeout(function () {
this.resume();
}.bind(this), 4000);
this.$errors++;
return;
}
this.$errors++;
QUI.getMessage().then(function (MH) {
MH.addError(QUILocale.get('quiqqer/quiqqer', 'exception.upload.error'));
});
}.bind(this);
// check server answer
this.$Request.onreadystatechange = function () {
if (self.$Request.readyState === 4) {
self.$parseResult(self.$Request.responseText);
}
};
this.parent(options);
// if something has already been uploaded
......@@ -182,7 +140,7 @@ define('controls/upload/File', [
* Create the DOMNode
*
* @method controls/upload/File#create
* @return {HTMLElement}
* @return {Element}
*/
create: function () {
var self = this;
......@@ -260,7 +218,7 @@ define('controls/upload/File', [
this.$Cancel = new QUIButton({
name : 'cancel-upload',
text : 'abbrechen',
text : QUILocale.get(lg, 'cancel'),
Control: this,
events : {
onClick: function () {
......@@ -278,10 +236,6 @@ define('controls/upload/File', [
events : {
onSubmit: function () {
self.cancel();
},
onCancel: function () {
//Win.getAttribute('Control').resume();
}
}
}).open();
......@@ -414,16 +368,6 @@ define('controls/upload/File', [
return;
}
//if (this.$File.type === '' || !this.$File.type) {
// QUI.getMessageHandler(function (MessageHandler) {
// MessageHandler.addError(
// Locale.get(lg, 'file.upload.unknown.filetype')
// );
// });
//
// return;
//}
if (this.$is_paused) {
return;
}
......@@ -495,7 +439,7 @@ define('controls/upload/File', [
* @method controls/upload/File#resume
*/
resume: function () {
if (this.$File instanceof File === false) {
if (!(this.$File instanceof File)) {
var Upload = this.getElm().getElement('input[type="file"]');
if (Upload) {
......@@ -574,56 +518,41 @@ define('controls/upload/File', [
}
// the file part
var data = this.$File[this.$slice_method](
var data = this.$File[this.$slice_method](
this.$range_start,
this.$range_end
),
// extra params for ajax function
params = ObjectUtils.combine((this.getAttribute('params') || {}), {
file : JSON.encode({
uploadstart: this.$upload_time,
chunksize : this.$chunk_size,
chunkstart : this.$range_start
}),
onfinish: this.getAttribute('phpfunc'),
onstart : this.getAttribute('phponstart'),
filesize: this.$file_size,
filename: this.getFilename(),
filetype: this.$File.type
});
);
// extra params for ajax function
var params = ObjectUtils.combine((this.getAttribute('params') || {}), {
file : JSON.encode({
uploadstart: this.$upload_time,
chunksize : this.$chunk_size,
chunkstart : this.$range_start
}),
onfinish: this.getAttribute('phpfunc'),
onstart : this.getAttribute('phponstart'),
filesize: this.$file_size,
filename: this.getFilename(),
filetype: this.$File.type
});
if (typeof params.lang === 'undefined') {
params.lang = QUILocale.getCurrent();
}
// $project, $parentid, $file, $data
var url = URL_LIB_DIR + 'QUI/Upload/bin/upload.php?';
url = url + Object.toQueryString(params);
this.$Request.open('PUT', url, true);
if (!!this.$Request.overrideMimeType) {
this.$Request.overrideMimeType('application/octet-stream');
}
if (this.$range_start !== 0) {
this.$Request.setRequestHeader(
'Content-Range',
'bytes ' + this.$range_start + '-' + this.$range_end + '/' + this.$file_size
);
}
try {
this.$Request.send(data);
} catch (e) {
// catch errors via onerror
}
// workaround because send is no promise :(
// @todo change / refactor to fetch()
this.$__updateRanges = function () {
// Update our ranges
var url = URL_LIB_DIR + 'QUI/Upload/bin/upload.php?' + Object.toQueryString(params);
fetch(url, {
method : 'PUT',
cache : 'no-cache',
headers: {
'Content-Type' : 'application/octet-stream',
'Content-Range': 'bytes ' + this.$range_start + '-' + this.$range_end + '/' + this.$file_size
},
body : data
}).then(function (response) {
this.$range_start = this.$range_end;
this.$range_end = this.$range_start + this.$chunk_size;
......@@ -635,9 +564,32 @@ define('controls/upload/File', [
this.$range_start = this.$file_size;
}
// set status
response.text().then(function (text) {
this.$parseResult(text);
}.bind(this));
this.refresh();
};
this.upload();
}.bind(this)).catch(function (err) {
console.error('Information about the upload error: ', err);
this.pause();
// test in view seconds again
if (this.$errors === 0) {
setTimeout(function () {
this.resume();
}.bind(this), 4000);
this.$errors++;
return;
}
this.$errors++;
QUI.getMessage().then(function (MH) {
MH.addError(QUILocale.get('quiqqer/quiqqer', 'exception.upload.error'));
});
}.bind(this));
},
/**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment