summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJakob L. Kreuze <zerodaysfordays@sdf.org>2020-05-25 19:07:31 -0400
committerJakob L. Kreuze <zerodaysfordays@sdf.org>2020-05-25 19:07:31 -0400
commit63ca9b9bde494f4857589b9dea03d93f981c5d45 (patch)
treefe384847c495bc9be0d2f428731062c39dcd4e25 /src
parent899f517859fe2d2981ec5214892c1af966358c6a (diff)
Implement `put_posts`.
Diffstat (limited to 'src')
-rw-r--r--src/birka-cli.rs10
-rw-r--r--src/database.rs14
-rw-r--r--src/main.rs20
3 files changed, 28 insertions, 16 deletions
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<String> {
- 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<String> = 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 <http://www.gnu.org/licenses/>.
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<String> {
+ 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?<tags>")]
-fn posts(tb: State<Mutex<TagDatabase>>, tags: String) -> Json<Vec<ImageResult>> {
+fn get_posts(tb: State<Mutex<TagDatabase>>, tags: String) -> Json<Vec<ImageResult>> {
let tb = tb.inner().lock().unwrap();
let tags = tags.split(",").collect::<Vec<&str>>();
@@ -75,6 +75,18 @@ fn posts(tb: State<Mutex<TagDatabase>>, tags: String) -> Json<Vec<ImageResult>>
)
}
+#[post("/posts/<filename>", data = "<data>")]
+fn put_posts(tb: State<Mutex<TagDatabase>>, 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();
}