diff --git a/backend/src/thumbnailer.rs b/backend/src/thumbnailer.rs new file mode 100644 index 0000000..baf49a7 --- /dev/null +++ b/backend/src/thumbnailer.rs @@ -0,0 +1,40 @@ +use std::collections::HashSet; +use std::fs; +use std::fs::File; +use std::io::{BufRead, BufReader}; +use std::path::Path; +use anyhow::{bail, Result}; + +#[derive(Debug)] +pub struct Thumbnailer { + try_exec: String, + exec: String, + mime_type: HashSet +} + +impl Thumbnailer { + /// Load an XDG thumbnailer (examples in /usr/share/thumbnailers) + pub fn load(p: &Path) -> Result { + let mut content = fs::read_to_string(p)?; + content = content.replace("\r\n", "\n"); + let lines = content.split("\n"); + + let mut t = Thumbnailer { + try_exec: "".to_string(), exec: "".to_string(), mime_type: HashSet::new() + }; + + lines.filter(|line| line.contains("=")) + .for_each(|line| { + let sp: Vec<&str> = line.splitn(2, "=").collect(); + let (key, val) = (sp[0].trim(), sp[1].trim().to_string()); + match key { + "TryExec" => t.try_exec = val, + "Exec" => t.exec = val, + "MimeType" => t.mime_type = HashSet::from_iter(val.split(";").map(str::to_string)), + _ => {}, + } + }); + + Ok(t) + } +}