mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-05 18:23:48 +00:00
CreateFolderStructureAction refactoring
This commit is contained in:
@@ -1,99 +1,68 @@
|
||||
<?php
|
||||
namespace Domain\Files\Actions;
|
||||
|
||||
use Str;
|
||||
use Domain\Folders\Models\Folder;
|
||||
use Illuminate\Support\Collection;
|
||||
use Domain\Files\Requests\UploadRequest;
|
||||
|
||||
class CreateFolderStructureAction
|
||||
{
|
||||
/**
|
||||
* Execute the action.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function __invoke($path, $parent, $user_id)
|
||||
public function __invoke(UploadRequest $request, string $userId): string|null
|
||||
{
|
||||
$folders = array_slice(explode('/', $path), 1, -1);
|
||||
// extract file path
|
||||
$uploadPath = array_slice(explode('/', $request->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];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Domain\Folders;
|
||||
|
||||
use Storage;
|
||||
|
||||
@@ -1,15 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Domain\Folders;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Users\Models\User;
|
||||
use Domain\Files\Models\File;
|
||||
use Domain\Folders\Models\Folder;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Tests\TestCase;
|
||||
|
||||
class FolderUploadTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function upload_single_file_to_the_folder()
|
||||
{
|
||||
$user = User::factory()
|
||||
->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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user