Skip to content

Commit

Permalink
better dispatch of conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Oct 14, 2024
1 parent 0467634 commit 25e1bbd
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 48 deletions.
61 changes: 35 additions & 26 deletions src/Commands/GenerateMediaConversionsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Elegantly\Media\Models\Media;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Laravel\Prompts\Progress;

use function Laravel\Prompts\confirm;

Expand All @@ -22,50 +22,59 @@ public function handle(): int
$pretend = (bool) $this->option('pretend');
/** @var string[] $conversions */
$conversions = (array) $this->option('conversions');
/** @var string[] $models */
$models = (array) $this->option('models');
/** @var string[] $collections */
$collections = (array) $this->option('collections');

/**
* @var class-string<Media> $model
*/
$model = config('media.model');

/** @var Collection<int, Media> */
$media = $model::query()
$query = $model::query()
->with(['model', 'conversions'])
->when(! empty($ids), fn (Builder $query) => $query->whereIn('id', $ids))
->when(! empty($models), fn (Builder $query) => $query->whereIn('model_type', $models))
->when(! empty($collections), fn (Builder $query) => $query->whereIn('collection_name', $collections))
->get();

$mediaByModel = $media->countBy('model_type');

$this->table(
['Model', 'Count'],
$mediaByModel->map(function (int $count, ?string $model_type) {
return [
$model_type,
$count,
];
})
);

if ($pretend || ! confirm('Continue?')) {
->when(! empty($collections), fn (Builder $query) => $query->whereIn('collection_name', $collections));

$count = $query->count();

if ($pretend || ! confirm("{$count} Media found. Continue?")) {
return self::SUCCESS;
}

$this->withProgressBar($media, function (Media $media) use ($conversions, $force) {
$progress = new Progress('Dispatching Media conversions', $count);

$query->chunkById(5_000, function ($items) use ($progress, $force, $conversions) {

$conversions = empty($conversions) ? array_keys($media->getConversionsDefinitions()) : $conversions;
foreach ($items as $media) {

foreach ($conversions as $name) {
$conversion = $media->getConversion((string) $name);
$media->dispatchConversions(

Check failure on line 53 in src/Commands/GenerateMediaConversionsCommand.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method TModel of Illuminate\Database\Eloquent\Model::dispatchConversions().

Check failure on line 53 in src/Commands/GenerateMediaConversionsCommand.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method TModel of Illuminate\Database\Eloquent\Model::dispatchConversions().
queued: true,
filter: function ($definition) use ($media, $force, $conversions) {

if ($force || ! $conversion) {
$media->dispatchConversion($name);
}
if (
! empty($conversions) &&
! in_array($definition->name, $conversions)
) {
return false;
}

if (
! $force &&
$media->hasConversion($definition->name)

Check failure on line 66 in src/Commands/GenerateMediaConversionsCommand.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method TModel of Illuminate\Database\Eloquent\Model::hasConversion().

Check failure on line 66 in src/Commands/GenerateMediaConversionsCommand.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method TModel of Illuminate\Database\Eloquent\Model::hasConversion().
) {
return false;
}

return true;
}
);

$progress->advance();
}

});

return self::SUCCESS;
Expand Down
4 changes: 3 additions & 1 deletion src/Concerns/HasMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ public function addMedia(
);
}

$media->dispatchConversions();
$media->dispatchConversions(
filter: fn ($definition) => $definition->immediate
);

return $media;
}
Expand Down
10 changes: 1 addition & 9 deletions src/Jobs/MediaConversionJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,7 @@ public function uniqueId(): string

public function handle(): void
{
if (
$this->media->model &&
$this->media->model->getMediaCollection($this->media->collection_name)
) {
$this->media->model->executeMediaConversion($this->media, $this->conversion);
} else {
$this->media->executeConversion($this->conversion);
}

$this->media->executeConversion($this->conversion);
}

/**
Expand Down
12 changes: 9 additions & 3 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,16 +361,21 @@ public function addConversion(

$this->dispatchConversions(
parent: $conversion,
filter: fn ($definition) => $definition->immediate
);

return $conversion;
}

/**
* @param null|(Closure(MediaConversionDefinition $definition):bool) $filter
* @param ?bool $queued force queueing the conversions
* @return $this
*/
public function dispatchConversions(
?MediaConversion $parent = null
?MediaConversion $parent = null,
?Closure $filter = null,
?bool $queued = null,
): static {
if ($parent) {
$definitions = $this->getChildrenConversionsDefinitions($parent->conversion_name);
Expand All @@ -379,7 +384,8 @@ public function dispatchConversions(
}

foreach ($definitions as $definition) {
if (! $definition->immediate) {

if ($filter && ! $filter($definition)) {
continue;
}

Expand All @@ -390,7 +396,7 @@ public function dispatchConversions(
continue;
}

if ($definition->queued) {
if ($definition->queued || $queued === true) {
$definition->dispatch(
media: $this,
parent: $parent,
Expand Down
46 changes: 38 additions & 8 deletions tests/Feature/HasMediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
expect($modelMedia)->toBeInstanceOf(Media::class);
});

it('generates conversions and nested conversions when adding media', function () {
it('generates and loads non queued conversions when adding media', function () {
Storage::fake('media');
$model = new Test;
$model->save();
Expand All @@ -107,13 +107,43 @@
disk: 'media'
);

expect(
$media->conversions->pluck('conversion_name')->toArray()
)->toBe([
'poster',
'poster.360',
// 'small' video conversion is queued
]);
expect($media->getConversion('poster'))->not->toBe(null);
expect($media->getConversion('poster.360'))->not->toBe(null);
});

it('generates immediate conversions when adding media', function () {
Storage::fake('media');
$model = new Test;
$model->save();

$media = $model->addMedia(
file: $this->getTestFile('videos/horizontal.mp4'),
collectionName: 'conversions',
disk: 'media'
);

$media->refresh(); // ensure queued conversions are loaded

expect($media->getConversion('poster'))->not->toBe(null);
expect($media->getConversion('poster.360'))->not->toBe(null);
expect($media->getConversion('small'))->not->toBe(null);
});

it('does not generate not immediate conversions when adding media', function () {
Storage::fake('media');
$model = new Test;
$model->save();

$media = $model->addMedia(
file: $this->getTestFile('videos/horizontal.mp4'),
collectionName: 'conversions',
disk: 'media'
);

$media->refresh(); // ensure queued conversions are loaded

expect($media->getConversion('delayed'))->toBe(null);
expect($media->getConversion('poster.delayed'))->toBe(null);

});

Expand Down
3 changes: 2 additions & 1 deletion tests/Feature/MediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,10 @@

$definitions = $media->getConversionsDefinitions();

expect($definitions)->toHaveLength(2);
expect($definitions)->toHaveLength(3);
expect($definitions['poster'])->toBeInstanceOf(MediaConversionDefinition::class);
expect($definitions['small'])->toBeInstanceOf(MediaConversionDefinition::class);
expect($definitions['delayed'])->toBeInstanceOf(MediaConversionDefinition::class);

});

Expand Down
10 changes: 10 additions & 0 deletions tests/Models/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,23 @@ public function registerMediaCollections(): Arrayable|iterable|null
width: 360,
queued: false,
),
new MediaConversionImage(
name: 'delayed',
immediate: false,
queued: false,
),
]
),
new MediaConversionVideo(
name: 'small',
queued: true,
width: 100,
),
new MediaConversionVideo(
name: 'delayed',
immediate: false,
queued: false,
),
]
),
];
Expand Down

0 comments on commit 25e1bbd

Please sign in to comment.