Initial commit

This commit is contained in:
G2-Games 2024-10-21 03:12:58 -05:00
commit 1809f13f31
6 changed files with 1834 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
/target
/tmp
/src/files

1718
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

9
Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "mochihost"
version = "0.1.0"
edition = "2021"
[dependencies]
maud = { version = "0.26", features = ["rocket"] }
rocket = "0.5"
uuid = { version = "1.11.0", features = ["v3", "v4", "v5"] }

12
Rocket.toml Normal file
View file

@ -0,0 +1,12 @@
[default]
address = "0.0.0.0"
port = 8950
temp_dir = "tmp"
[default.limits]
form = "1 GiB"
data-form = "1 GiB"
file = "1 GiB"
bytes = "1 GiB"
json = "1 GiB"
msgpack = "1 GiB"

28
src/js/form_handler.js Normal file
View file

@ -0,0 +1,28 @@
function formSubmit(form) {
let url = "/upload";
let request = new XMLHttpRequest();
request.open('POST', url, true);
request.onload = function() { // request successful
// we can use server response to our request now
console.log(request.responseText);
};
request.onerror = function() {
// request failed
};
// Create and send FormData
request.send(new FormData(form));
// Reset the form data since we've successfully submitted it
form.reset();
}
// and you can attach form submit event like this for example
function attachFormSubmitEvent(formId){
document.getElementById(formId).addEventListener("submit", formSubmit);
}
document.addEventListener("DOMContentLoaded", function(event){
attachFormSubmitEvent("uploadForm");
})

64
src/main.rs Normal file
View file

@ -0,0 +1,64 @@
use std::path::PathBuf;
use maud::{html, Markup, DOCTYPE, PreEscaped};
use rocket::{
form::Form, fs::{FileServer, Options, TempFile}, get, post, routes, FromForm
};
use uuid::Uuid;
const FORM_HANDLER_JS: &str = include_str!("js/form_handler.js");
fn head(page_title: &str) -> Markup {
html! {
(DOCTYPE)
meta charset="utf-8";
title { (page_title) }
script { (PreEscaped(FORM_HANDLER_JS)) }
}
}
#[get("/")]
fn home() -> Markup {
html! {
(head("Mochi"))
body {
h1 { "File Hosting!" }
h2 { "Everything will be deleted in like 24 hours or something idk" }
form id="uploadForm" {
input type="file" name="fileUpload" onchange="formSubmit(this.parentNode)" {
"Select File (Or drag here)"
}
}
}
}
}
#[derive(FromForm)]
struct Upload<'r> {
#[field(name = "fileUpload")]
file: TempFile<'r>,
}
/// Handle a file upload and store it
#[post("/upload", data = "<file_data>")]
async fn handle_upload(mut file_data: Form<Upload<'_>>) -> Result<(), std::io::Error> {
let mut out_path = PathBuf::from("files/");
out_path.push(get_filename());
file_data.file.persist_to(out_path).await?;
Ok(())
}
/// Get a random filename for use as the uploaded file's name
fn get_filename() -> String {
let uuid = Uuid::new_v4().to_string();
uuid
}
#[rocket::launch]
fn launch() -> _ {
rocket::build()
.mount("/", routes![home, handle_upload])
.mount("/files", FileServer::new("files/", Options::Missing | Options::NormalizeDirs))
}