Skip to content

Commit 52633ca

Browse files
committed
add replace conversion
1 parent 18334fe commit 52633ca

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

src/Models/Media.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,36 @@ public function getParentConversion(string $name): ?MediaConversion
275275
*/
276276
public function getChildrenConversions(string $name): EloquentCollection
277277
{
278-
return $this->conversions->filter(fn ($conversion) => str_starts_with($conversion->conversion_name, "{$name}."));
278+
return $this
279+
->conversions
280+
->filter(fn ($conversion) => str_starts_with($conversion->conversion_name, "{$name}."));
281+
}
282+
283+
public function replaceConversion(
284+
MediaConversion $conversion,
285+
): MediaConversion {
286+
287+
$existingConversion = $this->getConversion($conversion->conversion_name);
288+
289+
if (
290+
$conversion->exists ||
291+
$conversion->is($existingConversion)
292+
) {
293+
return $conversion;
294+
}
295+
296+
$this->conversions()->save($conversion);
297+
$this->conversions->push($conversion);
298+
299+
if ($existingConversion) {
300+
$existingConversion->delete();
301+
$this->setRelation(
302+
'conversions',
303+
$this->conversions->except([$existingConversion->id])
304+
);
305+
}
306+
307+
return $conversion;
279308
}
280309

281310
/**
@@ -320,7 +349,7 @@ public function addConversion(
320349

321350
if ($existingConversion) {
322351
$existingConversion->deleteFile();
323-
$this->deleteChildrenConversion($conversionName);
352+
$this->deleteChildrenConversions($conversionName);
324353
} else {
325354
$this->conversions->push($conversion);
326355
}
@@ -396,12 +425,10 @@ public function deleteConversion(string $conversionName): static
396425
return $this;
397426
}
398427

399-
public function deleteChildrenConversion(string $conversionName): static
428+
public function deleteChildrenConversions(string $conversionName): static
400429
{
401-
$deleted = $this->conversions
402-
->filter(function ($conversion) use ($conversionName) {
403-
return str($conversion->conversion_name)->startsWith("{$conversionName}.");
404-
})
430+
$deleted = $this
431+
->getChildrenConversions($conversionName)
405432
->each(fn ($conversion) => $conversion->delete());
406433

407434
$this->setRelation(

0 commit comments

Comments
 (0)