mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-18 00:02:15 +00:00
move create folder structure to new action
This commit is contained in:
@@ -2,13 +2,14 @@
|
||||
namespace Domain\Files\Actions;
|
||||
|
||||
use Domain\Sharing\Models\Share;
|
||||
use Domain\Folders\Models\Folder;
|
||||
// use Domain\Folders\Models\Folder;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Domain\Files\Requests\UploadRequest;
|
||||
use Domain\Files\Models\File as UserFile;
|
||||
use Domain\Traffic\Actions\RecordUploadAction;
|
||||
use Domain\Files\Actions\CreateFolderStructureAction;
|
||||
use App\Users\Actions\CheckStorageCapacityAction;
|
||||
|
||||
class UploadFileAction
|
||||
@@ -17,6 +18,7 @@ class UploadFileAction
|
||||
public RecordUploadAction $recordUpload,
|
||||
public CheckStorageCapacityAction $checkStorageCapacity,
|
||||
public CreateImageThumbnailAction $createImageThumbnail,
|
||||
public CreateFolderStructureAction $createFolderStructure,
|
||||
public MoveFileToExternalStorageAction $moveFileToExternalStorage,
|
||||
) {
|
||||
}
|
||||
@@ -31,8 +33,6 @@ class UploadFileAction
|
||||
// Get parent_id from request
|
||||
$file = $request->file('file');
|
||||
|
||||
// dd($this->createFolderStructure($request->input('path'), $request->input('folder_id')));
|
||||
|
||||
// File name
|
||||
$disk_file_name = basename('chunks/' . $file->getClientOriginalName(), '.part');
|
||||
$temp_filename = $file->getClientOriginalName();
|
||||
@@ -89,7 +89,7 @@ class UploadFileAction
|
||||
return UserFile::create([
|
||||
'mimetype' => get_file_type_from_mimetype($file_mimetype),
|
||||
'type' => get_file_type($file_mimetype),
|
||||
'folder_id' => $this->create_folder_structure($request->input('path'), $request->input('folder_id'), $user_id),
|
||||
'folder_id' => ($this->createFolderStructure)($request->input('path'), $request->input('folder_id'), $user_id),
|
||||
'metadata' => $metadata,
|
||||
'name' => $request->input('filename'),
|
||||
'basename' => $disk_file_name,
|
||||
@@ -100,121 +100,4 @@ class UploadFileAction
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create structure fot the Folder upload
|
||||
*/
|
||||
private function create_folder_structure ($path, $parent, $user_id)
|
||||
{
|
||||
$folders = array_slice(explode('/', $path), 1, -1);
|
||||
|
||||
$parent_id = $parent;
|
||||
|
||||
$last_folder = $parent;
|
||||
|
||||
// Get already created structure of the file parents
|
||||
$structure = Folder::whereIn('name', $folders)->with('parent')->get();
|
||||
|
||||
// If file have some parent folders
|
||||
if( count($folders) > 0) {
|
||||
|
||||
|
||||
if(count($folders) === count($structure) ) {
|
||||
|
||||
// Check if file parents are equal to already existed folder structure
|
||||
$last_folder = $this->check_folder_structure($structure, $folders);
|
||||
|
||||
} else if (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];
|
||||
|
||||
}
|
||||
|
||||
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 if is the structure correct
|
||||
*/
|
||||
private function check_folder_structure($structure, $folders)
|
||||
{
|
||||
|
||||
$parent_name = '';
|
||||
|
||||
$validate = false;
|
||||
|
||||
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;
|
||||
|
||||
$validate = true;
|
||||
|
||||
} else if ($parent && $folder != $parent_name || $parent_name == ''){
|
||||
|
||||
$validate = false;
|
||||
}
|
||||
}
|
||||
|
||||
if($validate) {
|
||||
return $structure->where('name', $folders[array_key_last($folders)])->first()->id;
|
||||
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the folders that is need to create in already created structure and last created parent
|
||||
*/
|
||||
private function check_exist_folders($structure, $folders)
|
||||
{
|
||||
|
||||
$create_folders = [];
|
||||
$last_parent = '';
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
return [$create_folders, $last_parent];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user