From 433c5fa1c56bee5a9032380a3c9a339869a47d16 Mon Sep 17 00:00:00 2001 From: "Jakob L. Kreuze" Date: Tue, 26 May 2020 19:46:20 -0400 Subject: Implement initial templating. --- src/main.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index f82f07b..6eb939f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,6 +33,9 @@ use database::{hash_file, Query, TagDatabase}; use image::ImageFormat; use rocket::{Data, State}; use rocket_contrib::json::Json; +use rocket_contrib::serve::StaticFiles; +use rocket_contrib::templates::Template; +use std::collections::HashMap; use std::env; use std::fs; use std::fs::File; @@ -59,13 +62,48 @@ impl DatabaseConnection { } #[get("/")] -fn index(tb: SiteState) -> &'static str { - "Hello, world!" +fn index() -> Template { + let context = HashMap::<&str, &str>::new(); + Template::render("index", &context) +} + +#[get("/posts?")] +fn display_posts(conn: SiteState, tags: String) -> Template { + #[derive(Serialize)] + struct TagResult { + name: String, + count: i64, + } + + #[derive(Serialize)] + struct Asdf { + tags: Vec, + images: Vec, + } + + let tag_results = { + let tb = &conn.inner().lock().unwrap().tb; + tb.top_tags(20) + .unwrap() + .iter() + .map(|tup| TagResult { + name: tup.1.clone(), + count: tup.0, + }) + .collect() + }; + let images = get_posts(conn, tags).into_inner(); + let context = Asdf { + tags: tag_results, + images, + }; + Template::render("index", context) } #[derive(Serialize)] struct ImageResult { filename: String, + thumb_filename: String, tags: Vec, } @@ -87,6 +125,7 @@ fn get_posts(conn: SiteState, tags: String) -> Json> { .iter() .map(|image| ImageResult { filename: image.filename.clone(), + thumb_filename: thumb_filename(&image.filename), tags: tb.tags_for_image(image.id).unwrap(), }) .collect(), @@ -105,6 +144,18 @@ fn put_posts(conn: SiteState, filename: String, data: Data) -> Json<&str> { Json("uploaded!") } +fn thumb_filename(filename: &str) -> String { + String::from(format!( + "{}_thumb.png", + // Strip extension. + if let Some(n) = filename.rfind('.') { + &filename[..n] + } else { + &filename[..] + } + )) +} + /// Maybe initialize the directory for storing image symlinks and thumbnails. fn create_image_directory(conn: &DatabaseConnection) { fs::create_dir(&conn.image_dir).ok(); @@ -115,7 +166,7 @@ fn create_image_directory(conn: &DatabaseConnection) { symlink(&src, &dst).ok(); let stem = Path::new(&dst).file_stem().unwrap().to_str().unwrap(); - let thumb_path = format!("{}_thumb.png", stem); + let thumb_path = format!("{}/{}", conn.image_dir, thumb_filename(&stem)); let thumb_path = Path::new(&thumb_path); if !thumb_path.exists() { @@ -130,9 +181,14 @@ fn create_image_directory(conn: &DatabaseConnection) { fn main() { let conn = DatabaseConnection::new().unwrap(); + let image_dir = conn.image_dir.clone(); create_image_directory(&conn); rocket::ignite() + .attach(Template::fairing()) .manage(Mutex::new(conn)) - .mount("/", routes![index, get_posts]) + .mount("/public", StaticFiles::from("./static/")) + .mount("/image", StaticFiles::from(image_dir)) + .mount("/", routes![index, display_posts]) + .mount("/api", routes![get_posts, put_posts]) .launch(); } -- cgit v1.3