mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-18 16:22:14 +00:00
CreateFolderStructureAction refactoring
This commit is contained in:
@@ -1,99 +1,68 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Domain\Files\Actions;
|
namespace Domain\Files\Actions;
|
||||||
|
|
||||||
|
use Str;
|
||||||
use Domain\Folders\Models\Folder;
|
use Domain\Folders\Models\Folder;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
use Domain\Files\Requests\UploadRequest;
|
||||||
|
|
||||||
class CreateFolderStructureAction
|
class CreateFolderStructureAction
|
||||||
{
|
{
|
||||||
/**
|
public function __invoke(UploadRequest $request, string $userId): string|null
|
||||||
* Execute the action.
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function __invoke($path, $parent, $user_id)
|
|
||||||
{
|
{
|
||||||
$folders = array_slice(explode('/', $path), 1, -1);
|
// extract file path
|
||||||
|
$uploadPath = array_slice(explode('/', $request->input('path')), 1, -1);
|
||||||
|
|
||||||
$parent_id = $parent;
|
// If there isn't folder path, return parent_id
|
||||||
|
if (empty($uploadPath)) {
|
||||||
$last_folder = $parent;
|
return $request->input('parent_id');
|
||||||
|
}
|
||||||
|
|
||||||
// Get already created structure of the file parents
|
// 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 uploading structure has same length as an already existed structure, get correct file parent from the already created structure
|
||||||
if (count($folders) > 0) {
|
if (count($uploadPath) === count($structure)) {
|
||||||
// If uploading structure has same lenght as a already existed structure
|
return $structure->where('name', $uploadPath[array_key_last($uploadPath)])->first()->id;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
/**
|
// Create folders and return parent_id from last item
|
||||||
* Find the file parent folder in already existed structure
|
foreach ($uploadPath as $name) {
|
||||||
*/
|
$parentId = Folder::create([
|
||||||
private function get_file_parent($structure, $folders)
|
'name' => $name,
|
||||||
{
|
'user_id' => $userId,
|
||||||
$parent_name = '';
|
'parent_id' => Str::isUuid($parentId) ? $parentId : null,
|
||||||
|
])->id;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $parentId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the folders that is need to create in already created structure and last created parent
|
* 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 = [];
|
$folderQueue = [];
|
||||||
$last_parent = '';
|
$lastParentId = '';
|
||||||
|
|
||||||
foreach ($folders as $folder) {
|
foreach ($uploadPath as $name) {
|
||||||
// Filter folders that is need to create
|
// Filter folders that is needed to create
|
||||||
if (! $structure->where('name', $folder)->first()) {
|
if ($folders->doesntContain('name', $name)) {
|
||||||
array_push($create_folders, $folder);
|
array_push($folderQueue, $name);
|
||||||
} else {
|
}
|
||||||
// Find last created folder
|
|
||||||
$last_parent = $structure->where('name', $folder)->first()->id;
|
// 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([
|
return UserFile::create([
|
||||||
'mimetype' => get_file_type_from_mimetype($file_mimetype),
|
'mimetype' => get_file_type_from_mimetype($file_mimetype),
|
||||||
'type' => get_file_type($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,
|
'metadata' => $metadata,
|
||||||
'name' => $request->input('filename'),
|
'name' => $request->input('filename'),
|
||||||
'basename' => $fileName,
|
'basename' => $fileName,
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Tests\Domain\Folders;
|
namespace Tests\Domain\Folders;
|
||||||
|
|
||||||
use Storage;
|
use Storage;
|
||||||
|
|||||||
@@ -1,15 +1,47 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Tests\Domain\Folders;
|
namespace Tests\Domain\Folders;
|
||||||
|
|
||||||
|
use Tests\TestCase;
|
||||||
use App\Users\Models\User;
|
use App\Users\Models\User;
|
||||||
use Domain\Files\Models\File;
|
use Domain\Files\Models\File;
|
||||||
use Domain\Folders\Models\Folder;
|
use Domain\Folders\Models\Folder;
|
||||||
use Illuminate\Http\UploadedFile;
|
use Illuminate\Http\UploadedFile;
|
||||||
use Tests\TestCase;
|
|
||||||
|
|
||||||
class FolderUploadTest extends 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
|
* @test
|
||||||
*/
|
*/
|
||||||
@@ -45,7 +77,6 @@ class FolderUploadTest extends TestCase
|
|||||||
$this->assertEquals($level_3->id, $file->parent_id);
|
$this->assertEquals($level_3->id, $file->parent_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
*/
|
*/
|
||||||
@@ -96,5 +127,7 @@ class FolderUploadTest extends TestCase
|
|||||||
// Check correctness of siblings folders appended to the home folder
|
// Check correctness of siblings folders appended to the home folder
|
||||||
$this->assertEquals($home->id, $brotherFolder->parent_id);
|
$this->assertEquals($home->id, $brotherFolder->parent_id);
|
||||||
$this->assertEquals($home->id, $sisterFolder->parent_id);
|
$this->assertEquals($home->id, $sisterFolder->parent_id);
|
||||||
|
|
||||||
|
$this->assertEquals(null, $home->parent_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user