Skip to content

Commit

Permalink
Add starting-from-id option to regenerate command (#2688)
Browse files Browse the repository at this point in the history
* Add after-date option to regenerate command

* refactor to starting-from-id

* Update regenerating-images.md to document new option

* Refactor to early return based on special cases, add option to exclude the passed id

* allow combining modelType and starting-from-id
  • Loading branch information
billypoke authored Dec 17, 2021
1 parent 0803822 commit cde0659
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 5 deletions.
19 changes: 19 additions & 0 deletions docs/converting-images/regenerating-images.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,22 @@ If you want to force responsive images to be regenerated, you can use the `--wit
```bash
php artisan media-library:regenerate --with-responsive-images
```

If you want to regenerate images starting at a specific id (inclusive), you can use the `--starting-from-id` option

```bash
php artisan media-library:regenerate --starting-from-id=1
```

You can also start after the provided id by also passing the `--exclude-starting-id` or `-X` options

```bash
php artisan media-library:regenerate --starting-from-id=1 --exclude-starting-id
php artisan media-library:regenerate --starting-from-id=1 -X
```

The `--starting-from-id` option can also be combined with the `modelType` argument

```bash
php artisan media-library:regenerate "App\Models\Post" --starting-from-id=1
```
19 changes: 14 additions & 5 deletions src/Conversions/Commands/RegenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class RegenerateCommand extends Command

protected $signature = 'media-library:regenerate {modelType?} {--ids=*}
{--only=* : Regenerate specific conversions}
{--starting-from-id= : Regenerate media with an id equal to or higher than the provided value}
{--X|exclude-starting-id : Exclude the provided id when regenerating from a specific id}
{--only-missing : Regenerate only missing conversions}
{--with-responsive-images : Regenerate responsive images}
{--force : Force the operation to run when in production}';
Expand Down Expand Up @@ -74,18 +76,25 @@ public function handle(MediaRepository $mediaRepository, FileManipulator $fileMa

public function getMediaToBeRegenerated(): Collection
{
// Get this arg first as it can also be passed to the greater-than-id branch
$modelType = $this->argument('modelType') ?? '';
$mediaIds = $this->getMediaIds();

if ($modelType === '' && count($mediaIds) === 0) {
return $this->mediaRepository->all();
$startingFromId = (int)$this->option('starting-from-id');
if ($startingFromId !== 0) {
$excludeStartingId = $this->option('exclude-starting-id') ?? false;
return $this->mediaRepository->getByIdGreaterThan($startingFromId, $excludeStartingId, $modelType);
}

if (! count($mediaIds)) {
if ($modelType !== '') {
return $this->mediaRepository->getByModelType($modelType);
}

return $this->mediaRepository->getByIds($mediaIds);
$mediaIds = $this->getMediaIds();
if (count($mediaIds) > 0) {
return $this->mediaRepository->getByIds($mediaIds);
}

return $this->mediaRepository->all();
}

protected function getMediaIds(): array
Expand Down
8 changes: 8 additions & 0 deletions src/MediaCollections/MediaRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ public function getByIds(array $ids): DbCollection
return $this->query()->whereIn($this->model->getKeyName(), $ids)->get();
}

public function getByIdGreaterThan(int $startingFromId, bool $excludeStartingId = false, string $modelType = ''): DbCollection
{
return $this->query()
->where($this->model->getKeyName(), $excludeStartingId ? '>' : '>=', $startingFromId)
->when($modelType !== '', fn(Builder $q) => $q->where('model_type', $modelType))
->get();
}

public function getByModelTypeAndCollectionName(string $modelType, string $collectionName): DbCollection
{
return $this->query()
Expand Down
129 changes: 129 additions & 0 deletions tests/Conversions/Commands/RegenerateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Spatie\MediaLibrary\Tests\Conversions\Commands;

use Spatie\MediaLibrary\Tests\TestCase;
use Spatie\MediaLibrary\Tests\TestSupport\TestModels\TestModel;
use Spatie\MediaLibrary\Tests\TestSupport\TestModels\TestModelWithConversion;

class RegenerateCommandTest extends TestCase
{
Expand Down Expand Up @@ -252,4 +254,131 @@ public function it_can_regenerate_responsive_images()
$this->assertFileExists($image);
}
}

/** @test */
public function it_can_regenerate_files_by_starting_from_id()
{
$media = $this->testModelWithConversion
->addMedia($this->getTestFilesDirectory('test.jpg'))
->preservingOriginal()
->toMediaCollection('images');

$media2 = $this->testModelWithConversion
->addMedia($this->getTestFilesDirectory('test.jpg'))
->toMediaCollection('images');

$derivedImage = $this->getMediaDirectory("{$media->id}/conversions/test-thumb.jpg");
$derivedImage2 = $this->getMediaDirectory("{$media2->id}/conversions/test-thumb.jpg");

unlink($derivedImage);
unlink($derivedImage2);

$this->assertFileDoesNotExist($derivedImage);
$this->assertFileDoesNotExist($derivedImage2);

$this->artisan('media-library:regenerate', ['--starting-from-id' => $media2->getKey()]);

$this->assertFileDoesNotExist($derivedImage);
$this->assertFileExists($derivedImage2);
}

/** @test */
public function it_can_regenerate_files_starting_after_the_provided_id()
{
$media = $this->testModelWithConversion
->addMedia($this->getTestFilesDirectory('test.jpg'))
->preservingOriginal()
->toMediaCollection('images');

$media2 = $this->testModelWithConversion
->addMedia($this->getTestFilesDirectory('test.jpg'))
->toMediaCollection('images');

$derivedImage = $this->getMediaDirectory("{$media->id}/conversions/test-thumb.jpg");
$derivedImage2 = $this->getMediaDirectory("{$media2->id}/conversions/test-thumb.jpg");

unlink($derivedImage);
unlink($derivedImage2);

$this->assertFileDoesNotExist($derivedImage);
$this->assertFileDoesNotExist($derivedImage2);

$this->artisan('media-library:regenerate', [
'--starting-from-id' => $media->getKey(),
'--exclude-starting-id' => true,
]);

$this->assertFileDoesNotExist($derivedImage);
$this->assertFileExists($derivedImage2);
}

/** @test */
public function it_can_regenerate_files_starting_after_the_provided_id_with_shortcut()
{
$media = $this->testModelWithConversion
->addMedia($this->getTestFilesDirectory('test.jpg'))
->preservingOriginal()
->toMediaCollection('images');

$media2 = $this->testModelWithConversion
->addMedia($this->getTestFilesDirectory('test.jpg'))
->toMediaCollection('images');

$derivedImage = $this->getMediaDirectory("{$media->id}/conversions/test-thumb.jpg");
$derivedImage2 = $this->getMediaDirectory("{$media2->id}/conversions/test-thumb.jpg");

unlink($derivedImage);
unlink($derivedImage2);

$this->assertFileDoesNotExist($derivedImage);
$this->assertFileDoesNotExist($derivedImage2);

$this->artisan('media-library:regenerate', [
'--starting-from-id' => $media->getKey(),
'-X' => true,
]);

$this->assertFileDoesNotExist($derivedImage);
$this->assertFileExists($derivedImage2);
}

/** @test */
public function it_can_regenerate_files_starting_from_id_with_model_type()
{
$media = $this->testModelWithConversionsOnOtherDisk
->addMedia($this->getTestFilesDirectory('test.jpg'))
->preservingOriginal()
->toMediaCollection('images');

$media2 = $this->testModelWithConversion
->addMedia($this->getTestFilesDirectory('test.jpg'))
->preservingOriginal()
->toMediaCollection('images');

$media3 = $this->testModelWithConversion
->addMedia($this->getTestFilesDirectory('test.jpg'))
->preservingOriginal()
->toMediaCollection('images');

$derivedImage = $this->getMediaDirectory("{$media->id}/conversions/test-thumb.jpg");
$derivedImage2 = $this->getMediaDirectory("{$media2->id}/conversions/test-thumb.jpg");
$derivedImage3 = $this->getMediaDirectory("{$media3->id}/conversions/test-thumb.jpg");

unlink($derivedImage);
unlink($derivedImage2);
unlink($derivedImage3);

$this->assertFileDoesNotExist($derivedImage);
$this->assertFileDoesNotExist($derivedImage2);
$this->assertFileDoesNotExist($derivedImage3);

$this->artisan('media-library:regenerate', [
'--starting-from-id' => $media->getKey(),
'modelType' => TestModelWithConversion::class,
]);

$this->assertFileDoesNotExist($derivedImage);
$this->assertFileExists($derivedImage2);
$this->assertFileExists($derivedImage3);
}
}

0 comments on commit cde0659

Please sign in to comment.