mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-06 02:33:48 +00:00
make queue for thumbnails generation
This commit is contained in:
@@ -29,25 +29,33 @@ return [
|
||||
],
|
||||
|
||||
'image_sizes' => [
|
||||
[
|
||||
'size' => 1440,
|
||||
'name' => 'xl',
|
||||
],
|
||||
[
|
||||
'size' => 960,
|
||||
'name' => 'lg',
|
||||
],
|
||||
[
|
||||
'size' => 480,
|
||||
'name' => 'md',
|
||||
],
|
||||
[
|
||||
'size' => 240,
|
||||
'name' => 'sm',
|
||||
],
|
||||
[
|
||||
'size' => 120,
|
||||
'name' => 'xs',
|
||||
|
||||
'execute' => [
|
||||
[
|
||||
'size' => 120,
|
||||
'name' => 'xs',
|
||||
],
|
||||
[
|
||||
'size' => 240,
|
||||
'name' => 'sm',
|
||||
],
|
||||
],
|
||||
|
||||
'queue' => [
|
||||
[
|
||||
'size' => 480,
|
||||
'name' => 'md',
|
||||
],
|
||||
[
|
||||
'size' => 960,
|
||||
'name' => 'lg',
|
||||
],
|
||||
[
|
||||
'size' => 1440,
|
||||
'name' => 'xl',
|
||||
],
|
||||
|
||||
]
|
||||
|
||||
],
|
||||
];
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1140,7 +1140,7 @@ class SetupDevEnvironment extends Command
|
||||
$this->info("Generating thumbnails for $file...");
|
||||
|
||||
// Generate avatar sizes
|
||||
collect(config('vuefilemanager.image_sizes'))
|
||||
collect(array_merge(config('vuefilemanager.image_sizes.queue'), config('vuefilemanager.image_sizes.execute')))
|
||||
->each(function ($size) use ($intervention, $file_name, $user) {
|
||||
// Create thumbnail only if image is larger than predefined image sizes
|
||||
if ($intervention->getWidth() > $size['size']) {
|
||||
|
||||
40
src/Domain/Files/Actions/CreateImageThumbnailAcionQueue.php
Normal file
40
src/Domain/Files/Actions/CreateImageThumbnailAcionQueue.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?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($size, $file_name, $user_id, $is_last)
|
||||
{
|
||||
// Get image from disk
|
||||
$image = Storage::disk('local')->get("temp/$user_id/{$file_name}");
|
||||
|
||||
// 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 ($is_last) {
|
||||
|
||||
// Delete file after generate a thumbnail
|
||||
Storage::disk('local')->delete("temp/$user_id/{$file_name}");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,22 @@
|
||||
namespace Domain\Files\Actions;
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Spatie\QueueableAction\QueueableAction;
|
||||
use Intervention\Image\ImageManagerStatic as Image;
|
||||
use Domain\Files\Actions\CreateImageThumbnailAcionQueue;
|
||||
|
||||
|
||||
class CreateImageThumbnailAction
|
||||
{
|
||||
use QueueableAction;
|
||||
|
||||
public function __construct(
|
||||
CreateImageThumbnailAcionQueue $action,
|
||||
)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
private array $availableFormats = [
|
||||
'image/gif',
|
||||
'image/jpeg',
|
||||
@@ -22,23 +34,50 @@ class CreateImageThumbnailAction
|
||||
$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 intervention image
|
||||
$intervention = Image::make($file)->orientate();
|
||||
|
||||
// Generate avatar sizes
|
||||
collect(config('vuefilemanager.image_sizes'))
|
||||
collect(config('vuefilemanager.image_sizes.execute') )
|
||||
->each(function ($size) use ($intervention, $file_name, $user_id) {
|
||||
|
||||
// Create thumbnail only if image is larger than predefined image sizes
|
||||
if ($intervention->getWidth() > $size['size']) {
|
||||
// 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);
|
||||
// Create thumbnail instantly
|
||||
$this->action->execute($size, $file_name, $user_id, null);
|
||||
}
|
||||
});
|
||||
|
||||
$last = last(config('vuefilemanager.image_sizes.queue'));
|
||||
|
||||
collect(config('vuefilemanager.image_sizes.queue'))
|
||||
->each(function ($size) use ($intervention, $file_name, $user_id, $last) {
|
||||
|
||||
$is_last = $last['size'] === $size['size'] ? true : false;
|
||||
|
||||
// Create thumbnail only if image is larger than predefined image sizes
|
||||
if ($intervention->getWidth() > $size['size']) {
|
||||
|
||||
// Create thumbnail queue job
|
||||
$this->action->onQueue()->execute($size, $file_name, $user_id, $is_last);
|
||||
|
||||
} else {
|
||||
|
||||
// Delete file after generate a thumbnail
|
||||
Storage::disk('local')->delete("temp/$user_id/{$file_name}");
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,12 +69,13 @@ class UploadFileAction
|
||||
// Check if user has enough space to upload file
|
||||
($this->checkStorageCapacity)($user_id, $fileSize, $chunkName);
|
||||
|
||||
// Create multiple image thumbnails
|
||||
($this->createImageThumbnail)($fileName, $file, $user_id);
|
||||
|
||||
// Move finished file from chunk to file-manager directory
|
||||
$disk_local->move("chunks/$chunkName", "files/$user_id/$fileName");
|
||||
|
||||
// Create multiple image thumbnails
|
||||
($this->createImageThumbnail)($fileName, $file, $user_id);
|
||||
|
||||
// Move files to external storage
|
||||
if (! is_storage_driver(['local'])) {
|
||||
($this->moveFileToExternalStorage)($fileName, $user_id);
|
||||
|
||||
@@ -91,10 +91,11 @@ class File extends Model
|
||||
public function getThumbnailAttribute(): array | null
|
||||
{
|
||||
$links = [];
|
||||
$thumbnail_sizes = array_merge(config('vuefilemanager.image_sizes.execute'), config('vuefilemanager.image_sizes.queue'));
|
||||
|
||||
// Generate thumbnail link for external storage service
|
||||
if ($this->type === 'image' && ! is_storage_driver(['local'])) {
|
||||
foreach (config('vuefilemanager.image_sizes') as $item) {
|
||||
foreach ($thumbnail_sizes as $item) {
|
||||
$filePath = "files/{$this->user_id}/{$item['name']}-{$this->basename}";
|
||||
|
||||
$links[$item['name']] = Storage::temporaryUrl($filePath, now()->addHour());
|
||||
@@ -105,7 +106,7 @@ class File extends Model
|
||||
|
||||
// Generate thumbnail link for local storage
|
||||
if ($this->type === 'image') {
|
||||
foreach (config('vuefilemanager.image_sizes') as $item) {
|
||||
foreach ($thumbnail_sizes as $item) {
|
||||
$route = route('thumbnail', ['name' => $item['name'] . '-' . $this->basename]);
|
||||
|
||||
if ($this->public_access) {
|
||||
|
||||
@@ -39,8 +39,13 @@ class DumpTrashController extends Controller
|
||||
|
||||
// Delete thumbnail if exist
|
||||
if ($file->thumbnail) {
|
||||
Storage::delete("/files/$user_id/{$file->getRawOriginal('thumbnail')}");
|
||||
}
|
||||
|
||||
collect(array_merge(config('vuefilemanager.image_sizes.queue'), config('vuefilemanager.image_sizes.execute')))
|
||||
->each(function ($size) use ($file) {
|
||||
|
||||
Storage::delete("/files/$file->user_id/{$size['name']}-{$file->basename}");
|
||||
});
|
||||
}
|
||||
|
||||
// Delete file permanently
|
||||
$file->forceDelete();
|
||||
|
||||
@@ -605,7 +605,7 @@ if (! function_exists('get_thumbnail_file_list')) {
|
||||
*/
|
||||
function get_thumbnail_file_list(string $basename): Collection
|
||||
{
|
||||
return collect(config('vuefilemanager.image_sizes'))
|
||||
return collect(array_merge(config('vuefilemanager.image_sizes.queue'), config('vuefilemanager.image_sizes.execute')))
|
||||
->map(fn ($item) => $item['name'] . '-' . $basename);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ class FileTest extends TestCase
|
||||
"chunks/$file->basename"
|
||||
);
|
||||
|
||||
collect(config('vuefilemanager.image_sizes'))
|
||||
collect(array_merge(config('vuefilemanager.image_sizes.queue'), config('vuefilemanager.image_sizes.execute')))
|
||||
->each(
|
||||
fn ($item) =>
|
||||
$disk->assertExists(
|
||||
|
||||
@@ -646,8 +646,10 @@ class TeamManagementTest extends TestCase
|
||||
// Put fake image into correct directory
|
||||
Storage::putFileAs("files/$member->id", $fakeFile, $fakeFile->name);
|
||||
|
||||
$thumbnail_sizes = collect(array_merge(config('vuefilemanager.image_sizes.queue'), config('vuefilemanager.image_sizes.execute')));
|
||||
|
||||
// Create fake image thumbnails
|
||||
collect(config('vuefilemanager.image_sizes'))
|
||||
$thumbnail_sizes
|
||||
->each(function ($item) use ($member) {
|
||||
$fakeFile = UploadedFile::fake()
|
||||
->create("{$item['name']}-fake-image.jpeg", 2000, 'image/jpeg');
|
||||
@@ -675,7 +677,7 @@ class TeamManagementTest extends TestCase
|
||||
Storage::assertExists("files/$user->id/fake-file.zip");
|
||||
|
||||
// Assert if image thumbnails was moved correctly to the new destination
|
||||
collect(config('vuefilemanager.image_sizes'))
|
||||
$thumbnail_sizes
|
||||
->each(function ($item) use ($user) {
|
||||
Storage::assertExists("files/$user->id/{$item['name']}-fake-image.jpeg");
|
||||
});
|
||||
|
||||
@@ -109,6 +109,8 @@ class TrashTest extends TestCase
|
||||
|
||||
$disk = Storage::disk('local');
|
||||
|
||||
$thumbnail_sizes = array_merge(config('vuefilemanager.image_sizes.execute'), config('vuefilemanager.image_sizes.queue'));
|
||||
|
||||
$disk->assertMissing(
|
||||
"files/$user->id/fake-image.jpg"
|
||||
);
|
||||
@@ -116,5 +118,12 @@ class TrashTest extends TestCase
|
||||
$disk->assertMissing(
|
||||
"files/$user->id/thumbnail-fake-image.jpg"
|
||||
);
|
||||
|
||||
foreach($thumbnail_sizes as $size) {
|
||||
$disk->assertMissing(
|
||||
"files/$user->id/{$size['name']}-fake-image.jpg"
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user