Index: trunk/extensions/UploadWizard/resources/mw.fileApi.js |
— | — | @@ -24,6 +24,15 @@ |
25 | 25 | var known = [ 'image/png', 'image/gif', 'image/jpeg', 'image/svg+xml'], |
26 | 26 | tooHuge = 10 * 1024 * 1024; |
27 | 27 | 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'); |
28 | 37 | } |
29 | 38 | |
30 | 39 | |
Index: trunk/extensions/UploadWizard/resources/mw.FormDataTransport.js |
— | — | @@ -28,35 +28,18 @@ |
29 | 29 | mw.FormDataTransport.prototype = { |
30 | 30 | upload: function() { |
31 | 31 | var _this = this, |
32 | | - file = this.uploadObject.file; |
33 | | - var bytesAvailable = file.size; |
34 | | - |
| 32 | + file = this.uploadObject.file, |
| 33 | + bytesAvailable = file.size; |
| 34 | + |
35 | 35 | if(file.size > this.chunkSize) { |
36 | 36 | this.uploadChunk(0); |
37 | 37 | } else { |
38 | 38 | this.xhr = new XMLHttpRequest(); |
39 | 39 | 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); |
50 | 41 | }, false); |
51 | 42 | 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); |
61 | 44 | }, false); |
62 | 45 | this.xhr.upload.addEventListener("progress", function (evt) { |
63 | 46 | if (evt.lengthComputable) { |
— | — | @@ -65,15 +48,7 @@ |
66 | 49 | } |
67 | 50 | }, false); |
68 | 51 | 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); |
78 | 53 | }, false); |
79 | 54 | |
80 | 55 | var formData = new FormData(); |
— | — | @@ -106,53 +81,39 @@ |
107 | 82 | |
108 | 83 | this.xhr = new XMLHttpRequest(); |
109 | 84 | this.xhr.addEventListener("load", function (evt) { |
110 | | - var response; |
111 | 85 | _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 |
136 | 92 | _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); |
137 | 99 | } 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 | + } |
141 | 110 | } |
142 | | - } |
| 111 | + }); |
143 | 112 | }, false); |
144 | 113 | this.xhr.addEventListener("error", function (evt) { |
145 | | - var response; |
146 | 114 | //failed to upload, try again in 3 second |
147 | 115 | _this.retries++; |
148 | 116 | 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); |
157 | 118 | } else { |
158 | 119 | setTimeout(function() { |
159 | 120 | _this.uploadChunk(offset); |
— | — | @@ -166,15 +127,7 @@ |
167 | 128 | } |
168 | 129 | }, false); |
169 | 130 | 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); |
179 | 132 | }, false); |
180 | 133 | |
181 | 134 | var formData; |
— | — | @@ -205,12 +158,26 @@ |
206 | 159 | this.xhr.send(formData); |
207 | 160 | } |
208 | 161 | }, |
| 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 | + }, |
209 | 176 | geckoFormData: function() { |
210 | 177 | var boundary = '------XX' + Math.random(), |
211 | 178 | dashdash = '--', |
212 | 179 | crlf = '\r\n', |
213 | 180 | builder = '', // Build RFC2388 string. |
214 | | - wait = 0; |
| 181 | + chunksRemaining = 0; |
215 | 182 | |
216 | 183 | builder += dashdash + boundary + crlf; |
217 | 184 | |
— | — | @@ -244,14 +211,14 @@ |
245 | 212 | builder += dashdash + boundary + crlf; |
246 | 213 | }, |
247 | 214 | appendBlob: function(name, blob, filename) { |
248 | | - wait++; |
| 215 | + chunksRemaining++; |
249 | 216 | var reader = new FileReader(); |
250 | 217 | reader.onload = function(e) { |
251 | 218 | formData.appendFile(name, e.target.result, |
252 | 219 | blob.type, filename); |
253 | 220 | // Call onload after last Blob |
254 | | - wait--; |
255 | | - if(!wait && formData.xhr) { |
| 221 | + chunksRemaining--; |
| 222 | + if(!chunksRemaining && formData.xhr) { |
256 | 223 | onload(); |
257 | 224 | } |
258 | 225 | }; |
— | — | @@ -259,7 +226,7 @@ |
260 | 227 | }, |
261 | 228 | send: function(xhr) { |
262 | 229 | formData.xhr = xhr; |
263 | | - if(!wait) { |
| 230 | + if(!chunksRemaining) { |
264 | 231 | onload(); |
265 | 232 | } |
266 | 233 | } |
Index: trunk/extensions/UploadWizard/resources/mw.UploadWizardUpload.js |
— | — | @@ -589,10 +589,7 @@ |
590 | 590 | if( mw.UploadWizard.config[ 'enableFirefogg' ] && typeof( Firefogg ) != 'undefined' ) { |
591 | 591 | mw.log("mw.UploadWizard::getUploadHandler> FirefoggHandler"); |
592 | 592 | 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()) { |
597 | 594 | mw.log("mw.UploadWizard::getUploadHandler> ApiUploadFormDataHandler"); |
598 | 595 | this.uploadHandler = new mw.ApiUploadFormDataHandler( this, this.api ); |
599 | 596 | } else { |