From 63ca9b9bde494f4857589b9dea03d93f981c5d45 Mon Sep 17 00:00:00 2001 From: "Jakob L. Kreuze" Date: Mon, 25 May 2020 19:07:31 -0400 Subject: Implement `put_posts`. --- src/birka-cli.rs | 10 ---------- src/database.rs | 14 ++++++++++++-- src/main.rs | 20 ++++++++++++++++---- 3 files changed, 28 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/birka-cli.rs b/src/birka-cli.rs index d2caba3..1306c93 100644 --- a/src/birka-cli.rs +++ b/src/birka-cli.rs @@ -22,20 +22,10 @@ extern crate dirs; mod database; use anyhow::Result; -use blake2::{Blake2b, Digest}; use database::TagDatabase; use std::env; -use std::io; use std::path::Path; -/// Return a base64-encoded BLAKE2b hash identifying the file at `path`. -fn hash_file(path: &Path) -> Result { - let mut file = std::fs::File::open(path)?; - let mut hasher = Blake2b::new(); - io::copy(&mut file, &mut hasher)?; - Ok(base64::encode(&hasher.result())) -} - fn main() { let args: Vec = env::args().collect(); if args.len() < 2 { diff --git a/src/database.rs b/src/database.rs index 7b69a21..db33117 100644 --- a/src/database.rs +++ b/src/database.rs @@ -16,7 +16,9 @@ // License along with бирка-тян. If not, see . use anyhow::Result; +use blake2::{Blake2b, Digest}; use rusqlite::{params, Connection}; +use std::io; use std::path::Path; /// A connection to бирка-тян's tag database. @@ -185,8 +187,8 @@ impl TagDatabase { let (n, id) = pair; format!( "INNER JOIN mapping as m{n} - ON images.id = m{n}.image - AND {id} = m{n}.tag", + ON images.id = m{n}.image + AND {id} = m{n}.tag", n = n, id = id ) @@ -263,6 +265,14 @@ pub struct Image { pub orig_dir: String, } +/// Return a base64-encoded BLAKE2b hash identifying the file at `path`. +pub fn hash_file(path: &Path) -> Result { + let mut file = std::fs::File::open(path)?; + let mut hasher = Blake2b::new(); + io::copy(&mut file, &mut hasher)?; + Ok(base64::encode(&hasher.result())) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/main.rs b/src/main.rs index a4f5946..4294e06 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,9 +29,9 @@ extern crate serde_derive; mod database; -use database::{Query, TagDatabase}; +use database::{hash_file, Query, TagDatabase}; use image::{GenericImageView, ImageFormat}; -use rocket::State; +use rocket::{Data, State}; use rocket_contrib::json::{Json, JsonValue}; use std::env; use std::fs; @@ -52,7 +52,7 @@ struct ImageResult { } #[get("/posts?")] -fn posts(tb: State>, tags: String) -> Json> { +fn get_posts(tb: State>, tags: String) -> Json> { let tb = tb.inner().lock().unwrap(); let tags = tags.split(",").collect::>(); @@ -75,6 +75,18 @@ fn posts(tb: State>, tags: String) -> Json> ) } +#[post("/posts/", data = "")] +fn put_posts(tb: State>, filename: String, data: Data) -> Json<&str> { + let tb = tb.inner().lock().unwrap(); + + let bin = env::current_exe().unwrap(); + let path = bin.parent().unwrap().join("image/").join(filename); + data.stream_to_file(&path).unwrap(); + tb.import_image(&path, &hash_file(&path).unwrap()).unwrap(); + + Json("uploaded!") +} + /// Maybe initialize the directory for storing image symlinks and thumbnails. fn create_image_directory() -> TagDatabase { let bin = env::current_exe().unwrap(); @@ -109,6 +121,6 @@ fn main() { let tb = create_image_directory(); rocket::ignite() .manage(Mutex::new(tb)) - .mount("/", routes![index, posts]) + .mount("/", routes![index, get_posts]) .launch(); } -- cgit v1.3