Skip to content
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)
{
$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);
}

/**
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);
}
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