Merge pull request 'Ensure Move endpoint does not overwrite an existing file' (#24) from feature/fix-file-move-overwrite into master

Reviewed-on: #24
This commit was merged in pull request #24.
This commit is contained in:
2024-01-22 02:37:29 +00:00

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(_) => {