summaryrefslogtreecommitdiffstats
path: root/src/util
diff options
context:
space:
mode:
authorJeff Zhao <jeff.no.zhao@gmail.com>2022-09-11 09:50:09 -0400
committerJeff Zhao <jeff.no.zhao@gmail.com>2022-09-11 09:51:01 -0400
commitf117ddaccb996942774d9efee9c5807fe7272ded (patch)
tree21a0958d9502a149064ee99a2827c49d3d7294e2 /src/util
parent1908ff94b7e5f981a4254ca697056a299b2e6b51 (diff)
add mimetype support via file command
Diffstat (limited to 'src/util')
-rw-r--r--src/util/mimetype.rs57
-rw-r--r--src/util/mod.rs1
-rw-r--r--src/util/process.rs6
3 files changed, 61 insertions, 3 deletions
diff --git a/src/util/mimetype.rs b/src/util/mimetype.rs
new file mode 100644
index 0000000..423cb2f
--- /dev/null
+++ b/src/util/mimetype.rs
@@ -0,0 +1,57 @@
+use std::path::Path;
+use std::{io, process::Command};
+
+use crate::error::{JoshutoError, JoshutoErrorKind, JoshutoResult};
+
+pub struct Mimetype {
+ _type: String,
+ _subtype: String,
+}
+
+impl Mimetype {
+ pub fn new(ttype: String, subtype: String) -> Self {
+ Self {
+ _type: ttype,
+ _subtype: subtype,
+ }
+ }
+
+ pub fn get_type(&self) -> &str {
+ &self._type
+ }
+
+ pub fn get_subtype(&self) -> &str {
+ &&self._subtype
+ }
+}
+
+pub fn get_mimetype(p: &Path) -> JoshutoResult<Mimetype> {
+ let res = Command::new("file")
+ .arg("--mime-type")
+ .arg("-Lb")
+ .arg(p)
+ .output();
+
+ let output = res?;
+ if !output.status.success() {
+ let stderr_msg = String::from_utf8_lossy(&output.stderr).to_string();
+
+ let error = JoshutoError::new(
+ JoshutoErrorKind::Io(io::ErrorKind::InvalidInput),
+ stderr_msg,
+ );
+ return Err(error);
+ }
+
+ let stdout_msg = String::from_utf8_lossy(&output.stdout).to_string();
+ match stdout_msg.trim().split_once('/') {
+ Some((ttype, subtype)) => Ok(Mimetype::new(ttype.to_string(), subtype.to_string())),
+ None => {
+ let error = JoshutoError::new(
+ JoshutoErrorKind::Io(io::ErrorKind::InvalidInput),
+ "Unknown mimetype".to_string(),
+ );
+ return Err(error);
+ }
+ }
+}
diff --git a/src/util/mod.rs b/src/util/mod.rs
index c7ad758..562ec57 100644
--- a/src/util/mod.rs
+++ b/src/util/mod.rs
@@ -3,6 +3,7 @@ pub mod devicons;
pub mod format;
pub mod keyparse;
+pub mod mimetype;
pub mod name_resolution;
pub mod process;
pub mod search;
diff --git a/src/util/process.rs b/src/util/process.rs
index 4bf34fa..fba1cff 100644
--- a/src/util/process.rs
+++ b/src/util/process.rs
@@ -3,11 +3,11 @@ use std::process;
use std::sync::mpsc;
use std::thread;
-use crate::config::AppMimetypeEntry;
+use crate::config::ProgramEntry;
use crate::event::AppEvent;
pub fn fork_execute<I, S>(
- entry: &AppMimetypeEntry,
+ entry: &ProgramEntry,
paths: I,
event_tx: mpsc::Sender<AppEvent>,
) -> std::io::Result<(u32, thread::JoinHandle<()>)>
@@ -38,7 +38,7 @@ where
Ok((child_id, handle))
}
-pub fn execute_and_wait<I, S>(entry: &AppMimetypeEntry, paths: I) -> std::io::Result<()>
+pub fn execute_and_wait<I, S>(entry: &ProgramEntry, paths: I) -> std::io::Result<()>
where
I: IntoIterator<Item = S>,
S: AsRef<std::ffi::OsStr>,