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,

View File

@@ -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);
}
}