Check url for extension during magic open

This commit is contained in:
Jonathan Turner 2019-06-09 07:24:12 +12:00
parent c65ca858cd
commit df6b5b3e68

View file

@ -12,13 +12,25 @@ pub fn open(args: CommandArgs) -> Result<OutputStream, ShellError> {
let cwd = args.env.lock().unwrap().cwd().to_path_buf(); let cwd = args.env.lock().unwrap().cwd().to_path_buf();
let mut full_path = PathBuf::from(cwd); let mut full_path = PathBuf::from(cwd);
let contents = match &args.positional[0].item { let (file_extension, contents) = match &args.positional[0].item {
Value::Primitive(Primitive::String(s)) => { Value::Primitive(Primitive::String(s)) => {
if s.starts_with("http:") || s.starts_with("https:") { if s.starts_with("http:") || s.starts_with("https:") {
let response = reqwest::get(s); let response = reqwest::get(s);
match response { match response {
Ok(mut r) => match r.text() { Ok(mut r) => match r.text() {
Ok(s) => s, Ok(s) => {
let fname = r
.url()
.path_segments()
.and_then(|segments| segments.last())
.and_then(|name| if name.is_empty() { None } else { Some(name) })
.and_then(|name| {
PathBuf::from(name)
.extension()
.map(|name| name.to_string_lossy().to_string())
});
(fname, s)
}
Err(_) => { Err(_) => {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"Web page contents corrupt", "Web page contents corrupt",
@ -38,7 +50,12 @@ pub fn open(args: CommandArgs) -> Result<OutputStream, ShellError> {
} else { } else {
full_path.push(Path::new(&s)); full_path.push(Path::new(&s));
match std::fs::read_to_string(&full_path) { match std::fs::read_to_string(&full_path) {
Ok(s) => s, Ok(s) => (
full_path
.extension()
.map(|name| name.to_string_lossy().to_string()),
s,
),
Err(_) => { Err(_) => {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"File cound not be opened", "File cound not be opened",
@ -75,7 +92,7 @@ pub fn open(args: CommandArgs) -> Result<OutputStream, ShellError> {
_ => false, _ => false,
}; };
match full_path.extension() { match file_extension {
Some(x) if x == "toml" && !open_raw => { Some(x) if x == "toml" && !open_raw => {
stream.push_back(ReturnValue::Value( stream.push_back(ReturnValue::Value(
crate::commands::from_toml::from_toml_string_to_value(contents), crate::commands::from_toml::from_toml_string_to_value(contents),