add: journals base
add: avatar/banner upload endpoints
This commit is contained in:
parent
b3cac5f97a
commit
bb682add85
14 changed files with 323 additions and 22 deletions
|
@ -1,11 +1,17 @@
|
|||
use axum::{Extension, body::Body, extract::Path, response::IntoResponse};
|
||||
use pathbufd::PathBufD;
|
||||
use axum::{Extension, Json, body::Body, extract::Path, response::IntoResponse};
|
||||
use axum_extra::extract::CookieJar;
|
||||
use pathbufd::{PathBufD, pathd};
|
||||
use std::{
|
||||
fs::{File, exists},
|
||||
io::Read,
|
||||
};
|
||||
use tetratto_core::model::{ApiReturn, Error};
|
||||
|
||||
use crate::State;
|
||||
use crate::{
|
||||
State,
|
||||
avif::{Image, save_avif_buffer},
|
||||
get_user_from_token,
|
||||
};
|
||||
|
||||
pub fn read_image(path: PathBufD) -> Vec<u8> {
|
||||
let mut bytes = Vec::new();
|
||||
|
@ -100,3 +106,79 @@ pub async fn banner_request(
|
|||
Body::from(read_image(path)),
|
||||
)
|
||||
}
|
||||
|
||||
static MAXIUMUM_FILE_SIZE: usize = 8388608;
|
||||
|
||||
/// Upload avatar
|
||||
pub async fn upload_avatar_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
img: Image,
|
||||
) -> impl IntoResponse {
|
||||
// get user from token
|
||||
let data = &(data.read().await).0;
|
||||
let auth_user = match get_user_from_token!(jar, data) {
|
||||
Some(ua) => ua,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
let path = pathd!("{}/avatars/{}.avif", data.0.dirs.media, &auth_user.id);
|
||||
|
||||
// check file size
|
||||
if img.0.len() > MAXIUMUM_FILE_SIZE {
|
||||
return Json(Error::DataTooLong("image".to_string()).into());
|
||||
}
|
||||
|
||||
// upload image
|
||||
let mut bytes = Vec::new();
|
||||
|
||||
for byte in img.0 {
|
||||
bytes.push(byte);
|
||||
}
|
||||
|
||||
match save_avif_buffer(&path, bytes) {
|
||||
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()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Upload banner
|
||||
pub async fn upload_banner_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
img: Image,
|
||||
) -> impl IntoResponse {
|
||||
// get user from token
|
||||
let data = &(data.read().await).0;
|
||||
let auth_user = match get_user_from_token!(jar, data) {
|
||||
Some(ua) => ua,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
let path = pathd!("{}/banners/{}.avif", data.0.dirs.media, &auth_user.id);
|
||||
|
||||
// check file size
|
||||
if img.0.len() > MAXIUMUM_FILE_SIZE {
|
||||
return Json(Error::DataTooLong("image".to_string()).into());
|
||||
}
|
||||
|
||||
// upload image
|
||||
let mut bytes = Vec::new();
|
||||
|
||||
for byte in img.0 {
|
||||
bytes.push(byte);
|
||||
}
|
||||
|
||||
match save_avif_buffer(&path, bytes) {
|
||||
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()),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue