|
| 1 | +use axum::{ |
| 2 | + Router, |
| 3 | + extract::{DefaultBodyLimit, Multipart}, |
| 4 | + http::StatusCode, |
| 5 | +}; |
| 6 | +use cds_db::{entity::user::Group, get_db}; |
| 7 | +use sea_orm::{ColumnTrait, EntityTrait, QueryFilter}; |
| 8 | +use serde_json::json; |
| 9 | + |
| 10 | +use crate::{ |
| 11 | + extract::{Extension, Path}, |
| 12 | + traits::{Ext, WebError, WebResponse}, |
| 13 | +}; |
| 14 | + |
| 15 | +pub fn router() -> Router { |
| 16 | + Router::new() |
| 17 | + .route( |
| 18 | + "/", |
| 19 | + axum::routing::post(save_challenge_attachment) |
| 20 | + .layer(DefaultBodyLimit::max(512 * 1024 * 1024 /* MB */)), |
| 21 | + ) |
| 22 | + .route("/", axum::routing::delete(delete_challenge_attachment)) |
| 23 | +} |
| 24 | + |
| 25 | +pub async fn save_challenge_attachment( |
| 26 | + Extension(ext): Extension<Ext>, Path(challenge_id): Path<uuid::Uuid>, mut multipart: Multipart, |
| 27 | +) -> Result<WebResponse<()>, WebError> { |
| 28 | + let operator = ext.operator.ok_or(WebError::Unauthorized(json!("")))?; |
| 29 | + if operator.group != Group::Admin { |
| 30 | + return Err(WebError::Forbidden(json!(""))); |
| 31 | + } |
| 32 | + |
| 33 | + let challenge = cds_db::entity::challenge::Entity::find_by_id(challenge_id) |
| 34 | + .filter(cds_db::entity::challenge::Column::DeletedAt.is_null()) |
| 35 | + .one(get_db()) |
| 36 | + .await? |
| 37 | + .ok_or(WebError::BadRequest(json!("challenge_not_found")))?; |
| 38 | + |
| 39 | + let path = format!("challenges/{}/attachment", challenge.id); |
| 40 | + let mut filename = String::new(); |
| 41 | + let mut data = Vec::<u8>::new(); |
| 42 | + while let Some(field) = multipart.next_field().await.unwrap() { |
| 43 | + if field.name() == Some("file") { |
| 44 | + filename = field.file_name().unwrap().to_string(); |
| 45 | + data = match field.bytes().await { |
| 46 | + Ok(bytes) => bytes.to_vec(), |
| 47 | + Err(_err) => { |
| 48 | + return Err(WebError::BadRequest(json!("size_too_large"))); |
| 49 | + } |
| 50 | + }; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + cds_media::delete_dir(path.clone()).await?; |
| 55 | + |
| 56 | + cds_media::save(path, filename, data) |
| 57 | + .await |
| 58 | + .map_err(|_| WebError::InternalServerError(json!("")))?; |
| 59 | + |
| 60 | + Ok(WebResponse { |
| 61 | + code: StatusCode::OK, |
| 62 | + ..Default::default() |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | +pub async fn delete_challenge_attachment( |
| 67 | + Extension(ext): Extension<Ext>, Path(challenge_id): Path<uuid::Uuid>, |
| 68 | +) -> Result<WebResponse<()>, WebError> { |
| 69 | + let operator = ext.operator.ok_or(WebError::Unauthorized(json!("")))?; |
| 70 | + if operator.group != Group::Admin { |
| 71 | + return Err(WebError::Forbidden(json!(""))); |
| 72 | + } |
| 73 | + |
| 74 | + let challenge = cds_db::entity::challenge::Entity::find_by_id(challenge_id) |
| 75 | + .filter(cds_db::entity::challenge::Column::DeletedAt.is_null()) |
| 76 | + .one(get_db()) |
| 77 | + .await? |
| 78 | + .ok_or(WebError::BadRequest(json!("challenge_not_found")))?; |
| 79 | + |
| 80 | + let path = format!("challenges/{}/attachment", challenge.id); |
| 81 | + |
| 82 | + cds_media::delete_dir(path) |
| 83 | + .await |
| 84 | + .map_err(|_| WebError::InternalServerError(json!("")))?; |
| 85 | + |
| 86 | + Ok(WebResponse { |
| 87 | + code: StatusCode::OK, |
| 88 | + ..Default::default() |
| 89 | + }) |
| 90 | +} |
0 commit comments