Fixed: Delete images

This commit is contained in:
philipp lang 2022-02-16 02:49:19 +01:00
parent 72ef92b78c
commit 30bd3dc25f
13 changed files with 249 additions and 58 deletions

1
.ackrc Normal file
View File

@ -0,0 +1 @@
--ignore-dir=/vendor

View File

@ -8,7 +8,10 @@ use Aweos\Resizer\Classes\ImageResizer;
use Aweos\Resizer\Classes\ResizeJob;
use Aweos\Resizer\Console\ResizeMake;
use Aweos\Resizer\Console\ResizePurge;
use Aweos\Resizer\Jobs\DeleteJob;
use Aweos\Resizer\Jobs\MoveJob;
use Aweos\Resizer\Lib\MediaPath;
use Aweos\Resizer\Lib\StorageMediaPath;
use Aweos\Resizer\Models\Setting;
use Event;
use MediaLibrary;
@ -65,22 +68,23 @@ class Plugin extends PluginBase
app()->bind('resize', fn () => app(CacheManager::class));
Event::listen('media.file.upload', function($widget, $filePath, $uploadedFile) {
if ((new MediaPath($filePath))->shouldProcess()) {
if ((new StorageMediaPath($filePath))->shouldProcess()) {
Queue::push(ResizeJob::class, [$filePath], Setting::get('queue'));
}
});
Event::listen('media.file.delete', function($widget, $filePath) {
app(FileObserver::class)->delete(new MediaPath($filePath));
app(CacheManager::class)->delete(new MediaPath($filePath));
Queue::push(DeleteJob::class, [$filePath], Setting::get('queue'));
});
Event::listen('media.file.move', function($widget, $old, $new) {
app(FileObserver::class)->rename(new MediaPath($old), new MediaPath($new));
app(CacheManager::class)->delete(new MediaPath($old));
if ((new StorageMediaPath($new))->shouldProcess() || (new StorageMediaPath($old))->shouldProcess()) {
Queue::push(MoveJob::class, [$old, $new], Setting::get('queue'));
}
});
Event::listen('media.file.rename', function($widget, $old, $new) {
app(FileObserver::class)->rename(new MediaPath($old), new MediaPath($new));
app(CacheManager::class)->delete(new MediaPath($old));
if ((new StorageMediaPath($old))->shouldProcess()) {
Queue::push(MoveJob::class, [$old, $new], Setting::get('queue'));
}
});
}
@ -103,7 +107,7 @@ class Plugin extends PluginBase
return [
'filters' => [
'resize' => fn ($media, $size = 'original', $sizes = null, $options = []) => app(CacheManager::class)->get(
new MediaPath($media),
new StorageMediaPath($media),
$size,
$sizes,
$options,

View File

@ -3,6 +3,7 @@
namespace Aweos\Resizer\Classes;
use Aweos\Resizer\Lib\MediaPath;
use Aweos\Resizer\Lib\StorageMediaPath;
use Cache;
class CacheManager
@ -51,7 +52,7 @@ class CacheManager
public function biggestVersion(string $path, string $size): string
{
$path = new MediaPath($path);
$path = new StorageMediaPath($path);
return $this->tagGenerator->singleFile($path, $size);
}

View File

@ -4,6 +4,7 @@ namespace Aweos\Resizer\Classes;
use Aweos\Resizer\Classes\CacheManager;
use Aweos\Resizer\Lib\MediaPath;
use Aweos\Resizer\Lib\TempMediaPath;
use Log;
use Throwable;
@ -13,9 +14,10 @@ class ResizeJob
public function fire($job, $params)
{
list($file) = $params;
$media = new MediaPath($file);
$media = new TempMediaPath($file);
app(ImageResizer::class)->generate($media, $params['update'] ?? false);
app(CacheManager::class)->delete($media);
unset ($media);
$job->delete();
}

View File

@ -20,7 +20,9 @@
"Aweos\\Resizer\\Compilers\\": "./compilers",
"Aweos\\Resizer\\Exceptions\\": "./exceptions",
"Aweos\\Resizer\\Compressors\\": "./compressors",
"Aweos\\Resizer\\Lib\\": "./lib"
"Aweos\\Resizer\\Lib\\": "./lib",
"Aweos\\Resizer\\Jobs\\": "./jobs",
"Aweos\\Resizer\\Classes\\": "./classes"
}
}
}

36
jobs/DeleteJob.php Normal file
View File

@ -0,0 +1,36 @@
<?php
namespace Aweos\Resizer\Jobs;
use Aweos\Resizer\Classes\CacheManager;
use Aweos\Resizer\Classes\FileObserver;
use Aweos\Resizer\Lib\MediaPath;
use Aweos\Resizer\Lib\StorageMediaPath;
use Log;
use Throwable;
class DeleteJob
{
public function fire($job, $params)
{
list($file) = $params;
$media = new StorageMediaPath($file);
app(FileObserver::class)->delete($media);
app(CacheManager::class)->delete($media);
unset ($media);
$job->delete();
}
public function failed($data, Throwable $e)
{
Log::error("Deleting of image failed", [
'message' => $e->getMessage(),
'exception' => $e,
'data' => $data,
]);
throw $e;
}
}

38
jobs/MoveJob.php Normal file
View File

@ -0,0 +1,38 @@
<?php
namespace Aweos\Resizer\Jobs;
use Aweos\Resizer\Classes\CacheManager;
use Aweos\Resizer\Classes\FileObserver;
use Aweos\Resizer\Lib\MediaPath;
use Aweos\Resizer\Lib\StorageMediaPath;
use Log;
use Throwable;
class MoveJob
{
public function fire($job, $params)
{
list($old, $new) = $params;
$old = new StorageMediaPath($old);
$new = new StorageMediaPath($new);
app(FileObserver::class)->rename($old, $new);
app(CacheManager::class)->delete($old);
unset ($old);
unset ($new);
$job->delete();
}
public function failed($data, Throwable $e)
{
Log::error("Deleting of image failed", [
'message' => $e->getMessage(),
'exception' => $e,
'data' => $data,
]);
throw $e;
}
}

35
jobs/ResizeJob.php Normal file
View File

@ -0,0 +1,35 @@
<?php
namespace Aweos\Resizer\Jobs;
use Aweos\Resizer\Classes\CacheManager;
use Aweos\Resizer\Lib\MediaPath;
use Aweos\Resizer\Lib\TempMediaPath;
use Log;
use Throwable;
class ResizeJob
{
public function fire($job, $params)
{
list($file) = $params;
$media = new TempMediaPath($file);
app(ImageResizer::class)->generate($media, $params['update'] ?? false);
app(CacheManager::class)->delete($media);
unset ($media);
$job->delete();
}
public function failed($data, Throwable $e)
{
Log::error("Resizing of image failed", [
'message' => $e->getMessage(),
'exception' => $e,
'data' => $data,
]);
throw $e;
}
}

View File

@ -9,26 +9,19 @@ use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Storage;
use MediaLibrary;
class MediaPath
abstract class MediaPath
{
private string $path;
protected string $path;
abstract public function get(): string;
abstract public function root(): string;
abstract public function publicUrl(): string;
public function __construct(string $path)
{
$this->path = $path;
}
public function root(): string
{
return Storage::path($this->storagePath());
}
public function storagePath(): string
{
return "media/{$this->normal()}";
}
public function normal(): string
{
return preg_replace('|^/*|', '', $this->path);
@ -41,7 +34,7 @@ class MediaPath
public function filename(): string
{
return pathinfo($this->path, PATHINFO_FILENAME);
return pathinfo($this->root(), PATHINFO_FILENAME);
}
public function exists(): bool
@ -60,7 +53,7 @@ class MediaPath
public function extension(): string
{
return pathinfo($this->path, PATHINFO_EXTENSION);
return pathinfo($this->root(), PATHINFO_EXTENSION);
}
public function versionsPath(): string
@ -70,17 +63,7 @@ class MediaPath
public function versionsDirPath(): string
{
return pathinfo("uploads/public/c/{$this->normal()}", PATHINFO_DIRNAME);
}
public function publicUrl(): string
{
return MediaLibrary::instance()->findFiles($this->path)[0]->publicUrl;
}
public function get(): string
{
return MediaLibrary::instance()->get($this->path);
return pathinfo($this->versionsPath(), PATHINFO_DIRNAME);
}
public function shouldProcess(): bool
@ -122,4 +105,9 @@ class MediaPath
return $return;
}
protected function storagePath(): string
{
return "media/{$this->normal()}";
}
}

36
lib/StorageMediaPath.php Normal file
View File

@ -0,0 +1,36 @@
<?php
namespace Aweos\Resizer\Lib;
use Aweos\Resizer\Compressors\Compressor;
use Aweos\Resizer\Compressors\Factory as CompressorFactory;
use Aweos\Resizer\Models\Setting;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Storage;
use MediaLibrary;
class StorageMediaPath extends MediaPath
{
public function __construct(string $path)
{
parent::__construct($path);
$this->path = $path;
}
public function root(): string
{
return Storage::path($this->storagePath());
}
public function publicUrl(): string
{
return MediaLibrary::instance()->findFiles($this->path)[0]->publicUrl;
}
public function get(): string
{
return MediaLibrary::instance()->get($this->path);
}
}

59
lib/TempMediaPath.php Normal file
View File

@ -0,0 +1,59 @@
<?php
namespace Aweos\Resizer\Lib;
use Aweos\Resizer\Compressors\Compressor;
use Aweos\Resizer\Compressors\Factory as CompressorFactory;
use Aweos\Resizer\Models\Setting;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Storage;
use MediaLibrary;
class TempMediaPath extends MediaPath
{
private string $tempPath;
public function __construct(string $path)
{
parent::__construct($path);
$this->createTemp();
}
public function __destruct()
{
$this->destroyTemp();
}
public function root(): string
{
return $this->tempPath;
}
public function publicUrl(): string
{
return '';
}
public function get(): string
{
return file_get_contents($this->tempPath);
}
private function createTemp(): void
{
$this->tempPath = '/tmp/'.pathinfo($this->path, PATHINFO_FILENAME);
if (Storage::exists($this->storagePath())) {
$stream = Storage::readStream($this->storagePath());
$contents = stream_get_contents($stream);
file_put_contents($this->tempPath, $contents);
}
}
private function destroyTemp(): void
{
@unlink($this->tempPath);
}
}

View File

@ -6,6 +6,7 @@ use Aweos\Resizer\Classes\CacheManager;
use Aweos\Resizer\Classes\TagGenerator;
use Aweos\Resizer\Exceptions\ResizerException;
use Aweos\Resizer\Lib\MediaPath;
use Aweos\Resizer\Lib\StorageMediaPath;
use Aweos\Resizer\Models\Setting;
use Aweos\Resizer\Tests\TestCase;
use Cache;
@ -21,11 +22,11 @@ class ImageTagTest extends TestCase
{
parent::setUp();
Storage::fake('local');
Setting::set('folders', [['folder' => '/pages']]);
}
public function testItGeneratesAnImageTagWithOriginalSize()
{
Setting::set('folders', ['pages']);
Setting::set('sizes', []);
Setting::set('breakpoints', [100,200]);
$this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get());
@ -35,25 +36,23 @@ class ImageTagTest extends TestCase
$this->assertEquals(
'width="500" height="500" sizes="(max-width: 100px) 100px, (max-width: 200px) 200px, (max-width: 500px) 500px" srcset="/storage/uploads/public/c/pages/test-100x100.jpg 100w, /storage/uploads/public/c/pages/test-200x200.jpg 200w, /storage/uploads/public/c/pages/test-500x500.jpg 500w" src="/storage/uploads/public/c/pages/test-500x500.jpg"',
app(CacheManager::class)->get(new MediaPath('pages/test.jpg'), 'original', null),
app(CacheManager::class)->get(new StorageMediaPath('pages/test.jpg'), 'original', null),
);
}
public function testItThrowsExceptionWhenSizeNotFound()
{
$this->expectException(ResizerException::class);
Setting::set('folders', ['pages']);
Setting::set('sizes', []);
Setting::set('breakpoints', []);
$this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get());
UploadedFile::fake()->image('test.jpg', 500, 500)->storeAs('uploads/public/c/pages', 'test-500x500.jpg', 'local');
dd(app(CacheManager::class)->get(new MediaPath('pages/test.jpg'), 'notfound', null));
dd(app(CacheManager::class)->get(new StorageMediaPath('pages/test.jpg'), 'notfound', null));
}
public function testFetchPdfImage()
{
Setting::set('folders', ['pages']);
Setting::set('sizes', []);
Setting::set('breakpoints', [100,200]);
$this->media->put('/pages/test.pdf', file_get_contents(__DIR__.'/stub/dummy.pdf'));
@ -68,7 +67,6 @@ class ImageTagTest extends TestCase
public function testItGeneratesLazyLoadingTags()
{
Setting::set('folders', ['pages']);
Setting::set('sizes', []);
Setting::set('breakpoints', [100]);
$this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get());
@ -76,13 +74,12 @@ class ImageTagTest extends TestCase
$this->assertEquals(
'width="100" height="100" sizes="(max-width: 100px) 100px" data-srcset="/storage/uploads/public/c/pages/test-100x100.jpg 100w" data-src="/storage/uploads/public/c/pages/test-100x100.jpg"',
app(CacheManager::class)->get(new MediaPath('pages/test.jpg'), 'original', null, ['lazy' => true]),
app(CacheManager::class)->get(new StorageMediaPath('pages/test.jpg'), 'original', null, ['lazy' => true]),
);
}
public function testItGeneratesPngImages()
{
Setting::set('folders', ['pages']);
Setting::set('sizes', []);
Setting::set('breakpoints', [100,200]);
$this->media->put('/pages/test.png', UploadedFile::fake()->image('test.png', 500, 500)->get());
@ -90,13 +87,12 @@ class ImageTagTest extends TestCase
$this->assertEquals(
'width="100" height="100" sizes="(max-width: 100px) 100px" srcset="/storage/uploads/public/c/pages/test-100x100.png 100w" src="/storage/uploads/public/c/pages/test-100x100.png"',
app(CacheManager::class)->get(new MediaPath('pages/test.png'), 'original', null),
app(CacheManager::class)->get(new StorageMediaPath('pages/test.png'), 'original', null),
);
}
public function testItSkipsImagesWithWrongAspectRatio()
{
Setting::set('folders', ['pages']);
Setting::set('sizes', []);
Setting::set('breakpoints', [100,200]);
$this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get());
@ -111,7 +107,6 @@ class ImageTagTest extends TestCase
public function testItCanGenerateAAspectRatio()
{
Setting::set('folders', ['pages']);
Setting::set('sizes', [['name' => 'size', 'aspect_ratio' => '1x2']]);
Setting::set('breakpoints', [100,200]);
$this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get());
@ -133,7 +128,6 @@ class ImageTagTest extends TestCase
public function testItServesAspectRatiosThatCanBeOnePixelLarger()
{
Setting::set('folders', ['pages']);
Setting::set('sizes', [['name' => 'size', 'aspect_ratio' => '1200x280']]);
Setting::set('breakpoints', [640]);
$this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 1920, 899)->get());
@ -147,7 +141,6 @@ class ImageTagTest extends TestCase
public function testGenerateCache()
{
Setting::set('folders', ['pages']);
Setting::set('sizes', [['name' => 'size', 'aspect_ratio' => '1200x280']]);
Setting::set('breakpoints', [640]);
$this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 1920, 899)->get());
@ -159,7 +152,6 @@ class ImageTagTest extends TestCase
public function testPickOriginalImageWhenSvgIsGiven()
{
Setting::set('folders', ['pages']);
Setting::set('sizes', [['name' => 'size', 'aspect_ratio' => '1200x280']]);
Setting::set('breakpoints', [640]);
$this->media->put('/pages/test.svg', file_get_contents(__DIR__.'/stub/close.svg'));
@ -174,7 +166,6 @@ class ImageTagTest extends TestCase
public function testNormalizeFilePathForCache()
{
Setting::set('folders', ['pages']);
Setting::set('sizes', [['name' => 'size', 'aspect_ratio' => '1200x280']]);
Setting::set('breakpoints', [640]);
$this->media->put('/pages/test.svg', file_get_contents(__DIR__.'/stub/close.svg'));
@ -185,7 +176,6 @@ class ImageTagTest extends TestCase
public function testGenerateSizesAttribute()
{
Setting::set('folders', ['pages']);
Setting::set('sizes', []);
Setting::set('breakpoints', [100, 200]);
$this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get());
@ -199,7 +189,6 @@ class ImageTagTest extends TestCase
public function testItGeneratesOnlySingleImage()
{
Setting::set('folders', ['pages']);
Setting::set('sizes', []);
Setting::set('breakpoints', [100]);
$this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get());

View File

@ -21,7 +21,7 @@ class MoveTest extends TestCase
public function testItMovesAllVersionsOfAFile()
{
Cache::tags(['resizer', 'resizer.pages/alt/test.jpg'])->set('resizer.original.pages/alt/test.jpg', 'aa');
Setting::set('folders', ['pages']);
Setting::set('folders', [['folder' => '/pages']]);
Setting::set('sizes', []);
Setting::set('breakpoints', []);
$this->media->put('/pages/alt/test.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get());
@ -36,7 +36,7 @@ class MoveTest extends TestCase
public function testItMovesFilesOnRename()
{
Setting::set('folders', ['pages']);
Setting::set('folders', [['folder' => '/pages']]);
Setting::set('sizes', []);
Setting::set('breakpoints', []);
$this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get());
@ -50,7 +50,7 @@ class MoveTest extends TestCase
public function testitDeletesOldAndNewCacheWhenMoving()
{
Setting::set('folders', ['pages']);
Setting::set('folders', [['folder' => '/pages']]);
Setting::set('sizes', []);
Setting::set('breakpoints', []);
$this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get());
@ -68,7 +68,7 @@ class MoveTest extends TestCase
public function testRenamePdfHeaderFile()
{
Setting::set('folders', ['pages']);
Setting::set('folders', [['folder' => '/pages']]);
Setting::set('sizes', []);
Setting::set('breakpoints', []);
$this->media->put('/pages/test.pdf', file_get_contents(__DIR__.'/stub/dummy.pdf'));
@ -83,7 +83,7 @@ class MoveTest extends TestCase
public function testItDoesntMoveOtherFilesInTheSameDirectory()
{
Cache::tags(['resizer', 'resizer.pages/test.jpg'])->set('resizer.original.pages/test.jpg', 'aa');
Setting::set('folders', ['pages']);
Setting::set('folders', [['folder' => '/pages']]);
Setting::set('sizes', []);
Setting::set('breakpoints', []);
$this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get());