mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-17 15:52:15 +00:00
refactoring
This commit is contained in:
@@ -30,7 +30,7 @@ return [
|
||||
|
||||
'image_sizes' => [
|
||||
|
||||
'execute' => [
|
||||
'immediately' => [
|
||||
[
|
||||
'size' => 120,
|
||||
'name' => 'xs',
|
||||
@@ -41,7 +41,7 @@ return [
|
||||
],
|
||||
],
|
||||
|
||||
'queue' => [
|
||||
'later' => [
|
||||
[
|
||||
'size' => 480,
|
||||
'name' => 'md',
|
||||
|
||||
@@ -1141,8 +1141,8 @@ class SetupDevEnvironment extends Command
|
||||
|
||||
// Generate avatar sizes
|
||||
collect([
|
||||
config('vuefilemanager.image_sizes.queue'),
|
||||
config('vuefilemanager.image_sizes.execute'),
|
||||
config('vuefilemanager.image_sizes.later'),
|
||||
config('vuefilemanager.image_sizes.immediately'),
|
||||
])->collapse()
|
||||
->each(function ($size) use ($intervention, $file_name, $user) {
|
||||
// Create thumbnail only if image is larger than predefined image sizes
|
||||
|
||||
@@ -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,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ class File extends Model
|
||||
public function getThumbnailAttribute(): array | null
|
||||
{
|
||||
$links = [];
|
||||
$thumbnail_sizes = array_merge(config('vuefilemanager.image_sizes.execute'), config('vuefilemanager.image_sizes.queue'));
|
||||
$thumbnail_sizes = collect(config('vuefilemanager.image_sizes'))->collapse()->all();
|
||||
|
||||
// Generate thumbnail link for external storage service
|
||||
if ($this->type === 'image' && ! is_storage_driver(['local'])) {
|
||||
|
||||
@@ -41,8 +41,8 @@ class DumpTrashController extends Controller
|
||||
if ($file->thumbnail) {
|
||||
|
||||
collect([
|
||||
config('vuefilemanager.image_sizes.queue'),
|
||||
config('vuefilemanager.image_sizes.execute'),
|
||||
config('vuefilemanager.image_sizes.later'),
|
||||
config('vuefilemanager.image_sizes.immediately'),
|
||||
])->collapse()
|
||||
->each(function ($size) use ($file) {
|
||||
|
||||
|
||||
@@ -606,8 +606,8 @@ if (! function_exists('get_thumbnail_file_list')) {
|
||||
function get_thumbnail_file_list(string $basename): Collection
|
||||
{
|
||||
return collect([
|
||||
config('vuefilemanager.image_sizes.queue'),
|
||||
config('vuefilemanager.image_sizes.execute'),
|
||||
config('vuefilemanager.image_sizes.later'),
|
||||
config('vuefilemanager.image_sizes.immediately'),
|
||||
])->collapse()
|
||||
->map(fn ($item) => $item['name'] . '-' . $basename);
|
||||
}
|
||||
|
||||
@@ -54,12 +54,12 @@ class FileTest extends TestCase
|
||||
);
|
||||
|
||||
collect([
|
||||
config('vuefilemanager.image_sizes.queue'),
|
||||
config('vuefilemanager.image_sizes.execute'),
|
||||
])->collapse()
|
||||
config('vuefilemanager.image_sizes.later'),
|
||||
config('vuefilemanager.image_sizes.immediately'),
|
||||
])
|
||||
->collapse()
|
||||
->each(
|
||||
fn ($item) =>
|
||||
$disk->assertExists(
|
||||
fn($item) => $disk->assertExists(
|
||||
"files/{$user->id}/{$item['name']}-{$file->basename}"
|
||||
)
|
||||
);
|
||||
|
||||
@@ -646,7 +646,7 @@ class TeamManagementTest extends TestCase
|
||||
// Put fake image into correct directory
|
||||
Storage::putFileAs("files/$member->id", $fakeFile, $fakeFile->name);
|
||||
|
||||
$thumbnail_sizes = collect([config('vuefilemanager.image_sizes.queue'), config('vuefilemanager.image_sizes.execute')])->collapse();
|
||||
$thumbnail_sizes = collect([config('vuefilemanager.image_sizes.later'), config('vuefilemanager.image_sizes.immediately')])->collapse();
|
||||
|
||||
// Create fake image thumbnails
|
||||
$thumbnail_sizes
|
||||
|
||||
@@ -109,7 +109,7 @@ class TrashTest extends TestCase
|
||||
|
||||
$disk = Storage::disk('local');
|
||||
|
||||
$thumbnail_sizes = array_merge(config('vuefilemanager.image_sizes.execute'), config('vuefilemanager.image_sizes.queue'));
|
||||
$thumbnail_sizes = collect(config('vuefilemanager.image_sizes'))->collapse()->all();
|
||||
|
||||
$disk->assertMissing(
|
||||
"files/$user->id/fake-image.jpg"
|
||||
|
||||
Reference in New Issue
Block a user