Skip to content

Commit

Permalink
Correctly open uris containing spaces (#777)
Browse files Browse the repository at this point in the history
* Do not escape uri when sanitizing

* Fix dropping possibly escaped and/or quoted uris

* Handle ShellError when unquoting

* Amend comments

---------

Co-authored-by: Jeremy Wootten <jeremy@Proteus-EL07R6-9b3c42bb.localdomain>
Co-authored-by: Ryo Nakano <ryonakaknock3@gmail.com>
  • Loading branch information
3 people authored Nov 4, 2024
1 parent f537340 commit b946a89
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
3 changes: 1 addition & 2 deletions src/Utils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ namespace Terminal.Utils {
public string? sanitize_path (string _path, string shell_location, bool add_file_scheme = true) {
/* Remove trailing whitespace, ensure scheme, substitute leading "~" and "..", remove extraneous "/" */
string scheme = "", path = "";

var parts_scheme = _path.split ("://", 2);
if (parts_scheme.length == 2) {
scheme = parts_scheme[0] + "://";
Expand Down Expand Up @@ -63,7 +62,7 @@ namespace Terminal.Utils {
parts_sep[index] = construct_parent_path (shell_location);
}

var result = escape_uri (scheme + string.joinv (Path.DIR_SEPARATOR_S, parts_sep).replace ("//", "/"));
var result = scheme + string.joinv (Path.DIR_SEPARATOR_S, parts_sep).replace ("//", "/");
return result;
}

Expand Down
24 changes: 19 additions & 5 deletions src/Widgets/TerminalWidget.vala
Original file line number Diff line number Diff line change
Expand Up @@ -776,21 +776,35 @@ namespace Terminal {
var uris = selection_data.get_uris ();
string path;
File file;

for (var i = 0; i < uris.length; i++) {
file = File.new_for_uri (uris[i]);
if ((path = file.get_path ()) != null) {
uris[i] = Shell.quote (path) + " ";
// Get unquoted path as some apps may drop uris that are escaped
// and quoted.
string? unquoted_uri;
try {
unquoted_uri = Shell.unquote (uris[i]);
} catch (Error e) {
warning ("Error unquoting %s. %s", uris[i], e.message);
unquoted_uri = uris[i];
}
// Sanitize the path as we do not want the `file://` scheme included
// and we assume dropped paths are absolute.
file = File.new_for_uri (Utils.sanitize_path (unquoted_uri, "", false));
path = file.get_path ();
if (path != null) {
uris[i] = Shell.quote (path) + " ";
} else {
// Ignore unvalid paths
uris[i] = "";
}
}

var uris_s = string.joinv ("", uris);
this.feed_child (uris_s.data);
break;

case DropTargets.STRING:
case DropTargets.TEXT:
var data = selection_data.get_text ();

if (data != null) {
this.feed_child (data.data);
}
Expand Down

0 comments on commit b946a89

Please sign in to comment.