2025-03-22 22:17:47 -04:00
|
|
|
use axum::{
|
|
|
|
body::Bytes,
|
|
|
|
extract::{FromRequest, Request},
|
|
|
|
http::{StatusCode, header::CONTENT_TYPE},
|
|
|
|
};
|
|
|
|
use axum_extra::extract::Multipart;
|
2025-05-11 14:27:55 -04:00
|
|
|
use serde::de::DeserializeOwned;
|
2025-03-22 22:17:47 -04:00
|
|
|
use std::{fs::File, io::BufWriter};
|
|
|
|
|
|
|
|
/// An image extractor accepting:
|
|
|
|
/// * `multipart/form-data`
|
|
|
|
/// * `image/png`
|
|
|
|
/// * `image/jpeg`
|
|
|
|
/// * `image/avif`
|
|
|
|
/// * `image/webp`
|
2025-05-04 16:19:34 -04:00
|
|
|
pub struct Image(pub Bytes, pub String);
|
2025-03-22 22:17:47 -04:00
|
|
|
|
|
|
|
impl<S> FromRequest<S> for Image
|
|
|
|
where
|
|
|
|
Bytes: FromRequest<S>,
|
|
|
|
S: Send + Sync,
|
|
|
|
{
|
|
|
|
type Rejection = StatusCode;
|
|
|
|
|
|
|
|
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
|
|
|
|
let Some(content_type) = req.headers().get(CONTENT_TYPE) else {
|
|
|
|
return Err(StatusCode::BAD_REQUEST);
|
|
|
|
};
|
|
|
|
|
2025-05-04 16:19:34 -04:00
|
|
|
let content_type = content_type.to_str().unwrap();
|
|
|
|
let content_type_string = content_type.to_string();
|
|
|
|
|
|
|
|
let body = if content_type.starts_with("multipart/form-data") {
|
2025-03-22 22:17:47 -04:00
|
|
|
let mut multipart = Multipart::from_request(req, state)
|
|
|
|
.await
|
|
|
|
.map_err(|_| StatusCode::BAD_REQUEST)?;
|
|
|
|
|
|
|
|
let Ok(Some(field)) = multipart.next_field().await else {
|
|
|
|
return Err(StatusCode::BAD_REQUEST);
|
|
|
|
};
|
|
|
|
|
|
|
|
field.bytes().await.map_err(|_| StatusCode::BAD_REQUEST)?
|
|
|
|
} else if (content_type == "image/avif")
|
|
|
|
| (content_type == "image/jpeg")
|
|
|
|
| (content_type == "image/png")
|
|
|
|
| (content_type == "image/webp")
|
2025-05-02 20:08:35 -04:00
|
|
|
| (content_type == "image/gif")
|
2025-03-22 22:17:47 -04:00
|
|
|
{
|
|
|
|
Bytes::from_request(req, state)
|
|
|
|
.await
|
|
|
|
.map_err(|_| StatusCode::BAD_REQUEST)?
|
|
|
|
} else {
|
|
|
|
return Err(StatusCode::BAD_REQUEST);
|
|
|
|
};
|
|
|
|
|
2025-05-04 16:19:34 -04:00
|
|
|
Ok(Self(body, content_type_string))
|
2025-03-22 22:17:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-04 16:19:34 -04:00
|
|
|
/// Create an image buffer given an input of `bytes`
|
|
|
|
pub fn save_buffer(path: &str, bytes: Vec<u8>, format: image::ImageFormat) -> std::io::Result<()> {
|
2025-03-22 22:17:47 -04:00
|
|
|
let pre_img_buffer = match image::load_from_memory(&bytes) {
|
|
|
|
Ok(i) => i,
|
|
|
|
Err(_) => {
|
|
|
|
return Err(std::io::Error::new(
|
|
|
|
std::io::ErrorKind::InvalidData,
|
|
|
|
"Image failed",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let file = File::create(path)?;
|
|
|
|
let mut writer = BufWriter::new(file);
|
|
|
|
|
2025-05-04 16:19:34 -04:00
|
|
|
if pre_img_buffer.write_to(&mut writer, format).is_err() {
|
2025-03-22 22:17:47 -04:00
|
|
|
return Err(std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
"Image conversion failed",
|
|
|
|
));
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2025-05-11 14:27:55 -04:00
|
|
|
|
|
|
|
/// A file extractor accepting:
|
|
|
|
/// * `multipart/form-data`
|
|
|
|
///
|
|
|
|
/// Will also attempt to parse out the **last** field in the multipart upload
|
|
|
|
/// as the given struct from JSON. Every other field is put into a vector of bytes,
|
|
|
|
/// as they are seen as raw binary data.
|
|
|
|
pub struct JsonMultipart<T: DeserializeOwned>(pub Vec<Bytes>, pub T);
|
|
|
|
|
|
|
|
impl<S, T> FromRequest<S> for JsonMultipart<T>
|
|
|
|
where
|
|
|
|
Bytes: FromRequest<S>,
|
|
|
|
S: Send + Sync,
|
|
|
|
T: DeserializeOwned,
|
|
|
|
{
|
|
|
|
type Rejection = (StatusCode, String);
|
|
|
|
|
|
|
|
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
|
|
|
|
let Some(content_type) = req.headers().get(CONTENT_TYPE) else {
|
|
|
|
return Err((
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
"no content type header".to_string(),
|
|
|
|
));
|
|
|
|
};
|
|
|
|
|
|
|
|
let content_type = content_type.to_str().unwrap();
|
|
|
|
|
|
|
|
if !content_type.starts_with("multipart/form-data") {
|
|
|
|
return Err((
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
"expected multipart/form-data".to_string(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut multipart = Multipart::from_request(req, state).await.map_err(|_| {
|
|
|
|
(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
"could not read multipart".to_string(),
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let mut body: Vec<Bytes> = {
|
|
|
|
let mut out = Vec::new();
|
|
|
|
|
|
|
|
while let Ok(Some(field)) = multipart.next_field().await {
|
|
|
|
out.push(field.bytes().await.map_err(|_| {
|
|
|
|
(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
"could not read field as bytes".to_string(),
|
|
|
|
)
|
|
|
|
})?);
|
|
|
|
}
|
|
|
|
|
|
|
|
out
|
|
|
|
};
|
|
|
|
|
|
|
|
let last = match body.pop() {
|
|
|
|
Some(b) => b,
|
|
|
|
None => {
|
|
|
|
return Err((
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
"could not read json data".to_string(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let json: T = match serde_json::from_str(&match String::from_utf8(last.to_vec()) {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(_) => return Err((StatusCode::BAD_REQUEST, "json data isn't utf8".to_string())),
|
|
|
|
}) {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(_) => {
|
|
|
|
return Err((
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
"could not parse json data as json".to_string(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Self(body, json))
|
|
|
|
}
|
|
|
|
}
|