https://www.mediawiki.org/wiki/Special:Code/MediaWiki/101815
Revision: 101815 Author: j Date: 2011-11-03 14:35:11 +0000 (Thu, 03 Nov 2011) Log Message: ----------- Update Firefogg integration to use FormData handler for upload. This only works with Firefox 8 / Firefogg 2.8 Modified Paths: -------------- trunk/extensions/UploadWizard/resources/mw.Firefogg.js trunk/extensions/UploadWizard/resources/mw.FirefoggHandler.js trunk/extensions/UploadWizard/resources/mw.FirefoggTransport.js trunk/extensions/UploadWizard/resources/mw.UploadWizardUpload.js Modified: trunk/extensions/UploadWizard/resources/mw.Firefogg.js =================================================================== --- trunk/extensions/UploadWizard/resources/mw.Firefogg.js 2011-11-03 14:32:31 UTC (rev 101814) +++ trunk/extensions/UploadWizard/resources/mw.Firefogg.js 2011-11-03 14:35:11 UTC (rev 101815) @@ -24,5 +24,8 @@ osLink = this.firefoggInstallLinks['win32']; } return osLink; + }, + isInstalled: function() { + return typeof( Firefogg ) != 'undefined' && Firefogg().version >= '2.8.05'; } -}; \ No newline at end of file +}; Modified: trunk/extensions/UploadWizard/resources/mw.FirefoggHandler.js =================================================================== --- trunk/extensions/UploadWizard/resources/mw.FirefoggHandler.js 2011-11-03 14:32:31 UTC (rev 101814) +++ trunk/extensions/UploadWizard/resources/mw.FirefoggHandler.js 2011-11-03 14:35:11 UTC (rev 101815) @@ -3,7 +3,7 @@ * @param an UploadInterface object, which contains a .form property which points to a real HTML form in the DOM */ -mw.FirefoggHandler = function( upload ) { +mw.FirefoggHandler = function( upload, api ) { return this.init( upload ); }; @@ -15,17 +15,17 @@ */ init: function( upload ){ this.upload = upload; - this.api = upload.api; + this.api = upload.api; // update the mwe-upwiz-file-input target - this.upload.ui.$fileInputCtrl = this.getInputControl() + this.upload.ui.$fileInputCtrl = this.getInputControl(); this.upload.ui.fileCtrlContainer.empty().append( this.upload.ui.$fileInputCtrl - ) + ); // update the "valid" extension to include firefogg transcode extensions: mw.UploadWizard.config[ 'fileExtensions' ] = $.merge( mw.UploadWizard.config[ 'fileExtensions' ], mw.UploadWizard.config[ 'transcodeExtensionList' ] - ) + ); }, // Setup local pointer to firefogg instance @@ -38,14 +38,15 @@ getTransport: function(){ var _this = this; if( !this.transport ){ - this.transport = new mw.FirefoggTransport( - this.getForm(), + this.transport = new mw.FirefoggTransport( + this.upload, + this.api, this.getFogg(), function( fraction ) { _this.upload.setTransportProgress( fraction ); // also update preview video: }, - function( result ) { + function( result ) { mw.log("FirefoggTransport::getTransport> Transport done " + JSON.stringify( result ) ); _this.upload.setTransported( result ); } @@ -85,12 +86,13 @@ if( _this.getFogg().selectVideo() ) { // Update the value of the input file: $j( this ) - .val( _this.getFogg().sourceFilename ) + .val( _this.getFogg().sourceFilename ); //.trigger('change'); // note the change trigger does not work because we replace the target: var title = _this.getTransport().getFileName().replace( /:/g, '_' ); _this.upload.title = new mw.Title( title , 'file' ); _this.upload.ui.fileChangedOk(); + _this.upload.filename = title; } } ); }, Modified: trunk/extensions/UploadWizard/resources/mw.FirefoggTransport.js =================================================================== --- trunk/extensions/UploadWizard/resources/mw.FirefoggTransport.js 2011-11-03 14:32:31 UTC (rev 101814) +++ trunk/extensions/UploadWizard/resources/mw.FirefoggTransport.js 2011-11-03 14:35:11 UTC (rev 101815) @@ -1,12 +1,15 @@ /** * Represents a "transport" for files to upload; in this case an firefogg. * - * @param form jQuery selector for HTML form + * @param upload UploadInterface + * @param api + * @param fogg Firefogg instance * @param progressCb callback to execute as the upload progresses * @param transportedCb callback to execute when we've finished the upload */ -mw.FirefoggTransport = function( $form, fogg, progressCb, transportedCb ) { - this.$form = $form; +mw.FirefoggTransport = function( upload, api, fogg, progressCb, transportedCb ) { + this.upload = upload; + this.api = api; this.fogg = fogg; this.progressCb = progressCb; this.transportedCb = transportedCb; @@ -14,26 +17,37 @@ mw.FirefoggTransport.prototype = { - passthrough: false, /** - * Do an upload on a given fogg object: + * Do an upload */ - doUpload: function(){ - // check if the server supports chunks: - if( this.isChunkUpload() ){ - mw.log("FirefoggTransport::doUpload> Chunks"); - // encode and upload at the same time: - this.doChunkUpload(); - } else { - mw.log("FirefoggTransport::doUpload> Encode then upload"); - this.doEncodeThenUpload(); - } + doUpload: function() { + var _this = this; + //Encode or passthrough Firefogg before upload + this.fogg.encode( JSON.stringify( this.getEncodeSettings() ), + function(result, file) { + result = JSON.parse(result); + if(result.progress == 1) { //encoding done + _this.doFormDataUpload(file); + } else { //encoding failed + var response = { + error: { + code: 500, + info: 'Encoding failed' + } + }; + _this.transportedCb(response); + } + }, function(progress) { //progress + progress = JSON.parse(progress); + _this.progressCb( progress.progress ); + } + ); }, - isChunkUpload: function(){ - // for now just test post - return false; - return ( mw.UploadWizard.config[ 'enableFirefoggChunkUpload' ] ); - }, + doFormDataUpload: function(file) { + this.upload.file = file; + this.uploadHandler = new mw.ApiUploadFormDataHandler( this.upload, this.api ); + this.uploadHandler.start(); + }, /** * Check if the asset should be uploaded in passthrough mode ( or if it should be encoded ) */ @@ -104,10 +118,10 @@ } }, getEncodeExt: function(){ - var encodeSettings = mw.UploadWizard.config[ 'firefoggEncodeSettings']; - if( encodeSettings['videoCodec'] + var encodeSettings = mw.UploadWizard.config[ 'firefoggEncodeSettings' ]; + if( encodeSettings[ 'videoCodec' ] && - encodeSettings['videoCodec'] == 'vp8' ) + encodeSettings[ 'videoCodec' ] == 'vp8' ) { return 'webm'; } else { @@ -123,124 +137,11 @@ return { 'passthrough' : true }; } // Get the default encode settings: - var encodeSettings = mw.UploadWizard.config[ 'firefoggEncodeSettings']; + var encodeSettings = mw.UploadWizard.config[ 'firefoggEncodeSettings' ]; // Update the format: - this.fogg.setFormat( ( this.getEncodeExt() == 'webm' )? 'webm' : 'ogg' ); + this.fogg.setFormat( ( this.getEncodeExt() == 'webm' ) ? 'webm' : 'ogg' ); mw.log("FirefoggTransport::getEncodeSettings> " + JSON.stringify( encodeSettings ) ); return encodeSettings; }, - - /** - * Encode then upload - */ - doEncodeThenUpload: function(){ - this.fogg.encode( JSON.stringify( this.getEncodeSettings() ) ); - this.monitorProgress(); - }, - - /** - * Do fogg post - */ - doFoggPost: function(){ - var _this = this; - // Get the upload request with a callback ( populates the request token ) - this.getUploadRequest( function( request ){ - mw.log("FirefoggTransport::doFoggPost> " + _this.getUploadUrl() + ' request:' + - JSON.stringify( request ) ); - - _this.fogg.post( _this.getUploadUrl(), - 'file', - JSON.stringify( request ) - ); - _this.monitorProgress(); - } ); - }, - - /** - * Encode and upload in chunks - */ - doChunkUpload: function(){ - var _this = this; - this.getUploadRequest( function( request ){ - this.fogg.upload( - JSON.stringify( _this.getEncodeSettings() ), - _this.getUploadUrl(), - JSON.stringify( request ) - ); - }); - _this.monitorProgress(); - }, - - /** - * Get the upload url - */ - getUploadUrl: function(){ - return mw.UploadWizard.config['apiUrl']; - }, - - /** - * Get the upload settings - * @param {function} callback function to send the request object - */ - getUploadRequest: function( callback ){ - var _this = this; - // ugly probably would be nice to have base reference to the upload class so we can use the - new mw.Api( { - 'url' : _this.getUploadUrl() - } ) - .getEditToken( function( token ) { - callback( { - 'action' : ( _this.isChunkUpload() )? 'firefoggupload' : 'upload', - 'stash' :1, - 'comment' : 'DUMMY TEXT', - 'format' : 'json', - 'filename' : _this.getFileName(), - 'token' : token - } ); - }, function( code, info ) { - _this.upload.setError( code, info ); - } ); - }, - /** - * Monitor progress on an upload: - */ - monitorProgress: function(){ - var _this = this; - var fogg = this.fogg; - var progress = fogg.progress(); - var state = fogg.state; - - //mw.log("FirefoggTransport::monitorProgress> " + progress + ' state: ' + state + ' status: ' + this.fogg.status() + ' rt: ' + this.getResponseText() ); - this.progressCb( progress ); - - if( state == 'encoding done' && ! this.isChunkUpload() ){ - // ( if encoding done, we are in a two step encode then upload process ) - this.doFoggPost(); - return ; - } - // If state is 'in progress' ... fire monitor progress - if( state == 'encoding' || state == 'uploading' || state == '' ){ - setTimeout( function(){ - _this.monitorProgress(); - }, mw.UploadWizard.config['uploadProgressInterval'] ); - } - // return the api result: - if( state == 'done' || state == 'upload done' ){ - this.transportedCb( JSON.parse( this.getResponseText() ) ); - } - - }, - /** - * Get the response text from a firefogg upload - */ - getResponseText: function(){ - var _this = this; - try { - var pstatus = JSON.parse( _this.fogg.uploadstatus() ); - return pstatus["responseText"]; - } catch( e ) { - mw.log( "Error:: Firefogg could not parse uploadstatus / could not get responseText: " + e ); - } - } }; Modified: trunk/extensions/UploadWizard/resources/mw.UploadWizardUpload.js =================================================================== --- trunk/extensions/UploadWizard/resources/mw.UploadWizardUpload.js 2011-11-03 14:32:31 UTC (rev 101814) +++ trunk/extensions/UploadWizard/resources/mw.UploadWizardUpload.js 2011-11-03 14:35:11 UTC (rev 101815) @@ -591,7 +591,7 @@ getUploadHandler: function(){ if( !this.uploadHandler ) { var constructor; // must be the name of a function in 'mw' namespace - if( mw.UploadWizard.config[ 'enableFirefogg' ] && typeof( Firefogg ) != 'undefined' ) { + if( mw.UploadWizard.config[ 'enableFirefogg' ] && mw.Firefogg.isInstalled() ) { constructor = 'FirefoggHandler'; } else if( mw.UploadWizard.config[ 'enableFormData' ] && mw.fileApi.isSliceAvailable()) { constructor = 'ApiUploadFormDataHandler'; _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs