Add update option

This commit is contained in:
philipp lang 2021-10-31 23:58:16 +01:00
parent 6daae8e9a3
commit 0777ff7901
7 changed files with 68 additions and 17 deletions

View File

@ -18,6 +18,7 @@ class ImageResizer
private string $uploadDir;
private MediaLibrary $media;
private MediaPath $file;
private bool $update;
public function __construct(FilesystemAdapter $disk, string $uploadDir, MediaLibrary $media)
{
@ -26,9 +27,10 @@ class ImageResizer
$this->media = $media;
}
public function generate(MediaPath $file): void
public function generate(MediaPath $file, bool $update): void
{
$this->file = $file;
$this->update = $update;
if (!$file->exists()) {
throw new ResizerException('File versions cannot be generated. Root file "'.$file->root().'" doesnt exist.');
@ -91,14 +93,14 @@ class ImageResizer
private function generateVersions(): void
{
foreach ($this->possibleSizes() as $size) {
$this->file->compressor()->resize($size);
}
foreach ($this->file->versions() as $version) {
if (!$this->disk->exists($version->get('path'))) {
throw new ResizerException('File versions cannot be generated. Version file "'.$version->get('path').'" of "'.$this->file->root().'" doesnt exist.');
}
$this->file->compressor()->make($this->disk->path($version->get('path')));
$this->file->compressor()->resize($size, $this->update, function($media, $file) {
if (!file_exists($file)) {
throw new ResizerException('File versions cannot be generated. Version file "'.$file.'" of "'.$this->file->root().'" doesnt exist.');
}
if (file_exists($file) || !$this->update) {
$this->file->compressor()->make($file);
}
});
}
}

View File

@ -13,7 +13,7 @@ class ResizeJob
{
try {
list($file) = $params;
app(ImageResizer::class)->generate(new MediaPath($file));
app(ImageResizer::class)->generate(new MediaPath($file), $params['update'] ?? false);
} catch (Throwable $e) {
Log::error('Resizing of image failed. Message: "'.$e->getMessage().'" in file "'.$e->getFile().'" on line '.$e->getLine());
throw $e;

View File

@ -41,7 +41,7 @@ class JpgCompressor extends Compressor {
return true;
}
public function resize(Collection $size): void
public function resize(Collection $size, bool $update, callable $callback): void
{
$extension = $this->media->extension();
$temp = microtime().'.'.$extension;
@ -54,7 +54,12 @@ class JpgCompressor extends Compressor {
$versionFilename = $this->media->versionsDirPath().'/'.$this->versionFilename($destWidth, $destHeight);
if ($update && Storage::exists($versionFilename)) {
return;
}
$this->moveTo($temp, $versionFilename);
call_user_func($callback, $this->media, Storage::path($versionFilename));
}
public function start(): void

View File

@ -76,7 +76,7 @@ class PdfCompressor extends Compressor
return true;
}
public function resize(Collection $size): void
public function resize(Collection $size, bool $update, callable $callback): void
{
$temp = $this->extractImage();
$tempBefore = PATHINFO($temp, PATHINFO_FILENAME).'compiled.'.pathinfo($temp, PATHINFO_EXTENSION);
@ -90,6 +90,7 @@ class PdfCompressor extends Compressor
Storage::put($versionFilename, file_get_contents($tempBefore));
unlink($tempBefore);
call_user_func($callback, $this->media, Storage::path($versionFilename));
}
}

View File

@ -51,7 +51,7 @@ class PngCompressor extends Compressor {
//
}
public function resize(Collection $size): void
public function resize(Collection $size, bool $update, callable $callback): void
{
$extension = $this->media->extension();
$temp = microtime().'.'.$extension;
@ -66,6 +66,7 @@ class PngCompressor extends Compressor {
$versionFilename = $this->media->versionsDirPath().'/'.$this->versionFilename($destWidth, $destHeight);
$this->moveTo($temp, $versionFilename);
call_user_func($callback, $this->media, Storage::path($versionFilename));
}
}

View File

@ -35,7 +35,7 @@ class ResizeMake extends Command
if ($item->type === 'folder') {
$this->resize($item->path);
} else {
Queue::push(ResizeJob::class, [$item->path]);
Queue::push(ResizeJob::class, [$item->path, 'update' => $this->option('update', false)]);
}
}
}
@ -51,12 +51,16 @@ class ResizeMake extends Command
if ($this->option('folder')) {
throw_unless(in_array($this->option('folder'), array_column(Setting::get('folders'), 'folder')), Exception::class, 'Folder not found');
Storage::deleteDirectory('uploads/public/c'.$this->option('folder'));
if (!$this->option('update')) {
Storage::deleteDirectory('uploads/public/c'.$this->option('folder'));
}
$this->resize($this->option('folder'));
return;
}
Storage::deleteDirectory('uploads/public/c');
if (!$this->option('update')) {
Storage::deleteDirectory('uploads/public/c');
}
$cacheManager->flush();
foreach (Setting::get('folders') as $folder) {
@ -82,7 +86,8 @@ class ResizeMake extends Command
protected function getOptions()
{
return [
['folder', 'f', InputOption::VALUE_OPTIONAL, 'Just resize for specific subfolders', null]
['folder', 'f', InputOption::VALUE_OPTIONAL, 'Just resize for specific subfolders', null],
['update', 'u', InputOption::VALUE_NONE, 'Just update missing files', null],
];
}
}

View File

@ -78,4 +78,41 @@ class ResizeMakeTest extends TestCase
$this->assertFileCount(0, 'pages');
}
public function testOnlyResizeMissingBreakpointsIfUpdateGiven()
{
Setting::set('folders', [
['folder' => '/pages'],
['folder' => 'otherdir'],
]);
Setting::set('sizes', []);
Setting::set('breakpoints', [100, 200]);
$this->media->put('pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get());
Storage::put('uploads/public/c/pages/test-200x200.jpg', UploadedFile::fake()->image('test.jpg', 200, 200)->get());
$lastModified = Storage::lastModified('uploads/public/c/pages/test-200x200.jpg');
sleep(1);
Artisan::call('resize:make', ['-u' => true]);
$this->assertFileCount(3, 'pages');
$this->assertEquals($lastModified, Storage::lastModified('uploads/public/c/pages/test-200x200.jpg'));
}
public function testOnlyResizeMissingVersionsIfUpdateGiven()
{
Setting::set('folders', [
['folder' => '/pages'],
['folder' => 'otherdir'],
]);
Setting::set('sizes', [['name' => 'testas', 'aspect_ratio' => '1x2']]);
Setting::set('breakpoints', [100, 200]);
$this->media->put('pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get());
Storage::put('uploads/public/c/pages/test-100x200.jpg', UploadedFile::fake()->image('test.jpg', 100, 200)->get());
$lastModified = Storage::lastModified('uploads/public/c/pages/test-100x200.jpg');
sleep(1);
Artisan::call('resize:make', ['-u' => true]);
$this->assertEquals($lastModified, Storage::lastModified('uploads/public/c/pages/test-100x200.jpg'));
}
}