Skip to content

Commit

Permalink
execute parent conversions when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Oct 19, 2024
1 parent 3a17c17 commit 123acbb
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,27 @@ public function dispatchConversion(string $conversion): ?PendingDispatch
return null;
}

public function getOrExecuteConversion(string $name): ?MediaConversion
{
if ($conversion = $this->getConversion($name)) {
return $conversion;
}

return $this->executeConversion($name);
}

public function executeConversion(string $conversion): ?MediaConversion
{
if ($definition = $this->getConversionDefinition($conversion)) {
return $definition->execute($this, $this->getParentConversion($conversion));

if (str_contains($conversion, '.')) {
$parent = $this->getOrExecuteConversion(str($conversion)->beforeLast('.'));
} else {
$parent = null;
}

return $definition->execute($this, $parent);

}

return null;
Expand Down
21 changes: 21 additions & 0 deletions tests/Feature/HasMediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,27 @@

});

it('generates non existing parents conversions when executing nested conversion', function () {
Storage::fake('media');
$model = new Test;
$model->save();

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

expect($media->conversions)->toHaveLength(0);

$media->executeConversion('poster.360');

expect($media->conversions)->toHaveLength(2);

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

it('deletes old media when adding to single collection', function () {
Storage::fake('media');
$model = new Test;
Expand Down
21 changes: 21 additions & 0 deletions tests/Models/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,27 @@ public function registerMediaCollections(): Arrayable|iterable|null
),
]
),
new MediaCollection(
name: 'conversions-delayed',
single: false,
public: false,
conversions: [
new MediaConversionPoster(
name: 'poster',
queued: false,
immediate: false,
conversions: [
new MediaConversionImage(
name: '360',
width: 360,
queued: false,
immediate: true,
),
]
),

]
),
];
}
}

0 comments on commit 123acbb

Please sign in to comment.