controller refactoring part 7

This commit is contained in:
Peter Papp
2021-07-20 12:22:48 +02:00
parent 0232a7abeb
commit dc98c839a2
17 changed files with 275 additions and 216 deletions

View File

@@ -0,0 +1,40 @@
<?php
namespace Domain\Browsing\Controllers;
use Domain\Sharing\Models\Share;
use Illuminate\Support\Collection;
use Support\Services\HelperService;
/**
* Browse shared folder
*/
class VisitorBrowseFolderContentController
{
public function __construct(
public HelperService $helper,
) {
}
public function __invoke(
string $id,
Share $shared,
): Collection {
// Check ability to access protected share record
$this->helper->check_protected_share_record($shared);
// Check if user can get directory
$this->helper->check_item_access($id, $shared);
// Get files and folders
list($folders, $files) = $this->helper->get_items_under_shared_by_folder_id($id, $shared);
// Set thumbnail links for public files
$files->map(function ($file) use ($shared) {
$file->setPublicUrl($shared->token);
});
// Collect folders and files to single array
return collect([$folders, $files])
->collapse();
}
}

View File

@@ -0,0 +1,70 @@
<?php
namespace Domain\Browsing\Controllers;
use Illuminate\Support\Arr;
use Illuminate\Http\Request;
use Domain\Files\Models\File;
use Domain\Sharing\Models\Share;
use Domain\Folders\Models\Folder;
use Illuminate\Support\Collection;
use Support\Services\HelperService;
use App\Http\Controllers\Controller;
/**
* Visitor search shared files
*/
class VisitorSearchFilesAndFoldersController extends Controller
{
public function __construct(
public HelperService $helper,
) {
}
public function __invoke(
Request $request,
Share $shared,
): Collection {
// Check ability to access protected share record
$this->helper->check_protected_share_record($shared);
$query = remove_accents(
$request->input('query')
);
// Search files id db
$searched_files = File::search($query)
->where('user_id', $shared->user_id)
->get();
$searched_folders = Folder::search($query)
->where('user_id', $shared->user_id)
->get();
// Get all children content
$foldersIds = Folder::with('folders:id,parent_id,id,name')
->where('user_id', $shared->user_id)
->where('parent_id', $shared->item_id)
->get();
// Get accessible folders
$accessible_folder_ids = Arr::flatten([filter_folders_ids($foldersIds), $shared->item_id]);
// Filter files
$files = $searched_files->filter(function ($file) use ($accessible_folder_ids, $shared) {
// Set public urls
$file->setPublicUrl($shared->token);
// check if item is in accessible folders
return in_array($file->folder_id, $accessible_folder_ids);
});
// Filter folders
$folders = $searched_folders->filter(function ($folder) use ($accessible_folder_ids) {
// check if item is in accessible folders
return in_array($folder->id, $accessible_folder_ids);
});
// Collect folders and files to single array
return collect([$folders, $files])
->collapse();
}
}