add: ability to manage uploads
This commit is contained in:
parent
6fabb38c10
commit
eb95be0f38
11 changed files with 234 additions and 48 deletions
|
@ -1,8 +1,6 @@
|
|||
use std::fs::exists;
|
||||
use axum::{body::Body, extract::Path, response::IntoResponse, Extension, Json};
|
||||
use axum::{extract::Path, response::IntoResponse, Extension, Json};
|
||||
use axum_extra::extract::CookieJar;
|
||||
use image::ImageFormat;
|
||||
use pathbufd::PathBufD;
|
||||
use tetratto_core::model::{
|
||||
communities::Post,
|
||||
permissions::FinePermission,
|
||||
|
@ -12,14 +10,12 @@ use tetratto_core::model::{
|
|||
use crate::{
|
||||
get_user_from_token,
|
||||
image::{save_buffer, JsonMultipart},
|
||||
routes::api::v1::{
|
||||
auth::images::read_image, CreatePost, CreateRepost, UpdatePostContent, UpdatePostContext,
|
||||
},
|
||||
routes::api::v1::{CreatePost, CreateRepost, UpdatePostContent, UpdatePostContext},
|
||||
State,
|
||||
};
|
||||
|
||||
// maximum file dimensions: 2048x2048px (4 MiB)
|
||||
pub const MAXIUMUM_FILE_SIZE: usize = 4194304;
|
||||
pub const MAXIMUM_FILE_SIZE: usize = 4194304;
|
||||
|
||||
pub async fn create_request(
|
||||
jar: CookieJar,
|
||||
|
@ -74,7 +70,7 @@ pub async fn create_request(
|
|||
|
||||
// check sizes
|
||||
for img in &images {
|
||||
if img.len() > MAXIUMUM_FILE_SIZE {
|
||||
if img.len() > MAXIMUM_FILE_SIZE {
|
||||
return Json(Error::DataTooLong("image".to_string()).into());
|
||||
}
|
||||
}
|
||||
|
@ -166,32 +162,6 @@ pub async fn create_repost_request(
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn get_upload_request(
|
||||
Path(id): Path<usize>,
|
||||
Extension(data): Extension<State>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
|
||||
let upload = data.get_upload_by_id(id).await.unwrap();
|
||||
let path = upload.path(&data.0);
|
||||
|
||||
if !exists(&path).unwrap() {
|
||||
return Err((
|
||||
[("Content-Type", "image/svg+xml")],
|
||||
Body::from(read_image(PathBufD::current().extend(&[
|
||||
data.0.dirs.media.as_str(),
|
||||
"images",
|
||||
"default-banner.svg",
|
||||
]))),
|
||||
));
|
||||
}
|
||||
|
||||
Ok((
|
||||
[("Content-Type", upload.what.mime())],
|
||||
Body::from(read_image(path)),
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn delete_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue