Procházet zdrojové kódy

Refactor file-extension checking into utility functions

JC Brand před 6 roky
rodič
revize
82239d281f
4 změnil soubory, kde provedl 98 přidání a 45 odebrání
  1. 2 0
      CHANGES.md
  2. 51 18
      dist/converse.js
  3. 1 1
      package-lock.json
  4. 44 26
      src/utils/core.js

+ 2 - 0
CHANGES.md

@@ -5,6 +5,7 @@
 - Bugfix. Handler not triggered when submitting MUC password form 2nd time
 - Bugfix. MUC features weren't being refreshed when saving the config form
 - Don't show duplicate notification messages
+- New config setting [show_images_inline](https://conversejs.org/docs/html/configuration.html#show-images-inline)
 - #537 Render `xmpp:` URI as link
 - #1062 Collapse multiple join/leave messages into one
 - #1063 URLs in the topic / subject are not clickable
@@ -17,6 +18,7 @@
 - #1214 Setting `allow_contact_requests` to `false` has no effect
 - #1221 Avoid creating a headlines box if we don't have anything to show inside it
 - #1222 Adding a bookmark should prefill the room name
+- #1228 Converse automatically visits links (to try and determine whether they're images to show inline)
 
 ## 4.0.2 (2018-10-02)
 

+ 51 - 18
dist/converse.js

@@ -81839,30 +81839,60 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
   };
 
   u.renderFileURL = function (_converse, url) {
-    const uri = new URI(url),
-          filename = uri.filename(),
-          lower_filename = filename.toLowerCase();
+    const uri = new URI(url);
 
-    if (!_.includes(["https", "http"], uri.protocol().toLowerCase()) || lower_filename.endsWith('mp3') || lower_filename.endsWith('mp4') || lower_filename.endsWith('ogg') || lower_filename.endsWith('jpg') || lower_filename.endsWith('jpeg') || lower_filename.endsWith('png') || lower_filename.endsWith('gif') || lower_filename.endsWith('m4a') || lower_filename.endsWith('webm') || lower_filename.endsWith('svg')) {
+    if (u.isImageURL(uri) || u.isVideoURL(uri) || u.isAudioURL(uri)) {
       return url;
     }
 
-    const __ = _converse.__;
+    const __ = _converse.__,
+          filename = uri.filename();
     return tpl_file({
       'url': url,
       'label_download': __('Download file "%1$s"', decodeURI(filename))
     });
   };
 
+  u.isAudioURL = function (url) {
+    if (!(url instanceof URI)) {
+      url = new URI(url);
+    }
+
+    const filename = url.filename().toLowerCase();
+
+    if (!_.includes(["https", "http"], url.protocol().toLowerCase())) {
+      return false;
+    }
+
+    return filename.endsWith('.ogg') || filename.endsWith('.mp3') || filename.endsWith('.m4a');
+  };
+
+  u.isVideoURL = function (url) {
+    if (!(url instanceof URI)) {
+      url = new URI(url);
+    }
+
+    const filename = url.filename().toLowerCase();
+
+    if (!_.includes(["https", "http"], url.protocol().toLowerCase())) {
+      return false;
+    }
+
+    return filename.endsWith('.mp4') || filename.endsWith('.webm');
+  };
+
   u.isImageURL = function (url) {
-    const uri = new URI(url),
-          filename = uri.filename().toLowerCase();
+    if (!(url instanceof URI)) {
+      url = new URI(url);
+    }
+
+    const filename = url.filename().toLowerCase();
 
-    if (!_.includes(["https", "http"], uri.protocol().toLowerCase())) {
+    if (!_.includes(["https", "http"], url.protocol().toLowerCase())) {
       return false;
     }
 
-    return filename.endsWith('jpg') || filename.endsWith('jpeg') || filename.endsWith('png') || filename.endsWith('gif') || filename.endsWith('svg');
+    return filename.endsWith('.jpg') || filename.endsWith('.jpeg') || filename.endsWith('.png') || filename.endsWith('.gif') || filename.endsWith('.bmp') || filename.endsWith('.tiff') || filename.endsWith('.svg');
   };
 
   u.renderImageURL = function (_converse, url) {
@@ -81870,9 +81900,10 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
       return u.addHyperlinks(url);
     }
 
-    if (u.isImageURL(url)) {
-      const __ = _converse.__,
-            uri = new URI(url);
+    const uri = new URI(url);
+
+    if (u.isImageURL(uri)) {
+      const __ = _converse.__;
       return tpl_image({
         'url': url,
         'label_download': __('Download image "%1$s"', decodeURI(uri.filename()))
@@ -81883,9 +81914,10 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
   };
 
   u.renderMovieURL = function (_converse, url) {
-    if (url.endsWith('mp4') || url.endsWith('webm')) {
-      const __ = _converse.__,
-            uri = new URI(url);
+    const uri = new URI(url);
+
+    if (u.isVideoURL(uri)) {
+      const __ = _converse.__;
       return tpl_video({
         'url': url,
         'label_download': __('Download video file "%1$s"', decodeURI(uri.filename()))
@@ -81896,9 +81928,10 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
   };
 
   u.renderAudioURL = function (_converse, url) {
-    if (url.endsWith('mp3') || url.endsWith('m4a') || url.endsWith('ogg')) {
-      const __ = _converse.__,
-            uri = new URI(url);
+    const uri = new URI(url);
+
+    if (u.isAudioURL(uri)) {
+      const __ = _converse.__;
       return tpl_audio({
         'url': url,
         'label_download': __('Download audio file "%1$s"', decodeURI(uri.filename()))

+ 1 - 1
package-lock.json

@@ -2252,7 +2252,7 @@
       }
     },
     "backbone.browserStorage": {
-      "version": "github:jcbrand/Backbone.browserStorage#7079bf7bf9a43474da1d48e31e3cda6c4a716382",
+      "version": "github:jcbrand/Backbone.browserStorage#78e4a58f7cee10cad6af4fd5f3213331a396aa5a",
       "dev": true,
       "requires": {
         "backbone": "1.3.3",

+ 44 - 26
src/utils/core.js

@@ -318,43 +318,61 @@
     };
 
     u.renderFileURL = function (_converse, url) {
-        const uri = new URI(url),
-              filename = uri.filename(),
-              lower_filename = filename.toLowerCase();
-        if (!_.includes(["https", "http"], uri.protocol().toLowerCase()) ||
-            lower_filename.endsWith('mp3') || lower_filename.endsWith('mp4') || lower_filename.endsWith('ogg') ||
-            lower_filename.endsWith('jpg') || lower_filename.endsWith('jpeg') ||
-            lower_filename.endsWith('png') || lower_filename.endsWith('gif') ||
-            lower_filename.endsWith('m4a') || lower_filename.endsWith('webm') ||
-            lower_filename.endsWith('svg')) {
-
+        const uri = new URI(url);
+        if (u.isImageURL(uri) || u.isVideoURL(uri) || u.isAudioURL(uri)) {
             return url;
         }
-        const { __ } = _converse;
+        const { __ } = _converse,
+              filename = uri.filename();
         return tpl_file({
             'url': url,
             'label_download': __('Download file "%1$s"', decodeURI(filename))
         })
     };
 
+    u.isAudioURL = function (url) {
+        if (!(url instanceof URI)) {
+            url = new URI(url);
+        }
+        const filename = url.filename().toLowerCase();
+        if (!_.includes(["https", "http"], url.protocol().toLowerCase())) {
+            return false;
+        }
+        return filename.endsWith('.ogg') || filename.endsWith('.mp3') || filename.endsWith('.m4a');
+    }
+
+    u.isVideoURL = function (url) {
+        if (!(url instanceof URI)) {
+            url = new URI(url);
+        }
+        const filename = url.filename().toLowerCase();
+        if (!_.includes(["https", "http"], url.protocol().toLowerCase())) {
+            return false;
+        }
+        return filename.endsWith('.mp4') || filename.endsWith('.webm');
+    }
+
     u.isImageURL = function (url) {
-        const uri = new URI(url),
-              filename = uri.filename().toLowerCase();
-        if (!_.includes(["https", "http"], uri.protocol().toLowerCase())) {
+        if (!(url instanceof URI)) {
+            url = new URI(url);
+        }
+        const filename = url.filename().toLowerCase();
+        if (!_.includes(["https", "http"], url.protocol().toLowerCase())) {
             return false;
         }
-        return filename.endsWith('jpg') || filename.endsWith('jpeg') ||
-               filename.endsWith('png') || filename.endsWith('gif') ||
-               filename.endsWith('svg');
+        return filename.endsWith('.jpg') || filename.endsWith('.jpeg') ||
+               filename.endsWith('.png') || filename.endsWith('.gif') ||
+               filename.endsWith('.bmp') || filename.endsWith('.tiff') ||
+               filename.endsWith('.svg');
     };
 
     u.renderImageURL = function (_converse, url) {
         if (!_converse.show_images_inline) {
             return u.addHyperlinks(url);
         }
-        if (u.isImageURL(url)) {
-            const { __ } = _converse,
-                  uri = new URI(url);
+        const uri = new URI(url);
+        if (u.isImageURL(uri)) {
+            const { __ } = _converse;
             return tpl_image({
                 'url': url,
                 'label_download': __('Download image "%1$s"', decodeURI(uri.filename()))
@@ -364,9 +382,9 @@
     };
 
     u.renderMovieURL = function (_converse, url) {
-        if (url.endsWith('mp4') || url.endsWith('webm')) {
-            const { __ } = _converse,
-                  uri = new URI(url);
+        const uri = new URI(url);
+        if (u.isVideoURL(uri)) {
+            const { __ } = _converse;
             return tpl_video({
                 'url': url,
                 'label_download': __('Download video file "%1$s"', decodeURI(uri.filename()))
@@ -376,9 +394,9 @@
     };
 
     u.renderAudioURL = function (_converse, url) {
-        if (url.endsWith('mp3') || url.endsWith('m4a') || url.endsWith('ogg')) {
-            const { __ } = _converse,
-                  uri = new URI(url);
+        const uri = new URI(url);
+        if (u.isAudioURL(uri)) {
+            const { __ } = _converse;
             return tpl_audio({
                 'url': url,
                 'label_download': __('Download audio file "%1$s"', decodeURI(uri.filename()))