r101815 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r101814‎ | r101815 | r101816 >
Date:14:35, 3 November 2011
Author:j
Status:deferred
Tags:
Comment:
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 (modified) (history)
  • /trunk/extensions/UploadWizard/resources/mw.FirefoggHandler.js (modified) (history)
  • /trunk/extensions/UploadWizard/resources/mw.FirefoggTransport.js (modified) (history)
  • /trunk/extensions/UploadWizard/resources/mw.UploadWizardUpload.js (modified) (history)

Diff [purge]

Index: trunk/extensions/UploadWizard/resources/mw.FirefoggHandler.js
@@ -3,7 +3,7 @@
44 * @param an UploadInterface object, which contains a .form property which points to a real HTML form in the DOM
55 */
66
7 -mw.FirefoggHandler = function( upload ) {
 7+mw.FirefoggHandler = function( upload, api ) {
88 return this.init( upload );
99 };
1010
@@ -15,17 +15,17 @@
1616 */
1717 init: function( upload ){
1818 this.upload = upload;
19 - this.api = upload.api;
 19+ this.api = upload.api;
2020 // update the mwe-upwiz-file-input target
21 - this.upload.ui.$fileInputCtrl = this.getInputControl()
 21+ this.upload.ui.$fileInputCtrl = this.getInputControl();
2222 this.upload.ui.fileCtrlContainer.empty().append(
2323 this.upload.ui.$fileInputCtrl
24 - )
 24+ );
2525 // update the "valid" extension to include firefogg transcode extensions:
2626 mw.UploadWizard.config[ 'fileExtensions' ] = $.merge(
2727 mw.UploadWizard.config[ 'fileExtensions' ],
2828 mw.UploadWizard.config[ 'transcodeExtensionList' ]
29 - )
 29+ );
3030
3131 },
3232 // Setup local pointer to firefogg instance
@@ -38,14 +38,15 @@
3939 getTransport: function(){
4040 var _this = this;
4141 if( !this.transport ){
42 - this.transport = new mw.FirefoggTransport(
43 - this.getForm(),
 42+ this.transport = new mw.FirefoggTransport(
 43+ this.upload,
 44+ this.api,
4445 this.getFogg(),
4546 function( fraction ) {
4647 _this.upload.setTransportProgress( fraction );
4748 // also update preview video:
4849 },
49 - function( result ) {
 50+ function( result ) {
5051 mw.log("FirefoggTransport::getTransport> Transport done " + JSON.stringify( result ) );
5152 _this.upload.setTransported( result );
5253 }
@@ -85,12 +86,13 @@
8687 if( _this.getFogg().selectVideo() ) {
8788 // Update the value of the input file:
8889 $j( this )
89 - .val( _this.getFogg().sourceFilename )
 90+ .val( _this.getFogg().sourceFilename );
9091 //.trigger('change');
9192 // note the change trigger does not work because we replace the target:
9293 var title = _this.getTransport().getFileName().replace( /:/g, '_' );
9394 _this.upload.title = new mw.Title( title , 'file' );
9495 _this.upload.ui.fileChangedOk();
 96+ _this.upload.filename = title;
9597 }
9698 } );
9799 },
Index: trunk/extensions/UploadWizard/resources/mw.Firefogg.js
@@ -24,5 +24,8 @@
2525 osLink = this.firefoggInstallLinks['win32'];
2626 }
2727 return osLink;
 28+ },
 29+ isInstalled: function() {
 30+ return typeof( Firefogg ) != 'undefined' && Firefogg().version >= '2.8.05';
2831 }
29 -};
\ No newline at end of file
 32+};
Index: trunk/extensions/UploadWizard/resources/mw.FirefoggTransport.js
@@ -1,12 +1,15 @@
22 /**
33 * Represents a "transport" for files to upload; in this case an firefogg.
44 *
5 - * @param form jQuery selector for HTML form
 5+ * @param upload UploadInterface
 6+ * @param api
 7+ * @param fogg Firefogg instance
68 * @param progressCb callback to execute as the upload progresses
79 * @param transportedCb callback to execute when we've finished the upload
810 */
9 -mw.FirefoggTransport = function( $form, fogg, progressCb, transportedCb ) {
10 - this.$form = $form;
 11+mw.FirefoggTransport = function( upload, api, fogg, progressCb, transportedCb ) {
 12+ this.upload = upload;
 13+ this.api = api;
1114 this.fogg = fogg;
1215 this.progressCb = progressCb;
1316 this.transportedCb = transportedCb;
@@ -14,26 +17,37 @@
1518
1619 mw.FirefoggTransport.prototype = {
1720
18 - passthrough: false,
1921 /**
20 - * Do an upload on a given fogg object:
 22+ * Do an upload
2123 */
22 - doUpload: function(){
23 - // check if the server supports chunks:
24 - if( this.isChunkUpload() ){
25 - mw.log("FirefoggTransport::doUpload> Chunks");
26 - // encode and upload at the same time:
27 - this.doChunkUpload();
28 - } else {
29 - mw.log("FirefoggTransport::doUpload> Encode then upload");
30 - this.doEncodeThenUpload();
31 - }
 24+ doUpload: function() {
 25+ var _this = this;
 26+ //Encode or passthrough Firefogg before upload
 27+ this.fogg.encode( JSON.stringify( this.getEncodeSettings() ),
 28+ function(result, file) {
 29+ result = JSON.parse(result);
 30+ if(result.progress == 1) { //encoding done
 31+ _this.doFormDataUpload(file);
 32+ } else { //encoding failed
 33+ var response = {
 34+ error: {
 35+ code: 500,
 36+ info: 'Encoding failed'
 37+ }
 38+ };
 39+ _this.transportedCb(response);
 40+ }
 41+ }, function(progress) { //progress
 42+ progress = JSON.parse(progress);
 43+ _this.progressCb( progress.progress );
 44+ }
 45+ );
3246 },
33 - isChunkUpload: function(){
34 - // for now just test post
35 - return false;
36 - return ( mw.UploadWizard.config[ 'enableFirefoggChunkUpload' ] );
37 - },
 47+ doFormDataUpload: function(file) {
 48+ this.upload.file = file;
 49+ this.uploadHandler = new mw.ApiUploadFormDataHandler( this.upload, this.api );
 50+ this.uploadHandler.start();
 51+ },
3852 /**
3953 * Check if the asset should be uploaded in passthrough mode ( or if it should be encoded )
4054 */
@@ -104,10 +118,10 @@
105119 }
106120 },
107121 getEncodeExt: function(){
108 - var encodeSettings = mw.UploadWizard.config[ 'firefoggEncodeSettings'];
109 - if( encodeSettings['videoCodec']
 122+ var encodeSettings = mw.UploadWizard.config[ 'firefoggEncodeSettings' ];
 123+ if( encodeSettings[ 'videoCodec' ]
110124 &&
111 - encodeSettings['videoCodec'] == 'vp8' )
 125+ encodeSettings[ 'videoCodec' ] == 'vp8' )
112126 {
113127 return 'webm';
114128 } else {
@@ -123,124 +137,11 @@
124138 return { 'passthrough' : true };
125139 }
126140 // Get the default encode settings:
127 - var encodeSettings = mw.UploadWizard.config[ 'firefoggEncodeSettings'];
 141+ var encodeSettings = mw.UploadWizard.config[ 'firefoggEncodeSettings' ];
128142 // Update the format:
129 - this.fogg.setFormat( ( this.getEncodeExt() == 'webm' )? 'webm' : 'ogg' );
 143+ this.fogg.setFormat( ( this.getEncodeExt() == 'webm' ) ? 'webm' : 'ogg' );
130144
131145 mw.log("FirefoggTransport::getEncodeSettings> " + JSON.stringify( encodeSettings ) );
132146 return encodeSettings;
133147 },
134 -
135 - /**
136 - * Encode then upload
137 - */
138 - doEncodeThenUpload: function(){
139 - this.fogg.encode( JSON.stringify( this.getEncodeSettings() ) );
140 - this.monitorProgress();
141 - },
142 -
143 - /**
144 - * Do fogg post
145 - */
146 - doFoggPost: function(){
147 - var _this = this;
148 - // Get the upload request with a callback ( populates the request token )
149 - this.getUploadRequest( function( request ){
150 - mw.log("FirefoggTransport::doFoggPost> " + _this.getUploadUrl() + ' request:' +
151 - JSON.stringify( request ) );
152 -
153 - _this.fogg.post( _this.getUploadUrl(),
154 - 'file',
155 - JSON.stringify( request )
156 - );
157 - _this.monitorProgress();
158 - } );
159 - },
160 -
161 - /**
162 - * Encode and upload in chunks
163 - */
164 - doChunkUpload: function(){
165 - var _this = this;
166 - this.getUploadRequest( function( request ){
167 - this.fogg.upload(
168 - JSON.stringify( _this.getEncodeSettings() ),
169 - _this.getUploadUrl(),
170 - JSON.stringify( request )
171 - );
172 - });
173 - _this.monitorProgress();
174 - },
175 -
176 - /**
177 - * Get the upload url
178 - */
179 - getUploadUrl: function(){
180 - return mw.UploadWizard.config['apiUrl'];
181 - },
182 -
183 - /**
184 - * Get the upload settings
185 - * @param {function} callback function to send the request object
186 - */
187 - getUploadRequest: function( callback ){
188 - var _this = this;
189 - // ugly probably would be nice to have base reference to the upload class so we can use the
190 - new mw.Api( {
191 - 'url' : _this.getUploadUrl()
192 - } )
193 - .getEditToken( function( token ) {
194 - callback( {
195 - 'action' : ( _this.isChunkUpload() )? 'firefoggupload' : 'upload',
196 - 'stash' :1,
197 - 'comment' : 'DUMMY TEXT',
198 - 'format' : 'json',
199 - 'filename' : _this.getFileName(),
200 - 'token' : token
201 - } );
202 - }, function( code, info ) {
203 - _this.upload.setError( code, info );
204 - } );
205 - },
206 - /**
207 - * Monitor progress on an upload:
208 - */
209 - monitorProgress: function(){
210 - var _this = this;
211 - var fogg = this.fogg;
212 - var progress = fogg.progress();
213 - var state = fogg.state;
214 -
215 - //mw.log("FirefoggTransport::monitorProgress> " + progress + ' state: ' + state + ' status: ' + this.fogg.status() + ' rt: ' + this.getResponseText() );
216 - this.progressCb( progress );
217 -
218 - if( state == 'encoding done' && ! this.isChunkUpload() ){
219 - // ( if encoding done, we are in a two step encode then upload process )
220 - this.doFoggPost();
221 - return ;
222 - }
223 - // If state is 'in progress' ... fire monitor progress
224 - if( state == 'encoding' || state == 'uploading' || state == '' ){
225 - setTimeout( function(){
226 - _this.monitorProgress();
227 - }, mw.UploadWizard.config['uploadProgressInterval'] );
228 - }
229 - // return the api result:
230 - if( state == 'done' || state == 'upload done' ){
231 - this.transportedCb( JSON.parse( this.getResponseText() ) );
232 - }
233 -
234 - },
235 - /**
236 - * Get the response text from a firefogg upload
237 - */
238 - getResponseText: function(){
239 - var _this = this;
240 - try {
241 - var pstatus = JSON.parse( _this.fogg.uploadstatus() );
242 - return pstatus["responseText"];
243 - } catch( e ) {
244 - mw.log( "Error:: Firefogg could not parse uploadstatus / could not get responseText: " + e );
245 - }
246 - }
247148 };
Index: trunk/extensions/UploadWizard/resources/mw.UploadWizardUpload.js
@@ -591,7 +591,7 @@
592592 getUploadHandler: function(){
593593 if( !this.uploadHandler ) {
594594 var constructor; // must be the name of a function in 'mw' namespace
595 - if( mw.UploadWizard.config[ 'enableFirefogg' ] && typeof( Firefogg ) != 'undefined' ) {
 595+ if( mw.UploadWizard.config[ 'enableFirefogg' ] && mw.Firefogg.isInstalled() ) {
596596 constructor = 'FirefoggHandler';
597597 } else if( mw.UploadWizard.config[ 'enableFormData' ] && mw.fileApi.isSliceAvailable()) {
598598 constructor = 'ApiUploadFormDataHandler';

Status & tagging log