diff --git a/src/Domain/Files/Actions/CreateFolderStructureAction.php b/src/Domain/Files/Actions/CreateFolderStructureAction.php deleted file mode 100644 index b3a7820f..00000000 --- a/src/Domain/Files/Actions/CreateFolderStructureAction.php +++ /dev/null @@ -1,68 +0,0 @@ -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]; - } -} diff --git a/src/Domain/Files/Actions/GetFileParentId.php b/src/Domain/Files/Actions/GetFileParentId.php new file mode 100644 index 00000000..aa8f6704 --- /dev/null +++ b/src/Domain/Files/Actions/GetFileParentId.php @@ -0,0 +1,69 @@ +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, + ]); + } +} diff --git a/src/Domain/Files/Actions/UploadFileAction.php b/src/Domain/Files/Actions/UploadFileAction.php index 4ee4e9bf..bc267e25 100644 --- a/src/Domain/Files/Actions/UploadFileAction.php +++ b/src/Domain/Files/Actions/UploadFileAction.php @@ -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, diff --git a/tests/Domain/Folders/FolderUploadTest.php b/tests/Domain/Folders/FolderUploadTest.php index 0061c7ce..0b5c8941 100644 --- a/tests/Domain/Folders/FolderUploadTest.php +++ b/tests/Domain/Folders/FolderUploadTest.php @@ -40,6 +40,8 @@ class FolderUploadTest extends TestCase $file = File::first(); $this->assertEquals($file->parent_id, $folder->id); + + $this->assertDatabaseCount('folders', 1); } /** @@ -64,6 +66,16 @@ class FolderUploadTest extends TestCase 'is_last' => 'true', ])->assertStatus(201); + $this + ->actingAs($user) + ->postJson('/api/upload', [ + 'filename' => $file->name, + 'file' => $file, + 'path' => "/level_1/level_2/level_3/$file->name", + 'parent_id' => null, + 'is_last' => 'true', + ])->assertStatus(201); + $file = File::first(); $level_1 = Folder::where('name', 'level_1')->first(); @@ -75,6 +87,62 @@ class FolderUploadTest extends TestCase $this->assertEquals($level_3->parent_id, $level_2->id); $this->assertEquals($level_3->id, $file->parent_id); + + $this->assertDatabaseCount('folders', 3); + } + + /** + * @test + */ + public function duplicity_paths_test() + { + $user = User::factory() + ->hasSettings() + ->create(); + + $level_1 = Folder::factory() + ->create([ + 'name' => 'level_1', + 'user_id' => $user->id, + 'parent_id' => null, + ]); + + $level_2 = Folder::factory() + ->create([ + 'name' => 'level_2', + 'user_id' => $user->id, + 'parent_id' => $level_1->id, + ]); + + Folder::factory() + ->create([ + 'name' => 'level_3', + 'user_id' => $user->id, + 'parent_id' => $level_2->parent_id, + ]); + + $file = UploadedFile::fake() + ->create('fake-file.pdf', 120000, 'application/pdf'); + + $this + ->actingAs($user) + ->postJson('/api/upload', [ + 'filename' => $file->name, + 'file' => $file, + 'path' => "/another_folder/level_2/level_3/$file->name", + 'parent_id' => null, + 'is_last' => 'true', + ])->assertStatus(201); + + // Root folders + $this->assertEquals(1, Folder::where('name', 'another_folder')->count()); + $this->assertEquals(1, Folder::where('name', 'level_1')->count()); + + // Unique children folders + $this->assertEquals(2, Folder::where('name', 'level_2')->count()); + $this->assertEquals(2, Folder::where('name', 'level_3')->count()); + + $this->assertDatabaseCount('folders', 6); } /** @@ -129,5 +197,7 @@ class FolderUploadTest extends TestCase $this->assertEquals($home->id, $sisterFolder->parent_id); $this->assertEquals(null, $home->parent_id); + + $this->assertDatabaseCount('folders', 3); } }