Add update option
This commit is contained in:
parent
6daae8e9a3
commit
0777ff7901
|
@ -18,6 +18,7 @@ class ImageResizer
|
||||||
private string $uploadDir;
|
private string $uploadDir;
|
||||||
private MediaLibrary $media;
|
private MediaLibrary $media;
|
||||||
private MediaPath $file;
|
private MediaPath $file;
|
||||||
|
private bool $update;
|
||||||
|
|
||||||
public function __construct(FilesystemAdapter $disk, string $uploadDir, MediaLibrary $media)
|
public function __construct(FilesystemAdapter $disk, string $uploadDir, MediaLibrary $media)
|
||||||
{
|
{
|
||||||
|
@ -26,9 +27,10 @@ class ImageResizer
|
||||||
$this->media = $media;
|
$this->media = $media;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function generate(MediaPath $file): void
|
public function generate(MediaPath $file, bool $update): void
|
||||||
{
|
{
|
||||||
$this->file = $file;
|
$this->file = $file;
|
||||||
|
$this->update = $update;
|
||||||
|
|
||||||
if (!$file->exists()) {
|
if (!$file->exists()) {
|
||||||
throw new ResizerException('File versions cannot be generated. Root file "'.$file->root().'" doesnt exist.');
|
throw new ResizerException('File versions cannot be generated. Root file "'.$file->root().'" doesnt exist.');
|
||||||
|
@ -91,14 +93,14 @@ class ImageResizer
|
||||||
private function generateVersions(): void
|
private function generateVersions(): void
|
||||||
{
|
{
|
||||||
foreach ($this->possibleSizes() as $size) {
|
foreach ($this->possibleSizes() as $size) {
|
||||||
$this->file->compressor()->resize($size);
|
$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) {
|
||||||
foreach ($this->file->versions() as $version) {
|
$this->file->compressor()->make($file);
|
||||||
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')));
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ class ResizeJob
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
list($file) = $params;
|
list($file) = $params;
|
||||||
app(ImageResizer::class)->generate(new MediaPath($file));
|
app(ImageResizer::class)->generate(new MediaPath($file), $params['update'] ?? false);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
Log::error('Resizing of image failed. Message: "'.$e->getMessage().'" in file "'.$e->getFile().'" on line '.$e->getLine());
|
Log::error('Resizing of image failed. Message: "'.$e->getMessage().'" in file "'.$e->getFile().'" on line '.$e->getLine());
|
||||||
throw $e;
|
throw $e;
|
||||||
|
|
|
@ -41,7 +41,7 @@ class JpgCompressor extends Compressor {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function resize(Collection $size): void
|
public function resize(Collection $size, bool $update, callable $callback): void
|
||||||
{
|
{
|
||||||
$extension = $this->media->extension();
|
$extension = $this->media->extension();
|
||||||
$temp = microtime().'.'.$extension;
|
$temp = microtime().'.'.$extension;
|
||||||
|
@ -54,7 +54,12 @@ class JpgCompressor extends Compressor {
|
||||||
|
|
||||||
$versionFilename = $this->media->versionsDirPath().'/'.$this->versionFilename($destWidth, $destHeight);
|
$versionFilename = $this->media->versionsDirPath().'/'.$this->versionFilename($destWidth, $destHeight);
|
||||||
|
|
||||||
|
if ($update && Storage::exists($versionFilename)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$this->moveTo($temp, $versionFilename);
|
$this->moveTo($temp, $versionFilename);
|
||||||
|
call_user_func($callback, $this->media, Storage::path($versionFilename));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function start(): void
|
public function start(): void
|
||||||
|
|
|
@ -76,7 +76,7 @@ class PdfCompressor extends Compressor
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function resize(Collection $size): void
|
public function resize(Collection $size, bool $update, callable $callback): void
|
||||||
{
|
{
|
||||||
$temp = $this->extractImage();
|
$temp = $this->extractImage();
|
||||||
$tempBefore = PATHINFO($temp, PATHINFO_FILENAME).'compiled.'.pathinfo($temp, PATHINFO_EXTENSION);
|
$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));
|
Storage::put($versionFilename, file_get_contents($tempBefore));
|
||||||
unlink($tempBefore);
|
unlink($tempBefore);
|
||||||
|
call_user_func($callback, $this->media, Storage::path($versionFilename));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
$extension = $this->media->extension();
|
||||||
$temp = microtime().'.'.$extension;
|
$temp = microtime().'.'.$extension;
|
||||||
|
@ -66,6 +66,7 @@ class PngCompressor extends Compressor {
|
||||||
$versionFilename = $this->media->versionsDirPath().'/'.$this->versionFilename($destWidth, $destHeight);
|
$versionFilename = $this->media->versionsDirPath().'/'.$this->versionFilename($destWidth, $destHeight);
|
||||||
|
|
||||||
$this->moveTo($temp, $versionFilename);
|
$this->moveTo($temp, $versionFilename);
|
||||||
|
call_user_func($callback, $this->media, Storage::path($versionFilename));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,7 +35,7 @@ class ResizeMake extends Command
|
||||||
if ($item->type === 'folder') {
|
if ($item->type === 'folder') {
|
||||||
$this->resize($item->path);
|
$this->resize($item->path);
|
||||||
} else {
|
} 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')) {
|
if ($this->option('folder')) {
|
||||||
throw_unless(in_array($this->option('folder'), array_column(Setting::get('folders'), 'folder')), Exception::class, 'Folder not found');
|
throw_unless(in_array($this->option('folder'), array_column(Setting::get('folders'), 'folder')), Exception::class, 'Folder not found');
|
||||||
|
if (!$this->option('update')) {
|
||||||
Storage::deleteDirectory('uploads/public/c'.$this->option('folder'));
|
Storage::deleteDirectory('uploads/public/c'.$this->option('folder'));
|
||||||
|
}
|
||||||
$this->resize($this->option('folder'));
|
$this->resize($this->option('folder'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!$this->option('update')) {
|
||||||
Storage::deleteDirectory('uploads/public/c');
|
Storage::deleteDirectory('uploads/public/c');
|
||||||
|
}
|
||||||
$cacheManager->flush();
|
$cacheManager->flush();
|
||||||
|
|
||||||
foreach (Setting::get('folders') as $folder) {
|
foreach (Setting::get('folders') as $folder) {
|
||||||
|
@ -82,7 +86,8 @@ class ResizeMake extends Command
|
||||||
protected function getOptions()
|
protected function getOptions()
|
||||||
{
|
{
|
||||||
return [
|
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],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,4 +78,41 @@ class ResizeMakeTest extends TestCase
|
||||||
$this->assertFileCount(0, 'pages');
|
$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'));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue