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 2 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
44 changes: 30 additions & 14 deletions app/Jobs/TileSingleImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
*
Expand All @@ -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)
mzur marked this conversation as resolved.
Show resolved Hide resolved
{
$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';
mzur marked this conversation as resolved.
Show resolved Hide resolved
$this->fragment = fragment_uuid_path($file->uuid);
mzur marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -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);
mzur marked this conversation as resolved.
Show resolved Hide resolved
}
Expand All @@ -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',
Expand All @@ -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;
}
}
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