controller refactoring part 23

This commit is contained in:
Peter Papp
2021-07-21 18:00:45 +02:00
parent 9fb9b8a1b0
commit 91cb795054
18 changed files with 360 additions and 312 deletions

View File

@@ -0,0 +1,42 @@
<?php
namespace Domain\Files\Controllers\FileAccess;
use App\Http\Controllers\Controller;
use Domain\Files\Models\File as UserFile;
use Domain\Traffic\Actions\RecordDownloadAction;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\StreamedResponse;
class GetFileController extends Controller
{
public function __construct(
private RecordDownloadAction $recordDownload,
){}
/**
* Get file
*/
public function __invoke(
Request $request,
string $filename,
): StreamedResponse {
// Get file record
$file = UserFile::withTrashed()
->where('user_id', Auth::id())
->where('basename', $filename)
->firstOrFail();
// Store user download size
($this->recordDownload)(
file_size: (int) $file->getRawOriginal('filesize'),
user_id: Auth::id(),
);
return $this->helper->download_file($file, Auth::id());
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace Domain\Files\Controllers\FileAccess;
use App\Http\Controllers\Controller;
use Domain\Files\Models\File as UserFile;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\StreamedResponse;
class GetThumbnailController extends Controller
{
/**
* Get image thumbnail
*/
public function __invoke(
Request $request,
string $filename,
): FileNotFoundException|StreamedResponse {
$file = UserFile::withTrashed()
->whereUserId(Auth::id())
->whereThumbnail($filename)
->firstOrFail();
return $this->helper->download_thumbnail_file($file, Auth::id());
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Domain\Files\Controllers\FileAccess;
use App\Http\Controllers\Controller;
use Domain\Files\Models\File as UserFile;
use Domain\Sharing\Models\Share;
use Domain\Traffic\Actions\RecordDownloadAction;
use Symfony\Component\HttpFoundation\StreamedResponse;
/**
* Get file public
*/
class VisitorGetFileController extends Controller
{
public function __construct(
private RecordDownloadAction $recordDownload
){}
public function __invoke(
$filename,
Share $shared,
): StreamedResponse {
// Check ability to access protected share files
$this->helper->check_protected_share_record($shared);
// Get file record
$file = UserFile::where('user_id', $shared->user_id)
->where('basename', $filename)
->firstOrFail();
// Check file access
$this->helper->check_guest_access_to_shared_items($shared, $file);
// Store user download size
($this->recordDownload)(
file_size: (int) $file->getRawOriginal('filesize'),
user_id: $shared->user_id,
);
return $this->helper->download_file($file, $shared->user_id);
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Domain\Files\Controllers\FileAccess;
use App\Http\Controllers\Controller;
use Domain\Files\Models\File as UserFile;
use Domain\Sharing\Models\Share;
use Domain\Traffic\Actions\RecordDownloadAction;
use Symfony\Component\HttpFoundation\StreamedResponse;
/**
* Get public image thumbnail
*/
class VisitorGetThumbnailController extends Controller
{
public function __construct(
private RecordDownloadAction $recordDownload,
) {}
public function __invoke(
$filename,
Share $shared,
): StreamedResponse {
// Check ability to access protected share files
$this->helper->check_protected_share_record($shared);
// Get file record
$file = UserFile::where('user_id', $shared->user_id)
->where('thumbnail', $filename)
->firstOrFail();
// Check file access
$this->helper->check_guest_access_to_shared_items($shared, $file);
// Store user download size
($this->recordDownload)(
file_size: (int) $file->getRawOriginal('filesize'),
user_id: $shared->user_id,
);
return $this->helper->download_thumbnail_file($file, $shared->user_id);
}
}