diff --git a/file-service/src/html.rs b/file-service/src/html.rs index d83b34a..02cffd3 100644 --- a/file-service/src/html.rs +++ b/file-service/src/html.rs @@ -133,3 +133,22 @@ impl Html for Button { ) } } + +#[derive(Clone, Debug)] +pub struct Image { + path: String, +} + +impl Image { + pub fn new(path: &str) -> Self { + Self { + path: path.to_owned(), + } + } +} + +impl Html for Image { + fn to_html_string(&self) -> String { + format!("", path = self.path,) + } +} diff --git a/file-service/src/main.rs b/file-service/src/main.rs index addd532..f15d2a0 100644 --- a/file-service/src/main.rs +++ b/file-service/src/main.rs @@ -11,6 +11,7 @@ use http::status::StatusCode; // use orizentic::{Permissions, ResourceName, Secret}; use build_html::Html; use std::{ + io::Read, net::{IpAddr, Ipv4Addr, SocketAddr}, path::Path, sync::{Arc, RwLock}, @@ -45,71 +46,6 @@ pub fn compare_etags(info: FileInfo, etag_list: &headers::IfNoneMatch) -> bool { mod files { use super::*; - pub struct IndexHandler { - pub app: Arc>, - pub template: Template, - } - - #[derive(Serialize)] - pub enum TemplateFile { - #[serde(rename = "error")] - Error { error: String }, - #[serde(rename = "file")] - File { - id: String, - size: u64, - date: String, - type_: String, - }, - } - - #[derive(Serialize)] - pub struct IndexTemplateParams { - files: Vec, - } - - impl Handler for IndexHandler { - fn handle(&self, req: &mut Request) -> IronResult { - let app = self.app.read().unwrap(); - let m_token = req.extensions.get::(); - match m_token { - Some(token) => { - if token.check_authorizations(is_admin) { - let files: Vec = app - .list_files() - .into_iter() - .map(|entry| match entry { - Ok(file) => TemplateFile::File { - id: file.info().id, - size: file.info().size, - date: format!( - "{}", - file.info().created.format("%Y-%m-%d %H:%M:%S") - ), - type_: file.info().file_type, - }, - Err(err) => TemplateFile::Error { - error: format!("{}", err), - }, - }) - .collect(); - Ok(Response::with(( - status::Ok, - Header(headers::ContentType::html()), - Header(headers::SetCookie(vec![format!("auth={}", token.text)])), - self.template - .render_to_string(&IndexTemplateParams { files }) - .expect("the template to render"), - ))) - } else { - Ok(Response::with(status::Forbidden)) - } - } - None => Ok(Response::with(status::Forbidden)), - } - } - } - pub struct GetHandler { pub app: Arc>, } @@ -295,14 +231,6 @@ pub async fn main() { let auth_middleware = Authentication::new(secret, auth_db_path); let mut router = Router::new(); - router.get( - "/", - files::IndexHandler { - app: app.clone(), - template: compile_path("templates/index.html").expect("the template to compile"), - }, - "index", - ); router.get( "/:id", @@ -371,7 +299,26 @@ pub async fn main() { } }); - let server = warp::serve(root); + let thumbnail = warp::path!(String / "tn").map({ + let app = app.clone(); + move |id: String| { + let mut content = Vec::new(); + match app.read().unwrap().get_thumbnail(&id) { + Ok((info, mut stream)) => { + let _ = stream.read_to_end(&mut content); + warp::http::Response::builder() + .header("content-type", info.file_type) + .status(StatusCode::OK) + .body(content) + } + Err(_err) => warp::http::Response::builder() + .status(StatusCode::NOT_FOUND) + .body(content), + } + } + }); + + let server = warp::serve(root.or(thumbnail)); server .run(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 8002)) .await; diff --git a/file-service/src/pages.rs b/file-service/src/pages.rs index e1385df..3f61170 100644 --- a/file-service/src/pages.rs +++ b/file-service/src/pages.rs @@ -1,5 +1,5 @@ use crate::{html::*, File, FileError}; -use build_html::{self, Container, ContainerType, HtmlContainer}; +use build_html::{self, Container, ContainerType, Html, HtmlContainer}; pub fn index(files: Vec>) -> build_html::HtmlPage { let mut page = build_html::HtmlPage::new() @@ -26,7 +26,7 @@ pub fn index(files: Vec>) -> build_html::HtmlPage { .with_attributes(vec![("class", "thumbnail")]) .with_link( format!("/file/{}", file.info().id), - " paragraph within the link ".to_owned(), + Image::new(&format!("{}/tn", file.info().id)).to_html_string(), ); container.add_html(tn); }
paragraph within the link