mirror of
https://github.com/Dangoware/confetti-box.git
synced 2025-04-19 07:12:58 -05:00
Initial commit
This commit is contained in:
commit
1809f13f31
6 changed files with 1834 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
/target
|
||||||
|
/tmp
|
||||||
|
/src/files
|
1718
Cargo.lock
generated
Normal file
1718
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
9
Cargo.toml
Normal file
9
Cargo.toml
Normal 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
12
Rocket.toml
Normal 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
28
src/js/form_handler.js
Normal 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
64
src/main.rs
Normal 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))
|
||||||
|
}
|
Loading…
Reference in a new issue