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

Change subject: Provide file extension fallback for FileTransferHandlers
......................................................................


Provide file extension fallback for FileTransferHandlers

In some browser/OS combinations, common files produce no file type
property in the upload API. In those cases look at the file extension
and match on that instead.

Bug: T96218
Change-Id: I61501a311e8b64b5be8d3dc5e10f5b83b7ddec68
---
M src/ui/datatransferhandlers/ve.ui.DSVFileTransferHandler.js
M src/ui/datatransferhandlers/ve.ui.HTMLFileTransferHandler.js
M src/ui/datatransferhandlers/ve.ui.PlainTextFileTransferHandler.js
M src/ui/ve.ui.DataTransferHandlerFactory.js
M src/ui/ve.ui.DataTransferItem.js
M src/ui/ve.ui.FileTransferHandler.js
6 files changed, 48 insertions(+), 7 deletions(-)

Approvals:
  Catrope: Looks good to me, approved
  Jforrester: Looks good to me, but someone else must approve
  jenkins-bot: Verified



diff --git a/src/ui/datatransferhandlers/ve.ui.DSVFileTransferHandler.js 
b/src/ui/datatransferhandlers/ve.ui.DSVFileTransferHandler.js
index 35d7de9..5baa3e2 100644
--- a/src/ui/datatransferhandlers/ve.ui.DSVFileTransferHandler.js
+++ b/src/ui/datatransferhandlers/ve.ui.DSVFileTransferHandler.js
@@ -29,6 +29,8 @@
 
 ve.ui.DSVFileTransferHandler.static.types = [ 'text/csv', 
'text/tab-separated-values' ];
 
+ve.ui.DSVFileTransferHandler.static.extensions = [ 'csv', 'tsv' ];
+
 /* Methods */
 
 /**
diff --git a/src/ui/datatransferhandlers/ve.ui.HTMLFileTransferHandler.js 
b/src/ui/datatransferhandlers/ve.ui.HTMLFileTransferHandler.js
index ce72992..04a0761 100644
--- a/src/ui/datatransferhandlers/ve.ui.HTMLFileTransferHandler.js
+++ b/src/ui/datatransferhandlers/ve.ui.HTMLFileTransferHandler.js
@@ -29,6 +29,8 @@
 
 ve.ui.HTMLFileTransferHandler.static.types = [ 'text/html', 
'application/xhtml+xml' ];
 
+ve.ui.HTMLFileTransferHandler.static.extensions = [ 'html', 'htm', 'xhtml' ];
+
 /* Methods */
 
 /**
diff --git a/src/ui/datatransferhandlers/ve.ui.PlainTextFileTransferHandler.js 
b/src/ui/datatransferhandlers/ve.ui.PlainTextFileTransferHandler.js
index f47fa2a..6771ac8 100644
--- a/src/ui/datatransferhandlers/ve.ui.PlainTextFileTransferHandler.js
+++ b/src/ui/datatransferhandlers/ve.ui.PlainTextFileTransferHandler.js
@@ -27,7 +27,9 @@
 
 ve.ui.PlainTextFileTransferHandler.static.name = 'plainTextFile';
 
-ve.ui.PlainTextFileTransferHandler.static.types = ['text/plain'];
+ve.ui.PlainTextFileTransferHandler.static.types = [ 'text/plain' ];
+
+ve.ui.PlainTextFileTransferHandler.static.extension = [ 'txt' ];
 
 /* Methods */
 
diff --git a/src/ui/ve.ui.DataTransferHandlerFactory.js 
b/src/ui/ve.ui.DataTransferHandlerFactory.js
index 7d4ec73..2e9d8ad 100644
--- a/src/ui/ve.ui.DataTransferHandlerFactory.js
+++ b/src/ui/ve.ui.DataTransferHandlerFactory.js
@@ -19,6 +19,8 @@
        this.handlerNamesByType = {};
        // Handlers which match a specific kind and type
        this.handlerNamesByKindAndType = {};
+       // Handlers which match a specific file extension as a fallback
+       this.handlerNamesByExtension = {};
 };
 
 /* Inheritance */
@@ -36,7 +38,8 @@
 
        var i, j, ilen, jlen,
                kinds = constructor.static.kinds,
-               types = constructor.static.types;
+               types = constructor.static.types,
+               extensions = constructor.static.extensions;
 
        if ( !kinds ) {
                for ( j = 0, jlen = types.length; j < jlen; j++ ) {
@@ -50,6 +53,11 @@
                        }
                }
        }
+       if ( constructor.prototype instanceof ve.ui.FileTransferHandler ) {
+               for ( i = 0, ilen = extensions.length; i < ilen; i++ ) {
+                       this.handlerNamesByExtension[extensions[i]] = 
constructor.static.name;
+               }
+       }
 };
 
 /**
@@ -61,8 +69,13 @@
  */
 ve.ui.DataTransferHandlerFactory.prototype.getHandlerNameForItem = function ( 
item, isPaste ) {
        var constructor,
-               name = ( this.handlerNamesByKindAndType[item.kind] && 
this.handlerNamesByKindAndType[item.kind][item.type] ) ||
-               this.handlerNamesByType[item.type];
+               name =
+                       // 1. Match by kind + type (e.g. 'file' + 'text/html')
+                       ( this.handlerNamesByKindAndType[item.kind] && 
this.handlerNamesByKindAndType[item.kind][item.type] ) ||
+                       // 2. Match by just type (e.g. 'image/jpeg')
+                       this.handlerNamesByType[item.type] ||
+                       // 3. Match by file extension (e.g. 'csv')
+                       this.handlerNamesByExtension[item.getExtension()];
 
        if ( !name ) {
                return;
diff --git a/src/ui/ve.ui.DataTransferItem.js b/src/ui/ve.ui.DataTransferItem.js
index 4b3cadb..0c85d6e 100644
--- a/src/ui/ve.ui.DataTransferItem.js
+++ b/src/ui/ve.ui.DataTransferItem.js
@@ -10,13 +10,15 @@
  * @param {Blob} [data.blob] File blob
  * @param {string} [data.stringData] String data
  * @param {DataTransferItem} [data.item] Native data transfer item
+ * @param {string} [name] Item's name, for types which support it, e.g. File
  */
-ve.ui.DataTransferItem = function VeUiDataTransferItem( kind, type, data ) {
+ve.ui.DataTransferItem = function VeUiDataTransferItem( kind, type, data, name 
) {
        this.kind = kind;
        this.type = type;
        this.data = data;
        this.blob = this.data.blob || null;
        this.stringData = this.data.stringData || ve.getProp( this.blob, 'name' 
) || null;
+       this.name = name;
 };
 
 /* Inheritance */
@@ -32,7 +34,7 @@
  * @return {ve.ui.DataTransferItem} New data transfer item
  */
 ve.ui.DataTransferItem.static.newFromBlob = function ( blob ) {
-       return new ve.ui.DataTransferItem( 'file', blob.type, { blob: blob } );
+       return new ve.ui.DataTransferItem( 'file', blob.type, { blob: blob }, 
blob.name );
 };
 
 /**
@@ -64,7 +66,7 @@
  * @return {ve.ui.DataTransferItem} New data transfer item
  */
 ve.ui.DataTransferItem.static.newFromItem = function ( item ) {
-       return new ve.ui.DataTransferItem( item.kind, item.type, { item: item } 
);
+       return new ve.ui.DataTransferItem( item.kind, item.type, { item: item 
}, item.getAsFile().name );
 };
 
 /**
@@ -97,6 +99,15 @@
 };
 
 /**
+ * Get the extension of the item's name
+ *
+ * @return {string|null} The extension of the item's name, or null if not 
present
+ */
+ve.ui.DataTransferItem.prototype.getExtension = function () {
+       return this.name ? this.name.split( '.' ).pop() : null;
+};
+
+/**
  * Get string data
  *
  * Differs from native DataTransferItem#getAsString by being synchronous
diff --git a/src/ui/ve.ui.FileTransferHandler.js 
b/src/ui/ve.ui.FileTransferHandler.js
index aa5564e..6e952a7 100644
--- a/src/ui/ve.ui.FileTransferHandler.js
+++ b/src/ui/ve.ui.FileTransferHandler.js
@@ -41,6 +41,17 @@
 
 ve.ui.FileTransferHandler.static.kinds = [ 'file' ];
 
+/**
+ * List of file extensions supported by this handler
+ *
+ * This is used as a fallback if no types were matched.
+ *
+ * @static
+ * @property {string[]}
+ * @inheritable
+ */
+ve.ui.FileTransferHandler.static.extensions = [];
+
 /* Methods */
 
 /**

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I61501a311e8b64b5be8d3dc5e10f5b83b7ddec68
Gerrit-PatchSet: 2
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Esanders <[email protected]>
Gerrit-Reviewer: Catrope <[email protected]>
Gerrit-Reviewer: Jforrester <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to