Skip to content

Commit

Permalink
Add a feature to upload files with a dedicated endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
dormant-user committed Mar 9, 2024
1 parent cbc5e44 commit c29cc82
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
55 changes: 55 additions & 0 deletions src/routes/uploads.rs
Original file line number Diff line number Diff line change
@@ -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<TempFile>,
}

#[post("/upload")]
pub async fn save_files(MultipartForm(form): MultipartForm<UploadForm>) -> 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#"<html>
<head><title>Upload Test</title></head>
<body>
<h3>Files have been uploaded successfully!!</h3>
</body>
</html>"#;
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#"<html>
<head><title>Upload Test</title></head>
<body>
<form target="/" method="post" enctype="multipart/form-data">
<input type="file" multiple name="file"/>
<button type="submit">Submit</button>
</form>
</body>
</html>"#;
HttpResponse::Ok().body(html)
}

0 comments on commit c29cc82

Please sign in to comment.