From ec92295fbf0327995471088f1d08c26ae3c88a27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=8Carodej?= Date: Mon, 24 Jan 2022 11:20:47 +0100 Subject: [PATCH] CreateFolderStructureAction refactoring --- .../Actions/CreateFolderStructureAction.php | 111 +++++++----------- src/Domain/Files/Actions/UploadFileAction.php | 2 +- tests/Domain/Folders/FolderTest.php | 1 - tests/Domain/Folders/FolderUploadTest.php | 41 ++++++- 4 files changed, 78 insertions(+), 77 deletions(-) diff --git a/src/Domain/Files/Actions/CreateFolderStructureAction.php b/src/Domain/Files/Actions/CreateFolderStructureAction.php index f5dc9f7f..b3a7820f 100644 --- a/src/Domain/Files/Actions/CreateFolderStructureAction.php +++ b/src/Domain/Files/Actions/CreateFolderStructureAction.php @@ -1,99 +1,68 @@ input('path')), 1, -1); - $parent_id = $parent; - - $last_folder = $parent; + // 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', $folders)->with('parent')->get(); + $structure = Folder::whereIn('name', $uploadPath) + ->with('parent') + ->get(); - // If file have some parent folders - if (count($folders) > 0) { - // If uploading structure has same lenght as a already existed structure - if (count($folders) === count($structure)) { - // Get correct file parent from the already craeted structure - $last_folder = $this->get_file_parent($structure, $folders); - } elseif (count($folders) !== count($structure)) { - if (count($structure) > 0) { - // Check what folders are missed in structure and return missed folder with last created folder in structure - $data = $this->check_exist_folders($structure, $folders); - - $folders = $data[0]; - - $parent_id = $data[1]; - } - - // Create folders - foreach ($folders as $folder) { - $new_folder = Folder::create([ - 'name' => $folder, - 'parent_id' => $parent_id, - 'user_id' => $user_id, - ]); - - $parent_id = $new_folder->id; - - $last_folder = $new_folder->id; - } - } + // 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; } - return $last_folder; - } + // 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); - /** - * Find the file parent folder in already existed structure - */ - private function get_file_parent($structure, $folders) - { - $parent_name = ''; - - foreach (array_reverse($folders) as $folder) { - $item = $structure->where('name', $folder); - - $parent = $item->pluck('parent')->pluck('name')[0]; - - // Check if folder have valid parent name - if ($parent && $folder === $parent_name || $parent_name == '') { - $parent_name = $parent; - } - - return $structure->where('name', $folders[array_key_last($folders)])->first()->id; + // 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($structure, $folders): array + private function check_exist_folders(Collection $folders, array $uploadPath): array { - $create_folders = []; - $last_parent = ''; + $folderQueue = []; + $lastParentId = ''; - foreach ($folders as $folder) { - // Filter folders that is need to create - if (! $structure->where('name', $folder)->first()) { - array_push($create_folders, $folder); - } else { - // Find last created folder - $last_parent = $structure->where('name', $folder)->first()->id; + 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 [$create_folders, $last_parent]; + return [$folderQueue, $lastParentId]; } } diff --git a/src/Domain/Files/Actions/UploadFileAction.php b/src/Domain/Files/Actions/UploadFileAction.php index 3ce501cc..4ee4e9bf 100644 --- a/src/Domain/Files/Actions/UploadFileAction.php +++ b/src/Domain/Files/Actions/UploadFileAction.php @@ -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->input('path'), $request->input('parent_id'), $user->id), + 'parent_id' => ($this->createFolderStructure)($request, $user->id), 'metadata' => $metadata, 'name' => $request->input('filename'), 'basename' => $fileName, diff --git a/tests/Domain/Folders/FolderTest.php b/tests/Domain/Folders/FolderTest.php index df42e18c..ba807747 100644 --- a/tests/Domain/Folders/FolderTest.php +++ b/tests/Domain/Folders/FolderTest.php @@ -1,5 +1,4 @@ hasSettings() + ->create(); + + $file = UploadedFile::fake() + ->create('fake-file.pdf', 120000, 'application/pdf'); + + $folder = Folder::factory() + ->create([ + 'user_id' => $user->id, + 'parent_id' => null, + ]); + + $this + ->actingAs($user) + ->postJson('/api/upload', [ + 'filename' => $file->name, + 'file' => $file, + 'path' => '/', + 'parent_id' => $folder->id, + 'is_last' => 'true', + ])->assertStatus(201); + + $file = File::first(); + + $this->assertEquals($file->parent_id, $folder->id); + } + /** * @test */ @@ -45,7 +77,6 @@ class FolderUploadTest extends TestCase $this->assertEquals($level_3->id, $file->parent_id); } - /** * @test */ @@ -96,5 +127,7 @@ class FolderUploadTest extends TestCase // Check correctness of siblings folders appended to the home folder $this->assertEquals($home->id, $brotherFolder->parent_id); $this->assertEquals($home->id, $sisterFolder->parent_id); + + $this->assertEquals(null, $home->parent_id); } -} \ No newline at end of file +}