Add exception when root file cannot be found

This commit is contained in:
philipp lang 2021-10-30 21:13:55 +02:00
parent 313d713cb5
commit 37c0cbffba
5 changed files with 36 additions and 3 deletions

View File

@ -2,6 +2,7 @@
namespace Aweos\Resizer\Classes; namespace Aweos\Resizer\Classes;
use Aweos\Resizer\Exceptions\ResizerException;
use Aweos\Resizer\Lib\MediaPath; use Aweos\Resizer\Lib\MediaPath;
use Aweos\Resizer\Models\Setting; use Aweos\Resizer\Models\Setting;
use Illuminate\Filesystem\FilesystemAdapter; use Illuminate\Filesystem\FilesystemAdapter;
@ -29,6 +30,10 @@ class ImageResizer
{ {
$this->file = $file; $this->file = $file;
if (!$file->exists()) {
throw new ResizerException('File versions cannot be generated. Root file "'.$file->root().'" doesnt exist.');
}
if ($this->file->compressor()->shouldGenerateVersions()) { if ($this->file->compressor()->shouldGenerateVersions()) {
$this->generateVersions(); $this->generateVersions();
} }

View File

@ -13,7 +13,8 @@
], ],
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"Aweos\\Resizer\\Compilers\\": "./compilers" "Aweos\\Resizer\\Compilers\\": "./compilers",
"Aweos\\Resizer\\Exceptions\\": "./exceptions",
} }
} }
} }

View File

@ -0,0 +1,9 @@
<?php
namespace Aweos\Resizer\Exceptions;
use Exception;
class ResizerException extends Exception {
}

View File

@ -44,9 +44,14 @@ class MediaPath
return pathinfo($this->path, PATHINFO_FILENAME); return pathinfo($this->path, PATHINFO_FILENAME);
} }
public function exists(): bool
{
return file_exists($this->root());
}
public function type(): ?string public function type(): ?string
{ {
if (!file_exists($this->root())) { if (!$this->exists()) {
return null; return null;
} }

View File

@ -2,6 +2,7 @@
namespace Aweos\Resizer\Tests; namespace Aweos\Resizer\Tests;
use Aweos\Resizer\Exceptions\ResizerException;
use Aweos\Resizer\Models\Setting; use Aweos\Resizer\Models\Setting;
use Aweos\Resizer\Tests\TestCase; use Aweos\Resizer\Tests\TestCase;
use Event; use Event;
@ -24,9 +25,21 @@ class ResizerTest extends TestCase
Setting::set('folders', []); Setting::set('folders', []);
Setting::set('sizes', []); Setting::set('sizes', []);
Setting::set('breakpoints', []); Setting::set('breakpoints', []);
$file = UploadedFile::fake()->image('test.jpg', 500, 600); $file = UploadedFile::fake()->image('test.jpg', 500, 600);
$this->media->put('/pages/test.jpg', $file); $this->media->put('/pages/test.jpg', $file);
Event::fire('media.file.upload', [null, '/pages/test.jpg', null]);
$this->assertFileCount(0, '');
}
public function testThrowExceptionWhenOriginalFileNotFound(): void
{
$this->expectException(ResizerException::class);
Setting::set('folders', [['folder' => '/pages']]);
Setting::set('sizes', []);
Setting::set('breakpoints', []);
Event::fire('media.file.upload', [null, '/pages/test.jpg', null]); Event::fire('media.file.upload', [null, '/pages/test.jpg', null]);
$this->assertFileCount(0, ''); $this->assertFileCount(0, '');