Delete cache when gfile is moved

This commit is contained in:
Philipp Lang 2021-09-06 17:22:57 +02:00
parent 70f66aaacc
commit 4ca17d1065
4 changed files with 14 additions and 7 deletions

View File

@ -77,9 +77,11 @@ class Plugin extends PluginBase
Event::listen('media.file.move', function($widget, $old, $new) { Event::listen('media.file.move', function($widget, $old, $new) {
app(FileObserver::class)->rename($old, $new); app(FileObserver::class)->rename($old, $new);
app(CacheManager::class)->delete($old);
}); });
Event::listen('media.file.rename', function($widget, $old, $new) { Event::listen('media.file.rename', function($widget, $old, $new) {
app(FileObserver::class)->rename($old, $new); app(FileObserver::class)->rename($old, $new);
app(CacheManager::class)->delete($old);
}); });
} }

View File

@ -25,19 +25,19 @@ class CacheManager
public function delete(string $path): void public function delete(string $path): void
{ {
$path = $this->fileObserver->normalizePath($path); $path = $this->fileObserver->normalizePath($path);
Cache::tags($path)->flush(); Cache::tags("resizer.$path")->flush();
} }
private function getTag(string $path): string private function getTag(string $path): array
{ {
return $this->fileObserver->normalizePath($path); return ['resizer', "resizer.{$this->fileObserver->normalizePath($path)}"];
} }
private function cacheKey(string $path, string $size): string private function cacheKey(string $path, string $size): string
{ {
$normalPath = $this->fileObserver->normalizePath($path); $normalPath = $this->fileObserver->normalizePath($path);
return "resize.{$size}.{$normalPath}"; return "resizer.{$size}.{$normalPath}";
} }
} }

View File

@ -96,7 +96,7 @@ class ImageTagTest extends TestCase
UploadedFile::fake()->image('test.jpg', 640, 149)->storeAs('uploads/public/c/pages', 'test-640x149.jpg', 'local'); UploadedFile::fake()->image('test.jpg', 640, 149)->storeAs('uploads/public/c/pages', 'test-640x149.jpg', 'local');
$output = app(Twig::class)->parse('{{"pages/test.jpg" | resize}}'); $output = app(Twig::class)->parse('{{"pages/test.jpg" | resize}}');
$this->assertEquals($output, Cache::tags('pages/test.jpg')->get('resize.original.pages/test.jpg')); $this->assertEquals($output, Cache::tags(['resizer', 'resizer.pages/test.jpg'])->get('resizer.original.pages/test.jpg'));
} }
public function testPickOriginalImageWhenSvgIsGiven() public function testPickOriginalImageWhenSvgIsGiven()
@ -111,7 +111,7 @@ class ImageTagTest extends TestCase
$this->assertFalse(str_contains($output, 'srcset')); $this->assertFalse(str_contains($output, 'srcset'));
$this->assertFalse(str_contains($output, 'sizes')); $this->assertFalse(str_contains($output, 'sizes'));
$this->assertTrue(str_contains($output, 'src="'.$this->media->findFiles('/pages/test.svg')[0]->publicUrl.'"')); $this->assertTrue(str_contains($output, 'src="'.$this->media->findFiles('/pages/test.svg')[0]->publicUrl.'"'));
$this->assertEquals($output, Cache::tags('pages/test.svg')->get('resize.original.pages/test.svg')); $this->assertEquals($output, Cache::tags(['resizer', 'resizer.pages/test.svg'])->get('resizer.original.pages/test.svg'));
} }
public function testNormalizeFilePathForCache() public function testNormalizeFilePathForCache()
@ -122,7 +122,7 @@ class ImageTagTest extends TestCase
$this->media->put('/pages/test.svg', file_get_contents(__DIR__.'/stub/close.svg')); $this->media->put('/pages/test.svg', file_get_contents(__DIR__.'/stub/close.svg'));
$output = app(Twig::class)->parse('{{"/pages/test.svg" | resize}}'); $output = app(Twig::class)->parse('{{"/pages/test.svg" | resize}}');
$this->assertEquals($output, Cache::tags('pages/test.svg')->get('resize.original.pages/test.svg')); $this->assertEquals($output, Cache::tags(['resizer', 'resizer.pages/test.svg'])->get('resizer.original.pages/test.svg'));
} }
} }

View File

@ -4,6 +4,7 @@ namespace Aweos\Resizer\Tests\MediaTest;
use Aweos\Resizer\Models\Setting; use Aweos\Resizer\Models\Setting;
use Aweos\Resizer\Tests\TestCase; use Aweos\Resizer\Tests\TestCase;
use Cache;
use Event; use Event;
use Illuminate\Http\UploadedFile; use Illuminate\Http\UploadedFile;
use Storage; use Storage;
@ -19,6 +20,7 @@ class MoveTest extends TestCase
public function testItMovesAllVersionsOfAFile() 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', ['pages']);
Setting::set('sizes', []); Setting::set('sizes', []);
Setting::set('breakpoints', []); Setting::set('breakpoints', []);
@ -31,6 +33,7 @@ class MoveTest extends TestCase
$this->assertFileCount(0, 'pages/alt'); $this->assertFileCount(0, 'pages/alt');
$this->assertHasFile('pages/neu/test.jpg'); $this->assertHasFile('pages/neu/test.jpg');
$this->assertHasFile('pages/neu/test-200x300.jpg'); $this->assertHasFile('pages/neu/test-200x300.jpg');
$this->assertNull(Cache::tags(['resizer', 'resizer.pages/alt/test.jpg'])->get('resizer.original.pages/alt/test.jpg'));
} }
public function testItMovesFilesOnRename() public function testItMovesFilesOnRename()
@ -51,6 +54,7 @@ class MoveTest extends TestCase
public function testItDoesntMoveOtherFilesInTheSameDirectory() public function testItDoesntMoveOtherFilesInTheSameDirectory()
{ {
Cache::tags(['resizer', 'resizer.pages/test.jpg'])->set('resizer.original.pages/test.jpg', 'aa');
Setting::set('folders', ['pages']); Setting::set('folders', ['pages']);
Setting::set('sizes', []); Setting::set('sizes', []);
Setting::set('breakpoints', []); Setting::set('breakpoints', []);
@ -64,6 +68,7 @@ class MoveTest extends TestCase
$this->assertFileCount(3, 'pages'); $this->assertFileCount(3, 'pages');
$this->assertHasFile('pages/testneu.jpg'); $this->assertHasFile('pages/testneu.jpg');
$this->assertHasFile('pages/testneu-200x300.jpg'); $this->assertHasFile('pages/testneu-200x300.jpg');
$this->assertNull(Cache::tags(['resizer', 'resizer.pages/test.jpg'])->get('resizer.original.pages/test.jpg'));
} }
} }