kitab

Unnamed repository; edit this file 'description' to name the repository.
Info | Log | Files | Refs | LICENSE

commit 7837a38bdba270b7adbd2bc171f22864f6c619c5
parent 5da4623992d55e2e5356da183985b39754a2986c
Author: lash <dev@holbrook.no>
Date:   Sun, 26 Jun 2022 07:37:32 +0000

Add magic mime type feature

Diffstat:
MCargo.toml | 24++++++++++++++++--------
Msrc/lib.rs | 8+++++++-
Msrc/meta.rs | 44+++++++++++++++++++++++++++++++++++++++++---
Msrc/rdf.rs | 2--
4 files changed, 64 insertions(+), 14 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml @@ -10,8 +10,8 @@ edition = "2021" xattr = "0.2.2" #inotify = "0.8.0" regex = "1.5.5" -rio_turtle = "~0.7.1" -rio_api = "~0.7.1" +#rio_turtle = "~0.7.1" +#rio_api = "~0.7.1" hex = "^0.4" mime = "^0.3.13" unic-langid-impl = "^0.9.0" @@ -25,11 +25,19 @@ urn = "^0.4.0" [dev-dependencies] tempfile = "^3.3.0" -#[dependencies.rdf] -#rio_turtle = "~0.7.1" -#rio_api = "~0.7.1" -#optional = true +[dependencies.rio_turtle] +version = "~0.7.1" +optional = true + +[dependencies.rio_api] +version = "^0.7.1" +optional = true + +[dependencies.tree_magic] +version = "^0.2.1" +optional = true -#[features] -#dump_rdf = ["rdf"] +[features] +rdf = ["rio_turtle", "rio_api"] #dump_bibtex = ["biblatex"] +magic = ["tree_magic"] diff --git a/src/lib.rs b/src/lib.rs @@ -4,9 +4,15 @@ pub mod meta; pub mod dc; -//#[cfg(feature = "dump_rdf")] +#[cfg(feature = "rdf")] pub mod rdf; #[cfg(test)] mod tests { + use env_logger; + + #[test] + fn test_setup_env_logger() { + env_logger::init(); + } } diff --git a/src/meta.rs b/src/meta.rs @@ -24,6 +24,9 @@ use biblatex::EntryType; use std::str::FromStr; use std::os::linux::fs::MetadataExt; +#[cfg(feature = "magic")] +use tree_magic; + use crate::dc::{ DCMetaData, DC_XATTR_TITLE, @@ -260,6 +263,9 @@ impl MetaData { _ => {}, } + #[cfg(feature = "magic")] + metadata.set_mime_magic(filepath); + metadata } @@ -343,6 +349,21 @@ impl MetaData { } } + #[cfg(feature = "magic")] + pub fn set_mime_magic(&mut self, path: &path::Path) { + if self.mime() == None { + let mime = tree_magic::from_filepath(path); + self.set_mime_str(&mime); + info!("magic set mime {}", mime); + } + } + + pub fn from_path(p: &path::Path) -> Result<MetaData, std::io::Error> { + let f = File::open(&p).unwrap(); + let mut m = MetaData::from_file(f).unwrap(); + Ok(m) + } + pub fn from_file(f: File) -> Result<MetaData, std::io::Error> { let mut m = MetaData::empty(); //let f = File::open(path).unwrap(); @@ -381,7 +402,10 @@ mod tests { use std::path; use tempfile::NamedTempFile; use biblatex::EntryType; - use std::fs::File; + use std::fs::{ + File, + write + }; use env_logger; #[test] @@ -420,8 +444,6 @@ mod tests { #[test] fn test_metadata_file() { - env_logger::init(); - let f = File::open("testdata/meta.txt").unwrap(); let m_check = MetaData::from_file(f).unwrap(); assert_eq!(m_check.title(), "foo"); @@ -431,4 +453,20 @@ mod tests { assert_eq!(m_check.mime().unwrap(), "text/plain"); assert_eq!(m_check.language().unwrap(), "nb-NO"); } + + #[test] + fn test_metadata_xattr_magic() { + let s = path::Path::new("testdata/bitcoin.pdf"); + let meta = MetaData::from_xattr(s); + + #[cfg(feature = "magic")] + { + assert_eq!(meta.mime().unwrap(), "application/pdf"); + let f = NamedTempFile::new_in(".").unwrap(); + let fp = f.path(); + write(&f, &[0, 1, 2, 3]); + let meta_empty = MetaData::from_xattr(fp); + assert_eq!(meta_empty.mime().unwrap(), "application/octet-stream"); + } + } } diff --git a/src/rdf.rs b/src/rdf.rs @@ -213,8 +213,6 @@ mod tests { #[test] fn test_turtle_read() { - env_logger::init(); - let f = File::open("testdata/meta.ttl").unwrap(); read(&f); }