From 1930cda8ce9420abff3f2bfa44aaacaa54c33056 Mon Sep 17 00:00:00 2001 From: Max Tiessen Date: Fri, 26 Jan 2024 18:05:34 +0100 Subject: [PATCH 01/12] Made TileSingleImage more general to be used with other formats such as GeoOverlays --- app/Jobs/TileSingleImage.php | 44 ++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/app/Jobs/TileSingleImage.php b/app/Jobs/TileSingleImage.php index a6c8cda11..58a388bc2 100644 --- a/app/Jobs/TileSingleImage.php +++ b/app/Jobs/TileSingleImage.php @@ -24,7 +24,7 @@ class TileSingleImage extends Job implements ShouldQueue * * @var Image */ - public $image; + public $file; /** * Path to the temporary storage file for the tiles. @@ -33,6 +33,20 @@ class TileSingleImage extends Job implements ShouldQueue */ public $tempPath; + /** + * The name of the permanent storage-disk where the tiles should be stored. + * + * @var string + */ + public $storage; + + /** + * Path to the tiles within the permanent storage-disk. + * + * @var string + */ + public $fragment; + /** * Ignore this job if the image does not exist any more. * @@ -43,14 +57,17 @@ class TileSingleImage extends Job implements ShouldQueue /** * Create a new job instance. * - * @param Image $image The image to generate tiles for. + * @param Image $file The image to generate tiles for. * * @return void */ - public function __construct(Image $image) + public function __construct(Image $file) { - $this->image = $image; - $this->tempPath = config('image.tiles.tmp_dir')."/{$image->uuid}"; + $this->file = $file; + $this->tempPath = config('image.tiles.tmp_dir')."/{$file->uuid}"; + // for uploadToStorage method + $this->storage = 'image.tiles.disk'; + $this->fragment = fragment_uuid_path($file->uuid); } /** @@ -61,10 +78,10 @@ public function __construct(Image $image) public function handle() { try { - FileCache::getOnce($this->image, [$this, 'generateTiles']); + FileCache::getOnce($this->file, [$this, 'generateTiles']); $this->uploadToStorage(); - $this->image->tilingInProgress = false; - $this->image->save(); + $this->file->tilingInProgress = false; + $this->file->save(); } finally { File::deleteDirectory($this->tempPath); } @@ -73,10 +90,10 @@ public function handle() /** * Generate tiles for the image and put them to temporary storage. * - * @param Image $image + * @param Image $file * @param string $path Path to the cached image file. */ - public function generateTiles(Image $image, $path) + public function generateTiles($file, $path) { $this->getVipsImage($path)->dzsave($this->tempPath, [ 'layout' => 'zoomify', @@ -93,14 +110,13 @@ public function uploadToStorage() // +1 for the connecting slash. $prefixLength = strlen($this->tempPath) + 1; $iterator = $this->getIterator($this->tempPath); - $disk = Storage::disk(config('image.tiles.disk')); - $fragment = fragment_uuid_path($this->image->uuid); + $disk = Storage::disk(config($this->storage)); try { foreach ($iterator as $pathname => $fileInfo) { - $disk->putFileAs($fragment, $fileInfo, substr($pathname, $prefixLength)); + $disk->putFileAs($this->fragment, $fileInfo, substr($pathname, $prefixLength)); } } catch (Exception $e) { - $disk->deleteDirectory($fragment); + $disk->deleteDirectory($this->fragment); throw $e; } } From ef584fe5abab860dd13ef7f23ac027e0085c8d7f Mon Sep 17 00:00:00 2001 From: Max Tiessen Date: Mon, 29 Jan 2024 14:30:40 +0100 Subject: [PATCH 02/12] Adapt variable-name when using TileSingleImage class --- tests/php/Jobs/ProcessNewImageTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/php/Jobs/ProcessNewImageTest.php b/tests/php/Jobs/ProcessNewImageTest.php index 73a9088a5..2926fa22c 100644 --- a/tests/php/Jobs/ProcessNewImageTest.php +++ b/tests/php/Jobs/ProcessNewImageTest.php @@ -130,7 +130,7 @@ public function testHandleTileLargeImage() Queue::fake(); with(new ProcessNewImageMock($image))->handle(); - Queue::assertPushed(TileSingleImage::class, fn ($job) => $job->image->id === $image->id); + Queue::assertPushed(TileSingleImage::class, fn ($job) => $job->file->id === $image->id); $image->refresh(); $this->assertTrue($image->tiled); $this->assertTrue($image->tilingInProgress); From 4f42cb7bf35ace46d1cbeacf4f6b98fc776ec3f0 Mon Sep 17 00:00:00 2001 From: Max Tiessen Date: Thu, 1 Feb 2024 21:13:34 +0100 Subject: [PATCH 03/12] changing type of TileSingleImage to more general 'File', added storage variable to constructor --- app/Jobs/ProcessNewImage.php | 2 +- app/Jobs/TileSingleImage.php | 12 ++++++------ tests/php/Jobs/TileSingleImageTest.php | 5 +++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/app/Jobs/ProcessNewImage.php b/app/Jobs/ProcessNewImage.php index 65f953733..326cde28a 100644 --- a/app/Jobs/ProcessNewImage.php +++ b/app/Jobs/ProcessNewImage.php @@ -342,6 +342,6 @@ protected function submitTileJob(Image $image) $image->tiled = true; $image->tilingInProgress = true; $image->save(); - TileSingleImage::dispatch($image); + TileSingleImage::dispatch($image, config('image.tiles.disk')); } } diff --git a/app/Jobs/TileSingleImage.php b/app/Jobs/TileSingleImage.php index 58a388bc2..55f580f2e 100644 --- a/app/Jobs/TileSingleImage.php +++ b/app/Jobs/TileSingleImage.php @@ -4,13 +4,13 @@ use Biigle\Image; use Exception; -use File; use FileCache; use FilesystemIterator; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Storage; +use Biigle\FileCache\Contracts\File; use RecursiveDirectoryIterator; use RecursiveIteratorIterator; use VipsImage; @@ -22,7 +22,7 @@ class TileSingleImage extends Job implements ShouldQueue /** * The image to generate tiles for. * - * @var Image + * @var File */ public $file; @@ -57,16 +57,16 @@ class TileSingleImage extends Job implements ShouldQueue /** * Create a new job instance. * - * @param Image $file The image to generate tiles for. + * @param File $file The image to generate tiles for. * * @return void */ - public function __construct(Image $file) + public function __construct(File $file, string $storage) { $this->file = $file; $this->tempPath = config('image.tiles.tmp_dir')."/{$file->uuid}"; // for uploadToStorage method - $this->storage = 'image.tiles.disk'; + $this->storage = $storage; $this->fragment = fragment_uuid_path($file->uuid); } @@ -110,7 +110,7 @@ public function uploadToStorage() // +1 for the connecting slash. $prefixLength = strlen($this->tempPath) + 1; $iterator = $this->getIterator($this->tempPath); - $disk = Storage::disk(config($this->storage)); + $disk = Storage::disk($this->storage); try { foreach ($iterator as $pathname => $fileInfo) { $disk->putFileAs($this->fragment, $fileInfo, substr($pathname, $prefixLength)); diff --git a/tests/php/Jobs/TileSingleImageTest.php b/tests/php/Jobs/TileSingleImageTest.php index 54512cc07..de4c769b5 100644 --- a/tests/php/Jobs/TileSingleImageTest.php +++ b/tests/php/Jobs/TileSingleImageTest.php @@ -15,7 +15,8 @@ class TileSingleImageTest extends TestCase public function testGenerateTiles() { $image = ImageTest::create(); - $job = new TileSingleImageStub($image); + Storage::fake('tiles'); + $job = new TileSingleImageStub($image, config('image.tiles.disk')); $mock = Mockery::mock(Image::class); $mock->shouldReceive('dzsave') @@ -36,7 +37,7 @@ public function testUploadToStorage() config(['image.tiles.disk' => 'tiles']); $image = ImageTest::create(); $fragment = fragment_uuid_path($image->uuid); - $job = new TileSingleImageStub($image); + $job = new TileSingleImageStub($image, config('image.tiles.disk')); File::makeDirectory($job->tempPath); File::put("{$job->tempPath}/test.txt", 'test'); From 0d84d7e911b06aa640fd65f1e8be3b0b13508625 Mon Sep 17 00:00:00 2001 From: Max Tiessen Date: Thu, 1 Feb 2024 21:42:14 +0100 Subject: [PATCH 04/12] Add storage variable in TileSingleImage as constructor argument and adapt the tests. --- app/Jobs/ProcessNewImage.php | 3 ++- app/Jobs/TileSingleImage.php | 14 ++++++++------ tests/php/Jobs/TileSingleImageTest.php | 11 ++++++----- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/app/Jobs/ProcessNewImage.php b/app/Jobs/ProcessNewImage.php index 326cde28a..8291f1ca1 100644 --- a/app/Jobs/ProcessNewImage.php +++ b/app/Jobs/ProcessNewImage.php @@ -342,6 +342,7 @@ protected function submitTileJob(Image $image) $image->tiled = true; $image->tilingInProgress = true; $image->save(); - TileSingleImage::dispatch($image, config('image.tiles.disk')); + $targetPath = fragment_uuid_path($image->uuid); + TileSingleImage::dispatch($image, config('image.tiles.disk'), $targetPath); } } diff --git a/app/Jobs/TileSingleImage.php b/app/Jobs/TileSingleImage.php index 55f580f2e..b9b8d8b19 100644 --- a/app/Jobs/TileSingleImage.php +++ b/app/Jobs/TileSingleImage.php @@ -34,7 +34,7 @@ class TileSingleImage extends Job implements ShouldQueue public $tempPath; /** - * The name of the permanent storage-disk where the tiles should be stored. + * The path of the permanent storage-disk where the tiles should be stored. * * @var string */ @@ -45,7 +45,7 @@ class TileSingleImage extends Job implements ShouldQueue * * @var string */ - public $fragment; + public $targetPath; /** * Ignore this job if the image does not exist any more. @@ -58,16 +58,18 @@ class TileSingleImage extends Job implements ShouldQueue * Create a new job instance. * * @param File $file The image to generate tiles for. + * @param string $storage The path to storage-disk where the tiles should be stored + * @param string targetPath The path to the tiles within the permanent storage-disk * * @return void */ - public function __construct(File $file, string $storage) + public function __construct(File $file, string $storage, string $targetPath) { $this->file = $file; $this->tempPath = config('image.tiles.tmp_dir')."/{$file->uuid}"; // for uploadToStorage method $this->storage = $storage; - $this->fragment = fragment_uuid_path($file->uuid); + $this->targetPath = $targetPath; } /** @@ -113,10 +115,10 @@ public function uploadToStorage() $disk = Storage::disk($this->storage); try { foreach ($iterator as $pathname => $fileInfo) { - $disk->putFileAs($this->fragment, $fileInfo, substr($pathname, $prefixLength)); + $disk->putFileAs($this->targetPath, $fileInfo, substr($pathname, $prefixLength)); } } catch (Exception $e) { - $disk->deleteDirectory($this->fragment); + $disk->deleteDirectory($this->targetPath); throw $e; } } diff --git a/tests/php/Jobs/TileSingleImageTest.php b/tests/php/Jobs/TileSingleImageTest.php index de4c769b5..d66ba352d 100644 --- a/tests/php/Jobs/TileSingleImageTest.php +++ b/tests/php/Jobs/TileSingleImageTest.php @@ -16,7 +16,8 @@ public function testGenerateTiles() { $image = ImageTest::create(); Storage::fake('tiles'); - $job = new TileSingleImageStub($image, config('image.tiles.disk')); + $targetPath = fragment_uuid_path($image->uuid); + $job = new TileSingleImageStub($image, config('image.tiles.disk'), $targetPath); $mock = Mockery::mock(Image::class); $mock->shouldReceive('dzsave') @@ -36,16 +37,16 @@ public function testUploadToStorage() { config(['image.tiles.disk' => 'tiles']); $image = ImageTest::create(); - $fragment = fragment_uuid_path($image->uuid); - $job = new TileSingleImageStub($image, config('image.tiles.disk')); + $targetPath = fragment_uuid_path($image->uuid); + $job = new TileSingleImageStub($image, config('image.tiles.disk'), $targetPath); File::makeDirectory($job->tempPath); File::put("{$job->tempPath}/test.txt", 'test'); try { Storage::fake('tiles'); $job->uploadToStorage(); - Storage::disk('tiles')->assertExists($fragment); - Storage::disk('tiles')->assertExists("{$fragment}/test.txt"); + Storage::disk('tiles')->assertExists($targetPath); + Storage::disk('tiles')->assertExists("{$targetPath}/test.txt"); } finally { File::deleteDirectory($job->tempPath); } From 1bdbbb45dfcf430114ea4a296c95ac184395bdca Mon Sep 17 00:00:00 2001 From: Max Tiessen Date: Thu, 1 Feb 2024 22:08:03 +0100 Subject: [PATCH 05/12] Adapt MigrateTiledImage class to conform with TileSIngleImage --- app/Console/Commands/MigrateTiledImages.php | 3 ++- app/Jobs/MigrateTiledImage.php | 14 ++++++++++---- app/Jobs/TileSingleImage.php | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/app/Console/Commands/MigrateTiledImages.php b/app/Console/Commands/MigrateTiledImages.php index c1b014910..bd847c977 100644 --- a/app/Console/Commands/MigrateTiledImages.php +++ b/app/Console/Commands/MigrateTiledImages.php @@ -40,7 +40,8 @@ public function handle() $query->eachById(function ($image) use ($dryRun, $bar, $disk) { if (!$dryRun) { - Queue::push(new MigrateTiledImage($image, $disk)); + $targetPath = fragment_uuid_path($image->uuid); + Queue::push(new MigrateTiledImage($image, $disk, $targetPath)); } $bar->advance(); }); diff --git a/app/Jobs/MigrateTiledImage.php b/app/Jobs/MigrateTiledImage.php index 2d8600bb6..18c8e7bef 100644 --- a/app/Jobs/MigrateTiledImage.php +++ b/app/Jobs/MigrateTiledImage.php @@ -16,6 +16,13 @@ class MigrateTiledImage extends TileSingleImage */ public $disk; + /** + * Path to the tiles within the permanent storage-disk. + * + * @var string + */ + public $targetPath; + /** * Create a new job instance. * @@ -24,9 +31,9 @@ class MigrateTiledImage extends TileSingleImage * * @return void */ - public function __construct(Image $image, $disk) + public function __construct(Image $image, string $disk, string $targetPath) { - parent::__construct($image); + parent::__construct($image, $disk, $targetPath); $this->disk = $disk; } @@ -38,9 +45,8 @@ public function __construct(Image $image, $disk) public function handle() { try { - $fragment = fragment_uuid_path($this->image->uuid); $tmpResource = tmpfile(); - $zipResource = Storage::disk($this->disk)->readStream($fragment); + $zipResource = Storage::disk($this->disk)->readStream($this->targetPath); stream_copy_to_stream($zipResource, $tmpResource); $zip = new ZipArchive; $zip->open(stream_get_meta_data($tmpResource)['uri']); diff --git a/app/Jobs/TileSingleImage.php b/app/Jobs/TileSingleImage.php index b9b8d8b19..4b2831cf2 100644 --- a/app/Jobs/TileSingleImage.php +++ b/app/Jobs/TileSingleImage.php @@ -2,6 +2,7 @@ namespace Biigle\Jobs; +use Biigle\FileCache\Contracts\File; use Biigle\Image; use Exception; use FileCache; @@ -10,7 +11,6 @@ use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Storage; -use Biigle\FileCache\Contracts\File; use RecursiveDirectoryIterator; use RecursiveIteratorIterator; use VipsImage; From da20c9d63d698f264a7e3dcf9d4d3b885fb1a8a4 Mon Sep 17 00:00:00 2001 From: Max Tiessen Date: Fri, 2 Feb 2024 21:04:47 +0100 Subject: [PATCH 06/12] remove doubled variable declaration --- app/Jobs/MigrateTiledImage.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/app/Jobs/MigrateTiledImage.php b/app/Jobs/MigrateTiledImage.php index 18c8e7bef..7edf2d270 100644 --- a/app/Jobs/MigrateTiledImage.php +++ b/app/Jobs/MigrateTiledImage.php @@ -16,13 +16,6 @@ class MigrateTiledImage extends TileSingleImage */ public $disk; - /** - * Path to the tiles within the permanent storage-disk. - * - * @var string - */ - public $targetPath; - /** * Create a new job instance. * From c5cda584c4a1353dc8ed9945e299ba17dd225417 Mon Sep 17 00:00:00 2001 From: Max T Date: Thu, 18 Jul 2024 13:51:49 +0200 Subject: [PATCH 07/12] Added missing File class to TileSingleImage + test for checking handle() method --- app/Jobs/TileSingleImage.php | 3 ++- tests/php/Jobs/TileSingleImageTest.php | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/Jobs/TileSingleImage.php b/app/Jobs/TileSingleImage.php index 4b2831cf2..a2c2ca4b1 100644 --- a/app/Jobs/TileSingleImage.php +++ b/app/Jobs/TileSingleImage.php @@ -2,6 +2,7 @@ namespace Biigle\Jobs; +use File as FileFacade; use Biigle\FileCache\Contracts\File; use Biigle\Image; use Exception; @@ -85,7 +86,7 @@ public function handle() $this->file->tilingInProgress = false; $this->file->save(); } finally { - File::deleteDirectory($this->tempPath); + FileFacade::deleteDirectory($this->tempPath); } } diff --git a/tests/php/Jobs/TileSingleImageTest.php b/tests/php/Jobs/TileSingleImageTest.php index d66ba352d..48ff17df1 100644 --- a/tests/php/Jobs/TileSingleImageTest.php +++ b/tests/php/Jobs/TileSingleImageTest.php @@ -43,7 +43,6 @@ public function testUploadToStorage() File::put("{$job->tempPath}/test.txt", 'test'); try { - Storage::fake('tiles'); $job->uploadToStorage(); Storage::disk('tiles')->assertExists($targetPath); Storage::disk('tiles')->assertExists("{$targetPath}/test.txt"); @@ -51,6 +50,22 @@ public function testUploadToStorage() File::deleteDirectory($job->tempPath); } } + + public function testHandleFunction() + { + $image = ImageTest::create(); + $image->tilingInProgress = true; + $targetPath = fragment_uuid_path($image->uuid); + $job = new TileSingleImage($image, config('image.tiles.disk'), $targetPath); + + $this->assertEquals($job->file->tilingInProgress, true); + // execute the job with handle() method + $job->handle(); + $this->assertEquals($job->file->tilingInProgress, false); + Storage::disk('tiles')->assertExists($targetPath); + // check that temporary storage path got properly deleted in handle() method + Storage::disk('tiles')->assertMissing($job->tempPath); + } } class TileSingleImageStub extends TileSingleImage From 45d2c18a4487dda1fbd1dcae36c93c37ece67e93 Mon Sep 17 00:00:00 2001 From: Max T Date: Thu, 18 Jul 2024 15:01:42 +0200 Subject: [PATCH 08/12] add Storage::fake() to the test-cases --- tests/php/Jobs/TileSingleImageTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/php/Jobs/TileSingleImageTest.php b/tests/php/Jobs/TileSingleImageTest.php index 48ff17df1..fc0d03fe3 100644 --- a/tests/php/Jobs/TileSingleImageTest.php +++ b/tests/php/Jobs/TileSingleImageTest.php @@ -43,6 +43,7 @@ public function testUploadToStorage() File::put("{$job->tempPath}/test.txt", 'test'); try { + Storage::fake('tiles'); $job->uploadToStorage(); Storage::disk('tiles')->assertExists($targetPath); Storage::disk('tiles')->assertExists("{$targetPath}/test.txt"); @@ -59,6 +60,7 @@ public function testHandleFunction() $job = new TileSingleImage($image, config('image.tiles.disk'), $targetPath); $this->assertEquals($job->file->tilingInProgress, true); + Storage::fake('tiles'); // execute the job with handle() method $job->handle(); $this->assertEquals($job->file->tilingInProgress, false); From f4e90bbe3ca6b42eb0b18d496d21339361d03531 Mon Sep 17 00:00:00 2001 From: Max T Date: Thu, 12 Sep 2024 10:35:18 +0200 Subject: [PATCH 09/12] Adapted testQueue-method to generalized constructor of TileSingleImage --- tests/php/Jobs/TileSingleImageTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/php/Jobs/TileSingleImageTest.php b/tests/php/Jobs/TileSingleImageTest.php index d9c00a090..040c9cd70 100644 --- a/tests/php/Jobs/TileSingleImageTest.php +++ b/tests/php/Jobs/TileSingleImageTest.php @@ -73,7 +73,8 @@ public function testQueue() { config(['image.tiles.queue' => 'myqueue']); $image = ImageTest::create(); - $job = new TileSingleImageStub($image); + $targetPath = fragment_uuid_path($image->uuid); + $job = new TileSingleImageStub($image, config('image.tiles.disk'), $targetPath); $this->assertSame('myqueue', $job->queue); } } From e09eb4d8280bb9a0344de9ff6923f8a7a70d41e0 Mon Sep 17 00:00:00 2001 From: Max T Date: Thu, 12 Sep 2024 10:50:54 +0200 Subject: [PATCH 10/12] Fixed missing argument in docs --- app/Jobs/TileSingleImage.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Jobs/TileSingleImage.php b/app/Jobs/TileSingleImage.php index 8c1bbfc02..154f76f21 100644 --- a/app/Jobs/TileSingleImage.php +++ b/app/Jobs/TileSingleImage.php @@ -60,7 +60,7 @@ class TileSingleImage extends Job implements ShouldQueue * * @param File $file The image to generate tiles for. * @param string $storage The path to storage-disk where the tiles should be stored - * @param string targetPath The path to the tiles within the permanent storage-disk + * @param string $targetPath The path to the tiles within the permanent storage-disk * * @return void */ From 06507ac2e42304da30698845e9957f26b44f434e Mon Sep 17 00:00:00 2001 From: Max T Date: Thu, 12 Sep 2024 11:15:57 +0200 Subject: [PATCH 11/12] fix the ordered_imports violation --- app/Jobs/TileSingleImage.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Jobs/TileSingleImage.php b/app/Jobs/TileSingleImage.php index 154f76f21..244fd3930 100644 --- a/app/Jobs/TileSingleImage.php +++ b/app/Jobs/TileSingleImage.php @@ -2,10 +2,10 @@ namespace Biigle\Jobs; -use File as FileFacade; use Biigle\FileCache\Contracts\File; use Biigle\Image; use Exception; +use File as FileFacade; use FileCache; use FilesystemIterator; use Illuminate\Contracts\Queue\ShouldQueue; From aa918234dc25300185a75e6bee21dd741efc6e38 Mon Sep 17 00:00:00 2001 From: Max T Date: Fri, 20 Sep 2024 10:54:44 +0200 Subject: [PATCH 12/12] Created abstract class TileSingleObject and child classes for image and geo-overlay processing --- app/Jobs/TileSingleImage.php | 117 ++---------------------------- app/Jobs/TileSingleObject.php | 133 ++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 112 deletions(-) create mode 100644 app/Jobs/TileSingleObject.php diff --git a/app/Jobs/TileSingleImage.php b/app/Jobs/TileSingleImage.php index 244fd3930..0f03ed8d9 100644 --- a/app/Jobs/TileSingleImage.php +++ b/app/Jobs/TileSingleImage.php @@ -2,75 +2,33 @@ namespace Biigle\Jobs; -use Biigle\FileCache\Contracts\File; use Biigle\Image; -use Exception; use File as FileFacade; use FileCache; -use FilesystemIterator; -use Illuminate\Contracts\Queue\ShouldQueue; -use Illuminate\Queue\InteractsWithQueue; -use Illuminate\Queue\SerializesModels; -use Illuminate\Support\Facades\Storage; -use Jcupitt\Vips\Image as VipsImage; -use RecursiveDirectoryIterator; -use RecursiveIteratorIterator; -class TileSingleImage extends Job implements ShouldQueue +class TileSingleImage extends TileSingleObject { - use InteractsWithQueue, SerializesModels; - /** * The image to generate tiles for. * - * @var File + * @var Image */ public $file; - /** - * Path to the temporary storage file for the tiles. - * - * @var string - */ - public $tempPath; - - /** - * The path of the permanent storage-disk where the tiles should be stored. - * - * @var string - */ - public $storage; - - /** - * Path to the tiles within the permanent storage-disk. - * - * @var string - */ - public $targetPath; - - /** - * Ignore this job if the image does not exist any more. - * - * @var bool - */ - protected $deleteWhenMissingModels = true; - /** * Create a new job instance. * - * @param File $file The image to generate tiles for. + * @param Image $file The image to generate tiles for. * @param string $storage The path to storage-disk where the tiles should be stored * @param string $targetPath The path to the tiles within the permanent storage-disk * * @return void */ - public function __construct(File $file, string $storage, string $targetPath) + public function __construct(Image $file, string $storage, string $targetPath) { + parent::__construct($storage, $targetPath); $this->file = $file; $this->tempPath = config('image.tiles.tmp_dir')."/{$file->uuid}"; - // for uploadToStorage method - $this->storage = $storage; - $this->targetPath = $targetPath; $this->queue = config('image.tiles.queue'); } @@ -90,69 +48,4 @@ public function handle() FileFacade::deleteDirectory($this->tempPath); } } - - /** - * Generate tiles for the image and put them to temporary storage. - * - * @param Image $file - * @param string $path Path to the cached image file. - */ - public function generateTiles($file, $path) - { - $this->getVipsImage($path)->dzsave($this->tempPath, [ - 'layout' => 'zoomify', - 'container' => 'fs', - 'strip' => true, - ]); - } - - /** - * Upload the tiles from temporary local storage to the tiles storage disk. - */ - public function uploadToStorage() - { - // +1 for the connecting slash. - $prefixLength = strlen($this->tempPath) + 1; - $iterator = $this->getIterator($this->tempPath); - $disk = Storage::disk($this->storage); - try { - foreach ($iterator as $pathname => $fileInfo) { - $disk->putFileAs($this->targetPath, $fileInfo, substr($pathname, $prefixLength)); - } - } catch (Exception $e) { - $disk->deleteDirectory($this->targetPath); - throw $e; - } - } - - /** - * Get the vips image instance. - * - * @param string $path - * - * @return \Jcupitt\Vips\Image - */ - protected function getVipsImage($path) - { - return VipsImage::newFromFile($path, ['access' => 'sequential']); - } - - /** - * Get the recursive directory iterator for the given path. - * - * @param string $path - * - * @return RecursiveIteratorIterator - */ - protected function getIterator($path) - { - return new RecursiveIteratorIterator( - new RecursiveDirectoryIterator( - $path, - FilesystemIterator::KEY_AS_PATHNAME | - FilesystemIterator::CURRENT_AS_FILEINFO | - FilesystemIterator::SKIP_DOTS - ) - ); - } } diff --git a/app/Jobs/TileSingleObject.php b/app/Jobs/TileSingleObject.php new file mode 100644 index 000000000..16073f1f5 --- /dev/null +++ b/app/Jobs/TileSingleObject.php @@ -0,0 +1,133 @@ +storage = $storage; + $this->targetPath = $targetPath; + } + + /** + * Execute the job. + * + * @return void + */ + abstract protected function handle(); + + /** + * Generate tiles for the object and put them to temporary storage. + * + * @param File $file + * @param string $path Path to the cached image file. + */ + public function generateTiles($file, $path) + { + $this->getVipsImage($path)->dzsave($this->tempPath, [ + 'layout' => 'zoomify', + 'container' => 'fs', + 'strip' => true, + ]); + } + + /** + * Upload the tiles from temporary local storage to the tiles storage disk. + */ + public function uploadToStorage() + { + // +1 for the connecting slash. + $prefixLength = strlen($this->tempPath) + 1; + $iterator = $this->getIterator($this->tempPath); + $disk = Storage::disk($this->storage); + try { + foreach ($iterator as $pathname => $fileInfo) { + $disk->putFileAs($this->targetPath, $fileInfo, substr($pathname, $prefixLength)); + } + } catch (Exception $e) { + $disk->deleteDirectory($this->targetPath); + throw $e; + } + } + + /** + * Get the vips image instance. + * + * @param string $path + * + * @return \Jcupitt\Vips\Image + */ + protected function getVipsImage($path) + { + return VipsImage::newFromFile($path, ['access' => 'sequential']); + } + + /** + * Get the recursive directory iterator for the given path. + * + * @param string $path + * + * @return RecursiveIteratorIterator + */ + protected function getIterator($path) + { + return new RecursiveIteratorIterator( + new RecursiveDirectoryIterator( + $path, + FilesystemIterator::KEY_AS_PATHNAME | + FilesystemIterator::CURRENT_AS_FILEINFO | + FilesystemIterator::SKIP_DOTS + ) + ); + } +}