add: uploads in post quotes

This commit is contained in:
trisua 2025-09-07 11:06:03 -04:00
parent abd23e0ccc
commit 6e4fd3da36
6 changed files with 84 additions and 16 deletions

View file

@ -241,7 +241,7 @@ pub async fn create_repost_request(
jar: CookieJar,
Extension(data): Extension<State>,
Path(id): Path<usize>,
Json(req): Json<CreateRepost>,
JsonMultipart(images, req): JsonMultipart<CreateRepost>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserCreatePosts) {
@ -264,13 +264,69 @@ pub async fn create_repost_request(
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
};
// check sizes
for img in &images {
if img.len() > MAXIMUM_FILE_SIZE {
return Json(Error::FileTooLarge.into());
}
}
// create uploads
for _ in 0..images.len() {
props.uploads.push(
match data
.2
.create_upload(MediaUpload::new(
MediaType::Webp,
props.owner,
"post_media".to_string(),
))
.await
{
Ok(u) => u.id,
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
},
);
}
// ...
let uploads = props.uploads.clone();
match data.create_post(props).await {
Ok(id) => Json(ApiReturn {
ok: true,
message: "Post reposted".to_string(),
payload: Some(id.to_string()),
}),
Ok(id) => {
// write to uploads
for (i, upload_id) in uploads.iter().enumerate() {
let image = match images.get(i) {
Some(img) => img,
None => {
if let Err(e) = data.2.delete_upload(*upload_id).await {
return Json(Error::MiscError(e.to_string()).into());
}
continue;
}
};
let upload = match data.2.get_upload_by_id(*upload_id).await {
Ok(u) => u,
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
};
if let Err(e) = save_webp_buffer(
&upload.path(&data.2.0.0.directory).to_string(),
image.to_vec(),
None,
) {
return Json(Error::MiscError(e.to_string()).into());
}
}
// ...
Json(ApiReturn {
ok: true,
message: "Post reposted".to_string(),
payload: Some(id.to_string()),
})
}
Err(e) => Json(e.into()),
}
}