[geary: 4/9] Improve passing of inline dropped file through to vala !343



commit fa430cac5d6d746058a81069649e82f232049ebe
Author: Chris Heywood <15127-creywood users noreply gitlab gnome org>
Date:   Sat Oct 26 19:27:54 2019 +0200

    Improve passing of inline dropped file through to vala !343

 src/client/composer/composer-web-view.vala | 51 +++++++++++++++---------------
 ui/composer-web-view.js                    | 13 ++++++--
 2 files changed, 35 insertions(+), 29 deletions(-)
---
diff --git a/src/client/composer/composer-web-view.vala b/src/client/composer/composer-web-view.vala
index 0df35c85..642e6a8e 100644
--- a/src/client/composer/composer-web-view.vala
+++ b/src/client/composer/composer-web-view.vala
@@ -529,37 +529,36 @@ public class ComposerWebView : ClientWebView {
      *  Handle a dropped image
      */
     private void on_drag_drop_received(WebKit.JavascriptResult result) {
-        string native_result;
-        try {
-            native_result = Util.JS.to_string(result.get_js_value());
-        } catch (Util.JS.Error err) {
-            warning("Failed to decode drag & drop data: %s", err.message);
-            return;
-        }
 
-        string[] pieces = native_result.split(",");
+        try {
+            JSC.Value object = result.get_js_value();
+            string filename = Util.JS.to_string(
+                Util.JS.get_property(object, "fileName")
+            );
+            string filename_unescaped = GLib.Uri.unescape_string(filename);
 
-        if (pieces.length != 4) {
-            warning("Invalid data received in drag & drop: %s", native_result);
-            return;
-        }
+            string file_type = Util.JS.to_string(
+                Util.JS.get_property(object, "fileType")
+            );
 
-        string filename = pieces[0];
-        string filename_unescaped = GLib.Uri.unescape_string(filename);
-        string file_type = pieces[1];
-        string content_base64 = pieces[3];
-        uint8[] image = GLib.Base64.decode(content_base64);
+            string content_base64 = Util.JS.to_string(
+                Util.JS.get_property(object, "content")
+            );
+            uint8[] image = GLib.Base64.decode(content_base64);
 
-        if (image.length == 0) {
-            warning("%s is empty", filename);
-            return;
-        }
+            if (image.length == 0) {
+                warning("%s is empty", filename);
+                return;
+            }
 
-        // A simple check to see if the file looks like an image. A problem here
-        // will be this accepting types which won't be supported by WebKit
-        // or recipients.
-        if (file_type.index_of("image/") == 0) {
-            image_file_dropped(filename_unescaped, file_type, image);
+            // A simple check to see if the file looks like an image. A problem here
+            // will be this accepting types which won't be supported by WebKit
+            // or recipients.
+            if (file_type.index_of("image/") == 0) {
+                image_file_dropped(filename_unescaped, file_type, image);
+            }
+        } catch (Util.JS.Error err) {
+            debug("Could not get deceptive link param: %s", err.message);
         }
     }
 }
diff --git a/ui/composer-web-view.js b/ui/composer-web-view.js
index 65b0c6c6..8de6e5f1 100644
--- a/ui/composer-web-view.js
+++ b/ui/composer-web-view.js
@@ -398,9 +398,16 @@ ComposerPageState.prototype = {
 
             const reader = new FileReader();
             reader.onload = (function(filename, imageType) { return function(loadEvent) {
-                window.webkit.messageHandlers.dragDropReceived.postMessage(
-                    encodeURIComponent(filename) + "," + imageType + "," + loadEvent.target.result
-                );
+                // Remove prefixed file type and encoding type
+                var parts = loadEvent.target.result.split(",");
+                if (parts.length < 2)
+                    return;
+
+                window.webkit.messageHandlers.dragDropReceived.postMessage({
+                    fileName: encodeURIComponent(filename),
+                    fileType: imageType,
+                    content: parts[1]
+                });
             }; })(file.name, file.type);
             reader.readAsDataURL(file);
         }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]