From 767fe5bfd312684e21851edca2e166c04e3b78fc Mon Sep 17 00:00:00 2001 From: Ryo Nakano Date: Sat, 24 Aug 2024 00:19:16 +0900 Subject: [PATCH] Fix crash when image path contains encoded URI - Correct the way to convert a URI to a path - Do not try to convert to a path if a variable is already a path - Do not name 'uri' if a variable contains a path --- src/Application.vala | 6 +++--- src/MainWindow.vala | 16 ++++++++++------ src/Utils/Image.vala | 11 +++++++++++ 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/Application.vala b/src/Application.vala index e70a2a8..7755b9d 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -31,13 +31,13 @@ class Application : Granite.Application { public override void open (File[] files, string hint) { if (files [0].query_exists ()) { foreach (File file in files) { - var uri = file.get_path ().replace ("%20", " ").replace ("file://", ""); + var path = file.get_path (); - var name = Image.get_file_name (uri); + var name = Image.get_file_name (path); var type = Image.get_file_type (file.get_basename ()); if (Image.is_valid (type.down ())) { - this.images += new Image (uri, name, type.down ()); + this.images += new Image (path, name, type.down ()); } else { // TODO: add an error message here } diff --git a/src/MainWindow.vala b/src/MainWindow.vala index a222239..81df9a3 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -189,13 +189,17 @@ public class MainWindow : Gtk.Window { */ private void on_drag_data_received (Gdk.DragContext drag_context, int x, int y, Gtk.SelectionData data, uint info, uint time) { foreach (string uri in data.get_uris ()) { - uri = uri.replace ("%20", " "). replace ("file://", ""); + var path = Image.to_path (uri); + if (path == null) { + warning ("Failed to convert URI \"%s\" to path", uri); + continue; + } - var name = Image.get_file_name (uri); + var name = Image.get_file_name (path); var type = Image.get_file_type (name); if (Image.is_valid (type.down ())) { - this.images += new Image (uri, name, type.down ()); + this.images += new Image (path, name, type.down ()); } else { // TODO: add an error message here } @@ -226,12 +230,12 @@ public class MainWindow : Gtk.Window { file_chooser.select_multiple = true; if (file_chooser.run () == Gtk.ResponseType.ACCEPT) { - foreach (string uri in file_chooser.get_filenames ()) { - var name = Image.get_file_name (uri); + foreach (string path in file_chooser.get_filenames ()) { + var name = Image.get_file_name (path); var type = Image.get_file_type (name); if (Image.is_valid (type.down ())) { - this.images += new Image (uri, name, type.down ()); + this.images += new Image (path, name, type.down ()); } else { // TODO: add an error message here } diff --git a/src/Utils/Image.vala b/src/Utils/Image.vala index e596270..753fc23 100644 --- a/src/Utils/Image.vala +++ b/src/Utils/Image.vala @@ -62,6 +62,17 @@ public class Image { this.size = file_size; } + /** + * Convert a URI to a path. + * + * @param string uri URI e.g. "file:///home/user/Pictures/test_pr%C3%BCfen_%E3%83%86%E3%82%B9%E3%83%88_%E6%B5%8B%E8%AF%95.png" + * @return string? Path e.g. "/home/user/Pictures/prüfen_测试.png" or null if no such file exists + */ + public static string? to_path (string uri) { + var file = File.new_for_uri (uri); + return file.get_path (); + } + /** * Get file name from a path. *