fix: username and community title validation

This commit is contained in:
trisua 2025-05-19 19:31:12 -04:00
parent a4d7f44aa3
commit efb259764e
7 changed files with 79 additions and 25 deletions

View file

@ -167,7 +167,7 @@ pub fn save_buffer(path: &str, bytes: Vec<u8>, format: image::ImageFormat) -> st
const WEBP_ENCODE_QUALITY: f32 = 85.0;
/// Create a WEBP image buffer given an input of `bytes`.
pub fn save_webp_buffer(path: &str, bytes: Vec<u8>) -> std::io::Result<()> {
pub fn save_webp_buffer(path: &str, bytes: Vec<u8>, quality: Option<f32>) -> std::io::Result<()> {
let img = match image::load_from_memory(&bytes) {
Ok(i) => i,
Err(_) => {
@ -188,7 +188,10 @@ pub fn save_webp_buffer(path: &str, bytes: Vec<u8>) -> std::io::Result<()> {
}
};
let mem = encoder.encode(WEBP_ENCODE_QUALITY);
let mem = encoder.encode(match quality {
Some(q) => q,
None => WEBP_ENCODE_QUALITY,
});
if std::fs::write(path, &*mem).is_err() {
return Err(std::io::Error::new(

View file

@ -1,10 +1,8 @@
use std::fs::exists;
use image::ImageFormat;
use pathbufd::PathBufD;
use crate::{
get_user_from_token,
image::{save_buffer, Image},
image::{save_webp_buffer, Image},
routes::api::v1::{auth::images::read_image, UpdateEmojiName},
State,
};
@ -146,11 +144,9 @@ pub async fn create_request(
return Json(Error::MiscError(e.to_string()).into());
}
} else {
if let Err(e) = save_buffer(
&upload.path(&data.0).to_string(),
img.0.to_vec(),
ImageFormat::WebP,
) {
if let Err(e) =
save_webp_buffer(&upload.path(&data.0).to_string(), img.0.to_vec(), None)
{
return Json(Error::MiscError(e.to_string()).into());
}
}

View file

@ -108,7 +108,8 @@ pub async fn create_request(
Err(e) => return Json(e.into()),
};
if let Err(e) = save_webp_buffer(&upload.path(&data.0).to_string(), image.to_vec())
if let Err(e) =
save_webp_buffer(&upload.path(&data.0).to_string(), image.to_vec(), None)
{
return Json(Error::MiscError(e.to_string()).into());
}