fix: user avatar mime change from gif to avif

This commit is contained in:
trisua 2025-06-25 23:15:24 -04:00
parent ffdb767518
commit 6e0f2985b9
20 changed files with 219 additions and 104 deletions

View file

@ -219,7 +219,7 @@ pub async fn upload_avatar_request(
std::fs::write(&path, img.0).unwrap();
// update user settings
auth_user.settings.avatar_mime = mime.to_string();
auth_user.settings.avatar_mime = "image/gif".to_string();
if let Err(e) = data
.update_user_settings(auth_user.id, auth_user.settings)
.await
@ -240,6 +240,15 @@ pub async fn upload_avatar_request(
return Json(Error::FileTooLarge.into());
}
// update user settings
auth_user.settings.avatar_mime = "image/avif".to_string();
if let Err(e) = data
.update_user_settings(auth_user.id, auth_user.settings)
.await
{
return Json(e.into());
}
// upload image
let mut bytes = Vec::new();
@ -247,32 +256,12 @@ pub async fn upload_avatar_request(
bytes.push(byte);
}
match save_buffer(
&path,
bytes,
if mime == "image/gif" {
image::ImageFormat::Gif
} else {
image::ImageFormat::Avif
},
) {
Ok(_) => {
// update user settings
auth_user.settings.avatar_mime = mime.to_string();
if let Err(e) = data
.update_user_settings(auth_user.id, auth_user.settings)
.await
{
return Json(e.into());
}
// ...
Json(ApiReturn {
ok: true,
message: "Avatar uploaded. It might take a bit to update".to_string(),
payload: (),
})
}
match save_buffer(&path, bytes, image::ImageFormat::Avif) {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Avatar uploaded. It might take a bit to update".to_string(),
payload: (),
}),
Err(e) => Json(Error::MiscError(e.to_string()).into()),
}
}
@ -331,7 +320,7 @@ pub async fn upload_banner_request(
std::fs::write(&path, img.0).unwrap();
// update user settings
auth_user.settings.banner_mime = mime.to_string();
auth_user.settings.banner_mime = "image/gif".to_string();
if let Err(e) = data
.update_user_settings(auth_user.id, auth_user.settings)
.await
@ -352,6 +341,15 @@ pub async fn upload_banner_request(
return Json(Error::FileTooLarge.into());
}
// update user settings
auth_user.settings.avatar_mime = "image/avif".to_string();
if let Err(e) = data
.update_user_settings(auth_user.id, auth_user.settings)
.await
{
return Json(e.into());
}
// upload image
let mut bytes = Vec::new();
@ -359,32 +357,12 @@ pub async fn upload_banner_request(
bytes.push(byte);
}
match save_buffer(
&path,
bytes,
if mime == "image/gif" {
image::ImageFormat::Gif
} else {
image::ImageFormat::Avif
},
) {
Ok(_) => {
// update user settings
auth_user.settings.banner_mime = mime.to_string();
if let Err(e) = data
.update_user_settings(auth_user.id, auth_user.settings)
.await
{
return Json(e.into());
}
// ...
Json(ApiReturn {
ok: true,
message: "Banner uploaded. It might take a bit to update".to_string(),
payload: (),
})
}
match save_buffer(&path, bytes, image::ImageFormat::Avif) {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Banner uploaded. It might take a bit to update".to_string(),
payload: (),
}),
Err(e) => Json(Error::MiscError(e.to_string()).into()),
}
}

View file

@ -708,7 +708,7 @@ pub async fn post_to_socket_request(
None => return Json(Error::NotAllowed.into()),
};
if user.id.to_string() != user_id {
if user.id.to_string() != user_id && !user.permissions.check(FinePermission::MANAGE_USERS) {
return Json(Error::NotAllowed.into());
}

View file

@ -0,0 +1,47 @@
use axum::{
extract::Path,
response::{Html, IntoResponse},
Extension,
};
use axum_extra::extract::CookieJar;
use tetratto_core::model::{permissions::FinePermission, Error};
use crate::{get_user_from_token, State};
use super::render_error;
/// `/links/{id}`
pub async fn navigate_request(
jar: CookieJar,
Path(id): Path<usize>,
Extension(data): Extension<State>,
) -> impl IntoResponse {
let data = data.read().await;
let user = match get_user_from_token!(jar, data.0) {
Some(ua) => ua,
None => {
return Err(Html(
render_error(Error::NotAllowed, &jar, &data, &None).await,
));
}
};
let link = match data.0.get_link_by_id(id).await {
Ok(x) => x,
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
};
let owner = match data.0.get_user_by_id(link.owner).await {
Ok(x) => x,
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
};
if owner.permissions.check(FinePermission::SUPPORTER) {
if let Err(e) = data.0.incr_link_clicks(link.id).await {
return Err(Html(render_error(e, &jar, &data, &Some(user)).await));
}
}
Ok(Html(format!(
"<!doctype html /><html><head><meta http-equiv=\"refresh\" content=\"0; url={}\" /></head><body>Navigating...</body></html>",
link.href
)))
}

View file

@ -4,6 +4,7 @@ pub mod communities;
pub mod developer;
pub mod forge;
pub mod journals;
pub mod links;
pub mod misc;
pub mod mod_panel;
pub mod profile;
@ -137,6 +138,8 @@ pub fn routes() -> Router {
.route("/journals/{journal}/{note}", get(journals::app_request))
.route("/@{owner}/{journal}", get(journals::index_view_request))
.route("/@{owner}/{journal}/{note}", get(journals::view_request))
// links
.route("/links/{id}", get(links::navigate_request))
}
pub async fn render_error(