Improve duration handling, remove CSS for now

This commit is contained in:
G2-Games 2024-10-23 04:28:57 -05:00
parent aee0cc9892
commit 4f47efbb27
4 changed files with 68 additions and 94 deletions

View file

@ -42,27 +42,27 @@ fn form_handler_js() -> RawJavaScript<&'static str> {
fn home() -> Markup { fn home() -> Markup {
html! { html! {
(head("Mochi")) (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" { div id="durationOptions" {
h2 class="sep center" { "Uploaded Files" }
} }
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<P: AsRef<Path>>(input: &P) -> Result<(Hash, usize), std::io::
fn server_info(settings: &State<Settings>) -> Json<ServerInfo> { fn server_info(settings: &State<Settings>) -> Json<ServerInfo> {
Json(ServerInfo { Json(ServerInfo {
max_filesize: settings.max_filesize, 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<Settings>) -> Json<ServerInfo> {
struct ServerInfo { struct ServerInfo {
max_filesize: u64, max_filesize: u64,
max_duration: u32, max_duration: u32,
default_duration: u32,
#[serde(skip_serializing_if = "Vec::is_empty")]
allowed_durations: Vec<u32>,
} }
#[rocket::main] #[rocket::main]

View file

@ -7,20 +7,26 @@ use rocket::serde::{Deserialize, Serialize};
#[serde(crate = "rocket::serde")] #[serde(crate = "rocket::serde")]
pub struct Settings { pub struct Settings {
/// Maximum filesize in bytes /// Maximum filesize in bytes
#[serde(default)]
pub max_filesize: u64, pub max_filesize: u64,
/// Maximum file lifetime, seconds /// Settings pertaining to duration information
pub max_duration: u32, pub duration: DurationSettings,
/// The path to the root directory of the program, ex `/filehost/` /// The path to the root directory of the program, ex `/filehost/`
#[serde(default)]
pub root_path: String, pub root_path: String,
/// The path to the database file /// The path to the database file
#[serde(default)]
pub database_path: PathBuf, pub database_path: PathBuf,
/// Temporary directory for stuff /// Temporary directory for stuff
#[serde(default)]
pub temp_dir: PathBuf, pub temp_dir: PathBuf,
/// Settings pertaining to the server configuration
#[serde(default)]
pub server: ServerSettings, pub server: ServerSettings,
#[serde(skip)] #[serde(skip)]
@ -30,8 +36,8 @@ pub struct Settings {
impl Default for Settings { impl Default for Settings {
fn default() -> Self { fn default() -> Self {
Self { Self {
max_filesize: 128_000_000, // 128MB max_filesize: 128_000_000, // 128 MB
max_duration: 86_400, // 1 day duration: DurationSettings::default(),
root_path: "/".into(), root_path: "/".into(),
server: ServerSettings::default(), server: ServerSettings::default(),
path: "./settings.toml".into(), 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<u32>,
}
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],
}
}
}

View file

@ -1,64 +1,2 @@
@import url('https://g2games.dev/assets/fonts/fonts.css'); @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;
}
}

View file

@ -8,8 +8,7 @@ let uploadInProgress = false;
const TOO_LARGE_TEXT = "File is too large!"; const TOO_LARGE_TEXT = "File is too large!";
const ERROR_TEXT = "An error occured!"; const ERROR_TEXT = "An error occured!";
let MAX_FILESIZE; let CAPABILITIES;
let MAX_DURATION;
async function formSubmit(form) { async function formSubmit(form) {
if (uploadInProgress) { if (uploadInProgress) {
@ -19,9 +18,9 @@ async function formSubmit(form) {
// Get file size and don't upload if it's too large // Get file size and don't upload if it's too large
let file_upload = document.getElementById("fileInput"); let file_upload = document.getElementById("fileInput");
let file = file_upload.files[0]; let file = file_upload.files[0];
if (file.size > MAX_FILESIZE) { if (file.size > CAPABILITIES.max_filesize) {
progressValue.textContent = TOO_LARGE_TEXT; 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; return;
} }
@ -42,7 +41,7 @@ async function formSubmit(form) {
} }
// Reset the form data since we've successfully submitted it // Reset the form data since we've successfully submitted it
form.reset(); form.elements["fileUpload"].value = "";
} }
function networkErrorHandler(_err) { function networkErrorHandler(_err) {
@ -90,9 +89,8 @@ function uploadProgress(progress) {
} }
async function getServerCapabilities() { async function getServerCapabilities() {
let capabilities = await fetch("info").then((response) => response.json()); CAPABILITIES = await fetch("info").then((response) => response.json());
MAX_FILESIZE = capabilities.max_filesize; console.log(CAPABILITIES);
MAX_DURATION = capabilities.max_duration;
} }
document.addEventListener("DOMContentLoaded", function(_event){ document.addEventListener("DOMContentLoaded", function(_event){