controller refactoring part 24

This commit is contained in:
Peter Papp
2021-07-21 18:14:23 +02:00
parent 91cb795054
commit 54f1f4c9a8
18 changed files with 136 additions and 138 deletions

File diff suppressed because one or more lines are too long

View File

@@ -2,13 +2,13 @@
// Get avatars and system images
use App\Users\Controllers\GetAvatarController;
use Domain\Zipping\Controllers\GetZipController;
use Domain\Settings\Controllers\GetIAppController;
use Domain\Zipping\Controllers\VisitorGetZipController;
use Domain\Files\Controllers\FileAccess\GetFileController;
use Domain\Files\Controllers\FileAccess\GetThumbnailController;
use Domain\Files\Controllers\FileAccess\VisitorGetFileController;
use Domain\Files\Controllers\FileAccess\VisitorGetThumbnailController;
use Domain\Settings\Controllers\GetIAppController;
use Domain\Zipping\Controllers\GetZipController;
use Domain\Zipping\Controllers\VisitorGetZipController;
Route::get('/avatars/{avatar}', GetAvatarController::class);
Route::get('/system/{image}', GetIAppController::class);

View File

@@ -2,12 +2,12 @@
use Domain\Sharing\Controllers\ShareController;
use Domain\Sharing\Controllers\OGSiteController;
use Domain\Files\Controllers\VisitorShowFileController;
use Domain\Files\Controllers\VisitorUploadFileController;
use Domain\Zipping\Controllers\VisitorZipFilesController;
use Domain\Zipping\Controllers\VisitorZipFolderController;
use Domain\Files\Controllers\VisitorUploadFileController;
use Domain\Folders\Controllers\VisitorCreateFolderController;
use Domain\Items\Controllers\VisitorMoveFileOrFolderController;
use Domain\Files\Controllers\VisitorShowFileController;
use Domain\Items\Controllers\VisitorDeleteFileOrFolderController;
use Domain\Items\Controllers\VisitorRenameFileOrFolderController;
use Domain\Browsing\Controllers\VisitorBrowseFolderContentController;

View File

@@ -1,12 +1,9 @@
<?php
namespace App\Users\Controllers;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
class GetAvatarController
{
@@ -15,8 +12,7 @@ class GetAvatarController
*/
public function __invoke(
string $basename
): StreamedResponse|FileNotFoundException {
): StreamedResponse | FileNotFoundException {
// Check if file exist
if (! Storage::exists("/avatars/$basename")) {
abort(404);
@@ -25,4 +21,4 @@ class GetAvatarController
// Return avatar
return Storage::download("/avatars/$basename", $basename);
}
}
}

View File

@@ -2,8 +2,8 @@
namespace Domain\Browsing\Controllers;
use Domain\Files\Models\File;
use Domain\Folders\Models\Folder;
use Domain\Sharing\Models\Share;
use Domain\Folders\Models\Folder;
use Illuminate\Support\Collection;
use Support\Services\HelperService;
@@ -14,7 +14,8 @@ class VisitorBrowseFolderContentController
{
public function __construct(
public HelperService $helper,
) {}
) {
}
public function __invoke(
string $id,

View File

@@ -0,0 +1,37 @@
<?php
namespace Domain\Files\Actions;
use Domain\Files\Models\File;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class DownloadFileAction
{
/**
* Call and download file
*/
public function __invoke(
File $file,
string $user_id,
): BinaryFileResponse {
// Get file path
$path = "files/$user_id/$file->basename";
// Check if file exist
if (! Storage::exists($path)) {
abort(404);
}
// Get pretty name
$pretty_name = get_pretty_name($file->basename, $file->name, $file->mimetype);
return response()
->download(Storage::path($path), $pretty_name, [
'Accept-Ranges' => 'bytes',
'Content-Type' => Storage::mimeType($path),
'Content-Length' => Storage::size($path),
'Content-Range' => 'bytes 0-600/' . Storage::size($path),
'Content-Disposition' => "attachment; filename=$pretty_name",
]);
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace Domain\Files\Actions;
use Domain\Files\Models\File;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\StreamedResponse;
class DownloadThumbnailAction
{
/**
* Get image thumbnail for browser
*/
public function __invoke(
File $file,
string $user_id
): StreamedResponse {
// Get file path
$path = "/files/$user_id/{$file->getRawOriginal('thumbnail')}";
// Check if file exist
if (! Storage::exists($path)) {
abort(404);
}
// Return image thumbnail
return Storage::download($path, $file->getRawOriginal('thumbnail'));
}
}

View File

@@ -1,21 +1,20 @@
<?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 App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Domain\Files\Models\File as UserFile;
use Domain\Files\Actions\DownloadFileAction;
use Domain\Traffic\Actions\RecordDownloadAction;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class GetFileController extends Controller
{
public function __construct(
private RecordDownloadAction $recordDownload,
){}
private DownloadFileAction $downloadFile,
) {}
/**
* Get file
@@ -23,8 +22,7 @@ class GetFileController extends Controller
public function __invoke(
Request $request,
string $filename,
): StreamedResponse {
): BinaryFileResponse {
// Get file record
$file = UserFile::withTrashed()
->where('user_id', Auth::id())
@@ -37,6 +35,6 @@ class GetFileController extends Controller
user_id: Auth::id(),
);
return $this->helper->download_file($file, Auth::id());
return ($this->downloadFile)($file, Auth::id());
}
}
}

View File

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

View File

@@ -1,14 +1,12 @@
<?php
namespace Domain\Files\Controllers\FileAccess;
use Domain\Sharing\Models\Share;
use App\Http\Controllers\Controller;
use Domain\Files\Models\File as UserFile;
use Domain\Sharing\Models\Share;
use Domain\Files\Actions\DownloadFileAction;
use Domain\Traffic\Actions\RecordDownloadAction;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
/**
* Get file public
@@ -16,14 +14,15 @@ use Symfony\Component\HttpFoundation\StreamedResponse;
class VisitorGetFileController extends Controller
{
public function __construct(
private RecordDownloadAction $recordDownload
){}
private DownloadFileAction $downloadFile,
private RecordDownloadAction $recordDownload,
) {
}
public function __invoke(
$filename,
Share $shared,
): StreamedResponse {
): BinaryFileResponse {
// Check ability to access protected share files
$this->helper->check_protected_share_record($shared);
@@ -41,6 +40,6 @@ class VisitorGetFileController extends Controller
user_id: $shared->user_id,
);
return $this->helper->download_file($file, $shared->user_id);
return ($this->downloadFile)($file, $shared->user_id);
}
}
}

View File

@@ -1,13 +1,11 @@
<?php
namespace Domain\Files\Controllers\FileAccess;
use Domain\Sharing\Models\Share;
use App\Http\Controllers\Controller;
use Domain\Files\Models\File as UserFile;
use Domain\Sharing\Models\Share;
use Domain\Traffic\Actions\RecordDownloadAction;
use Domain\Files\Actions\DownloadThumbnailAction;
use Symfony\Component\HttpFoundation\StreamedResponse;
/**
@@ -17,10 +15,11 @@ class VisitorGetThumbnailController extends Controller
{
public function __construct(
private RecordDownloadAction $recordDownload,
) {}
private DownloadThumbnailAction $downloadThumbnail,
) {
}
public function __invoke(
$filename,
Share $shared,
): StreamedResponse {
@@ -41,6 +40,6 @@ class VisitorGetThumbnailController extends Controller
user_id: $shared->user_id,
);
return $this->helper->download_thumbnail_file($file, $shared->user_id);
return ($this->downloadThumbnail)($file, $shared->user_id);
}
}
}

View File

@@ -1,12 +1,9 @@
<?php
namespace Domain\Settings\Controllers;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
class GetIAppController
{
@@ -15,8 +12,7 @@ class GetIAppController
*/
public function __invoke(
string $basename
): StreamedResponse|FileNotFoundException {
): StreamedResponse | FileNotFoundException {
// Check if file exist
if (! Storage::exists("/system/$basename")) {
abort(404);
@@ -25,4 +21,4 @@ class GetIAppController
// Return avatar
return Storage::download("/system/$basename", $basename);
}
}
}

View File

@@ -11,6 +11,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
/**
* @method static whereNotNull(string $string)
* @method static where(string $string, string $token)
* @property string user_id
*/
class Share extends Model
{

View File

@@ -1,30 +1,26 @@
<?php
namespace Domain\Zipping\Controllers;
use App\Http\Controllers\Controller;
use Domain\Traffic\Actions\RecordDownloadAction;
use Domain\Zipping\Models\Zip;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Domain\Traffic\Actions\RecordDownloadAction;
use Symfony\Component\HttpFoundation\StreamedResponse;
class GetZipController extends Controller
{
public function __construct(
private RecordDownloadAction $recordDownload,
){}
) {
}
/**
* Get generated zip for user
*/
public function __invoke(
string $id,
): StreamedResponse
{
): StreamedResponse {
$disk = Storage::disk('local');
$zip = Zip::whereId($id)
@@ -45,4 +41,4 @@ class GetZipController extends Controller
'Content-Disposition' => "attachment; filename=$zip->basename",
]);
}
}
}

View File

@@ -1,20 +1,18 @@
<?php
namespace Domain\Zipping\Controllers;
use App\Http\Controllers\Controller;
use Domain\Traffic\Actions\RecordDownloadAction;
use Domain\Zipping\Models\Zip;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
use Domain\Traffic\Actions\RecordDownloadAction;
use Symfony\Component\HttpFoundation\StreamedResponse;
class VisitorGetZipController extends Controller
{
public function __construct(
private RecordDownloadAction $recordDownload,
){}
) {
}
/**
* Get generated zip for visitor
@@ -22,8 +20,7 @@ class VisitorGetZipController extends Controller
public function __invoke(
$id,
$token,
): StreamedResponse
{
): StreamedResponse {
$disk = Storage::disk('local');
$zip = Zip::where('id', $id)
@@ -44,4 +41,4 @@ class VisitorGetZipController extends Controller
'Content-Disposition' => 'attachment; filename=' . $zip->basename,
]);
}
}
}

View File

@@ -45,7 +45,7 @@ class VisitorZipFilesController extends Controller
// Get file
return response([
'url' => url("/zip/{$zip->id}/public/{$shared->token}"),
'url' => url("/zip/{$zip->id}/public/{$shared->token}"),
'name' => $zip->basename,
], 201);
}

View File

@@ -15,7 +15,8 @@ class VisitorZipFolderController extends Controller
{
public function __construct(
public HelperService $helper,
) {}
) {
}
public function __invoke(
ZipFolderAction $zipFolder,
@@ -40,7 +41,7 @@ class VisitorZipFolderController extends Controller
// Get file
return response([
'url' => url("/zip/{$zip->id}/public/{$shared->token}"),
'url' => url("/zip/{$zip->id}/public/{$shared->token}"),
'name' => $zip->basename,
], 201);
}

View File

@@ -5,8 +5,6 @@ use Illuminate\Support\Arr;
use Domain\Files\Models\File;
use Domain\Sharing\Models\Share;
use Domain\Folders\Models\Folder;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\ImageManagerStatic as Image;
class HelperService
{
@@ -64,57 +62,6 @@ class HelperService
}
}
/**
* Call and download file
*
* @param $file
* @param $user_id
* @return mixed
*/
public function download_file($file, $user_id)
{
// Get file path
$path = "files/$user_id/$file->basename";
// Check if file exist
if (! Storage::exists($path)) {
abort(404);
}
// Get pretty name
$pretty_name = get_pretty_name($file->basename, $file->name, $file->mimetype);
return response()
->download(Storage::path($path), $pretty_name, [
'Accept-Ranges' => 'bytes',
'Content-Type' => Storage::mimeType($path),
'Content-Length' => Storage::size($path),
'Content-Range' => 'bytes 0-600/' . Storage::size($path),
'Content-Disposition' => "attachment; filename=$pretty_name",
]);
}
/**
* Get image thumbnail for browser
*
* @param $file
* @param $user_id
* @return mixed
*/
public function download_thumbnail_file($file, $user_id)
{
// Get file path
$path = "/files/$user_id/{$file->getRawOriginal('thumbnail')}";
// Check if file exist
if (! Storage::exists($path)) {
abort(404);
}
// Return image thumbnail
return Storage::download($path, $file->getRawOriginal('thumbnail'));
}
/**
* @param Share $shared
*/