mirror of
https://github.com/crivion/laranode.git
synced 2026-06-20 08:32:13 +08:00
Delete Files functionining
This commit is contained in:
@@ -2,95 +2,58 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Actions\Filemanager\CreateFileAction;
|
||||
use App\Actions\Filemanager\DeleteFilesAction;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
use League\Flysystem\Filesystem;
|
||||
use League\Flysystem\Local\LocalFilesystemAdapter;
|
||||
use App\Actions\Filemanager\GetDirectoryContentsAction;
|
||||
use App\Actions\Filemanager\GetFileContentsAction;
|
||||
use App\Actions\Filemanager\UpdateFileContentsAction;
|
||||
use League\Flysystem\UnixVisibility\PortableVisibilityConverter;
|
||||
use Illuminate\Http\StreamedResponse;
|
||||
|
||||
class FilemanagerController extends Controller
|
||||
{
|
||||
public Filesystem $filesystem;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
// @todo: change to actual user path
|
||||
$path = base_path();
|
||||
$adapter = new LocalFilesystemAdapter($path, null, LOCK_EX, LocalFilesystemAdapter::DISALLOW_LINKS);
|
||||
|
||||
$this->filesystem = new Filesystem($adapter);
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
return Inertia::render('Filemanager/Filemanager');
|
||||
}
|
||||
|
||||
public function getContents(Request $r)
|
||||
public function getDirectoryContents(Request $r)
|
||||
{
|
||||
return (new GetDirectoryContentsAction($this->filesystem))->execute($r);
|
||||
}
|
||||
|
||||
// @todo: here we'll get the actual user path from db
|
||||
$path = base_path();
|
||||
$goBack = false;
|
||||
|
||||
|
||||
// create Filesystem
|
||||
$adapter = new LocalFilesystemAdapter($path, null, LOCK_EX, LocalFilesystemAdapter::DISALLOW_LINKS);
|
||||
$filesystem = new Filesystem($adapter);
|
||||
$recursive = false;
|
||||
|
||||
try {
|
||||
|
||||
// everything starts from ORIGINAL $path so no need to worry about ../ /.. etc tricks
|
||||
if ($r->has('path')) {
|
||||
$gotopath = $r->path;
|
||||
$path = $gotopath . '/';
|
||||
$goBack = explode('/', $gotopath);
|
||||
|
||||
if (count($goBack) == 1) {
|
||||
$goBack = '/';
|
||||
} else {
|
||||
// remove last path
|
||||
array_pop($goBack);
|
||||
$goBack = implode('/', $goBack);
|
||||
}
|
||||
} else {
|
||||
$path = './';
|
||||
}
|
||||
|
||||
return response()->streamJson([
|
||||
'files' => $filesystem->listContents($path, $recursive)->sortByPath(),
|
||||
'goBack' => $goBack
|
||||
]);
|
||||
} catch (\Exception $exception) {
|
||||
|
||||
return response()->json([
|
||||
'error' => $exception->getMessage(),
|
||||
], 500);
|
||||
};
|
||||
public function getFileContents(Request $r)
|
||||
{
|
||||
return (new GetFileContentsAction($this->filesystem))->execute($r);
|
||||
}
|
||||
|
||||
public function createFile(Request $r)
|
||||
{
|
||||
$r->validate([
|
||||
'path' => 'required',
|
||||
'fileType' => 'required|in:directory,file',
|
||||
'fileName' => 'required',
|
||||
]);
|
||||
return (new CreateFileAction($this->filesystem))->execute($r);
|
||||
}
|
||||
|
||||
try {
|
||||
// @todo: change to actual user path
|
||||
$path = base_path();
|
||||
$adapter = new LocalFilesystemAdapter($path, null, LOCK_EX, LocalFilesystemAdapter::DISALLOW_LINKS);
|
||||
$filesystem = new Filesystem($adapter);
|
||||
public function updateFileContents(Request $r)
|
||||
{
|
||||
return (new UpdateFileContentsAction($this->filesystem))->execute($r);
|
||||
}
|
||||
|
||||
if ($filesystem->fileExists($r->path . '/' . $r->fileName) || $filesystem->directoryExists($r->path . '/' . $r->fileName)) {
|
||||
throw new \Exception($r->fileType . ' ' . $r->path . '/' . $r->fileName . ' already exists!');
|
||||
}
|
||||
|
||||
if ($r->fileType == 'directory') {
|
||||
$filesystem->createDirectory($r->path . '/' . $r->fileName);
|
||||
} else {
|
||||
$filesystem->write($r->path . '/' . $r->fileName, '');
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'message' => $r->fileType . ' ' . $r->path . '/' . $r->fileName . ' created successfully!',
|
||||
]);
|
||||
} catch (\Exception $exception) {
|
||||
return response()->json([
|
||||
'error' => $exception->getMessage(),
|
||||
], 500);
|
||||
};
|
||||
public function deleteFiles(Request $r)
|
||||
{
|
||||
return (new DeleteFilesAction($this->filesystem))->execute($r);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user