Improved CSS and JS a bit more

This commit is contained in:
G2-Games 2024-10-23 04:50:33 -05:00
parent 4f47efbb27
commit 26f1f70a47
4 changed files with 50 additions and 32 deletions

View file

@ -23,6 +23,7 @@ fn head(page_title: &str) -> Markup {
title { (page_title) } title { (page_title) }
// Javascript stuff for client side handling // Javascript stuff for client side handling
script src="request.js" { } script src="request.js" { }
link rel="stylesheet" href="main.css";
} }
} }
@ -43,12 +44,13 @@ fn home() -> Markup {
html! { html! {
(head("Mochi")) (head("Mochi"))
center {
div id="durationOptions" { div id="durationOptions" {
} }
form id="uploadForm" { form id="uploadForm" {
label for="fileUpload" class="file-upload" onclick="document.getElementById('fileInput').click()" { label for="fileUpload" class="file_upload" onclick="document.getElementById('fileInput').click()" {
"Upload File" "Upload File"
} }
input id="fileInput" type="file" name="fileUpload" onchange="formSubmit(this.parentNode)" style="display:none;"; input id="fileInput" type="file" name="fileUpload" onchange="formSubmit(this.parentNode)" style="display:none;";
@ -56,15 +58,16 @@ fn home() -> Markup {
input type="text" name="duration" minlength="2" maxlength="4"; input type="text" name="duration" minlength="2" maxlength="4";
} }
div class="progress-box" { div class="progress_box" {
progress id="uploadProgress" value="0" max="100" {} progress id="uploadProgress" value="0" max="100" {}
p id="uploadProgressValue" class="progress-value" { "" } p id="uploadProgressValue" class="progress_value" { "" }
} }
div id="uploadedFilesDisplay" { div id="uploadedFilesDisplay" {
h2 class="sep center" { "Uploaded Files" } h2 class="sep center" { "Uploaded Files" }
} }
} }
}
} }
#[derive(FromForm)] #[derive(FromForm)]
@ -223,11 +226,11 @@ async fn main() {
let rocket = rocket::build() let rocket = rocket::build()
.mount( .mount(
config.root_path.clone() + "/", config.server.root_path.clone() + "/",
routes![home, handle_upload, form_handler_js, stylesheet, server_info] routes![home, handle_upload, form_handler_js, stylesheet, server_info]
) )
.mount( .mount(
config.root_path.clone() + "/files", config.server.root_path.clone() + "/files",
FileServer::new("files/", Options::Missing | Options::NormalizeDirs) FileServer::new("files/", Options::Missing | Options::NormalizeDirs)
) )
.manage(database) .manage(database)

View file

@ -13,10 +13,6 @@ pub struct Settings {
/// Settings pertaining to duration information /// Settings pertaining to duration information
pub duration: DurationSettings, 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 /// The path to the database file
#[serde(default)] #[serde(default)]
pub database_path: PathBuf, pub database_path: PathBuf,
@ -38,7 +34,6 @@ impl Default for Settings {
Self { Self {
max_filesize: 128_000_000, // 128 MB max_filesize: 128_000_000, // 128 MB
duration: DurationSettings::default(), duration: DurationSettings::default(),
root_path: "/".into(),
server: ServerSettings::default(), server: ServerSettings::default(),
path: "./settings.toml".into(), path: "./settings.toml".into(),
database_path: "./database.mochi".into(), database_path: "./database.mochi".into(),
@ -84,12 +79,16 @@ impl Settings {
pub struct ServerSettings { pub struct ServerSettings {
pub address: String, pub address: String,
pub port: u16, pub port: u16,
/// The path to the root directory of the program, ex `/filehost/`
pub root_path: String,
} }
impl Default for ServerSettings { impl Default for ServerSettings {
fn default() -> Self { fn default() -> Self {
Self { Self {
address: "127.0.0.1".into(), address: "127.0.0.1".into(),
root_path: "/".into(),
port: 8955 port: 8955
} }
} }

View file

@ -1,2 +1,16 @@
@import url('https://g2games.dev/assets/fonts/fonts.css'); @import url('https://g2games.dev/assets/fonts/fonts.css');
body {
background-color: black;
color: white;
}
.file_upload {
display: block;
width: fit-content;
padding: 5px;
border: 1px solid grey;
cursor: pointer;
margin: 10px;
border-radius: 5px;
}

View file

@ -9,6 +9,11 @@ const TOO_LARGE_TEXT = "File is too large!";
const ERROR_TEXT = "An error occured!"; const ERROR_TEXT = "An error occured!";
let CAPABILITIES; let CAPABILITIES;
async function getServerCapabilities() {
CAPABILITIES = await fetch("info").then((response) => response.json());
console.log(CAPABILITIES);
}
getServerCapabilities();
async function formSubmit(form) { async function formSubmit(form) {
if (uploadInProgress) { if (uploadInProgress) {
@ -20,7 +25,10 @@ async function formSubmit(form) {
let file = file_upload.files[0]; let file = file_upload.files[0];
if (file.size > CAPABILITIES.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", CAPABILITIES.max_filesize, "bytes"); console.error(
"Provided file is too large", file.size, "bytes; max",
CAPABILITIES.max_filesize, "bytes"
);
return; return;
} }
@ -28,6 +36,7 @@ async function formSubmit(form) {
let request = new XMLHttpRequest(); let request = new XMLHttpRequest();
request.open('POST', url, true); request.open('POST', url, true);
// Set up the listeners
request.addEventListener('load', uploadComplete, false); request.addEventListener('load', uploadComplete, false);
request.addEventListener('error', networkErrorHandler, false); request.addEventListener('error', networkErrorHandler, false);
request.upload.addEventListener('progress', uploadProgress, false); request.upload.addEventListener('progress', uploadProgress, false);
@ -40,7 +49,7 @@ async function formSubmit(form) {
console.error("An error occured while uploading", e); console.error("An error occured while uploading", e);
} }
// Reset the form data since we've successfully submitted it // Reset the form file data since we've successfully submitted it
form.elements["fileUpload"].value = ""; form.elements["fileUpload"].value = "";
} }
@ -88,11 +97,6 @@ function uploadProgress(progress) {
} }
} }
async function getServerCapabilities() {
CAPABILITIES = await fetch("info").then((response) => response.json());
console.log(CAPABILITIES);
}
document.addEventListener("DOMContentLoaded", function(_event){ document.addEventListener("DOMContentLoaded", function(_event){
document.getElementById("uploadForm").addEventListener("submit", formSubmit); document.getElementById("uploadForm").addEventListener("submit", formSubmit);
progressBar = document.getElementById("uploadProgress"); progressBar = document.getElementById("uploadProgress");
@ -100,5 +104,3 @@ document.addEventListener("DOMContentLoaded", function(_event){
statusNotifier = document.getElementById("uploadStatus"); statusNotifier = document.getElementById("uploadStatus");
uploadedFilesDisplay = document.getElementById("uploadedFilesDisplay"); uploadedFilesDisplay = document.getElementById("uploadedFilesDisplay");
}); });
getServerCapabilities();