Merge remote-tracking branch 'origin/thumbnails_queue' into subscription

# Conflicts:
#	public/mix-manifest.json
#	src/Domain/Files/Models/File.php
#	webpack.mix.js
This commit is contained in:
Čarodej
2021-12-21 10:33:05 +01:00
12 changed files with 170 additions and 84 deletions
@@ -1,44 +0,0 @@
<?php
namespace Domain\Files\Actions;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\ImageManagerStatic as Image;
class CreateImageThumbnailAction
{
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)) {
// Create intervention image
$intervention = Image::make($file)->orientate();
// Generate avatar sizes
collect(config('vuefilemanager.image_sizes'))
->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);
}
});
}
}
}
@@ -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");
}
}
}
@@ -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,
) {
}
@@ -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);