r99013 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r99012‎ | r99013 | r99014 >
Date:17:25, 5 October 2011
Author:j
Status:ok (Comments)
Tags:config 
Comment:
refine FormData upload from r94627 based on review
- use $j.parseJSON and set response.error if it fails
- move function to detect slice api support into mw.fileApi
- rename wait to chunsRemaining
Modified paths:
  • /trunk/extensions/UploadWizard/resources/mw.FormDataTransport.js (modified) (history)
  • /trunk/extensions/UploadWizard/resources/mw.UploadWizardUpload.js (modified) (history)
  • /trunk/extensions/UploadWizard/resources/mw.fileApi.js (modified) (history)

Diff [purge]

Index: trunk/extensions/UploadWizard/resources/mw.fileApi.js
@@ -24,6 +24,15 @@
2525 var known = [ 'image/png', 'image/gif', 'image/jpeg', 'image/svg+xml'],
2626 tooHuge = 10 * 1024 * 1024;
2727 return ( $.inArray( file.type, known ) !== -1 ) && file.size > 0 && file.size < tooHuge;
 28+ },
 29+
 30+ /**
 31+ * Is the slice function of FileAPI available with sufficient functionality?
 32+ * @todo is there a way to check this instead of hardcoding browsers and version?
 33+ */
 34+ isSliceAvailable: function() {
 35+ return ($j.browser.mozilla && $j.browser.version >= '5.0') ||
 36+ ($j.browser.webkit && $j.browser.version >= '534.28');
2837 }
2938
3039
Index: trunk/extensions/UploadWizard/resources/mw.FormDataTransport.js
@@ -28,35 +28,18 @@
2929 mw.FormDataTransport.prototype = {
3030 upload: function() {
3131 var _this = this,
32 - file = this.uploadObject.file;
33 - var bytesAvailable = file.size;
34 -
 32+ file = this.uploadObject.file,
 33+ bytesAvailable = file.size;
 34+
3535 if(file.size > this.chunkSize) {
3636 this.uploadChunk(0);
3737 } else {
3838 this.xhr = new XMLHttpRequest();
3939 this.xhr.addEventListener("load", function (evt) {
40 - var response;
41 - try {
42 - response = JSON.parse(evt.target.responseText);
43 - } catch(e) {
44 - response = {
45 - responseText: evt.target.responseText
46 - };
47 - }
48 - //upload finished and can be unstashed later
49 - _this.transportedCb(response);
 40+ _this.parseResponse(evt, _this.transportedCb);
5041 }, false);
5142 this.xhr.addEventListener("error", function (evt) {
52 - var response;
53 - try {
54 - response = JSON.parse(evt.target.responseText);
55 - } catch(e) {
56 - response = {
57 - responseText: evt.target.responseText
58 - };
59 - }
60 - _this.transportedCb(response);
 43+ _this.parseResponse(evt, _this.transportedCb);
6144 }, false);
6245 this.xhr.upload.addEventListener("progress", function (evt) {
6346 if (evt.lengthComputable) {
@@ -65,15 +48,7 @@
6649 }
6750 }, false);
6851 this.xhr.addEventListener("abort", function (evt) {
69 - var response;
70 - try {
71 - response = JSON.parse(evt.target.responseText);
72 - } catch(e) {
73 - response = {
74 - responseText: evt.target.responseText
75 - };
76 - }
77 - _this.transportedCb(response);
 52+ _this.parseResponse(evt, _this.transportedCb);
7853 }, false);
7954
8055 var formData = new FormData();
@@ -106,53 +81,39 @@
10782
10883 this.xhr = new XMLHttpRequest();
10984 this.xhr.addEventListener("load", function (evt) {
110 - var response;
11185 _this.responseText = evt.target.responseText;
112 - try {
113 - response = JSON.parse(evt.target.responseText);
114 - } catch(e) {
115 - response = {
116 - responseText: evt.target.responseText
117 - };
118 - }
119 - if(response.upload && response.upload.filekey) {
120 - _this.filekey = response.upload.filekey;
121 - }
122 - if (response.upload && response.upload.result == 'Success') {
123 - //upload finished and can be unstashed later
124 - _this.transportedCb(response);
125 - }
126 - else if (response.upload && response.upload.result == 'Continue') {
127 - //reset retry counter
128 - _this.retries = 0;
129 - //start uploading next chunk
130 - _this.uploadChunk(response.upload.offset);
131 - } else {
132 - //failed to upload, try again in 3 second
133 - _this.retries++;
134 - if (_this.maxRetries > 0 && _this.retries >= _this.maxRetries) {
135 - //upload failed, raise response
 86+ _this.parseResponse(evt, function(response) {
 87+ if(response.upload && response.upload.filekey) {
 88+ _this.filekey = response.upload.filekey;
 89+ }
 90+ if (response.upload && response.upload.result == 'Success') {
 91+ //upload finished and can be unstashed later
13692 _this.transportedCb(response);
 93+ }
 94+ else if (response.upload && response.upload.result == 'Continue') {
 95+ //reset retry counter
 96+ _this.retries = 0;
 97+ //start uploading next chunk
 98+ _this.uploadChunk(response.upload.offset);
13799 } else {
138 - setTimeout(function() {
139 - _this.uploadChunk(offset);
140 - }, 3000);
 100+ //failed to upload, try again in 3 second
 101+ _this.retries++;
 102+ if (_this.maxRetries > 0 && _this.retries >= _this.maxRetries) {
 103+ //upload failed, raise response
 104+ _this.transportedCb(response);
 105+ } else {
 106+ setTimeout(function() {
 107+ _this.uploadChunk(offset);
 108+ }, 3000);
 109+ }
141110 }
142 - }
 111+ });
143112 }, false);
144113 this.xhr.addEventListener("error", function (evt) {
145 - var response;
146114 //failed to upload, try again in 3 second
147115 _this.retries++;
148116 if (_this.maxRetries > 0 && _this.retries >= _this.maxRetries) {
149 - try {
150 - response = JSON.parse(evt.target.responseText);
151 - } catch(e) {
152 - response = {
153 - responseText: evt.target.responseText
154 - };
155 - }
156 - _this.transportedCb(response);
 117+ _this.parseResponse(evt, _this.transportedCb);
157118 } else {
158119 setTimeout(function() {
159120 _this.uploadChunk(offset);
@@ -166,15 +127,7 @@
167128 }
168129 }, false);
169130 this.xhr.addEventListener("abort", function (evt) {
170 - var response;
171 - try {
172 - response = JSON.parse(evt.target.responseText);
173 - } catch(e) {
174 - response = {
175 - responseText: evt.target.responseText
176 - };
177 - }
178 - _this.transportedCb(response);
 131+ _this.parseResponse(evt, _this.transportedCb);
179132 }, false);
180133
181134 var formData;
@@ -205,12 +158,26 @@
206159 this.xhr.send(formData);
207160 }
208161 },
 162+ parseResponse: function(evt, callback) {
 163+ var response;
 164+ try {
 165+ response = $j.parseJSON(evt.target.responseText);
 166+ } catch(e) {
 167+ response = {
 168+ error: {
 169+ code: evt.target.code,
 170+ info: evt.target.responseText
 171+ }
 172+ };
 173+ }
 174+ callback(response);
 175+ },
209176 geckoFormData: function() {
210177 var boundary = '------XX' + Math.random(),
211178 dashdash = '--',
212179 crlf = '\r\n',
213180 builder = '', // Build RFC2388 string.
214 - wait = 0;
 181+ chunksRemaining = 0;
215182
216183 builder += dashdash + boundary + crlf;
217184
@@ -244,14 +211,14 @@
245212 builder += dashdash + boundary + crlf;
246213 },
247214 appendBlob: function(name, blob, filename) {
248 - wait++;
 215+ chunksRemaining++;
249216 var reader = new FileReader();
250217 reader.onload = function(e) {
251218 formData.appendFile(name, e.target.result,
252219 blob.type, filename);
253220 // Call onload after last Blob
254 - wait--;
255 - if(!wait && formData.xhr) {
 221+ chunksRemaining--;
 222+ if(!chunksRemaining && formData.xhr) {
256223 onload();
257224 }
258225 };
@@ -259,7 +226,7 @@
260227 },
261228 send: function(xhr) {
262229 formData.xhr = xhr;
263 - if(!wait) {
 230+ if(!chunksRemaining) {
264231 onload();
265232 }
266233 }
Index: trunk/extensions/UploadWizard/resources/mw.UploadWizardUpload.js
@@ -589,10 +589,7 @@
590590 if( mw.UploadWizard.config[ 'enableFirefogg' ] && typeof( Firefogg ) != 'undefined' ) {
591591 mw.log("mw.UploadWizard::getUploadHandler> FirefoggHandler");
592592 this.uploadHandler = new mw.FirefoggHandler( this, this.api );
593 - } else if( mw.UploadWizard.config[ 'enableFormData' ] &&
594 - (($j.browser.mozilla && $j.browser.version >= '5.0') ||
595 - ($j.browser.webkit && $j.browser.version >= '534.28'))
596 - ) {
 593+ } else if( mw.UploadWizard.config[ 'enableFormData' ] && mw.fileApi.isSliceAvailable()) {
597594 mw.log("mw.UploadWizard::getUploadHandler> ApiUploadFormDataHandler");
598595 this.uploadHandler = new mw.ApiUploadFormDataHandler( this, this.api );
599596 } else {

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r94627Add FormData upload transport....j12:33, 16 August 2011

Comments

#Comment by Raindrift (talk | contribs)   00:26, 14 October 2011

Looks good. For deploy, will need this in settings:

$wgUploadWizardConfig['enableFormData'] = true;

Status & tagging log