mirror of
https://github.com/crivion/laranode.git
synced 2026-05-23 12:49:32 +08:00
41 lines
1.0 KiB
PHP
41 lines
1.0 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\Filemanager;
|
|
|
|
use Illuminate\Http\Request;
|
|
use League\Flysystem\Filesystem;
|
|
|
|
class DeleteFilesAction
|
|
{
|
|
|
|
public function __construct(private Filesystem $filesystem) {}
|
|
|
|
public function execute(Request $r)
|
|
{
|
|
$r->validate([
|
|
'filesToDelete' => 'required|array',
|
|
]);
|
|
|
|
|
|
try {
|
|
$filesystem = $this->filesystem;
|
|
|
|
foreach ($r->filesToDelete as $file) {
|
|
if (!$filesystem->fileExists($file) && !$filesystem->directoryExists($file)) {
|
|
throw new \Exception($file . ' does not exist!');
|
|
}
|
|
|
|
$filesystem->fileExists($file) ? $filesystem->delete($file) : $filesystem->deleteDirectory($file);
|
|
}
|
|
|
|
return response()->json([
|
|
'message' => 'Files deleted successfully!',
|
|
]);
|
|
} catch (\Exception $exception) {
|
|
return response()->json([
|
|
'error' => $exception->getMessage(),
|
|
], 500);
|
|
};
|
|
}
|
|
}
|