make template filling code prettier

This commit is contained in:
neri 2022-07-03 01:02:27 +02:00
parent 53c568082d
commit b414fda39a
3 changed files with 32 additions and 49 deletions

2
Cargo.lock generated
View File

@ -403,7 +403,7 @@ dependencies = [
[[package]] [[package]]
name = "datatrash" name = "datatrash"
version = "1.1.3" version = "1.1.4"
dependencies = [ dependencies = [
"actix-files", "actix-files",
"actix-multipart", "actix-multipart",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "datatrash" name = "datatrash"
version = "1.1.3" version = "1.1.4"
authors = ["neri"] authors = ["neri"]
edition = "2021" edition = "2021"

View File

@ -8,19 +8,17 @@ use crate::config::Config;
const INDEX_HTML: &str = include_str!("../template/index.html"); const INDEX_HTML: &str = include_str!("../template/index.html");
const AUTH_HIDE_JS: &str = include_str!("../template/auth-hide.js"); 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 AUTH_SNIPPET_HTML: &str = include_str!("../snippet/auth.html.snippet");
const MAX_SIZE_SNIPPET_HTML: &str = include_str!("../snippet/max_size.html.snippet").trim_end; const MAX_SIZE_SNIPPET_HTML: &str = include_str!("../snippet/max_size.html.snippet");
pub async fn write_prefillable_templates(config: &Config) { 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 index_path = config.static_dir.join("index.html");
let auth_hide_path = config.static_dir.join("auth-hide.js"); fs::write(index_path, build_index_html(config))
fs::write(index_path, index_html)
.await .await
.expect("could not write index.html to static folder"); .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) fs::write(auth_hide_path, auth_hide_js)
.await .await
.expect("could not write auth-hide.js to static folder"); .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 { fn build_index_html(config: &Config) -> String {
let auth_snippet = config let mut html = INDEX_HTML.to_owned();
.no_auth_limits if let Some(limit) = config.no_auth_limits.as_ref() {
.as_ref() html = html
.map_or("", |_| AUTH_SNIPPET_HTML); .replace("{auth_snippet}", AUTH_SNIPPET_HTML.trim_end())
let max_size_snippet = config .replace("{auth_time}", &render_duration(limit.max_time))
.max_file_size .replace(
.as_ref() "{auth_large_time}",
.map_or("", |_| MAX_SIZE_SNIPPET_HTML); &render_duration(limit.large_file_max_time),
INDEX_HTML )
.replace("{max_size_snippet}", max_size_snippet) .replace(
.replace( "{auth_large_size}",
"{max_size}", &render_file_size(limit.large_file_size),
&render_file_size(config.max_file_size.unwrap_or(0)), );
) } else {
.replace("{auth_snippet}", auth_snippet) html = html.replace("{auth_snippet}", "");
.replace( }
"{auth_time}", if let Some(max_file_size) = config.max_file_size {
&config html = html
.no_auth_limits .replace("{max_size_snippet}", MAX_SIZE_SNIPPET_HTML.trim_end())
.as_ref() .replace("{max_size}", &render_file_size(max_file_size));
.map(|limit| limit.max_time) } else {
.map_or("".into(), render_duration), html = html.replace("{max_size_snippet}", MAX_SIZE_SNIPPET_HTML.trim_end())
) };
.replace( html
"{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),
)
} }
fn render_file_size(size: u64) -> String { fn render_file_size(size: u64) -> String {