67 lines
1.9 KiB
Rust
67 lines
1.9 KiB
Rust
use std::fs::exists;
|
|
use axum::{body::Body, extract::Path, response::IntoResponse, Extension, Json};
|
|
use axum_extra::extract::CookieJar;
|
|
use pathbufd::PathBufD;
|
|
use crate::{get_user_from_token, State};
|
|
use super::auth::images::read_image;
|
|
use tetratto_core::model::{oauth, ApiReturn, Error};
|
|
|
|
pub async fn get_request(
|
|
Path(id): Path<usize>,
|
|
Extension(data): Extension<State>,
|
|
) -> impl IntoResponse {
|
|
let data = &(data.read().await).0;
|
|
|
|
let upload = match data.get_upload_by_id(id).await {
|
|
Ok(u) => u,
|
|
Err(_) => {
|
|
return Err((
|
|
[("Content-Type", "image/svg+xml")],
|
|
Body::from(read_image(PathBufD::current().extend(&[
|
|
data.0.0.dirs.media.as_str(),
|
|
"images",
|
|
"default-avatar.svg",
|
|
]))),
|
|
));
|
|
}
|
|
};
|
|
|
|
let path = upload.path(&data.0.0);
|
|
|
|
if !exists(&path).unwrap() {
|
|
return Err((
|
|
[("Content-Type", "image/svg+xml")],
|
|
Body::from(read_image(PathBufD::current().extend(&[
|
|
data.0.0.dirs.media.as_str(),
|
|
"images",
|
|
"default-avatar.svg",
|
|
]))),
|
|
));
|
|
}
|
|
|
|
Ok((
|
|
[("Content-Type", upload.what.mime())],
|
|
Body::from(read_image(path)),
|
|
))
|
|
}
|
|
|
|
pub async fn delete_request(
|
|
jar: CookieJar,
|
|
Extension(data): Extension<State>,
|
|
Path(id): Path<usize>,
|
|
) -> impl IntoResponse {
|
|
let data = &(data.read().await).0;
|
|
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserManageUploads) {
|
|
Some(ua) => ua,
|
|
None => return Json(Error::NotAllowed.into()),
|
|
};
|
|
|
|
match data.delete_upload_checked(id, &user).await {
|
|
Ok(_) => Json(ApiReturn {
|
|
ok: true,
|
|
message: "Upload deleted".to_string(),
|
|
payload: (),
|
|
}),
|
|
Err(e) => Json(e.into()),
|
|
}
|
|
}
|