http://www.mediawiki.org/wiki/Special:Code/MediaWiki/99013
Revision: 99013 Author: j Date: 2011-10-05 17:25:04 +0000 (Wed, 05 Oct 2011) Log Message: ----------- 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 trunk/extensions/UploadWizard/resources/mw.UploadWizardUpload.js trunk/extensions/UploadWizard/resources/mw.fileApi.js Modified: trunk/extensions/UploadWizard/resources/mw.FormDataTransport.js =================================================================== --- trunk/extensions/UploadWizard/resources/mw.FormDataTransport.js 2011-10-05 17:19:21 UTC (rev 99012) +++ trunk/extensions/UploadWizard/resources/mw.FormDataTransport.js 2011-10-05 17:25:04 UTC (rev 99013) @@ -28,35 +28,18 @@ mw.FormDataTransport.prototype = { upload: function() { var _this = this, - file = this.uploadObject.file; - var bytesAvailable = file.size; - + file = this.uploadObject.file, + bytesAvailable = file.size; + if(file.size > this.chunkSize) { this.uploadChunk(0); } else { this.xhr = new XMLHttpRequest(); this.xhr.addEventListener("load", function (evt) { - var response; - try { - response = JSON.parse(evt.target.responseText); - } catch(e) { - response = { - responseText: evt.target.responseText - }; - } - //upload finished and can be unstashed later - _this.transportedCb(response); + _this.parseResponse(evt, _this.transportedCb); }, false); this.xhr.addEventListener("error", function (evt) { - var response; - try { - response = JSON.parse(evt.target.responseText); - } catch(e) { - response = { - responseText: evt.target.responseText - }; - } - _this.transportedCb(response); + _this.parseResponse(evt, _this.transportedCb); }, false); this.xhr.upload.addEventListener("progress", function (evt) { if (evt.lengthComputable) { @@ -65,15 +48,7 @@ } }, false); this.xhr.addEventListener("abort", function (evt) { - var response; - try { - response = JSON.parse(evt.target.responseText); - } catch(e) { - response = { - responseText: evt.target.responseText - }; - } - _this.transportedCb(response); + _this.parseResponse(evt, _this.transportedCb); }, false); var formData = new FormData(); @@ -106,53 +81,39 @@ this.xhr = new XMLHttpRequest(); this.xhr.addEventListener("load", function (evt) { - var response; _this.responseText = evt.target.responseText; - try { - response = JSON.parse(evt.target.responseText); - } catch(e) { - response = { - responseText: evt.target.responseText - }; - } - if(response.upload && response.upload.filekey) { - _this.filekey = response.upload.filekey; - } - if (response.upload && response.upload.result == 'Success') { - //upload finished and can be unstashed later - _this.transportedCb(response); - } - else if (response.upload && response.upload.result == 'Continue') { - //reset retry counter - _this.retries = 0; - //start uploading next chunk - _this.uploadChunk(response.upload.offset); - } else { - //failed to upload, try again in 3 second - _this.retries++; - if (_this.maxRetries > 0 && _this.retries >= _this.maxRetries) { - //upload failed, raise response + _this.parseResponse(evt, function(response) { + if(response.upload && response.upload.filekey) { + _this.filekey = response.upload.filekey; + } + if (response.upload && response.upload.result == 'Success') { + //upload finished and can be unstashed later _this.transportedCb(response); + } + else if (response.upload && response.upload.result == 'Continue') { + //reset retry counter + _this.retries = 0; + //start uploading next chunk + _this.uploadChunk(response.upload.offset); } else { - setTimeout(function() { - _this.uploadChunk(offset); - }, 3000); + //failed to upload, try again in 3 second + _this.retries++; + if (_this.maxRetries > 0 && _this.retries >= _this.maxRetries) { + //upload failed, raise response + _this.transportedCb(response); + } else { + setTimeout(function() { + _this.uploadChunk(offset); + }, 3000); + } } - } + }); }, false); this.xhr.addEventListener("error", function (evt) { - var response; //failed to upload, try again in 3 second _this.retries++; if (_this.maxRetries > 0 && _this.retries >= _this.maxRetries) { - try { - response = JSON.parse(evt.target.responseText); - } catch(e) { - response = { - responseText: evt.target.responseText - }; - } - _this.transportedCb(response); + _this.parseResponse(evt, _this.transportedCb); } else { setTimeout(function() { _this.uploadChunk(offset); @@ -166,15 +127,7 @@ } }, false); this.xhr.addEventListener("abort", function (evt) { - var response; - try { - response = JSON.parse(evt.target.responseText); - } catch(e) { - response = { - responseText: evt.target.responseText - }; - } - _this.transportedCb(response); + _this.parseResponse(evt, _this.transportedCb); }, false); var formData; @@ -205,12 +158,26 @@ this.xhr.send(formData); } }, + parseResponse: function(evt, callback) { + var response; + try { + response = $j.parseJSON(evt.target.responseText); + } catch(e) { + response = { + error: { + code: evt.target.code, + info: evt.target.responseText + } + }; + } + callback(response); + }, geckoFormData: function() { var boundary = '------XX' + Math.random(), dashdash = '--', crlf = '\r\n', builder = '', // Build RFC2388 string. - wait = 0; + chunksRemaining = 0; builder += dashdash + boundary + crlf; @@ -244,14 +211,14 @@ builder += dashdash + boundary + crlf; }, appendBlob: function(name, blob, filename) { - wait++; + chunksRemaining++; var reader = new FileReader(); reader.onload = function(e) { formData.appendFile(name, e.target.result, blob.type, filename); // Call onload after last Blob - wait--; - if(!wait && formData.xhr) { + chunksRemaining--; + if(!chunksRemaining && formData.xhr) { onload(); } }; @@ -259,7 +226,7 @@ }, send: function(xhr) { formData.xhr = xhr; - if(!wait) { + if(!chunksRemaining) { onload(); } } Modified: trunk/extensions/UploadWizard/resources/mw.UploadWizardUpload.js =================================================================== --- trunk/extensions/UploadWizard/resources/mw.UploadWizardUpload.js 2011-10-05 17:19:21 UTC (rev 99012) +++ trunk/extensions/UploadWizard/resources/mw.UploadWizardUpload.js 2011-10-05 17:25:04 UTC (rev 99013) @@ -589,10 +589,7 @@ if( mw.UploadWizard.config[ 'enableFirefogg' ] && typeof( Firefogg ) != 'undefined' ) { mw.log("mw.UploadWizard::getUploadHandler> FirefoggHandler"); this.uploadHandler = new mw.FirefoggHandler( this, this.api ); - } else if( mw.UploadWizard.config[ 'enableFormData' ] && - (($j.browser.mozilla && $j.browser.version >= '5.0') || - ($j.browser.webkit && $j.browser.version >= '534.28')) - ) { + } else if( mw.UploadWizard.config[ 'enableFormData' ] && mw.fileApi.isSliceAvailable()) { mw.log("mw.UploadWizard::getUploadHandler> ApiUploadFormDataHandler"); this.uploadHandler = new mw.ApiUploadFormDataHandler( this, this.api ); } else { Modified: trunk/extensions/UploadWizard/resources/mw.fileApi.js =================================================================== --- trunk/extensions/UploadWizard/resources/mw.fileApi.js 2011-10-05 17:19:21 UTC (rev 99012) +++ trunk/extensions/UploadWizard/resources/mw.fileApi.js 2011-10-05 17:25:04 UTC (rev 99013) @@ -24,6 +24,15 @@ var known = [ 'image/png', 'image/gif', 'image/jpeg', 'image/svg+xml'], tooHuge = 10 * 1024 * 1024; return ( $.inArray( file.type, known ) !== -1 ) && file.size > 0 && file.size < tooHuge; + }, + + /** + * Is the slice function of FileAPI available with sufficient functionality? + * @todo is there a way to check this instead of hardcoding browsers and version? + */ + isSliceAvailable: function() { + return ($j.browser.mozilla && $j.browser.version >= '5.0') || + ($j.browser.webkit && $j.browser.version >= '534.28'); } _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs