From b414fda39aa9c021341a1efba0910b5922783967 Mon Sep 17 00:00:00 2001 From: neri Date: Sun, 3 Jul 2022 01:02:27 +0200 Subject: [PATCH] make template filling code prettier --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/template.rs | 77 +++++++++++++++++++------------------------------ 3 files changed, 32 insertions(+), 49 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 77969d0..2da76e6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -403,7 +403,7 @@ dependencies = [ [[package]] name = "datatrash" -version = "1.1.3" +version = "1.1.4" dependencies = [ "actix-files", "actix-multipart", diff --git a/Cargo.toml b/Cargo.toml index f63ce95..c9857ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "datatrash" -version = "1.1.3" +version = "1.1.4" authors = ["neri"] edition = "2021" diff --git a/src/template.rs b/src/template.rs index c14d9f4..7982029 100644 --- a/src/template.rs +++ b/src/template.rs @@ -8,19 +8,17 @@ use crate::config::Config; const INDEX_HTML: &str = include_str!("../template/index.html"); const AUTH_HIDE_JS: &str = include_str!("../template/auth-hide.js"); -const AUTH_SNIPPET_HTML: &str = include_str!("../snippet/auth.html.snippet").trim_end(); -const MAX_SIZE_SNIPPET_HTML: &str = include_str!("../snippet/max_size.html.snippet").trim_end; +const AUTH_SNIPPET_HTML: &str = include_str!("../snippet/auth.html.snippet"); +const MAX_SIZE_SNIPPET_HTML: &str = include_str!("../snippet/max_size.html.snippet"); pub async fn write_prefillable_templates(config: &Config) { - let index_html = build_index_html(config); - let auth_hide_js = build_auth_hide_js(config); let index_path = config.static_dir.join("index.html"); - let auth_hide_path = config.static_dir.join("auth-hide.js"); - - fs::write(index_path, index_html) + fs::write(index_path, build_index_html(config)) .await .expect("could not write index.html to static folder"); - if let Some(auth_hide_js) = auth_hide_js { + + let auth_hide_path = config.static_dir.join("auth-hide.js"); + if let Some(auth_hide_js) = build_auth_hide_js(config) { fs::write(auth_hide_path, auth_hide_js) .await .expect("could not write auth-hide.js to static folder"); @@ -33,45 +31,30 @@ pub async fn write_prefillable_templates(config: &Config) { } fn build_index_html(config: &Config) -> String { - let auth_snippet = config - .no_auth_limits - .as_ref() - .map_or("", |_| AUTH_SNIPPET_HTML); - let max_size_snippet = config - .max_file_size - .as_ref() - .map_or("", |_| MAX_SIZE_SNIPPET_HTML); - INDEX_HTML - .replace("{max_size_snippet}", max_size_snippet) - .replace( - "{max_size}", - &render_file_size(config.max_file_size.unwrap_or(0)), - ) - .replace("{auth_snippet}", auth_snippet) - .replace( - "{auth_time}", - &config - .no_auth_limits - .as_ref() - .map(|limit| limit.max_time) - .map_or("".into(), render_duration), - ) - .replace( - "{auth_large_time}", - &config - .no_auth_limits - .as_ref() - .map(|limit| limit.large_file_max_time) - .map_or("".into(), render_duration), - ) - .replace( - "{auth_large_size}", - &config - .no_auth_limits - .as_ref() - .map(|limit| limit.large_file_size) - .map_or("".into(), render_file_size), - ) + let mut html = INDEX_HTML.to_owned(); + if let Some(limit) = config.no_auth_limits.as_ref() { + html = html + .replace("{auth_snippet}", AUTH_SNIPPET_HTML.trim_end()) + .replace("{auth_time}", &render_duration(limit.max_time)) + .replace( + "{auth_large_time}", + &render_duration(limit.large_file_max_time), + ) + .replace( + "{auth_large_size}", + &render_file_size(limit.large_file_size), + ); + } else { + html = html.replace("{auth_snippet}", ""); + } + if let Some(max_file_size) = config.max_file_size { + html = html + .replace("{max_size_snippet}", MAX_SIZE_SNIPPET_HTML.trim_end()) + .replace("{max_size}", &render_file_size(max_file_size)); + } else { + html = html.replace("{max_size_snippet}", MAX_SIZE_SNIPPET_HTML.trim_end()) + }; + html } fn render_file_size(size: u64) -> String {