Impl OptionalFromRequest on MultipartFile and change behaviour on MultipartFiles to contain 0 files

This commit is contained in:
2025-09-06 15:28:24 +02:00
parent e0500b8e97
commit 113011399b
6 changed files with 1201 additions and 572 deletions

View File

@ -17,58 +17,13 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "async-trait"
version = "0.1.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c6fa2087f2753a7da8cc1c0dbfcf89579dd57458e36769de5ac750b4671737ca"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "axum"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a6c9af12842a67734c9a2e355436e5d03b22383ed60cf13cd0c18fbfe3dcbcf"
dependencies = [
"async-trait",
"axum-core 0.4.3",
"bytes",
"futures-util",
"http",
"http-body",
"http-body-util",
"hyper",
"hyper-util",
"itoa",
"matchit 0.7.3",
"memchr",
"mime",
"percent-encoding",
"pin-project-lite",
"rustversion",
"serde",
"serde_json",
"serde_path_to_error",
"serde_urlencoded",
"sync_wrapper 1.0.1",
"tokio",
"tower 0.4.13",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]
name = "axum"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "021e862c184ae977658b36c4500f7feac3221ca5da43e3f25bd04ab6c79a29b5"
dependencies = [
"axum-core 0.5.2",
"axum-core",
"bytes",
"form_urlencoded",
"futures-util",
@ -78,7 +33,7 @@ dependencies = [
"hyper",
"hyper-util",
"itoa",
"matchit 0.8.4",
"matchit",
"memchr",
"mime",
"multer",
@ -89,7 +44,7 @@ dependencies = [
"serde_json",
"serde_path_to_error",
"serde_urlencoded",
"sync_wrapper 1.0.1",
"sync_wrapper",
"tokio",
"tower 0.5.2",
"tower-layer",
@ -97,27 +52,6 @@ dependencies = [
"tracing",
]
[[package]]
name = "axum-core"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a15c63fd72d41492dc4f497196f5da1fb04fb7529e631d73630d1b491e47a2e3"
dependencies = [
"async-trait",
"bytes",
"futures-util",
"http",
"http-body",
"http-body-util",
"mime",
"pin-project-lite",
"rustversion",
"sync_wrapper 0.1.2",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]
name = "axum-core"
version = "0.5.2"
@ -132,7 +66,7 @@ dependencies = [
"mime",
"pin-project-lite",
"rustversion",
"sync_wrapper 1.0.1",
"sync_wrapper",
"tower-layer",
"tower-service",
"tracing",
@ -375,9 +309,10 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
name = "lib"
version = "2.0.0"
dependencies = [
"axum 0.8.4",
"axum",
"derive_more",
"mime",
"serde",
"thiserror",
"tokio",
"tower 0.5.2",
@ -398,12 +333,6 @@ version = "0.4.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
[[package]]
name = "matchit"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94"
[[package]]
name = "matchit"
version = "0.8.4"
@ -464,7 +393,7 @@ dependencies = [
name = "multipart_file"
version = "0.1.0"
dependencies = [
"axum 0.7.5",
"axum",
"lib",
"tokio",
]
@ -675,12 +604,6 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "sync_wrapper"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160"
[[package]]
name = "sync_wrapper"
version = "1.0.1"
@ -758,7 +681,6 @@ dependencies = [
"tokio",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]
@ -770,7 +692,7 @@ dependencies = [
"futures-core",
"futures-util",
"pin-project-lite",
"sync_wrapper 1.0.1",
"sync_wrapper",
"tokio",
"tower-layer",
"tower-service",

View File

@ -1,9 +1,9 @@
[package]
name = "multipart_file"
version = "0.1.0"
edition = "2021"
edition = "2024"
[dependencies]
lib = { path = "../..", features = ["axum"] }
axum = "0.7.5"
tokio = { version = "1.40", features = ["rt-multi-thread", "macros"] }
axum = "0.8"
tokio = { version = "1.47", features = ["rt-multi-thread", "macros"] }

View File

@ -1,22 +1,10 @@
use axum::extract::DefaultBodyLimit;
use lib::axum::app::AppBuilder;
use lib::axum::extractor::MultipartFiles;
use lib::axum::extractor::{MultipartFile, MultipartFiles};
use lib::routes;
// 0 or more
async fn with_optional_file(files: Option<MultipartFiles>) -> String {
format!(
"{:?}",
files.map(|files| files
.0
.into_iter()
.map(|file| file.filename)
.collect::<Vec<_>>())
)
}
// 1 or more files
async fn handler(MultipartFiles(files): MultipartFiles) -> String {
// 0 or more files
async fn several_files(MultipartFiles(files): MultipartFiles) -> String {
format!(
"{:?} uploaded",
files
@ -26,11 +14,26 @@ async fn handler(MultipartFiles(files): MultipartFiles) -> String {
)
}
// 1 file exactly
async fn single_file(MultipartFile(file): MultipartFile) -> String {
format!("{:?} uploaded", file.filename)
}
// 0 or 1 file
async fn optional_single_file(file: Option<MultipartFile>) -> String {
format!(
"{:?} uploaded",
file.map(|file| file.0.filename)
.unwrap_or(String::from("No file found"))
)
}
#[tokio::main]
async fn main() {
let route = routes!(
get "/" => handler,
get "/opt" => with_optional_file
get "/" => several_files,
get "/file" => single_file,
get "/opt/file" => optional_single_file
)
.layer(DefaultBodyLimit::disable());
AppBuilder::new().route(route).serve().await.unwrap();