mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-22 17:32:14 +00:00
refactoring
This commit is contained in:
@@ -1,50 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Domain\Files\Actions;
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Spatie\QueueableAction\QueueableAction;
|
||||
use Intervention\Image\ImageManagerStatic as Image;
|
||||
|
||||
|
||||
class CreateImageThumbnailAcionQueue
|
||||
{
|
||||
use QueueableAction;
|
||||
|
||||
/**
|
||||
* Execute the action.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function execute($file_name, $user_id, $thumnails_sizes, $source)
|
||||
{
|
||||
// Get image from disk
|
||||
$image = Storage::disk('local')->get("temp/$user_id/{$file_name}");
|
||||
|
||||
// Get image width
|
||||
$image_width = getimagesize(storage_path("app/temp/$user_id/{$file_name}"))[0];
|
||||
|
||||
collect($thumnails_sizes)
|
||||
->each(function ($size) use ($image, $user_id, $file_name, $image_width) {
|
||||
|
||||
if ($image_width > $size['size']) {
|
||||
|
||||
// Create intervention image
|
||||
$intervention = Image::make($image)->orientate();
|
||||
|
||||
// Generate thumbnail
|
||||
$intervention->resize($size['size'], null, fn ($constraint) => $constraint->aspectRatio())->stream();
|
||||
|
||||
// Store thumbnail to disk
|
||||
Storage::put("files/$user_id/{$size['name']}-{$file_name}", $intervention);
|
||||
}
|
||||
});
|
||||
|
||||
if($source === 'queue') {
|
||||
|
||||
// Delete file after generate a thumbnail
|
||||
Storage::disk('local')->delete("temp/$user_id/{$file_name}");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
<?php
|
||||
namespace Domain\Files\Actions;
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Intervention\Image\ImageManagerStatic as Image;
|
||||
use Domain\Files\Actions\CreateImageThumbnailAcionQueue;
|
||||
|
||||
|
||||
class CreateImageThumbnailAction
|
||||
{
|
||||
|
||||
public function __construct(
|
||||
CreateImageThumbnailAcionQueue $action,
|
||||
)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
private array $availableFormats = [
|
||||
'image/gif',
|
||||
'image/jpeg',
|
||||
'image/jpg',
|
||||
'image/png',
|
||||
'image/webp',
|
||||
];
|
||||
|
||||
/**
|
||||
* Create image thumbnail from uploaded image
|
||||
*/
|
||||
public function __invoke(
|
||||
string $file_name,
|
||||
$file,
|
||||
string $user_id
|
||||
): void {
|
||||
|
||||
// Create thumbnail from image
|
||||
if (in_array($file->getClientMimeType(), $this->availableFormats)) {
|
||||
|
||||
// Make copy of file for the thumbnail generation
|
||||
Storage::disk('local')->copy("files/$user_id/{$file_name}", "temp/$user_id/{$file_name}");
|
||||
|
||||
// Create thumbnail instantly
|
||||
$this->action->execute($file_name, $user_id, config('vuefilemanager.image_sizes.execute'), 'execute');
|
||||
|
||||
// Create thumbnail queue job
|
||||
$this->action->onQueue()->execute($file_name, $user_id, config('vuefilemanager.image_sizes.queue'), 'queue');
|
||||
}
|
||||
}
|
||||
}
|
||||
44
src/Domain/Files/Actions/GenerateImageThumbnailAction.php
Normal file
44
src/Domain/Files/Actions/GenerateImageThumbnailAction.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Domain\Files\Actions;
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Spatie\QueueableAction\QueueableAction;
|
||||
use Intervention\Image\ImageManagerStatic as Image;
|
||||
|
||||
class GenerateImageThumbnailAction
|
||||
{
|
||||
use QueueableAction;
|
||||
|
||||
public function __invoke($fileName, $userId, $execution): void
|
||||
{
|
||||
$localDisk = Storage::disk('local');
|
||||
|
||||
// Get image from disk
|
||||
$image = $localDisk->get("temp/$userId/$fileName");
|
||||
|
||||
// Get image width
|
||||
$imageWidth = getimagesize($localDisk->path("temp/$userId/$fileName"))[0];
|
||||
|
||||
collect(config("vuefilemanager.image_sizes.$execution"))
|
||||
->each(function ($size) use ($image, $userId, $fileName, $imageWidth) {
|
||||
|
||||
if ($imageWidth > $size['size']) {
|
||||
|
||||
// Create intervention image
|
||||
$intervention = Image::make($image)->orientate();
|
||||
|
||||
// Generate thumbnail
|
||||
$intervention->resize($size['size'], null, fn ($constraint) => $constraint->aspectRatio())->stream();
|
||||
|
||||
// Store thumbnail to disk
|
||||
Storage::put("files/$userId/{$size['name']}-$fileName", $intervention);
|
||||
}
|
||||
});
|
||||
|
||||
// Delete file after generate a thumbnail
|
||||
if ($execution === 'later') {
|
||||
Storage::disk('local')->delete("temp/$userId/$fileName");
|
||||
}
|
||||
}
|
||||
}
|
||||
52
src/Domain/Files/Actions/ProcessImageThumbnailAction.php
Normal file
52
src/Domain/Files/Actions/ProcessImageThumbnailAction.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
namespace Domain\Files\Actions;
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
|
||||
class ProcessImageThumbnailAction
|
||||
{
|
||||
|
||||
public function __construct(
|
||||
public GenerateImageThumbnailAction $generateImageThumbnail,
|
||||
) {}
|
||||
|
||||
private array $availableFormats = [
|
||||
'image/gif',
|
||||
'image/jpeg',
|
||||
'image/jpg',
|
||||
'image/png',
|
||||
'image/webp',
|
||||
];
|
||||
|
||||
/**
|
||||
* Create image thumbnail from uploaded image
|
||||
*/
|
||||
public function __invoke(
|
||||
string $fileName,
|
||||
$file,
|
||||
string $userId
|
||||
): void {
|
||||
|
||||
// Create thumbnail from image
|
||||
if (in_array($file->getClientMimeType(), $this->availableFormats)) {
|
||||
|
||||
// Make copy of file for the thumbnail generation
|
||||
Storage::disk('local')->copy("files/$userId/{$fileName}", "temp/$userId/{$fileName}");
|
||||
|
||||
// Create thumbnail instantly
|
||||
($this->generateImageThumbnail)(
|
||||
fileName: $fileName,
|
||||
userId: $userId,
|
||||
execution: 'immediately'
|
||||
);
|
||||
|
||||
// Create thumbnail later
|
||||
($this->generateImageThumbnail)->onQueue()->execute(
|
||||
fileName: $fileName,
|
||||
userId: $userId,
|
||||
execution: 'later'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,9 +14,9 @@ use App\Users\Actions\CheckStorageCapacityAction;
|
||||
class UploadFileAction
|
||||
{
|
||||
public function __construct(
|
||||
public RecordUploadAction $recordUpload,
|
||||
public CheckStorageCapacityAction $checkStorageCapacity,
|
||||
public CreateImageThumbnailAction $createImageThumbnail,
|
||||
public RecordUploadAction $recordUpload,
|
||||
public CheckStorageCapacityAction $checkStorageCapacity,
|
||||
public ProcessImageThumbnailAction $createImageThumbnail,
|
||||
public MoveFileToExternalStorageAction $moveFileToExternalStorage,
|
||||
) {
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user