summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock14
-rw-r--r--Cargo.toml2
-rw-r--r--src/main.rs42
3 files changed, 39 insertions, 19 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 4e7ac4a..5bb4da3 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1,6 +1,18 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
+name = "anyhow"
+version = "1.0.31"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "85bb70cc08ec97ca5450e6eba421deeea5f172c0fc61f78b5357b2a8e8be195f"
+
+[[package]]
+name = "base64"
+version = "0.12.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "53d1ccbaf7d9ec9537465a97bf19edc1a4e158ecb49fc16178202238c569cc42"
+
+[[package]]
name = "bitflags"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -117,6 +129,8 @@ checksum = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677"
name = "roru"
version = "0.1.0"
dependencies = [
+ "anyhow",
+ "base64",
"blake2",
"rusqlite",
]
diff --git a/Cargo.toml b/Cargo.toml
index 99c4e70..fac672e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,6 +7,8 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+anyhow = "1.0.31"
+base64 = "0.12.1"
blake2 = "0.8"
[dependencies.rusqlite]
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(())
}
}