make queue for thumbnails generation

This commit is contained in:
Milos Holba
2021-11-30 15:49:33 +01:00
parent 73cd950054
commit 067c123aa8
12 changed files with 240 additions and 1527 deletions
+27 -19
View File
@@ -29,25 +29,33 @@ return [
], ],
'image_sizes' => [ 'image_sizes' => [
[
'size' => 1440, 'execute' => [
'name' => 'xl', [
], 'size' => 120,
[ 'name' => 'xs',
'size' => 960, ],
'name' => 'lg', [
], 'size' => 240,
[ 'name' => 'sm',
'size' => 480, ],
'name' => 'md',
],
[
'size' => 240,
'name' => 'sm',
],
[
'size' => 120,
'name' => 'xs',
], ],
'queue' => [
[
'size' => 480,
'name' => 'md',
],
[
'size' => 960,
'name' => 'lg',
],
[
'size' => 1440,
'name' => 'xl',
],
]
], ],
]; ];
+100 -1492
View File
File diff suppressed because it is too large Load Diff
@@ -1140,7 +1140,7 @@ class SetupDevEnvironment extends Command
$this->info("Generating thumbnails for $file..."); $this->info("Generating thumbnails for $file...");
// Generate avatar sizes // 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) { ->each(function ($size) use ($intervention, $file_name, $user) {
// Create thumbnail only if image is larger than predefined image sizes // Create thumbnail only if image is larger than predefined image sizes
if ($intervention->getWidth() > $size['size']) { if ($intervention->getWidth() > $size['size']) {
@@ -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; namespace Domain\Files\Actions;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Spatie\QueueableAction\QueueableAction;
use Intervention\Image\ImageManagerStatic as Image; use Intervention\Image\ImageManagerStatic as Image;
use Domain\Files\Actions\CreateImageThumbnailAcionQueue;
class CreateImageThumbnailAction class CreateImageThumbnailAction
{ {
use QueueableAction;
public function __construct(
CreateImageThumbnailAcionQueue $action,
)
{
$this->action = $action;
}
private array $availableFormats = [ private array $availableFormats = [
'image/gif', 'image/gif',
'image/jpeg', 'image/jpeg',
@@ -22,23 +34,50 @@ class CreateImageThumbnailAction
$file, $file,
string $user_id string $user_id
): void { ): void {
// Create thumbnail from image // Create thumbnail from image
if (in_array($file->getClientMimeType(), $this->availableFormats)) { 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 // Create intervention image
$intervention = Image::make($file)->orientate(); $intervention = Image::make($file)->orientate();
// Generate avatar sizes // Generate avatar sizes
collect(config('vuefilemanager.image_sizes')) collect(config('vuefilemanager.image_sizes.execute') )
->each(function ($size) use ($intervention, $file_name, $user_id) { ->each(function ($size) use ($intervention, $file_name, $user_id) {
// Create thumbnail only if image is larger than predefined image sizes // Create thumbnail only if image is larger than predefined image sizes
if ($intervention->getWidth() > $size['size']) { if ($intervention->getWidth() > $size['size']) {
// Generate thumbnail
$intervention->resize($size['size'], null, fn ($constraint) => $constraint->aspectRatio())->stream();
// Store thumbnail to disk // Create thumbnail instantly
Storage::put("files/$user_id/{$size['name']}-{$file_name}", $intervention); $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 // Check if user has enough space to upload file
($this->checkStorageCapacity)($user_id, $fileSize, $chunkName); ($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 // Move finished file from chunk to file-manager directory
$disk_local->move("chunks/$chunkName", "files/$user_id/$fileName"); $disk_local->move("chunks/$chunkName", "files/$user_id/$fileName");
// Create multiple image thumbnails
($this->createImageThumbnail)($fileName, $file, $user_id);
// Move files to external storage // Move files to external storage
if (! is_storage_driver(['local'])) { if (! is_storage_driver(['local'])) {
($this->moveFileToExternalStorage)($fileName, $user_id); ($this->moveFileToExternalStorage)($fileName, $user_id);
+3 -2
View File
@@ -91,10 +91,11 @@ class File extends Model
public function getThumbnailAttribute(): array | null public function getThumbnailAttribute(): array | null
{ {
$links = []; $links = [];
$thumbnail_sizes = array_merge(config('vuefilemanager.image_sizes.execute'), config('vuefilemanager.image_sizes.queue'));
// Generate thumbnail link for external storage service // Generate thumbnail link for external storage service
if ($this->type === 'image' && ! is_storage_driver(['local'])) { 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}"; $filePath = "files/{$this->user_id}/{$item['name']}-{$this->basename}";
$links[$item['name']] = Storage::temporaryUrl($filePath, now()->addHour()); $links[$item['name']] = Storage::temporaryUrl($filePath, now()->addHour());
@@ -105,7 +106,7 @@ class File extends Model
// Generate thumbnail link for local storage // Generate thumbnail link for local storage
if ($this->type === 'image') { if ($this->type === 'image') {
foreach (config('vuefilemanager.image_sizes') as $item) { foreach ($thumbnail_sizes as $item) {
$route = route('thumbnail', ['name' => $item['name'] . '-' . $this->basename]); $route = route('thumbnail', ['name' => $item['name'] . '-' . $this->basename]);
if ($this->public_access) { if ($this->public_access) {
@@ -39,8 +39,13 @@ class DumpTrashController extends Controller
// Delete thumbnail if exist // Delete thumbnail if exist
if ($file->thumbnail) { 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 // Delete file permanently
$file->forceDelete(); $file->forceDelete();
+1 -1
View File
@@ -605,7 +605,7 @@ if (! function_exists('get_thumbnail_file_list')) {
*/ */
function get_thumbnail_file_list(string $basename): Collection 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); ->map(fn ($item) => $item['name'] . '-' . $basename);
} }
} }
+1 -1
View File
@@ -53,7 +53,7 @@ class FileTest extends TestCase
"chunks/$file->basename" "chunks/$file->basename"
); );
collect(config('vuefilemanager.image_sizes')) collect(array_merge(config('vuefilemanager.image_sizes.queue'), config('vuefilemanager.image_sizes.execute')))
->each( ->each(
fn ($item) => fn ($item) =>
$disk->assertExists( $disk->assertExists(
+4 -2
View File
@@ -646,8 +646,10 @@ class TeamManagementTest extends TestCase
// Put fake image into correct directory // Put fake image into correct directory
Storage::putFileAs("files/$member->id", $fakeFile, $fakeFile->name); 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 // Create fake image thumbnails
collect(config('vuefilemanager.image_sizes')) $thumbnail_sizes
->each(function ($item) use ($member) { ->each(function ($item) use ($member) {
$fakeFile = UploadedFile::fake() $fakeFile = UploadedFile::fake()
->create("{$item['name']}-fake-image.jpeg", 2000, 'image/jpeg'); ->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"); Storage::assertExists("files/$user->id/fake-file.zip");
// Assert if image thumbnails was moved correctly to the new destination // Assert if image thumbnails was moved correctly to the new destination
collect(config('vuefilemanager.image_sizes')) $thumbnail_sizes
->each(function ($item) use ($user) { ->each(function ($item) use ($user) {
Storage::assertExists("files/$user->id/{$item['name']}-fake-image.jpeg"); Storage::assertExists("files/$user->id/{$item['name']}-fake-image.jpeg");
}); });
+9
View File
@@ -109,6 +109,8 @@ class TrashTest extends TestCase
$disk = Storage::disk('local'); $disk = Storage::disk('local');
$thumbnail_sizes = array_merge(config('vuefilemanager.image_sizes.execute'), config('vuefilemanager.image_sizes.queue'));
$disk->assertMissing( $disk->assertMissing(
"files/$user->id/fake-image.jpg" "files/$user->id/fake-image.jpg"
); );
@@ -116,5 +118,12 @@ class TrashTest extends TestCase
$disk->assertMissing( $disk->assertMissing(
"files/$user->id/thumbnail-fake-image.jpg" "files/$user->id/thumbnail-fake-image.jpg"
); );
foreach($thumbnail_sizes as $size) {
$disk->assertMissing(
"files/$user->id/{$size['name']}-fake-image.jpg"
);
}
} }
} }