add: post image uploads (supporter)
This commit is contained in:
parent
ba1f8ef063
commit
70965298b5
18 changed files with 455 additions and 50 deletions
|
@ -309,7 +309,7 @@ pub async fn delete_membership(
|
|||
Err(e) => return Json(e.into()),
|
||||
};
|
||||
|
||||
match data.delete_membership(membership.id, user).await {
|
||||
match data.delete_membership(membership.id, &user).await {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Membership deleted".to_string(),
|
||||
|
|
|
@ -1,17 +1,30 @@
|
|||
use axum::{Extension, Json, extract::Path, response::IntoResponse};
|
||||
use std::fs::exists;
|
||||
use axum::{body::Body, extract::Path, response::IntoResponse, Extension, Json};
|
||||
use axum_extra::extract::CookieJar;
|
||||
use tetratto_core::model::{ApiReturn, Error, communities::Post};
|
||||
|
||||
use image::ImageFormat;
|
||||
use pathbufd::PathBufD;
|
||||
use tetratto_core::model::{
|
||||
communities::Post,
|
||||
permissions::FinePermission,
|
||||
uploads::{MediaType, MediaUpload},
|
||||
ApiReturn, Error,
|
||||
};
|
||||
use crate::{
|
||||
get_user_from_token,
|
||||
routes::api::v1::{CreatePost, CreateRepost, UpdatePostContent, UpdatePostContext},
|
||||
image::{save_buffer, JsonMultipart},
|
||||
routes::api::v1::{
|
||||
auth::images::read_image, CreatePost, CreateRepost, UpdatePostContent, UpdatePostContext,
|
||||
},
|
||||
State,
|
||||
};
|
||||
|
||||
// maximum file dimensions: 2048x2048px (4 MiB)
|
||||
pub const MAXIUMUM_FILE_SIZE: usize = 4194304;
|
||||
|
||||
pub async fn create_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Json(req): Json<CreatePost>,
|
||||
JsonMultipart(images, req): JsonMultipart<CreatePost>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data) {
|
||||
|
@ -19,6 +32,15 @@ pub async fn create_request(
|
|||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
if !user.permissions.check(FinePermission::SUPPORTER) {
|
||||
if images.len() > 0 {
|
||||
// this is currently supporter only until it's been tested better...
|
||||
// after it's fully release, file limit will be raised to 8 MiB for supporters,
|
||||
// and left at 4 for non-supporters
|
||||
return Json(Error::RequiresSupporter.into());
|
||||
}
|
||||
}
|
||||
|
||||
let mut props = Post::new(
|
||||
req.content,
|
||||
match req.community.parse::<usize>() {
|
||||
|
@ -44,12 +66,63 @@ pub async fn create_request(
|
|||
};
|
||||
}
|
||||
|
||||
match data.create_post(props).await {
|
||||
Ok(id) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Post created".to_string(),
|
||||
payload: Some(id.to_string()),
|
||||
}),
|
||||
// check sizes
|
||||
for img in &images {
|
||||
if img.len() > MAXIUMUM_FILE_SIZE {
|
||||
return Json(Error::DataTooLong("image".to_string()).into());
|
||||
}
|
||||
}
|
||||
|
||||
// create uploads
|
||||
for _ in 0..images.len() {
|
||||
props.uploads.push(
|
||||
match data
|
||||
.create_upload(MediaUpload::new(MediaType::Webp, props.owner))
|
||||
.await
|
||||
{
|
||||
Ok(u) => u.id,
|
||||
Err(e) => return Json(e.into()),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// ...
|
||||
match data.create_post(props.clone()).await {
|
||||
Ok(id) => {
|
||||
// write to uploads
|
||||
for (i, upload_id) in props.uploads.iter().enumerate() {
|
||||
let image = match images.get(i) {
|
||||
Some(img) => img,
|
||||
None => {
|
||||
if let Err(e) = data.delete_upload(*upload_id).await {
|
||||
return Json(e.into());
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let upload = match data.get_upload_by_id(*upload_id).await {
|
||||
Ok(u) => u,
|
||||
Err(e) => return Json(e.into()),
|
||||
};
|
||||
|
||||
if let Err(e) = save_buffer(
|
||||
&upload.path(&data.0).to_string(),
|
||||
image.to_vec(),
|
||||
ImageFormat::WebP,
|
||||
) {
|
||||
return Json(Error::MiscError(e.to_string()).into());
|
||||
}
|
||||
}
|
||||
|
||||
// return
|
||||
Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Post created".to_string(),
|
||||
payload: Some(id.to_string()),
|
||||
})
|
||||
}
|
||||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
||||
|
@ -87,6 +160,32 @@ 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>,
|
||||
|
|
|
@ -348,6 +348,8 @@ pub fn routes() -> Router {
|
|||
.route("/stacks/{id}/users", post(stacks::add_user_request))
|
||||
.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))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue