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]]
name = "datatrash"
version = "1.1.3"
version = "1.1.4"
dependencies = [
"actix-files",
"actix-multipart",

View File

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

View File

@ -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 {