diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/src/main.rs b/src/main.rs index 07cf4d6..94e9804 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,9 @@ -extern crate rusqlite; +#[macro_use] +extern crate anyhow; +use anyhow::Result; use blake2::{Blake2b, Digest}; -use rusqlite::{params, Connection, Result}; +use rusqlite::{params, Connection}; use std::io; use std::path::Path; @@ -48,29 +50,31 @@ impl TagBase { Ok(TagBase { conn }) } - // TODO: Return a proper error. + /// Insert the image at `path` into the data. fn import_image(&self, path: &Path) -> Result<()> { if path.is_dir() { - return Err(rusqlite::Error::InvalidQuery); + return Err(anyhow!("`path` does not name a file.")); } - let mut file = std::fs::File::open(path).unwrap(); + let mut file = std::fs::File::open(path)?; let mut hasher = Blake2b::new(); - let n = io::copy(&mut file, &mut hasher).unwrap(); - let hash = hexlify(&hasher.result()); + let _ = io::copy(&mut file, &mut hasher)?; + let hash = base64::encode(&hasher.result()); - let parsed_or_none = path.file_name().and_then(|os| os.to_str()); - if let (Some(path), Some(file_name)) = (path.to_str(), parsed_or_none) { - let split_index = path.len() - file_name.len(); - let parent_directory = &path[0..split_index]; - self.conn.execute( - "INSERT INTO images (sha256, filename, orig_dir) VALUES(?,?,?)", - params![0, file_name, parent_directory], - )?; - Ok(()) - } else { - Err(rusqlite::Error::InvalidQuery) - } + let file_name = path + .file_name() + .and_then(|os| os.to_str()) + .ok_or(anyhow!("Couldn't parse file name."))?; + let path = path.to_str().ok_or(anyhow!("Couldn't parse path."))?; + + let split_index = path.len() - file_name.len(); + let parent_directory = &path[0..split_index]; + self.conn.execute( + "INSERT INTO images (blake2, filename, orig_dir) VALUES(?,?,?)", + params![hash, file_name, parent_directory], + )?; + + Ok(()) } } |