CreateFolderStructureAction refactoring

This commit is contained in:
Čarodej
2022-01-25 10:55:48 +01:00
parent ec92295fbf
commit 5725753052
4 changed files with 141 additions and 70 deletions

View File

@@ -1,68 +0,0 @@
<?php
namespace Domain\Files\Actions;
use Str;
use Domain\Folders\Models\Folder;
use Illuminate\Support\Collection;
use Domain\Files\Requests\UploadRequest;
class CreateFolderStructureAction
{
public function __invoke(UploadRequest $request, string $userId): string|null
{
// extract file path
$uploadPath = array_slice(explode('/', $request->input('path')), 1, -1);
// If there isn't folder path, return parent_id
if (empty($uploadPath)) {
return $request->input('parent_id');
}
// Get already created structure of the file parents
$structure = Folder::whereIn('name', $uploadPath)
->with('parent')
->get();
// If uploading structure has same length as an already existed structure, get correct file parent from the already created structure
if (count($uploadPath) === count($structure)) {
return $structure->where('name', $uploadPath[array_key_last($uploadPath)])->first()->id;
}
// Check what folders are missed in structure and return missed folder with last created folder in structure
[$uploadPath, $parentId] = $this->check_exist_folders($structure, $uploadPath);
// Create folders and return parent_id from last item
foreach ($uploadPath as $name) {
$parentId = Folder::create([
'name' => $name,
'user_id' => $userId,
'parent_id' => Str::isUuid($parentId) ? $parentId : null,
])->id;
}
return $parentId;
}
/**
* Return the folders that is need to create in already created structure and last created parent
*/
private function check_exist_folders(Collection $folders, array $uploadPath): array
{
$folderQueue = [];
$lastParentId = '';
foreach ($uploadPath as $name) {
// Filter folders that is needed to create
if ($folders->doesntContain('name', $name)) {
array_push($folderQueue, $name);
}
// Find last created folder
if ($folders->contains('name', $name)) {
$lastParentId = $folders->where('name', $name)->first()->id;
}
}
return [$folderQueue, $lastParentId];
}
}

View 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,
]);
}
}

View File

@@ -17,7 +17,7 @@ class UploadFileAction
public function __construct(
public RecordUploadAction $recordUpload,
public ProcessImageThumbnailAction $createImageThumbnail,
public CreateFolderStructureAction $createFolderStructure,
public GetFileParentId $getFileParentId,
public MoveFileToExternalStorageAction $moveFileToExternalStorage,
) {
}
@@ -94,7 +94,7 @@ class UploadFileAction
return UserFile::create([
'mimetype' => get_file_type_from_mimetype($file_mimetype),
'type' => get_file_type($file_mimetype),
'parent_id' => ($this->createFolderStructure)($request, $user->id),
'parent_id' => ($this->getFileParentId)($request, $user->id),
'metadata' => $metadata,
'name' => $request->input('filename'),
'basename' => $fileName,