controller refactoring part 24

This commit is contained in:
Peter Papp
2021-07-21 18:46:55 +02:00
parent 54f1f4c9a8
commit 6d8a7a429c
29 changed files with 561 additions and 209 deletions

View File

@@ -14,7 +14,8 @@ class GetFileController extends Controller
public function __construct(
private RecordDownloadAction $recordDownload,
private DownloadFileAction $downloadFile,
) {}
) {
}
/**
* Get file

View File

@@ -1,12 +1,14 @@
<?php
namespace Domain\Files\Controllers\FileAccess;
use Domain\Files\Models\File;
use Domain\Sharing\Models\Share;
use App\Http\Controllers\Controller;
use Domain\Files\Models\File as UserFile;
use Domain\Files\Actions\DownloadFileAction;
use Domain\Traffic\Actions\RecordDownloadAction;
use Domain\Sharing\Actions\ProtectShareRecordAction;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Domain\Sharing\Actions\VerifyAccessToItemWithinAction;
/**
* Get file public
@@ -16,6 +18,8 @@ class VisitorGetFileController extends Controller
public function __construct(
private DownloadFileAction $downloadFile,
private RecordDownloadAction $recordDownload,
private ProtectShareRecordAction $protectShareRecord,
private VerifyAccessToItemWithinAction $verifyAccessToItemWithin,
) {
}
@@ -24,15 +28,15 @@ class VisitorGetFileController extends Controller
Share $shared,
): BinaryFileResponse {
// Check ability to access protected share files
$this->helper->check_protected_share_record($shared);
($this->protectShareRecord)($shared);
// Get file record
$file = UserFile::where('user_id', $shared->user_id)
$file = File::where('user_id', $shared->user_id)
->where('basename', $filename)
->firstOrFail();
// Check file access
$this->helper->check_guest_access_to_shared_items($shared, $file);
($this->verifyAccessToItemWithin)($shared, $file);
// Store user download size
($this->recordDownload)(
@@ -40,6 +44,7 @@ class VisitorGetFileController extends Controller
user_id: $shared->user_id,
);
// Finally download file
return ($this->downloadFile)($file, $shared->user_id);
}
}

View File

@@ -6,7 +6,9 @@ use App\Http\Controllers\Controller;
use Domain\Files\Models\File as UserFile;
use Domain\Traffic\Actions\RecordDownloadAction;
use Domain\Files\Actions\DownloadThumbnailAction;
use Domain\Sharing\Actions\ProtectShareRecordAction;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Domain\Sharing\Actions\VerifyAccessToItemWithinAction;
/**
* Get public image thumbnail
@@ -16,6 +18,8 @@ class VisitorGetThumbnailController extends Controller
public function __construct(
private RecordDownloadAction $recordDownload,
private DownloadThumbnailAction $downloadThumbnail,
private ProtectShareRecordAction $protectShareRecord,
private VerifyAccessToItemWithinAction $verifyAccessToItemWithin,
) {
}
@@ -24,7 +28,7 @@ class VisitorGetThumbnailController extends Controller
Share $shared,
): StreamedResponse {
// Check ability to access protected share files
$this->helper->check_protected_share_record($shared);
($this->protectShareRecord)($shared);
// Get file record
$file = UserFile::where('user_id', $shared->user_id)
@@ -32,7 +36,7 @@ class VisitorGetThumbnailController extends Controller
->firstOrFail();
// Check file access
$this->helper->check_guest_access_to_shared_items($shared, $file);
($this->verifyAccessToItemWithin)($shared, $file);
// Store user download size
($this->recordDownload)(
@@ -40,6 +44,7 @@ class VisitorGetThumbnailController extends Controller
user_id: $shared->user_id,
);
// Finally download thumbnail
return ($this->downloadThumbnail)($file, $shared->user_id);
}
}

View File

@@ -4,8 +4,8 @@ namespace Domain\Files\Controllers;
use Domain\Files\Models\File;
use Illuminate\Http\Response;
use Domain\Sharing\Models\Share;
use Support\Services\HelperService;
use Domain\Files\Resources\FileResource;
use Domain\Sharing\Actions\ProtectShareRecordAction;
/**
* Get shared file record
@@ -13,7 +13,7 @@ use Domain\Files\Resources\FileResource;
class VisitorShowFileController
{
public function __construct(
public HelperService $helper,
private ProtectShareRecordAction $protectShareRecord,
) {
}
@@ -21,7 +21,7 @@ class VisitorShowFileController
Share $shared
): Response {
// Check ability to access protected share files
$this->helper->check_protected_share_record($shared);
($this->protectShareRecord)($shared);
// Get file
$file = File::whereUserId($shared->user_id)

View File

@@ -3,11 +3,12 @@ namespace Domain\Files\Controllers;
use Illuminate\Http\Response;
use Domain\Sharing\Models\Share;
use Support\Services\HelperService;
use App\Http\Controllers\Controller;
use Domain\Files\Requests\UploadRequest;
use Domain\Files\Actions\UploadFileAction;
use Support\Demo\Actions\FakeUploadFileAction;
use Domain\Sharing\Actions\ProtectShareRecordAction;
use Domain\Sharing\Actions\VerifyAccessToItemAction;
/**
* guest user upload file into shared folder
@@ -15,22 +16,23 @@ use Support\Demo\Actions\FakeUploadFileAction;
class VisitorUploadFileController extends Controller
{
public function __construct(
public HelperService $helper,
private UploadFileAction $uploadFile,
private FakeUploadFileAction $fakeUploadFile,
private ProtectShareRecordAction $protectShareRecord,
private VerifyAccessToItemAction $verifyAccessToItem,
) {
}
public function __invoke(
FakeUploadFileAction $fakeUploadFile,
UploadFileAction $uploadFile,
UploadRequest $request,
Share $shared,
): Response | array {
if (is_demo_account($shared->user->email)) {
return ($fakeUploadFile)($request);
return ($this->fakeUploadFile)($request);
}
// Check ability to access protected share record
$this->helper->check_protected_share_record($shared);
($this->protectShareRecord)($shared);
// Check shared permission
if (is_visitor($shared)) {
@@ -38,10 +40,10 @@ class VisitorUploadFileController extends Controller
}
// Check access to requested directory
$this->helper->check_item_access($request->folder_id, $shared);
($this->verifyAccessToItem)($request->folder_id, $shared);
// Return new uploaded file
$new_file = ($uploadFile)($request, $shared);
$new_file = ($this->uploadFile)($request, $shared);
// Set public access url
$new_file->setPublicUrl($shared->token);