From 4f47efbb27645bc1a298310489adbb19ba7051ed Mon Sep 17 00:00:00 2001 From: G2-Games Date: Wed, 23 Oct 2024 04:28:57 -0500 Subject: [PATCH] Improve duration handling, remove CSS for now --- src/main.rs | 45 +++++++++++++++++-------------- src/settings.rs | 41 +++++++++++++++++++++++++--- src/static/main.css | 62 ------------------------------------------- src/static/request.js | 14 +++++----- 4 files changed, 68 insertions(+), 94 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4596d0a..d14f0ff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,27 +42,27 @@ fn form_handler_js() -> RawJavaScript<&'static str> { fn home() -> Markup { html! { (head("Mochi")) - body { - main { - section class="centered" { - form id="uploadForm" { - label for="fileUpload" class="file-upload" onclick="document.getElementById('fileInput').click()" { - "Upload File" - } - input id="fileInput" type="file" name="fileUpload" onchange="formSubmit(this.parentNode)" style="display:none;"; - br; - input type="text" name="duration" minlength="2" maxlength="4"; - } - div class="progress-box" { - progress id="uploadProgress" value="0" max="100" {} - p id="uploadProgressValue" class="progress-value" { "0%" } - } - } - section class="centered" id="uploadedFilesDisplay" { - h2 class="sep center" { "Uploaded Files" } - } + div id="durationOptions" { + + } + + form id="uploadForm" { + label for="fileUpload" class="file-upload" onclick="document.getElementById('fileInput').click()" { + "Upload File" } + input id="fileInput" type="file" name="fileUpload" onchange="formSubmit(this.parentNode)" style="display:none;"; + br; + input type="text" name="duration" minlength="2" maxlength="4"; + } + + div class="progress-box" { + progress id="uploadProgress" value="0" max="100" {} + p id="uploadProgressValue" class="progress-value" { "" } + } + + div id="uploadedFilesDisplay" { + h2 class="sep center" { "Uploaded Files" } } } } @@ -178,7 +178,9 @@ async fn hash_file>(input: &P) -> Result<(Hash, usize), std::io:: fn server_info(settings: &State) -> Json { Json(ServerInfo { max_filesize: settings.max_filesize, - max_duration: settings.max_duration, + max_duration: settings.duration.maximum, + default_duration: settings.duration.default, + allowed_durations: settings.duration.allowed.clone(), }) } @@ -187,6 +189,9 @@ fn server_info(settings: &State) -> Json { struct ServerInfo { max_filesize: u64, max_duration: u32, + default_duration: u32, + #[serde(skip_serializing_if = "Vec::is_empty")] + allowed_durations: Vec, } #[rocket::main] diff --git a/src/settings.rs b/src/settings.rs index 5105a93..46891b9 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -7,20 +7,26 @@ use rocket::serde::{Deserialize, Serialize}; #[serde(crate = "rocket::serde")] pub struct Settings { /// Maximum filesize in bytes + #[serde(default)] pub max_filesize: u64, - /// Maximum file lifetime, seconds - pub max_duration: u32, + /// Settings pertaining to duration information + pub duration: DurationSettings, /// The path to the root directory of the program, ex `/filehost/` + #[serde(default)] pub root_path: String, /// The path to the database file + #[serde(default)] pub database_path: PathBuf, /// Temporary directory for stuff + #[serde(default)] pub temp_dir: PathBuf, + /// Settings pertaining to the server configuration + #[serde(default)] pub server: ServerSettings, #[serde(skip)] @@ -30,8 +36,8 @@ pub struct Settings { impl Default for Settings { fn default() -> Self { Self { - max_filesize: 128_000_000, // 128MB - max_duration: 86_400, // 1 day + max_filesize: 128_000_000, // 128 MB + duration: DurationSettings::default(), root_path: "/".into(), server: ServerSettings::default(), path: "./settings.toml".into(), @@ -88,3 +94,30 @@ impl Default for ServerSettings { } } } + +#[derive(Deserialize, Serialize, Debug)] +#[serde(crate = "rocket::serde")] +pub struct DurationSettings { + /// Maximum file lifetime, seconds + #[serde(default)] + pub maximum: u32, + + /// Default file lifetime, seconds + #[serde(default)] + pub default: u32, + + /// List of allowed durations. An empty list means any are allowed. + #[serde(default)] + pub allowed: Vec, +} + +impl Default for DurationSettings { + fn default() -> Self { + Self { + maximum: 259_200, // 72 hours + default: 21_600, // 6 hours + // 1 hour, 6 hours, 24 hours, and 48 hours + allowed: vec![3600, 21_600, 86_400, 172_800], + } + } +} diff --git a/src/static/main.css b/src/static/main.css index 858278a..2b19564 100644 --- a/src/static/main.css +++ b/src/static/main.css @@ -1,64 +1,2 @@ @import url('https://g2games.dev/assets/fonts/fonts.css'); -.main-wrapper { - margin: auto; -} - -.file-upload { - padding: 20px; - position: relative; - display: block; - font-size: 16pt; - font-weight: bold; - - background-color: #fff098; - color: black; - width: fit-content; - border-radius: 13px; - border: 2px solid grey; - - margin: 10px auto; - cursor: pointer; -} - -progress[value] { - border-radius: 5px; - -webkit-appearance: none; - appearance: none; - background-color: #fffde6; - width: 100%; - height: 100%; -} - -::-webkit-progress-bar { - border-radius: 5px; - background-color: #fffde6; -} - -progress[value]::-moz-progress-bar { - border-radius: 5px; - background-color: #a6e3a1; -} - -::-webkit-progress-value { - background-color: #a6e3a1; - border-radius: 5px; -} - -.progress-box { - display: flex; - position: relative; - max-width: 500px; - width: 80vw; - height: 20px; - margin: 10px auto; - - .progress-value { - width: fit-content; - position: absolute; - left: 50%; - top: 50%; - translate: -50% -50%; - color: black; - } -} diff --git a/src/static/request.js b/src/static/request.js index dc96421..2d0228d 100644 --- a/src/static/request.js +++ b/src/static/request.js @@ -8,8 +8,7 @@ let uploadInProgress = false; const TOO_LARGE_TEXT = "File is too large!"; const ERROR_TEXT = "An error occured!"; -let MAX_FILESIZE; -let MAX_DURATION; +let CAPABILITIES; async function formSubmit(form) { if (uploadInProgress) { @@ -19,9 +18,9 @@ async function formSubmit(form) { // Get file size and don't upload if it's too large let file_upload = document.getElementById("fileInput"); let file = file_upload.files[0]; - if (file.size > MAX_FILESIZE) { + if (file.size > CAPABILITIES.max_filesize) { progressValue.textContent = TOO_LARGE_TEXT; - console.error("Provided file is too large", file.size, "bytes; max", MAX_FILESIZE, "bytes"); + console.error("Provided file is too large", file.size, "bytes; max", CAPABILITIES.max_filesize, "bytes"); return; } @@ -42,7 +41,7 @@ async function formSubmit(form) { } // Reset the form data since we've successfully submitted it - form.reset(); + form.elements["fileUpload"].value = ""; } function networkErrorHandler(_err) { @@ -90,9 +89,8 @@ function uploadProgress(progress) { } async function getServerCapabilities() { - let capabilities = await fetch("info").then((response) => response.json()); - MAX_FILESIZE = capabilities.max_filesize; - MAX_DURATION = capabilities.max_duration; + CAPABILITIES = await fetch("info").then((response) => response.json()); + console.log(CAPABILITIES); } document.addEventListener("DOMContentLoaded", function(_event){