Skip to content

Commit

Permalink
fix: attachment could be downloaded when has_attachment is false
Browse files Browse the repository at this point in the history
  • Loading branch information
ElaBosak233 committed Jan 24, 2025
1 parent 59d14db commit 74319ce
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions crates/web/src/router/api/challenge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,16 @@ pub async fn get_attachment(
Extension(ext): Extension<Ext>, Path(id): Path<uuid::Uuid>,
) -> Result<impl IntoResponse, WebError> {
let _ = ext.operator.ok_or(WebError::Unauthorized(json!("")))?;

let challenge = cds_db::entity::challenge::Entity::find_by_id(id)
.one(get_db())
.await?
.ok_or(WebError::BadRequest(json!("challenge_not_found")))?;

if !challenge.has_attachment {
return Err(WebError::NotFound(json!("challenge_has_not_attachment")));
}

let path = format!("challenges/{}/attachment", id);
match cds_media::scan_dir(path.clone()).await?.first() {
Some((filename, _size)) => {
Expand All @@ -332,6 +342,15 @@ pub async fn get_attachment_metadata(
) -> Result<WebResponse<Metadata>, WebError> {
let _ = ext.operator.ok_or(WebError::Unauthorized(json!("")))?;

let challenge = cds_db::entity::challenge::Entity::find_by_id(id)
.one(get_db())
.await?
.ok_or(WebError::BadRequest(json!("challenge_not_found")))?;

if !challenge.has_attachment {
return Err(WebError::NotFound(json!("challenge_has_not_attachment")));
}

let path = format!("challenges/{}/attachment", id);
match cds_media::scan_dir(path.clone()).await?.first() {
Some((filename, size)) => Ok(WebResponse {
Expand Down

0 comments on commit 74319ce

Please sign in to comment.