Skip to content

Commit 123acbb

Browse files
committed
execute parent conversions when needed
1 parent 3a17c17 commit 123acbb

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

src/Models/Media.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,27 @@ public function dispatchConversion(string $conversion): ?PendingDispatch
249249
return null;
250250
}
251251

252+
public function getOrExecuteConversion(string $name): ?MediaConversion
253+
{
254+
if ($conversion = $this->getConversion($name)) {
255+
return $conversion;
256+
}
257+
258+
return $this->executeConversion($name);
259+
}
260+
252261
public function executeConversion(string $conversion): ?MediaConversion
253262
{
254263
if ($definition = $this->getConversionDefinition($conversion)) {
255-
return $definition->execute($this, $this->getParentConversion($conversion));
264+
265+
if (str_contains($conversion, '.')) {
266+
$parent = $this->getOrExecuteConversion(str($conversion)->beforeLast('.'));
267+
} else {
268+
$parent = null;
269+
}
270+
271+
return $definition->execute($this, $parent);
272+
256273
}
257274

258275
return null;

tests/Feature/HasMediaTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,27 @@
147147

148148
});
149149

150+
it('generates non existing parents conversions when executing nested conversion', function () {
151+
Storage::fake('media');
152+
$model = new Test;
153+
$model->save();
154+
155+
$media = $model->addMedia(
156+
file: $this->getTestFile('videos/horizontal.mp4'),
157+
collectionName: 'conversions-delayed',
158+
disk: 'media'
159+
);
160+
161+
expect($media->conversions)->toHaveLength(0);
162+
163+
$media->executeConversion('poster.360');
164+
165+
expect($media->conversions)->toHaveLength(2);
166+
167+
expect($media->getConversion('poster'))->not->toBe(null);
168+
expect($media->getConversion('poster.360'))->not->toBe(null);
169+
});
170+
150171
it('deletes old media when adding to single collection', function () {
151172
Storage::fake('media');
152173
$model = new Test;

tests/Models/Test.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,27 @@ public function registerMediaCollections(): Arrayable|iterable|null
9494
),
9595
]
9696
),
97+
new MediaCollection(
98+
name: 'conversions-delayed',
99+
single: false,
100+
public: false,
101+
conversions: [
102+
new MediaConversionPoster(
103+
name: 'poster',
104+
queued: false,
105+
immediate: false,
106+
conversions: [
107+
new MediaConversionImage(
108+
name: '360',
109+
width: 360,
110+
queued: false,
111+
immediate: true,
112+
),
113+
]
114+
),
115+
116+
]
117+
),
97118
];
98119
}
99120
}

0 commit comments

Comments
 (0)