Browse Source

Fix #557: Enhance default file type parsing to intelligently not render unpreviewable content

Kartik Visweswaran 9 years ago
parent
commit
64af9e6f58
3 changed files with 28 additions and 24 deletions
  1. 2 1
      CHANGE.md
  2. 26 23
      js/fileinput.js
  3. 0 0
      js/fileinput.min.js

+ 2 - 1
CHANGE.md

@@ -3,9 +3,10 @@ Change Log: `bootstrap-fileinput`
 
 
 ## version 4.3.1
 ## version 4.3.1
 
 
-**Date:** 28-Jan-2016
+**Date:** 29-Jan-2016
 
 
 1. (enh #555): Set default value for `removeFromPreviewOnError` to `false`.
 1. (enh #555): Set default value for `removeFromPreviewOnError` to `false`.
+2. (enh #557): Enhance default file type parsing to intelligently not render unpreviewable content.
 
 
 ## version 4.3.0
 ## version 4.3.0
 
 

+ 26 - 23
js/fileinput.js

@@ -34,14 +34,19 @@
 
 
     $.fn.fileinputLocales = {};
     $.fn.fileinputLocales = {};
 
 
-    var isIE, isEdge, handler, previewCache, getNum, hasFileAPISupport, hasDragDropSupport, hasFileUploadSupport, addCss,
-        STYLE_SETTING, OBJECT_PARAMS, DEFAULT_PREVIEW, defaultFileActionSettings, tMain1, tMain2, tPreview, tIcon, tClose,
-        tCaption, tBtnDefault, tBtnLink, tBtnBrowse, tModal, tProgress, tFooter, tActions, tActionDelete, tActionUpload,
-        tZoom, tGeneric, tHtml, tImage, tText, tVideo, tAudio, tFlash, tObject, tOther, defaultLayoutTemplates,
-        defaultPreviewTemplates, defaultPreviewTypes, defaultPreviewSettings, defaultFileTypeSettings, isEmpty, isArray,
-        isSet, getElement, uniqId, htmlEncode, replaceTags, objUrl, FileInput, NAMESPACE;
+    var NAMESPACE, objUrl, compare, isIE, isEdge, handler, previewCache, getNum, hasFileAPISupport, hasDragDropSupport,
+        hasFileUploadSupport, addCss, STYLE_SETTING, OBJECT_PARAMS, DEFAULT_PREVIEW, defaultFileActionSettings, tMain1,
+        tMain2, tPreview, tIcon, tClose, tCaption, tBtnDefault, tBtnLink, tBtnBrowse, tModal, tProgress, tFooter,
+        tActions, tActionDelete, tActionUpload, tZoom, tGeneric, tHtml, tImage, tText, tVideo, tAudio, tFlash, tObject,
+        tOther, defaultLayoutTemplates, defaultPreviewTemplates, defaultPreviewTypes, defaultPreviewSettings,
+        defaultFileTypeSettings, isEmpty, isArray, isSet, getElement, uniqId, htmlEncode, replaceTags, FileInput;
 
 
     NAMESPACE = '.fileinput';
     NAMESPACE = '.fileinput';
+    //noinspection JSUnresolvedVariable
+    objUrl = window.URL || window.webkitURL;
+    compare = function (input, str, exact) {
+        return input !== undefined && (exact ? input === str : input.match(str));
+    };
     isIE = function (ver) {
     isIE = function (ver) {
         // check for IE versions < 11
         // check for IE versions < 11
         if (navigator.appName !== 'Microsoft Internet Explorer') {
         if (navigator.appName !== 'Microsoft Internet Explorer') {
@@ -455,27 +460,28 @@
     };
     };
     defaultFileTypeSettings = {
     defaultFileTypeSettings = {
         image: function (vType, vName) {
         image: function (vType, vName) {
-            return (vType !== undefined) ? vType.match('image.*') : vName.match(/\.(gif|png|jpe?g)$/i);
+            return compare(vType, 'image.*') || compare(vName, /\.(gif|png|jpe?g)$/i);
         },
         },
         html: function (vType, vName) {
         html: function (vType, vName) {
-            return (vType !== undefined) ? vType === 'text/html' : vName.match(/\.(htm|html)$/i);
+            return compare(vType, 'text/html') || compare(vName, /\.(htm|html)$/i);
         },
         },
         text: function (vType, vName) {
         text: function (vType, vName) {
-            return (vType !== undefined && vType.match('text.*')) || vName.match(
-                    /\.(txt|md|csv|nfo|ini|json|php|js|css)$/i);
+            return compare(vType, 'text.*') || compare(vType, /\.(xml|javascript)$/i) ||
+                compare(vName, /\.(txt|md|csv|nfo|ini|json|php|js|css)$/i);
         },
         },
         video: function (vType, vName) {
         video: function (vType, vName) {
-            return (vType !== undefined && vType.match(/\.video\/(ogg|mp4|webm|3gp)$/i)) || vName.match(
-                    /\.(og?|mp4|webm|3gp)$/i);
+            return compare(vType, 'video.*') && (compare(vType, /(ogg|mp4|mp?g|webm|3gp)$/i) ||
+                compare(vName, /\.(og?|mp4|webm|mp?g|3gp)$/i));
         },
         },
         audio: function (vType, vName) {
         audio: function (vType, vName) {
-            return (vType !== undefined && vType.match(/\.audio\/(ogg|mp3|wav)$/i)) || vName.match(/\.(ogg|mp3|wav)$/i);
+            return compare(vType, 'audio.*') && (compare(vType, /(ogg|mp3|mp?g|wav)$/i) ||
+                compare(vName, /\.(og?|mp3|mp?g|wav)$/i));
         },
         },
         flash: function (vType, vName) {
         flash: function (vType, vName) {
-            return (vType !== undefined && vType === 'application/x-shockwave-flash') || vName.match(/\.(swf)$/i);
+            return compare(vType, 'application/x-shockwave-flash', true) || compare(vName, /\.(swf)$/i);
         },
         },
-        object: function () {
-            return true;
+        object: function (vType, vName) {
+            return compare(vType, 'application/pdf', true) || compare(vName, /\.(pdf)$/i);
         },
         },
         other: function () {
         other: function () {
             return true;
             return true;
@@ -516,8 +522,6 @@
         });
         });
         return out;
         return out;
     };
     };
-    //noinspection JSUnresolvedVariable
-    objUrl = window.URL || window.webkitURL;
     FileInput = function (element, options) {
     FileInput = function (element, options) {
         var self = this;
         var self = this;
         self.$element = $(element);
         self.$element = $(element);
@@ -2108,7 +2112,7 @@
                     }
                     }
                 }
                 }
                 if (fileCount === 0 && !isEmpty(fileExt) && isArray(fileExt) && !isEmpty(fileExtExpr)) {
                 if (fileCount === 0 && !isEmpty(fileExt) && isArray(fileExt) && !isEmpty(fileExtExpr)) {
-                    chk = caption.match(fileExtExpr);
+                    chk = compare(caption, fileExtExpr);
                     fileCount += isEmpty(chk) ? 0 : chk.length;
                     fileCount += isEmpty(chk) ? 0 : chk.length;
                     if (fileCount === 0) {
                     if (fileCount === 0) {
                         msg = self.msgInvalidFileExtension.replace('{name}', caption).replace('{extensions}',
                         msg = self.msgInvalidFileExtension.replace('{name}', caption).replace('{extensions}',
@@ -2262,8 +2266,7 @@
             }
             }
             self.resetErrors();
             self.resetErrors();
             len = tfiles.length;
             len = tfiles.length;
-            total = self.isUploadable ? self.getFileStack().length + len : len;
-            total = self.getFileCount(total);
+            total = self.getFileCount(self.isUploadable ? (self.getFileStack().length + len) : len);
             if (self.maxFileCount > 0 && total > self.maxFileCount) {
             if (self.maxFileCount > 0 && total > self.maxFileCount) {
                 if (!self.autoReplace || len > self.maxFileCount) {
                 if (!self.autoReplace || len > self.maxFileCount) {
                     n = (self.autoReplace && len > self.maxFileCount) ? len : total;
                     n = (self.autoReplace && len > self.maxFileCount) ? len : total;
@@ -2338,8 +2341,8 @@
             }
             }
         },
         },
         checkDimensions: function (i, chk, $img, $thumb, fname, type, params) {
         checkDimensions: function (i, chk, $img, $thumb, fname, type, params) {
-            var self = this, msg, dim, tag = chk === 'Small' ? 'min' : 'max',
-                limit = self[tag + 'Image' + type], $imgEl, isValid;
+            var self = this, msg, dim, tag = chk === 'Small' ? 'min' : 'max', limit = self[tag + 'Image' + type],
+                $imgEl, isValid;
             if (isEmpty(limit) || !$img.length) {
             if (isEmpty(limit) || !$img.length) {
                 return;
                 return;
             }
             }

File diff suppressed because it is too large
+ 0 - 0
js/fileinput.min.js


Some files were not shown because too many files changed in this diff