Skip to content

Commit

Permalink
Merge pull request #73 from ryonakano/fix-handle-path
Browse files Browse the repository at this point in the history
Fix crash when image path contains encoded URI
  • Loading branch information
GijsGoudzwaard authored Aug 23, 2024
2 parents 7ab3f77 + 767fe5b commit c08f95f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 10 additions & 6 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
11 changes: 11 additions & 0 deletions src/Utils/Image.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit c08f95f

Please sign in to comment.