Skip to content

Commit

Permalink
fix: copy file only if file path changed
Browse files Browse the repository at this point in the history
  • Loading branch information
harshkhandeparkar committed Nov 19, 2024
1 parent 5e0293c commit d019180
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions backend/src/routing/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,29 @@ pub async fn edit(
let old_filepath = state.env_vars.paths.get_path_from_slug(&old_filelink);
let new_filepath = state.env_vars.paths.get_path_from_slug(&new_qp.filelink);

println!("{}, {}", new_filepath.to_string_lossy(), old_filepath.to_string_lossy());
if old_filepath.canonicalize()? != new_filepath.canonicalize()? {
if let Err(e) = fs::copy(old_filepath, new_filepath).await {
tracing::error!("Error copying file: {}", e);

if fs::copy(old_filepath, new_filepath).await.is_ok() {
// Commit the transaction
tx.commit().await?;
tx.rollback().await?;
Ok(BackendResponse::error(
"Error copying question paper file.".into(),
StatusCode::INTERNAL_SERVER_ERROR,
))
} else {
// Commit the transaction
tx.commit().await?;

Ok(BackendResponse::ok(
"Successfully updated paper details.".into(),
new_qp.with_url(&state.env_vars)?,
))
}
} else {
Ok(BackendResponse::ok(
"Successfully updated paper details.".into(),
new_qp.with_url(&state.env_vars)?,
))
} else {
tx.rollback().await?;
Ok(BackendResponse::error(
"Error copying question paper file.".into(),
StatusCode::INTERNAL_SERVER_ERROR,
))
}
}

Expand Down

0 comments on commit d019180

Please sign in to comment.