jenkins-bot has submitted this change and it was merged.

Change subject: mw.UploadWizardUpload: Disentangle JPEG metadata from filling 
uploads
......................................................................


mw.UploadWizardUpload: Disentangle JPEG metadata from filling uploads

As far as I can tell (it's hard to tell because it's entangled), we
only use this to render the thumbnails with the right rotation and
aspect ratio. So let's just do it as part of the thumbnail generation.

This should let us make other stuff less horrible. In particular, this
means that the this.uploads array now holds uploads in the same order
in which they were created (and in which they're shown in the interface),
which in turn results in UW uploading files in the same order, and
which allows us to remove some crappy sorting from elsewhere (added in
a37bba8dbc2e87b108c7e22c7ef0dabe847f07c5).

Change-Id: Ie9c0098074320a1fb9e63acab5672965f37a4700
---
M resources/controller/uw.controller.Step.js
M resources/mw.UploadWizardUpload.js
2 files changed, 57 insertions(+), 64 deletions(-)

Approvals:
  MarkTraceur: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/resources/controller/uw.controller.Step.js 
b/resources/controller/uw.controller.Step.js
index c1fbef6..64804b1 100644
--- a/resources/controller/uw.controller.Step.js
+++ b/resources/controller/uw.controller.Step.js
@@ -88,18 +88,6 @@
 
                this.uploads = uploads || [];
 
-               // Keep the uploads sorted in the order they were created in 
initially.
-               this.uploads = this.uploads.sort( function ( uploadA, uploadB ) 
{
-                       // Can the uploads be undefined? Code below would imply 
they can, no idea how.
-                       if ( !uploadA ) {
-                               return 1;
-                       }
-                       if ( !uploadB ) {
-                               return -1;
-                       }
-                       return uploadA.index - uploadB.index;
-               } );
-
                $.each( this.uploads, function ( i, upload ) {
                        if ( upload !== undefined ) {
                                upload.state = step.stepName;
diff --git a/resources/mw.UploadWizardUpload.js 
b/resources/mw.UploadWizardUpload.js
index 907bebf..3b74e52 100644
--- a/resources/mw.UploadWizardUpload.js
+++ b/resources/mw.UploadWizardUpload.js
@@ -377,7 +377,7 @@
         */
        mw.UploadWizardUpload.prototype.checkFile = function ( filename, file ) 
{
                var duplicate, extension,
-                       actualMaxSize, binReader,
+                       actualMaxSize,
                        upload = this,
 
                        // Check if filename is acceptable
@@ -463,55 +463,8 @@
                                                this.imageinfo = {};
                                        }
                                        this.filename = filename;
-
-                                       // For JPEGs, we use the JsJpegMeta 
library in core to extract metadata,
-                                       // including EXIF tags. This is done 
asynchronously once each file has been
-                                       // read. Only then is the file properly 
added to UploadWizard via fileChangedOk().
-                                       //
-                                       // For all other file types, we don't 
need or want to run this.
-                                       //
-                                       // TODO: This should be refactored.
-
-                                       if ( this.file.type === 'image/jpeg' ) {
-                                               binReader = new FileReader();
-                                               binReader.onload = function () {
-                                                       var binStr, arr, i, 
meta;
-                                                       if ( typeof 
binReader.result === 'string' ) {
-                                                               binStr = 
binReader.result;
-                                                       } else {
-                                                               // Array 
buffer; convert to binary string for the library.
-                                                               arr = new 
Uint8Array( binReader.result );
-                                                               binStr = '';
-                                                               for ( i = 0; i 
< arr.byteLength; i++ ) {
-                                                                       binStr 
+= String.fromCharCode( arr[ i ] );
-                                                               }
-                                                       }
-                                                       try {
-                                                               meta = 
mw.libs.jpegmeta( binStr, upload.file.fileName );
-                                                               // jscs:disable 
requireCamelCaseOrUpperCaseIdentifiers, disallowDanglingUnderscores
-                                                               
meta._binary_data = null;
-                                                               // jscs:enable
-                                                       } catch ( e ) {
-                                                               meta = null;
-                                                       }
-                                                       
upload.extractMetadataFromJpegMeta( meta );
-                                                       upload.filename = 
filename;
-                                                       if ( upload.hasError 
=== false ) {
-                                                               
finishCallback();
-                                                       }
-                                               };
-                                               if ( 'readAsBinaryString' in 
binReader ) {
-                                                       
binReader.readAsBinaryString( upload.file );
-                                               } else if ( 'readAsArrayBuffer' 
in binReader ) {
-                                                       
binReader.readAsArrayBuffer( upload.file );
-                                               } else {
-                                                       // We should never get 
here. :P
-                                                       throw new Error( 
'Cannot read thumbnail as binary string or array buffer.' );
-                                               }
-                                       } else {
-                                               if ( this.hasError === false ) {
-                                                       finishCallback();
-                                               }
+                                       if ( this.hasError === false ) {
+                                               finishCallback();
                                        }
 
                                } else {
@@ -551,11 +504,62 @@
        };
 
        /**
+        * Extract some JPEG metadata that we need to render thumbnails (EXIF 
rotation mostly).
+        *
+        * For JPEGs, we use the JsJpegMeta library in core to extract metadata,
+        * including EXIF tags. This is done asynchronously once each file has 
been
+        * read.
+        *
+        * For all other file types, we don't need or want to run this, and 
this function does nothing.
+        *
+        * @return {jQuery.Promise} A promise, resolved when we're done
+        */
+       mw.UploadWizardUpload.prototype.extractMetadataFromJpegMeta = function 
() {
+               var binReader,
+                       deferred = $.Deferred(),
+                       upload = this;
+               if ( this.file.type === 'image/jpeg' ) {
+                       binReader = new FileReader();
+                       binReader.onload = function () {
+                               var binStr, arr, i, meta;
+                               if ( typeof binReader.result === 'string' ) {
+                                       binStr = binReader.result;
+                               } else {
+                                       // Array buffer; convert to binary 
string for the library.
+                                       arr = new Uint8Array( binReader.result 
);
+                                       binStr = '';
+                                       for ( i = 0; i < arr.byteLength; i++ ) {
+                                               binStr += String.fromCharCode( 
arr[ i ] );
+                                       }
+                               }
+                               try {
+                                       meta = mw.libs.jpegmeta( binStr, 
upload.file.fileName );
+                                       // jscs:disable 
requireCamelCaseOrUpperCaseIdentifiers, disallowDanglingUnderscores
+                                       meta._binary_data = null;
+                                       // jscs:enable
+                               } catch ( e ) {
+                                       meta = null;
+                               }
+                               upload.extractMetadataFromJpegMetaCallback( 
meta );
+                               deferred.resolve();
+                       };
+                       if ( 'readAsBinaryString' in binReader ) {
+                               binReader.readAsBinaryString( upload.file );
+                       } else if ( 'readAsArrayBuffer' in binReader ) {
+                               binReader.readAsArrayBuffer( upload.file );
+                       }
+               } else {
+                       deferred.resolve();
+               }
+               return deferred.promise();
+       };
+
+       /**
         * Map fields from jpegmeta's metadata return into our format (which is 
more like the imageinfo returned from the API
         *
         * @param {Object} meta As returned by jpegmeta
         */
-       mw.UploadWizardUpload.prototype.extractMetadataFromJpegMeta = function 
( meta ) {
+       mw.UploadWizardUpload.prototype.extractMetadataFromJpegMetaCallback = 
function ( meta ) {
                var pixelHeightDim, pixelWidthDim, degrees;
 
                if ( meta !== undefined && meta !== null && typeof meta === 
'object' ) {
@@ -1061,7 +1065,8 @@
                        deferred.resolve( image );
                }
 
-               this.makePreview()
+               this.extractMetadataFromJpegMeta()
+                       .then( upload.makePreview.bind( upload ) )
                        .done( imageCallback )
                        .fail( function () {
                                // Can't generate the thumbnail locally, get 
the thumbnail via API after

-- 
To view, visit https://gerrit.wikimedia.org/r/277714
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ie9c0098074320a1fb9e63acab5672965f37a4700
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/UploadWizard
Gerrit-Branch: master
Gerrit-Owner: Bartosz DziewoƄski <[email protected]>
Gerrit-Reviewer: MarkTraceur <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to