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,9 +42,11 @@ fn form_handler_js() -> RawJavaScript<&'static str> {
fn home() -> Markup {
html! {
(head("Mochi"))
body {
main {
section class="centered" {
div id="durationOptions" {
}
form id="uploadForm" {
label for="fileUpload" class="file-upload" onclick="document.getElementById('fileInput').click()" {
"Upload File"
@ -53,19 +55,17 @@ fn home() -> Markup {
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%" }
}
p id="uploadProgressValue" class="progress-value" { "" }
}
section class="centered" id="uploadedFilesDisplay" {
div id="uploadedFilesDisplay" {
h2 class="sep center" { "Uploaded Files" }
}
}
}
}
}
#[derive(FromForm)]
struct Upload<'r> {
@ -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> {
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<Settings>) -> Json<ServerInfo> {
struct ServerInfo {
max_filesize: u64,
max_duration: u32,
default_duration: u32,
#[serde(skip_serializing_if = "Vec::is_empty")]
allowed_durations: Vec<u32>,
}
#[rocket::main]

View file

@ -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)]
@ -31,7 +37,7 @@ impl Default for Settings {
fn default() -> Self {
Self {
max_filesize: 128_000_000, // 128 MB
max_duration: 86_400, // 1 day
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<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');
.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 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){