mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-19 00:22:15 +00:00
CreateFolderStructureAction refactoring
This commit is contained in:
69
src/Domain/Files/Actions/GetFileParentId.php
Normal file
69
src/Domain/Files/Actions/GetFileParentId.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
namespace Domain\Files\Actions;
|
||||
|
||||
use Domain\Folders\Models\Folder;
|
||||
use Illuminate\Support\Collection;
|
||||
use Domain\Files\Requests\UploadRequest;
|
||||
|
||||
class GetFileParentId
|
||||
{
|
||||
public function __invoke(UploadRequest $request, string $userId): ?string
|
||||
{
|
||||
// extract file path
|
||||
$directoryPath = collect(
|
||||
array_slice(explode('/', $request->input('path')), 1, -1)
|
||||
);
|
||||
|
||||
// If there isn't directory path, return parent_id
|
||||
if ($directoryPath->isEmpty()) {
|
||||
return $request->input('parent_id');
|
||||
}
|
||||
|
||||
return $this->getOrCreateParentFolders($directoryPath, $userId, null);
|
||||
}
|
||||
|
||||
private function getOrCreateParentFolders(Collection $directoryPath, string $userId, ?string $parentId): ?string
|
||||
{
|
||||
// Break the end of recursive
|
||||
if ($directoryPath->isEmpty()) {
|
||||
return $parentId;
|
||||
}
|
||||
|
||||
// Get requested directory name
|
||||
$directoryName = $directoryPath->shift();
|
||||
|
||||
// Get requested directory
|
||||
$requestedDirectory = Folder::where('name', $directoryName);
|
||||
|
||||
// Check if root exists, if not, create him
|
||||
if ($requestedDirectory->exists()) {
|
||||
// Get parent folder
|
||||
$parentCheck = Folder::where('name', $directoryName)
|
||||
->where('parent_id', $parentId);
|
||||
|
||||
// Check if parent folder of requested directory name exists, if not, create it
|
||||
if ($parentCheck->exists()) {
|
||||
$folder = $parentCheck->first();
|
||||
} else {
|
||||
$folder = $this->createFolder($directoryName, $userId, $parentId);
|
||||
}
|
||||
}
|
||||
|
||||
// Create directory if not exists
|
||||
if ($requestedDirectory->doesntExist()) {
|
||||
$folder = $this->createFolder($directoryName, $userId, $parentId);
|
||||
}
|
||||
|
||||
// Repeat yourself
|
||||
return $this->getOrCreateParentFolders($directoryPath, $userId, $folder->id);
|
||||
}
|
||||
|
||||
private function createFolder($directoryName, $userId, $parentId): Folder
|
||||
{
|
||||
return Folder::create([
|
||||
'name' => $directoryName,
|
||||
'parent_id' => $parentId,
|
||||
'user_id' => $userId,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user