Fix handling of file:// paths

This commit is contained in:
Gregory 2021-04-25 00:00:19 -04:00
parent 8e2b4b5b3b
commit b4ebda37e0
No known key found for this signature in database
GPG key ID: 2E44FAEEDC94B1E2
2 changed files with 16 additions and 6 deletions

View file

@ -2,7 +2,7 @@ use crate::{Error, Result};
use mime::Mime;
use std::{
convert::TryFrom,
path::{Path, PathBuf},
path::{Path},
str::FromStr,
};
@ -30,14 +30,14 @@ impl TryFrom<&str> for MimeType {
fn try_from(arg: &str) -> Result<Self> {
match url::Url::parse(arg) {
Ok(url) if url.scheme() == "file" => {
Self::try_from(&*PathBuf::from(url.path()))
Self::try_from(Path::new(url.path()))
}
Ok(url) => Ok(Self(
format!("x-scheme-handler/{}", url.scheme())
.parse::<Mime>()
.unwrap(),
)),
Err(_) => Self::try_from(&*PathBuf::from(arg)),
Err(_) => Self::try_from(Path::new(arg)),
}
}
}

View file

@ -39,13 +39,23 @@ fn main() -> Result<()> {
Cmd::Open { paths } => {
let mut handlers: HashMap<Handler, Vec<String>> =
HashMap::new();
for path in paths.into_iter() {
let path = match url::Url::parse(&path) {
Ok(url) if url.scheme() == "file" => {
url.path().to_owned()
}
_ => path,
};
let mime = MimeType::try_from(path.as_str())?.0;
let handler = apps.get_handler(&mime)?;
handlers.entry(handler).or_insert(Vec::new()).push(path);
handlers.entry(handler).or_default().push(path);
}
for (handler, handled_paths) in handlers.into_iter() {
handler.open(handled_paths)?;
for (handler, paths) in handlers.into_iter() {
handler.open(paths)?;
}
}
Cmd::List { all } => {