mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-05-15 17:55:02 +00:00
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:
+27
-19
@@ -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',
|
||||
|
||||
'immediately' => [
|
||||
[
|
||||
'size' => 120,
|
||||
'name' => 'xs',
|
||||
],
|
||||
[
|
||||
'size' => 240,
|
||||
'name' => 'sm',
|
||||
],
|
||||
],
|
||||
|
||||
'later' => [
|
||||
[
|
||||
'size' => 480,
|
||||
'name' => 'md',
|
||||
],
|
||||
[
|
||||
'size' => 960,
|
||||
'name' => 'lg',
|
||||
],
|
||||
[
|
||||
'size' => 1440,
|
||||
'name' => 'xl',
|
||||
],
|
||||
|
||||
]
|
||||
|
||||
],
|
||||
];
|
||||
|
||||
@@ -1180,7 +1180,10 @@ class SetupDevEnvironment extends Command
|
||||
$this->info("Generating thumbnails for $file...");
|
||||
|
||||
// Generate avatar sizes
|
||||
collect(config('vuefilemanager.image_sizes'))
|
||||
collect([
|
||||
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
|
||||
if ($intervention->getWidth() > $size['size']) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -91,10 +91,11 @@ class File extends Model
|
||||
public function getThumbnailAttribute(): array | null
|
||||
{
|
||||
$links = [];
|
||||
$thumbnail_sizes = collect(config('vuefilemanager.image_sizes'))->collapse()->all();
|
||||
|
||||
// Generate thumbnail link for external storage service
|
||||
if ($this->type === 'image' && ! is_storage_driver('local')) {
|
||||
foreach (config('vuefilemanager.image_sizes') as $item) {
|
||||
if ($this->type === 'image' && ! is_storage_driver(['local'])) {
|
||||
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,16 @@ class DumpTrashController extends Controller
|
||||
|
||||
// Delete thumbnail if exist
|
||||
if ($file->thumbnail) {
|
||||
Storage::delete("/files/$user_id/{$file->getRawOriginal('thumbnail')}");
|
||||
}
|
||||
|
||||
collect([
|
||||
config('vuefilemanager.image_sizes.later'),
|
||||
config('vuefilemanager.image_sizes.immediately'),
|
||||
])->collapse()
|
||||
->each(function ($size) use ($file) {
|
||||
|
||||
Storage::delete("/files/$file->user_id/{$size['name']}-{$file->basename}");
|
||||
});
|
||||
}
|
||||
|
||||
// Delete file permanently
|
||||
$file->forceDelete();
|
||||
|
||||
@@ -601,8 +601,11 @@ if (! function_exists('get_thumbnail_file_list')) {
|
||||
*/
|
||||
function get_thumbnail_file_list(string $basename): Collection
|
||||
{
|
||||
return collect(config('vuefilemanager.image_sizes'))
|
||||
->map(fn ($item) => $item['name'] . '-' . $basename);
|
||||
return collect([
|
||||
config('vuefilemanager.image_sizes.later'),
|
||||
config('vuefilemanager.image_sizes.immediately'),
|
||||
])->collapse()
|
||||
->map(fn ($item) => $item['name'] . '-' . $basename);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -53,10 +53,13 @@ class FileTest extends TestCase
|
||||
"chunks/$file->basename"
|
||||
);
|
||||
|
||||
collect(config('vuefilemanager.image_sizes'))
|
||||
collect([
|
||||
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}"
|
||||
)
|
||||
);
|
||||
|
||||
@@ -622,8 +622,10 @@ 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.later'), config('vuefilemanager.image_sizes.immediately')])->collapse();
|
||||
|
||||
// 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');
|
||||
@@ -651,7 +653,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,12 +109,17 @@ class TrashTest extends TestCase
|
||||
|
||||
$disk = Storage::disk('local');
|
||||
|
||||
$thumbnail_sizes = collect(config('vuefilemanager.image_sizes'))->collapse()->all();
|
||||
|
||||
$disk->assertMissing(
|
||||
"files/$user->id/fake-image.jpg"
|
||||
);
|
||||
|
||||
$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