Render thumbnails

This commit is contained in:
Savanni D'Gerinel 2023-09-20 23:31:52 -04:00
parent 634c404ae9
commit f05e0a15f1
3 changed files with 42 additions and 76 deletions

View File

@ -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!("<img src={path} />", path = self.path,)
}
}

View File

@ -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<RwLock<App>>,
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<TemplateFile>,
}
impl Handler for IndexHandler {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let app = self.app.read().unwrap();
let m_token = req.extensions.get::<Authentication>();
match m_token {
Some(token) => {
if token.check_authorizations(is_admin) {
let files: Vec<TemplateFile> = 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<RwLock<App>>,
}
@ -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;

View File

@ -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<Result<File, FileError>>) -> build_html::HtmlPage {
let mut page = build_html::HtmlPage::new()
@ -26,7 +26,7 @@ pub fn index(files: Vec<Result<File, FileError>>) -> build_html::HtmlPage {
.with_attributes(vec![("class", "thumbnail")])
.with_link(
format!("/file/{}", file.info().id),
"<p> paragraph within the link </p>".to_owned(),
Image::new(&format!("{}/tn", file.info().id)).to_html_string(),
);
container.add_html(tn);
}