mirror of
https://github.com/actix/examples
synced 2025-06-27 01:27:43 +02:00
Updated forms/multipart to v4. (#503)
This commit is contained in:
committed by
GitHub
parent
e903184a12
commit
5df5bd10ea
@ -9,23 +9,20 @@ async fn save_file(mut payload: Multipart) -> Result<HttpResponse, Error> {
|
||||
// iterate over multipart stream
|
||||
while let Some(mut field) = payload.try_next().await? {
|
||||
// A multipart/form-data stream has to contain `content_disposition`
|
||||
let content_disposition = field
|
||||
.content_disposition()
|
||||
.ok_or_else(|| HttpResponse::BadRequest().finish())?;
|
||||
let content_disposition = field.content_disposition();
|
||||
|
||||
let filename = content_disposition.get_filename().map_or_else(
|
||||
|| Uuid::new_v4().to_string(),
|
||||
|f| sanitize_filename::sanitize(f),
|
||||
);
|
||||
let filename = content_disposition
|
||||
.get_filename()
|
||||
.map_or_else(|| Uuid::new_v4().to_string(), sanitize_filename::sanitize);
|
||||
let filepath = format!("./tmp/{}", filename);
|
||||
|
||||
// File::create is blocking operation, use threadpool
|
||||
let mut f = web::block(|| std::fs::File::create(filepath)).await?;
|
||||
let mut f = web::block(|| std::fs::File::create(filepath)).await??;
|
||||
|
||||
// Field in turn is stream of *Bytes* object
|
||||
while let Some(chunk) = field.try_next().await? {
|
||||
// filesystem operations are blocking, we have to use threadpool
|
||||
f = web::block(move || f.write_all(&chunk).map(|_| f)).await?;
|
||||
f = web::block(move || f.write_all(&chunk).map(|_| f)).await??;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user