2019-12-09 01:28:09 +01:00
|
|
|
use std::io::Write;
|
|
|
|
|
2019-12-07 15:40:31 +01:00
|
|
|
use actix_multipart::Multipart;
|
|
|
|
use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer};
|
2021-10-07 04:27:50 +02:00
|
|
|
use futures_util::TryStreamExt as _;
|
|
|
|
use uuid::Uuid;
|
2019-12-07 15:40:31 +01:00
|
|
|
|
|
|
|
async fn save_file(mut payload: Multipart) -> Result<HttpResponse, Error> {
|
|
|
|
// iterate over multipart stream
|
2021-10-07 04:27:50 +02:00
|
|
|
while let Some(mut field) = payload.try_next().await? {
|
|
|
|
// A multipart/form-data stream has to contain `content_disposition`
|
2022-01-29 17:40:50 +01:00
|
|
|
let content_disposition = field.content_disposition();
|
2021-10-07 04:27:50 +02:00
|
|
|
|
2022-01-29 17:40:50 +01:00
|
|
|
let filename = content_disposition
|
|
|
|
.get_filename()
|
|
|
|
.map_or_else(|| Uuid::new_v4().to_string(), sanitize_filename::sanitize);
|
2022-06-08 04:53:38 +02:00
|
|
|
let filepath = format!("./tmp/{filename}");
|
2020-09-12 17:49:45 +02:00
|
|
|
|
2019-12-09 01:28:09 +01:00
|
|
|
// File::create is blocking operation, use threadpool
|
2022-01-29 17:40:50 +01:00
|
|
|
let mut f = web::block(|| std::fs::File::create(filepath)).await??;
|
2020-09-12 17:49:45 +02:00
|
|
|
|
2019-12-07 15:40:31 +01:00
|
|
|
// Field in turn is stream of *Bytes* object
|
2021-10-07 04:27:50 +02:00
|
|
|
while let Some(chunk) = field.try_next().await? {
|
2019-12-09 01:28:09 +01:00
|
|
|
// filesystem operations are blocking, we have to use threadpool
|
2022-01-29 17:40:50 +01:00
|
|
|
f = web::block(move || f.write_all(&chunk).map(|_| f)).await??;
|
2019-12-07 15:40:31 +01:00
|
|
|
}
|
|
|
|
}
|
2021-10-07 04:27:50 +02:00
|
|
|
|
2019-12-07 15:40:31 +01:00
|
|
|
Ok(HttpResponse::Ok().into())
|
2018-06-08 06:34:14 +02:00
|
|
|
}
|
|
|
|
|
2022-02-02 01:44:58 +01:00
|
|
|
async fn index() -> HttpResponse {
|
2018-06-08 06:34:14 +02:00
|
|
|
let html = r#"<html>
|
|
|
|
<head><title>Upload Test</title></head>
|
|
|
|
<body>
|
|
|
|
<form target="/" method="post" enctype="multipart/form-data">
|
2019-12-07 15:40:31 +01:00
|
|
|
<input type="file" multiple name="file"/>
|
2020-08-05 22:08:54 +02:00
|
|
|
<button type="submit">Submit</button>
|
2018-06-08 06:34:14 +02:00
|
|
|
</form>
|
|
|
|
</body>
|
|
|
|
</html>"#;
|
|
|
|
|
2021-10-07 04:27:50 +02:00
|
|
|
HttpResponse::Ok().body(html)
|
2018-04-13 03:18:42 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:49:45 +02:00
|
|
|
#[actix_web::main]
|
2019-12-07 18:59:24 +01:00
|
|
|
async fn main() -> std::io::Result<()> {
|
2021-10-07 04:27:50 +02:00
|
|
|
std::env::set_var("RUST_LOG", "info");
|
|
|
|
std::fs::create_dir_all("./tmp")?;
|
2019-12-07 18:59:24 +01:00
|
|
|
|
2019-03-29 21:43:03 +01:00
|
|
|
HttpServer::new(|| {
|
2019-12-07 18:59:24 +01:00
|
|
|
App::new().wrap(middleware::Logger::default()).service(
|
|
|
|
web::resource("/")
|
|
|
|
.route(web::get().to(index))
|
|
|
|
.route(web::post().to(save_file)),
|
2019-12-07 15:40:31 +01:00
|
|
|
)
|
2019-03-10 03:03:09 +01:00
|
|
|
})
|
2021-10-07 04:27:50 +02:00
|
|
|
.bind(("127.0.0.1", 8080))?
|
2019-12-25 17:48:33 +01:00
|
|
|
.run()
|
2019-12-07 18:59:24 +01:00
|
|
|
.await
|
2018-04-13 03:18:42 +02:00
|
|
|
}
|