add: ability to manage uploads
This commit is contained in:
parent
6fabb38c10
commit
eb95be0f38
11 changed files with 234 additions and 48 deletions
|
@ -164,8 +164,8 @@ pub async fn banner_request(
|
|||
))
|
||||
}
|
||||
|
||||
pub static MAXIUMUM_FILE_SIZE: usize = 8388608;
|
||||
pub static MAXIUMUM_GIF_FILE_SIZE: usize = 2097152;
|
||||
pub static MAXIMUM_FILE_SIZE: usize = 8388608;
|
||||
pub static MAXIMUM_GIF_FILE_SIZE: usize = 2097152;
|
||||
|
||||
/// Upload avatar
|
||||
pub async fn upload_avatar_request(
|
||||
|
@ -223,7 +223,7 @@ pub async fn upload_avatar_request(
|
|||
// upload image (gif)
|
||||
if mime == "image/gif" {
|
||||
// gif image, don't encode
|
||||
if img.0.len() > MAXIUMUM_GIF_FILE_SIZE {
|
||||
if img.0.len() > MAXIMUM_GIF_FILE_SIZE {
|
||||
return Json(Error::DataTooLong("gif".to_string()).into());
|
||||
}
|
||||
|
||||
|
@ -236,7 +236,7 @@ pub async fn upload_avatar_request(
|
|||
}
|
||||
|
||||
// check file size
|
||||
if img.0.len() > MAXIUMUM_FILE_SIZE {
|
||||
if img.0.len() > MAXIMUM_FILE_SIZE {
|
||||
return Json(Error::DataTooLong("image".to_string()).into());
|
||||
}
|
||||
|
||||
|
@ -321,7 +321,7 @@ pub async fn upload_banner_request(
|
|||
// upload image (gif)
|
||||
if mime == "image/gif" {
|
||||
// gif image, don't encode
|
||||
if img.0.len() > MAXIUMUM_GIF_FILE_SIZE {
|
||||
if img.0.len() > MAXIMUM_GIF_FILE_SIZE {
|
||||
return Json(Error::DataTooLong("gif".to_string()).into());
|
||||
}
|
||||
|
||||
|
@ -334,7 +334,7 @@ pub async fn upload_banner_request(
|
|||
}
|
||||
|
||||
// check file size
|
||||
if img.0.len() > MAXIUMUM_FILE_SIZE {
|
||||
if img.0.len() > MAXIMUM_FILE_SIZE {
|
||||
return Json(Error::DataTooLong("image".to_string()).into());
|
||||
}
|
||||
|
||||
|
|
|
@ -81,7 +81,7 @@ pub async fn get_request(
|
|||
}
|
||||
|
||||
// maximum file dimensions: 512x512px (256KiB)
|
||||
pub const MAXIUMUM_FILE_SIZE: usize = 262144;
|
||||
pub const MAXIMUM_FILE_SIZE: usize = 262144;
|
||||
|
||||
pub async fn create_request(
|
||||
jar: CookieJar,
|
||||
|
@ -96,7 +96,7 @@ pub async fn create_request(
|
|||
};
|
||||
|
||||
// check file size
|
||||
if img.0.len() > MAXIUMUM_FILE_SIZE {
|
||||
if img.0.len() > MAXIMUM_FILE_SIZE {
|
||||
return Json(Error::DataTooLong("image".to_string()).into());
|
||||
}
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ use crate::{
|
|||
State,
|
||||
image::{Image, save_buffer},
|
||||
get_user_from_token,
|
||||
routes::api::v1::auth::images::{MAXIUMUM_FILE_SIZE, read_image},
|
||||
routes::api::v1::auth::images::{MAXIMUM_FILE_SIZE, read_image},
|
||||
};
|
||||
|
||||
/// Get a community's avatar image
|
||||
|
@ -135,7 +135,7 @@ pub async fn upload_avatar_request(
|
|||
);
|
||||
|
||||
// check file size
|
||||
if img.0.len() > MAXIUMUM_FILE_SIZE {
|
||||
if img.0.len() > MAXIMUM_FILE_SIZE {
|
||||
return Json(Error::DataTooLong("image".to_string()).into());
|
||||
}
|
||||
|
||||
|
@ -190,7 +190,7 @@ pub async fn upload_banner_request(
|
|||
);
|
||||
|
||||
// check file size
|
||||
if img.0.len() > MAXIUMUM_FILE_SIZE {
|
||||
if img.0.len() > MAXIMUM_FILE_SIZE {
|
||||
return Json(Error::DataTooLong("image".to_string()).into());
|
||||
}
|
||||
|
||||
|
|
|
@ -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>,
|
||||
|
|
|
@ -5,6 +5,7 @@ pub mod reactions;
|
|||
pub mod reports;
|
||||
pub mod requests;
|
||||
pub mod stacks;
|
||||
pub mod uploads;
|
||||
pub mod util;
|
||||
|
||||
#[cfg(feature = "redis")]
|
||||
|
@ -349,7 +350,8 @@ pub fn routes() -> Router {
|
|||
.route("/stacks/{id}/users", delete(stacks::remove_user_request))
|
||||
.route("/stacks/{id}", delete(stacks::delete_request))
|
||||
// uploads
|
||||
.route("/uploads/{id}", get(communities::posts::get_upload_request))
|
||||
.route("/uploads/{id}", get(uploads::get_request))
|
||||
.route("/uploads/{id}", delete(uploads::delete_request))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
|
54
crates/app/src/routes/api/v1/uploads.rs
Normal file
54
crates/app/src/routes/api/v1/uploads.rs
Normal file
|
@ -0,0 +1,54 @@
|
|||
use std::fs::exists;
|
||||
use axum::{body::Body, extract::Path, response::IntoResponse, Extension, Json};
|
||||
use axum_extra::extract::CookieJar;
|
||||
use pathbufd::PathBufD;
|
||||
use crate::{get_user_from_token, State};
|
||||
use super::auth::images::read_image;
|
||||
use tetratto_core::model::{ApiReturn, Error};
|
||||
|
||||
pub async fn get_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-avatar.svg",
|
||||
]))),
|
||||
));
|
||||
}
|
||||
|
||||
Ok((
|
||||
[("Content-Type", upload.what.mime())],
|
||||
Body::from(read_image(path)),
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn delete_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Path(id): Path<usize>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data) {
|
||||
Some(ua) => ua,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
match data.delete_upload_checked(id, &user).await {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Upload deleted".to_string(),
|
||||
payload: (),
|
||||
}),
|
||||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue