Skip to content

Commit 73455d3

Browse files
authored
Merge pull request #20 from ElegantEngineeringTech/v4
adjust children regeneration logic
2 parents 39c3a29 + bef6767 commit 73455d3

File tree

5 files changed

+83
-32
lines changed

5 files changed

+83
-32
lines changed

README.md

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ I developed this package with the highest degree of flexibility possible and I h
4040
- [Delayed conversions](#delayed-conversions)
4141
- [`onAdded` MediaCollection Callback](#onadded-mediacollection-callback)
4242
- [`onCompleted` MediaConversionDefinition Callback](#oncompleted-mediaconversiondefinition-callback)
43+
- [Regenerate children conversions on parent execution](#regenerate-children-conversions-when-parent-is-executed)
4344
- [Custom conversions](#custom-conversions)
4445
- [Manually generate conversions](#manually-generate-conversions)
4546
- [Format Media Url](#format-media-url)
@@ -505,10 +506,6 @@ new MediaCollection(
505506
width: 360
506507
)
507508
),
508-
new MediaConversionImage(
509-
name: '360',
510-
width: 360,
511-
)
512509
]
513510
)
514511
```
@@ -557,7 +554,7 @@ The `onCompleted` callback allows you to define custom logic that will be execut
557554
To use it, simply set the `onCompleted` parameter when defining a `MediaConversionDefinition`. For example:
558555

559556
```php
560-
new MediaConversionImage(
557+
new MediaConversionDefinition(
561558
name: '360',
562559
onCompleted: function ($conversion, $media, $parent) {
563560
// Example: Refresh your UI
@@ -571,6 +568,44 @@ This allows you to hook into the conversion process and execute additional logic
571568
> [!TIP]
572569
> The same behavior can be achieved by listening to `Elegantly\Media\Events\MediaConversionAddedEvent`.
573570
571+
### Regenerating Child Conversions When a Parent Runs
572+
573+
In some cases, you may want all **child conversions** of a given conversion to be **regenerated whenever the parent conversion is re-executed**.
574+
575+
There are two ways to achieve this, depending on where you handle conversions:
576+
577+
#### From the conversion definition
578+
579+
You can hook into the `onCompleted` callback of a conversion definition and instruct Laravel to delete all child conversions when the parent finishes.
580+
581+
```php
582+
new MediaConversionDefinition(
583+
name: '360',
584+
onCompleted: function (?MediaConversion $conversion, Media $media, ?MediaConversion $parent) {
585+
if ($conversion) {
586+
// Delete any children so they’ll be regenerated automatically
587+
$media->deleteChildrenConversions($conversion->conversion_name);
588+
}
589+
}
590+
);
591+
```
592+
593+
#### From `addConversion`
594+
595+
When adding a new conversion programmatically, you can explicitly tell it to remove children and regenerate them automatically:
596+
597+
```php
598+
$mediaConversion = $media->addConversion(
599+
file: $file,
600+
deleteChildren: true, // ensures child conversions are refreshed
601+
);
602+
603+
// regenerate children conversion
604+
$this->media->generateConversions(
605+
parent: $mediaConversion,
606+
);
607+
```
608+
574609
### Custom Conversions
575610

576611
Conversions can be anything: a variant of a file, a transcription of a video, a completely new file, or even just a string.

src/Converters/MediaConverter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ public function handle(): ?MediaConversion
150150
return $this->convert($this->media, $parent, $copy, $storage, $temporaryDirectory);
151151
});
152152

153+
if ($onCompleted = $definition->onCompleted) {
154+
$onCompleted($mediaConversion, $this->media, $parent);
155+
}
156+
153157
if (
154158
$mediaConversion &&
155159
$mediaConversion->state === MediaConversionState::Succeeded &&

src/Models/Media.php

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
use Elegantly\Media\Traits\HasUuid;
2121
use Elegantly\Media\UrlFormatters\AbstractUrlFormatter;
2222
use Exception;
23-
use Illuminate\Database\Eloquent\Casts\ArrayObject;
24-
use Illuminate\Database\Eloquent\Casts\AsArrayObject;
2523
use Illuminate\Database\Eloquent\Casts\Attribute;
2624
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
2725
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -40,7 +38,7 @@
4038
* @property ?string $collection_group
4139
* @property ?string $average_color
4240
* @property ?int $order_column
43-
* @property ?ArrayObject<array-key, mixed> $metadata
41+
* @property ?array<array-key, mixed> $metadata
4442
* @property ?InteractWithMedia<Media> $model
4543
* @property ?string $model_type
4644
* @property ?int $model_id
@@ -67,14 +65,17 @@ class Media extends Model
6765
protected $appends = ['url'];
6866

6967
/**
70-
* @var array<string, string>
68+
* @return array<string, string>
7169
*/
72-
protected $casts = [
73-
'type' => MediaType::class,
74-
'metadata' => AsArrayObject::class,
75-
'duration' => 'float',
76-
'aspect_ratio' => 'float',
77-
];
70+
public function casts(): array
71+
{
72+
return [
73+
'type' => MediaType::class,
74+
'metadata' => 'array',
75+
'duration' => 'float',
76+
'aspect_ratio' => 'float',
77+
];
78+
}
7879

7980
public static function booted()
8081
{
@@ -407,6 +408,8 @@ public function replaceConversion(MediaConversion $conversion): MediaConversion
407408
* Store a file as a conversion and dispatch children conversions
408409
*
409410
* @param string|resource|UploadedFile|HttpFile $file
411+
* @param array<array-key, mixed> $metadata
412+
* @param array<array-key, mixed> $attributes
410413
*/
411414
public function addConversion(
412415
$file,
@@ -415,7 +418,9 @@ public function addConversion(
415418
?string $name = null,
416419
?string $destination = null,
417420
?string $disk = null,
418-
bool $regenerateChildren = true
421+
?array $metadata = null,
422+
array $attributes = [],
423+
bool $deleteChildren = false
419424
): MediaConversion {
420425
/**
421426
* Prefix name with parent if not already done
@@ -441,8 +446,26 @@ public function addConversion(
441446

442447
$conversion->conversion_name = $conversionName;
443448
$conversion->media_id = $this->id;
449+
450+
// reset values
451+
$conversion->type = null;
452+
$conversion->name = null;
453+
$conversion->extension = null;
454+
$conversion->file_name = null;
455+
$conversion->mime_type = null;
456+
$conversion->width = null;
457+
$conversion->height = null;
458+
$conversion->aspect_ratio = null;
459+
$conversion->duration = null;
460+
$conversion->average_color = null;
461+
$conversion->size = null;
462+
$conversion->contents = null;
463+
444464
$conversion->state = MediaConversionState::Succeeded;
445465
$conversion->state_set_at = now();
466+
$conversion->metadata = $metadata;
467+
$conversion->created_at = now();
468+
$conversion->fill($attributes);
446469

447470
$conversion->storeFile(
448471
file: $file,
@@ -458,22 +481,12 @@ public function addConversion(
458481
) {
459482
$existingConversionReplicate->deleteFile();
460483
}
461-
462-
if ($regenerateChildren) {
463-
/**
464-
* Because the conversion has been regenerated, its children are not up to date anymore
465-
*/
466-
$this->deleteChildrenConversions($conversionName);
467-
}
468-
469484
} else {
470485
$this->conversions->push($conversion);
471486
}
472487

473-
$definition = $this->getConversionDefinition($conversionName);
474-
475-
if ($onCompleted = $definition?->onCompleted) {
476-
$onCompleted($conversion, $this, $parent);
488+
if ($deleteChildren) {
489+
$this->deleteChildrenConversions($conversionName);
477490
}
478491

479492
event(new MediaConversionAddedEvent($conversion));

src/Models/MediaConversion.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
use Elegantly\Media\TemporaryDirectory;
1616
use Elegantly\Media\Traits\HasUuid;
1717
use Exception;
18-
use Illuminate\Database\Eloquent\Casts\ArrayObject;
19-
use Illuminate\Database\Eloquent\Casts\AsArrayObject;
2018
use Illuminate\Database\Eloquent\Casts\Attribute;
2119
use Illuminate\Database\Eloquent\Factories\HasFactory;
2220
use Illuminate\Database\Eloquent\Model;
@@ -45,7 +43,7 @@
4543
* @property ?int $size
4644
* @property ?float $duration
4745
* @property ?string $contents Arbitrary stored value
48-
* @property ?ArrayObject<array-key, mixed> $metadata
46+
* @property ?array<array-key, mixed> $metadata
4947
* @property int $media_id
5048
* @property Media $media
5149
* @property Carbon $created_at
@@ -71,7 +69,7 @@ public function casts()
7169
{
7270
return [
7371
'type' => MediaType::class,
74-
'metadata' => AsArrayObject::class,
72+
'metadata' => 'array',
7573
'duration' => 'float',
7674
'aspect_ratio' => 'float',
7775
'state' => MediaConversionState::class,

tests/Feature/MediaTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@
293293
file: $file,
294294
conversionName: 'poster',
295295
name: 'new-poster',
296+
deleteChildren: true
296297
);
297298

298299
foreach ($children as $child) {

0 commit comments

Comments
 (0)