Browse Source

Add query_mime_info()

master
Manos Pitsidianakis 2 years ago
parent
commit
23ce2e2ac9
Signed by: epilys GPG Key ID: 73627C2F690DF710
  1. 29
      src/utilities.rs

29
src/utilities.rs

@ -4,6 +4,7 @@ use std::fs;
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::str;
/// Returns the path of a binary that is the default application of MIME type `query`
@ -266,3 +267,31 @@ fn desktop_file_to_binary(
Err(())
}
/** Returns the MIME type of given file
** https://cgit.freedesktop.org/xdg/xdg-utils/tree/scripts/xdg-mime.in */
pub fn query_mime_info<T: AsRef<str>>(query: T) -> Result<Vec<u8>, std::io::Error> {
let mut command_obj = Command::new("mimetype")
.args(&["--brief", "--dereference", query.as_ref()])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn();
if command_obj.is_err() {
command_obj = Command::new("file")
.args(&["--brief", "--dereference", "--mime-type", query.as_ref()])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn();
}
Ok(drop_right_whitespace(
command_obj?.wait_with_output()?.stdout,
))
}
fn drop_right_whitespace(mut vec: Vec<u8>) -> Vec<u8> {
while vec.last() == Some(&b'\n') {
vec.pop();
}
vec
}