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 3 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
14 changes: 10 additions & 4 deletions app/Jobs/MigrateTiledImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ class MigrateTiledImage extends TileSingleImage
*/
public $disk;

/**
* Path to the tiles within the permanent storage-disk.
*
* @var string
*/
public $targetPath;

mzur marked this conversation as resolved.
Show resolved Hide resolved
/**
* Create a new job instance.
*
Expand All @@ -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;
}

Expand All @@ -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']);
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);
}
}
24 changes: 13 additions & 11 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,7 +22,7 @@
/**
* The image to generate tiles for.
*
* @var Image
* @var File
*/
public $file;

Expand All @@ -34,7 +34,7 @@
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
*/
Expand All @@ -45,7 +45,7 @@
*
* @var string
*/
public $fragment;
public $targetPath;

/**
* Ignore this job if the image does not exist any more.
Expand All @@ -57,17 +57,19 @@
/**
* Create a new job instance.
*
* @param Image $file 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 $file)
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 = 'image.tiles.disk';
$this->fragment = fragment_uuid_path($file->uuid);
$this->storage = $storage;
$this->targetPath = $targetPath;
}

/**
Expand All @@ -83,7 +85,7 @@
$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
}
}

Expand All @@ -110,13 +112,13 @@
// +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));
$disk->putFileAs($this->targetPath, $fileInfo, substr($pathname, $prefixLength));
}
} catch (Exception $e) {
$disk->deleteDirectory($this->fragment);
$disk->deleteDirectory($this->targetPath);
throw $e;
}
}
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