Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalize TileSingleImage #761

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/Console/Commands/MigrateTiledImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
7 changes: 3 additions & 4 deletions app/Jobs/MigrateTiledImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,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;
}

Expand All @@ -38,9 +38,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']);
Expand Down
3 changes: 2 additions & 1 deletion app/Jobs/ProcessNewImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ protected function submitTileJob(Image $image)
$image->tiled = true;
$image->tilingInProgress = true;
$image->save();
TileSingleImage::dispatch($image);
$targetPath = fragment_uuid_path($image->uuid);
TileSingleImage::dispatch($image, config('image.tiles.disk'), $targetPath);
}
}
50 changes: 34 additions & 16 deletions app/Jobs/TileSingleImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Biigle\Jobs;

use Biigle\FileCache\Contracts\File;
use Biigle\Image;
use Exception;
use File;
use FileCache;
use FilesystemIterator;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand All @@ -22,9 +22,9 @@
/**
* The image to generate tiles for.
*
* @var Image
* @var File
*/
public $image;
public $file;

/**
* Path to the temporary storage file for the tiles.
Expand All @@ -33,6 +33,20 @@
*/
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.
*
Expand All @@ -43,14 +57,19 @@
/**
* Create a new job instance.
*
* @param Image $image The image to generate tiles for.
* @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(Image $image)
public function __construct(File $file, string $storage, string $targetPath)
{
$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 = $storage;
$this->targetPath = $targetPath;
}

/**
Expand All @@ -61,22 +80,22 @@
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);

Check failure on line 88 in app/Jobs/TileSingleImage.php

View workflow job for this annotation

GitHub Actions / lint-php

UndefinedClass

app/Jobs/TileSingleImage.php:88:13: UndefinedClass: Class, interface or enum named Biigle\FileCache\Contracts\File does not exist (see https://psalm.dev/019)
mzur marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* 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',
Expand All @@ -93,14 +112,13 @@
// +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($this->storage);
try {
foreach ($iterator as $pathname => $fileInfo) {
$disk->putFileAs($fragment, $fileInfo, substr($pathname, $prefixLength));
$disk->putFileAs($this->targetPath, $fileInfo, substr($pathname, $prefixLength));
}
} catch (Exception $e) {
$disk->deleteDirectory($fragment);
$disk->deleteDirectory($this->targetPath);
throw $e;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/php/Jobs/ProcessNewImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 7 additions & 5 deletions tests/php/Jobs/TileSingleImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class TileSingleImageTest extends TestCase
public function testGenerateTiles()
{
$image = ImageTest::create();
$job = new TileSingleImageStub($image);
Storage::fake('tiles');
$targetPath = fragment_uuid_path($image->uuid);
$job = new TileSingleImageStub($image, config('image.tiles.disk'), $targetPath);

$mock = Mockery::mock(Image::class);
$mock->shouldReceive('dzsave')
Expand All @@ -35,16 +37,16 @@ public function testUploadToStorage()
{
config(['image.tiles.disk' => 'tiles']);
$image = ImageTest::create();
$fragment = fragment_uuid_path($image->uuid);
$job = new TileSingleImageStub($image);
$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);
}
Expand Down
Loading