Ensure Move endpoint does not overwrite an existing file #24

Merged
cameron merged 1 commits from feature/fix-file-move-overwrite into master 2024-01-22 02:37:30 +00:00
Showing only changes of commit 0faad2fbdb - Show all commits

View File

@@ -192,11 +192,21 @@ pub async fn move_file<FS: FileSystemAccess>(
request: web::Json<MoveFileRequest>, request: web::Json<MoveFileRequest>,
) -> HttpResponse { ) -> HttpResponse {
match is_valid_full_path(&app_state.base_path, &request.source, false) match is_valid_full_path(&app_state.base_path, &request.source, false)
.ok_or(ErrorKind::InvalidData)
.and_then(|source| { .and_then(|source| {
is_valid_full_path(&app_state.base_path, &request.destination, true) is_valid_full_path(&app_state.base_path, &request.destination, true)
.ok_or(ErrorKind::InvalidData)
.and_then(|dest| {
if dest.exists() {
error!("Destination already exists, not moving file: {:?}", source);
Err(ErrorKind::AlreadyExists)
} else {
Ok(dest)
}
})
.map(|dest| (source, dest)) .map(|dest| (source, dest))
}) })
.ok_or(ErrorKind::InvalidData)
.map(|(source, dest)| file_system.move_file(source, dest)) .map(|(source, dest)| file_system.move_file(source, dest))
{ {
Ok(_) => { Ok(_) => {