1
0
mirror of https://github.com/actix/examples synced 2025-02-02 09:39:03 +01:00

remove example use async_std::fs (#461)

This commit is contained in:
fakeshadow 2021-10-22 15:44:40 +08:00 committed by GitHub
parent e188d612c2
commit 414bf927b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 0 additions and 121 deletions

View File

@ -25,7 +25,6 @@ members = [
"database_interactions/sqlx_todo",
"forms/form",
"forms/multipart",
"forms/multipart-async-std",
"forms/multipart-s3",
"graphql/graphql-demo",
"graphql/juniper",

View File

@ -1,3 +0,0 @@
/target
**/*.rs.bk
/tmp

View File

@ -1,18 +0,0 @@
[package]
name = "multipart-async-std-example"
version = "0.3.0"
authors = ["Bevan Hunt <bevan@bevanhunt.com>"]
edition = "2018"
license = "MIT"
description = "Simple file uploader in Actix Web with Async/Await"
keywords = ["actix", "actix-web", "multipart"]
repository = "https://github.com/actix/examples"
readme = "README.md"
[dependencies]
actix-web = "3"
actix-multipart = "0.3"
futures-util = "0.3"
async-std = "1.8"
sanitize-filename = "0.2"
uuid = { version = "0.8", features = ["v4"] }

View File

@ -1,21 +0,0 @@
MIT License
Copyright (c) [2019] [Bevan Hunt]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -1,16 +0,0 @@
# Actix Web File Upload with Async/Await
### Run
```bash
cd forms/multipart-async-std
cargo run
```
``` open web browser to localhost:3000 and upload file(s) ```
### Result
``` file(s) will show up in ./tmp in the same directory as the running process ```
Note: this is a naive implementation and will panic on any error

View File

@ -1,62 +0,0 @@
use actix_multipart::Multipart;
use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer};
use async_std::prelude::*;
use futures_util::TryStreamExt as _;
use uuid::Uuid;
async fn save_file(mut payload: Multipart) -> Result<HttpResponse, Error> {
// iterate over multipart stream
while let Ok(Some(mut field)) = payload.try_next().await {
let content_disposition = field
.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/{}", sanitize_filename::sanitize(&filename));
let mut f = async_std::fs::File::create(filepath).await?;
// Field in turn is stream of *Bytes* object
while let Some(chunk) = field.try_next().await? {
f.write_all(&chunk).await?;
}
}
Ok(HttpResponse::Ok().into())
}
fn index() -> HttpResponse {
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()
.content_type("text/html; charset=utf-8")
.body(html)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "info");
async_std::fs::create_dir_all("./tmp").await?;
HttpServer::new(|| {
App::new().wrap(middleware::Logger::default()).service(
web::resource("/")
.route(web::get().to(index))
.route(web::post().to(save_file)),
)
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}