mirror of
https://github.com/actix/examples
synced 2024-11-23 22:41:07 +01:00
parent
465b6a9c0a
commit
0c4ab86a9a
3
.gitignore
vendored
3
.gitignore
vendored
@ -23,3 +23,6 @@ upload.png
|
|||||||
|
|
||||||
# any dotenv files
|
# any dotenv files
|
||||||
.env
|
.env
|
||||||
|
|
||||||
|
# file uploads
|
||||||
|
/tmp
|
||||||
|
8
Cargo.lock
generated
8
Cargo.lock
generated
@ -3419,7 +3419,7 @@ dependencies = [
|
|||||||
"actix-web 3.3.2",
|
"actix-web 3.3.2",
|
||||||
"bytes 0.5.6",
|
"bytes 0.5.6",
|
||||||
"env_logger 0.8.4",
|
"env_logger 0.8.4",
|
||||||
"futures",
|
"futures-util",
|
||||||
"log",
|
"log",
|
||||||
"serde 1.0.130",
|
"serde 1.0.130",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
@ -3951,8 +3951,9 @@ dependencies = [
|
|||||||
"actix-multipart",
|
"actix-multipart",
|
||||||
"actix-web 3.3.2",
|
"actix-web 3.3.2",
|
||||||
"async-std",
|
"async-std",
|
||||||
"futures",
|
"futures-util",
|
||||||
"sanitize-filename",
|
"sanitize-filename",
|
||||||
|
"uuid",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -3961,8 +3962,9 @@ version = "0.3.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"actix-multipart",
|
"actix-multipart",
|
||||||
"actix-web 3.3.2",
|
"actix-web 3.3.2",
|
||||||
"futures",
|
"futures-util",
|
||||||
"sanitize-filename",
|
"sanitize-filename",
|
||||||
|
"uuid",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -12,6 +12,7 @@ readme = "README.md"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "3"
|
actix-web = "3"
|
||||||
actix-multipart = "0.3"
|
actix-multipart = "0.3"
|
||||||
futures = "0.3.5"
|
futures-util = "0.3"
|
||||||
async-std = "1.8.0"
|
async-std = "1.8"
|
||||||
sanitize-filename = "0.2"
|
sanitize-filename = "0.2"
|
||||||
|
uuid = { version = "0.8", features = ["v4"] }
|
||||||
|
@ -1,24 +1,30 @@
|
|||||||
use actix_multipart::Multipart;
|
use actix_multipart::Multipart;
|
||||||
use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer};
|
use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer};
|
||||||
use async_std::prelude::*;
|
use async_std::prelude::*;
|
||||||
use futures::{StreamExt, TryStreamExt};
|
use futures_util::TryStreamExt as _;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
async fn save_file(mut payload: Multipart) -> Result<HttpResponse, Error> {
|
async fn save_file(mut payload: Multipart) -> Result<HttpResponse, Error> {
|
||||||
// iterate over multipart stream
|
// iterate over multipart stream
|
||||||
while let Ok(Some(mut field)) = payload.try_next().await {
|
while let Ok(Some(mut field)) = payload.try_next().await {
|
||||||
let content_type = field
|
let content_disposition = field
|
||||||
.content_disposition().ok_or(actix_web::error::ParseError::Incomplete)?;
|
.content_disposition()
|
||||||
let filename = content_type
|
.ok_or_else(|| HttpResponse::BadRequest().finish())?;
|
||||||
.get_filename().ok_or(actix_web::error::ParseError::Incomplete)?;
|
|
||||||
|
let filename = content_disposition.get_filename().map_or_else(
|
||||||
|
|| Uuid::new_v4().to_string(),
|
||||||
|
|f| sanitize_filename::sanitize(f),
|
||||||
|
);
|
||||||
|
|
||||||
let filepath = format!("./tmp/{}", sanitize_filename::sanitize(&filename));
|
let filepath = format!("./tmp/{}", sanitize_filename::sanitize(&filename));
|
||||||
let mut f = async_std::fs::File::create(filepath).await?;
|
let mut f = async_std::fs::File::create(filepath).await?;
|
||||||
|
|
||||||
// Field in turn is stream of *Bytes* object
|
// Field in turn is stream of *Bytes* object
|
||||||
while let Some(chunk) = field.next().await {
|
while let Some(chunk) = field.try_next().await? {
|
||||||
let data = chunk.unwrap();
|
f.write_all(&chunk).await?;
|
||||||
f.write_all(&data).await?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(HttpResponse::Ok().into())
|
Ok(HttpResponse::Ok().into())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,11 +46,9 @@ fn index() -> HttpResponse {
|
|||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info");
|
std::env::set_var("RUST_LOG", "info");
|
||||||
async_std::fs::create_dir_all("./tmp").await?;
|
async_std::fs::create_dir_all("./tmp").await?;
|
||||||
|
|
||||||
let ip = "0.0.0.0:3000";
|
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new().wrap(middleware::Logger::default()).service(
|
App::new().wrap(middleware::Logger::default()).service(
|
||||||
web::resource("/")
|
web::resource("/")
|
||||||
@ -52,7 +56,7 @@ async fn main() -> std::io::Result<()> {
|
|||||||
.route(web::post().to(save_file)),
|
.route(web::post().to(save_file)),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.bind(ip)?
|
.bind(("127.0.0.1", 8080))?
|
||||||
.run()
|
.run()
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
@ -12,5 +12,7 @@ readme = "README.md"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
actix-multipart = "0.3"
|
actix-multipart = "0.3"
|
||||||
actix-web = "3"
|
actix-web = "3"
|
||||||
futures = "0.3.1"
|
|
||||||
|
futures-util = "0.3"
|
||||||
sanitize-filename = "0.2"
|
sanitize-filename = "0.2"
|
||||||
|
uuid = { version = "0.8", features = ["v4"] }
|
||||||
|
@ -2,27 +2,33 @@ use std::io::Write;
|
|||||||
|
|
||||||
use actix_multipart::Multipart;
|
use actix_multipart::Multipart;
|
||||||
use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer};
|
use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer};
|
||||||
use futures::{StreamExt, TryStreamExt};
|
use futures_util::TryStreamExt as _;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
async fn save_file(mut payload: Multipart) -> Result<HttpResponse, Error> {
|
async fn save_file(mut payload: Multipart) -> Result<HttpResponse, Error> {
|
||||||
// iterate over multipart stream
|
// iterate over multipart stream
|
||||||
while let Ok(Some(mut field)) = payload.try_next().await {
|
while let Some(mut field) = payload.try_next().await? {
|
||||||
let content_type = field.content_disposition().unwrap();
|
// A multipart/form-data stream has to contain `content_disposition`
|
||||||
let filename = content_type.get_filename().unwrap();
|
let content_disposition = field
|
||||||
let filepath = format!("./tmp/{}", sanitize_filename::sanitize(&filename));
|
.content_disposition()
|
||||||
|
.ok_or_else(|| HttpResponse::BadRequest().finish())?;
|
||||||
|
|
||||||
|
let filename = content_disposition.get_filename().map_or_else(
|
||||||
|
|| Uuid::new_v4().to_string(),
|
||||||
|
|f| sanitize_filename::sanitize(f),
|
||||||
|
);
|
||||||
|
let filepath = format!("./tmp/{}", filename);
|
||||||
|
|
||||||
// File::create is blocking operation, use threadpool
|
// File::create is blocking operation, use threadpool
|
||||||
let mut f = web::block(|| std::fs::File::create(filepath))
|
let mut f = web::block(|| std::fs::File::create(filepath)).await?;
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// Field in turn is stream of *Bytes* object
|
// Field in turn is stream of *Bytes* object
|
||||||
while let Some(chunk) = field.next().await {
|
while let Some(chunk) = field.try_next().await? {
|
||||||
let data = chunk.unwrap();
|
|
||||||
// filesystem operations are blocking, we have to use threadpool
|
// filesystem operations are blocking, we have to use threadpool
|
||||||
f = web::block(move || f.write_all(&data).map(|_| f)).await?;
|
f = web::block(move || f.write_all(&chunk).map(|_| f)).await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(HttpResponse::Ok().into())
|
Ok(HttpResponse::Ok().into())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,17 +43,13 @@ fn index() -> HttpResponse {
|
|||||||
</body>
|
</body>
|
||||||
</html>"#;
|
</html>"#;
|
||||||
|
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok().body(html)
|
||||||
.content_type("text/html; charset=utf-8")
|
|
||||||
.body(html)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info");
|
std::env::set_var("RUST_LOG", "info");
|
||||||
std::fs::create_dir_all("./tmp").unwrap();
|
std::fs::create_dir_all("./tmp")?;
|
||||||
|
|
||||||
let ip = "0.0.0.0:3000";
|
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new().wrap(middleware::Logger::default()).service(
|
App::new().wrap(middleware::Logger::default()).service(
|
||||||
@ -56,7 +58,7 @@ async fn main() -> std::io::Result<()> {
|
|||||||
.route(web::post().to(save_file)),
|
.route(web::post().to(save_file)),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.bind(ip)?
|
.bind(("127.0.0.1", 8080))?
|
||||||
.run()
|
.run()
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
@ -6,9 +6,10 @@ edition = "2018"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "3"
|
actix-web = "3"
|
||||||
|
|
||||||
bytes = "0.5"
|
bytes = "0.5"
|
||||||
env_logger = "0.8"
|
env_logger = "0.8"
|
||||||
futures = "0.3.1"
|
futures-util = "0.3"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
|
@ -1,14 +1,17 @@
|
|||||||
// Allow this lint since it's fine to use type directly in the short example.
|
// Allow this lint since it's fine to use type directly in the short example.
|
||||||
#![allow(clippy::type_complexity)]
|
#![allow(clippy::type_complexity)]
|
||||||
|
|
||||||
use std::error;
|
use std::{
|
||||||
use std::pin::Pin;
|
error,
|
||||||
use std::sync::{Arc, RwLock};
|
future::Future,
|
||||||
use std::time::Duration;
|
pin::Pin,
|
||||||
|
sync::{Arc, RwLock},
|
||||||
|
time::Duration,
|
||||||
|
};
|
||||||
|
|
||||||
use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer};
|
use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer};
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures::{Future, FutureExt};
|
use futures_util::FutureExt as _;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
Loading…
Reference in New Issue
Block a user