chore: move image stuff to axum-image
This commit is contained in:
parent
dbed2b2457
commit
e8cc541f45
15 changed files with 48 additions and 244 deletions
|
@ -1,9 +1,4 @@
|
|||
use crate::{
|
||||
cookie::CookieJar,
|
||||
get_user_from_token,
|
||||
image::{save_webp_buffer, JsonMultipart},
|
||||
State,
|
||||
};
|
||||
use crate::{cookie::CookieJar, get_user_from_token, State};
|
||||
use axum::{
|
||||
extract::Path,
|
||||
response::{Html, IntoResponse},
|
||||
|
@ -17,6 +12,7 @@ use tetratto_core::model::{
|
|||
ApiReturn, Error,
|
||||
};
|
||||
use super::{CreateAd, UpdateAdIsRunning};
|
||||
use axum_image::{encode::save_webp_buffer, extract::JsonMultipart};
|
||||
|
||||
const MAXIMUM_AD_FILE_SIZE: usize = 2_097_152;
|
||||
|
||||
|
|
|
@ -7,12 +7,8 @@ use tetratto_core::model::{
|
|||
uploads::{MediaType, MediaUpload},
|
||||
ApiReturn, Error,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
State,
|
||||
image::{Image, save_buffer},
|
||||
get_user_from_token,
|
||||
};
|
||||
use crate::{State, get_user_from_token};
|
||||
use axum_image::{encode::save_image_buffer, extract::Image};
|
||||
|
||||
pub fn read_image(path: PathBufD) -> Vec<u8> {
|
||||
let mut bytes = Vec::new();
|
||||
|
@ -102,7 +98,11 @@ pub async fn upload_avatar_request(
|
|||
}
|
||||
|
||||
// upload image
|
||||
match save_buffer(&path.to_string(), img.0.to_vec(), image::ImageFormat::Avif) {
|
||||
match save_image_buffer(
|
||||
&path.to_string(),
|
||||
img.0.to_vec(),
|
||||
axum_image::ImageFormat::Avif,
|
||||
) {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Avatar uploaded. It might take a bit to update".to_string(),
|
||||
|
@ -187,7 +187,11 @@ pub async fn upload_banner_request(
|
|||
}
|
||||
|
||||
// upload image
|
||||
match save_buffer(&path.to_string(), img.0.to_vec(), image::ImageFormat::Avif) {
|
||||
match save_image_buffer(
|
||||
&path.to_string(),
|
||||
img.0.to_vec(),
|
||||
axum_image::ImageFormat::Avif,
|
||||
) {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Banner uploaded. It might take a bit to update".to_string(),
|
||||
|
|
|
@ -2,7 +2,6 @@ use std::fs::exists;
|
|||
use pathbufd::PathBufD;
|
||||
use crate::{
|
||||
get_user_from_token,
|
||||
image::{save_webp_buffer, Image},
|
||||
routes::api::v1::{auth::images::read_image, UpdateEmojiName},
|
||||
State,
|
||||
};
|
||||
|
@ -13,6 +12,7 @@ use tetratto_core::model::{
|
|||
uploads::{CustomEmoji, MediaType, MediaUpload},
|
||||
ApiReturn, Error,
|
||||
};
|
||||
use axum_image::{encode::save_webp_buffer, extract::Image};
|
||||
|
||||
/// Expand a unicode emoji into its Gemoji shortcode.
|
||||
pub async fn get_emoji_shortcode(emoji: String) -> impl IntoResponse {
|
||||
|
|
|
@ -3,13 +3,11 @@ use crate::cookie::CookieJar;
|
|||
use pathbufd::{PathBufD, pathd};
|
||||
use std::fs::exists;
|
||||
use tetratto_core::model::{ApiReturn, Error, permissions::FinePermission, oauth};
|
||||
|
||||
use crate::{
|
||||
State,
|
||||
image::{Image, save_buffer},
|
||||
get_user_from_token,
|
||||
State, get_user_from_token,
|
||||
routes::api::v1::auth::images::{MAXIMUM_FILE_SIZE, read_image},
|
||||
};
|
||||
use axum_image::{encode::save_image_buffer, extract::Image};
|
||||
|
||||
/// Get a community's avatar image
|
||||
/// `/api/v1/communities/{id}/avatar`
|
||||
|
@ -146,7 +144,7 @@ pub async fn upload_avatar_request(
|
|||
bytes.push(byte);
|
||||
}
|
||||
|
||||
match save_buffer(&path, bytes, image::ImageFormat::Avif) {
|
||||
match save_image_buffer(&path, bytes, axum_image::ImageFormat::Avif) {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Avatar uploaded. It might take a bit to update".to_string(),
|
||||
|
@ -201,7 +199,7 @@ pub async fn upload_banner_request(
|
|||
bytes.push(byte);
|
||||
}
|
||||
|
||||
match save_buffer(&path, bytes, image::ImageFormat::Avif) {
|
||||
match save_image_buffer(&path, bytes, axum_image::ImageFormat::Avif) {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Banner uploaded. It might take a bit to update".to_string(),
|
||||
|
|
|
@ -16,7 +16,6 @@ use tetratto_core::model::{
|
|||
};
|
||||
use crate::{
|
||||
check_user_blocked_or_private, get_user_from_token,
|
||||
image::{save_webp_buffer, JsonMultipart},
|
||||
routes::{
|
||||
api::v1::{
|
||||
CreatePost, CreateRepost, UpdatePostContent, UpdatePostContext, UpdatePostIsOpen,
|
||||
|
@ -26,6 +25,7 @@ use crate::{
|
|||
},
|
||||
State,
|
||||
};
|
||||
use axum_image::{encode::save_webp_buffer, extract::JsonMultipart};
|
||||
|
||||
// maximum file dimensions: 2048x2048px (4 MiB)
|
||||
pub const MAXIMUM_FILE_SIZE: usize = 4194304;
|
||||
|
|
|
@ -15,10 +15,10 @@ use tetratto_core::model::{
|
|||
};
|
||||
use crate::{
|
||||
get_user_from_token,
|
||||
image::JsonMultipart,
|
||||
routes::{api::v1::CreateQuestion, pages::PaginatedQuery},
|
||||
State,
|
||||
};
|
||||
use axum_image::extract::JsonMultipart;
|
||||
|
||||
pub async fn create_request(
|
||||
jar: CookieJar,
|
||||
|
|
|
@ -704,7 +704,7 @@ pub fn routes() -> Router {
|
|||
delete(notes::delete_by_dir_request),
|
||||
)
|
||||
// uploads
|
||||
.route("/uploads/{id}", delete(uploads::delete_request))
|
||||
.route("/uploads/{bucket}/{id}", delete(uploads::delete_request))
|
||||
.route("/uploads/{id}/alt", post(uploads::update_alt_request))
|
||||
// services
|
||||
.route("/services", get(services::list_request))
|
||||
|
|
|
@ -1,9 +1,4 @@
|
|||
use crate::{
|
||||
cookie::CookieJar,
|
||||
get_user_from_token,
|
||||
image::{save_webp_buffer, JsonMultipart},
|
||||
State,
|
||||
};
|
||||
use crate::{cookie::CookieJar, get_user_from_token, State};
|
||||
use axum::{extract::Path, response::IntoResponse, Extension, Json};
|
||||
use tetratto_core::model::{
|
||||
economy::{Product, ProductFulfillmentMethod},
|
||||
|
@ -17,6 +12,7 @@ use super::{
|
|||
UpdateProductDescription, UpdateProductMethod, UpdateProductOnSale, UpdateProductPrice,
|
||||
UpdateProductSingleUse, UpdateProductStock, UpdateProductTitle, UpdateProductUploads,
|
||||
};
|
||||
use axum_image::{encode::save_webp_buffer, extract::JsonMultipart};
|
||||
|
||||
pub async fn create_request(
|
||||
jar: CookieJar,
|
||||
|
|
|
@ -6,7 +6,7 @@ use tetratto_core::model::{oauth, ApiReturn, Error};
|
|||
pub async fn delete_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Path(id): Path<usize>,
|
||||
Path((bucket, id)): Path<(String, usize)>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserManageUploads) {
|
||||
|
@ -14,7 +14,7 @@ pub async fn delete_request(
|
|||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
let upload = match data.2.get_upload_by_id(id).await {
|
||||
let upload = match data.2.get_upload_by_id_bucket(id, &bucket).await {
|
||||
Ok(x) => x,
|
||||
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
|
||||
};
|
||||
|
@ -23,7 +23,7 @@ pub async fn delete_request(
|
|||
return Json(Error::NotAllowed.into());
|
||||
}
|
||||
|
||||
match data.2.delete_upload(id).await {
|
||||
match data.2.delete_upload_with_bucket(id, &bucket).await {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Upload deleted".to_string(),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue