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

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'));
}
}