diff --git a/.gitignore b/.gitignore index 5da00b9..5271b02 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ # will have compiled files and executables debug/ target/ +uploads/ # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html diff --git a/Cargo.toml b/Cargo.toml index 30db311..ebf0e8d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,12 +30,13 @@ actix-rt = "2.9.0" actix-web = { version = "4.5.1", features = ["openssl"] } actix-files = "0.6.5" actix-cors = "0.7.0" +actix-multipart = "0.6.1" serde = { version = "1.0.196", features = ["derive"] } serde_json = "1.0.113" chrono = { version = "0.4.33", features = ["serde"] } env_logger = "0.11.1" log = "0.4.20" -base64 = "0.21.7" +base64 = "0.22.0" sha2 = "0.10.8" rand = "0.8.5" fernet = "0.2.1" diff --git a/src/lib.rs b/src/lib.rs index 2a6cee6..4fc4b36 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -78,6 +78,8 @@ pub async fn start() -> io::Result<()> { .service(routes::media::track) .service(routes::media::stream) .service(routes::media::streaming_endpoint) + .service(routes::uploads::upload_files) + .service(routes::uploads::save_files) }; let server = HttpServer::new(application) .workers(config.workers as usize) diff --git a/src/routes/mod.rs b/src/routes/mod.rs index fb99ed6..5ec7928 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -4,3 +4,5 @@ pub mod basics; pub mod media; /// Module for `/home`, `/login`, `/logout` and `/error` entrypoints. pub mod auth; +/// Module to handle upload entrypoint. +pub mod uploads; diff --git a/src/routes/uploads.rs b/src/routes/uploads.rs new file mode 100644 index 0000000..1346c6c --- /dev/null +++ b/src/routes/uploads.rs @@ -0,0 +1,55 @@ +use std::path::{Path, PathBuf}; + +use actix_multipart::form::{ + MultipartForm, + tempfile::TempFile, +}; +use actix_web::HttpResponse; + +#[derive(Debug, MultipartForm)] +struct UploadForm { + #[multipart(rename = "file")] + files: Vec, +} + +#[post("/upload")] +pub async fn save_files(MultipartForm(form): MultipartForm) -> HttpResponse { + for file in form.files { + let filename = file.file_name.unwrap(); + let path = format!("uploads/{}", filename); + log::info!("Saving to {path}"); + file.file.persist(path).unwrap(); + } + let html = r#" + Upload Test + +

Files have been uploaded successfully!!

+ + "#; + HttpResponse::Ok().body(html) +} + +#[get("/upload")] +pub async fn upload_files() -> HttpResponse { + let upload_path = Path::new("uploads"); + if !upload_path.exists() { + match std::fs::create_dir("uploads") { + Ok(_) => { + log::info!("Created uploads directory successfully"); + } + Err(err) => { + log::error!("{}", err); + } + } + } + let html = r#" + Upload Test + +
+ + +
+ + "#; + HttpResponse::Ok().body(html) +}