Make internal drag and drop robust
This commit is contained in:
@@ -122,6 +122,28 @@ function unav_copy_recursive(string $source, string $destination): void {
|
||||
}
|
||||
}
|
||||
|
||||
function unav_delete_recursive(string $path): void {
|
||||
if (is_link($path) || is_file($path)) {
|
||||
if (!unlink($path)) {
|
||||
unav_error(500, 'Could not remove source after move');
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (is_dir($path)) {
|
||||
foreach (scandir($path) ?: [] as $entry) {
|
||||
if ($entry === '.' || $entry === '..') {
|
||||
continue;
|
||||
}
|
||||
unav_delete_recursive($path . '/' . $entry);
|
||||
}
|
||||
if (!rmdir($path)) {
|
||||
unav_error(500, 'Could not remove source directory after move');
|
||||
}
|
||||
return;
|
||||
}
|
||||
unav_error(400, 'Only files and directories can be moved');
|
||||
}
|
||||
|
||||
function unav_guard_destination(string $source, string $destination): void {
|
||||
if (file_exists($destination)) {
|
||||
unav_error(409, 'Destination already exists');
|
||||
|
||||
@@ -24,7 +24,8 @@ unav_guard_destination($source['path'], $destination['path']);
|
||||
|
||||
if ($action === 'move') {
|
||||
if (!rename($source['path'], $destination['path'])) {
|
||||
unav_error(500, 'Move failed');
|
||||
unav_copy_recursive($source['path'], $destination['path']);
|
||||
unav_delete_recursive($source['path']);
|
||||
}
|
||||
} elseif ($action === 'copy') {
|
||||
unav_copy_recursive($source['path'], $destination['path']);
|
||||
|
||||
Reference in New Issue
Block a user