20
20
use Elegantly \Media \Traits \HasUuid ;
21
21
use Elegantly \Media \UrlFormatters \AbstractUrlFormatter ;
22
22
use Exception ;
23
- use Illuminate \Database \Eloquent \Casts \ArrayObject ;
24
- use Illuminate \Database \Eloquent \Casts \AsArrayObject ;
25
23
use Illuminate \Database \Eloquent \Casts \Attribute ;
26
24
use Illuminate \Database \Eloquent \Collection as EloquentCollection ;
27
25
use Illuminate \Database \Eloquent \Factories \HasFactory ;
40
38
* @property ?string $collection_group
41
39
* @property ?string $average_color
42
40
* @property ?int $order_column
43
- * @property ?ArrayObject <array-key, mixed> $metadata
41
+ * @property ?array <array-key, mixed> $metadata
44
42
* @property ?InteractWithMedia<Media> $model
45
43
* @property ?string $model_type
46
44
* @property ?int $model_id
@@ -67,14 +65,17 @@ class Media extends Model
67
65
protected $ appends = ['url ' ];
68
66
69
67
/**
70
- * @var array<string, string>
68
+ * @return array<string, string>
71
69
*/
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
+ }
78
79
79
80
public static function booted ()
80
81
{
@@ -407,6 +408,8 @@ public function replaceConversion(MediaConversion $conversion): MediaConversion
407
408
* Store a file as a conversion and dispatch children conversions
408
409
*
409
410
* @param string|resource|UploadedFile|HttpFile $file
411
+ * @param array<array-key, mixed> $metadata
412
+ * @param array<array-key, mixed> $attributes
410
413
*/
411
414
public function addConversion (
412
415
$ file ,
@@ -415,7 +418,9 @@ public function addConversion(
415
418
?string $ name = null ,
416
419
?string $ destination = null ,
417
420
?string $ disk = null ,
418
- bool $ regenerateChildren = true
421
+ ?array $ metadata = null ,
422
+ array $ attributes = [],
423
+ bool $ deleteChildren = false
419
424
): MediaConversion {
420
425
/**
421
426
* Prefix name with parent if not already done
@@ -441,8 +446,26 @@ public function addConversion(
441
446
442
447
$ conversion ->conversion_name = $ conversionName ;
443
448
$ 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
+
444
464
$ conversion ->state = MediaConversionState::Succeeded;
445
465
$ conversion ->state_set_at = now ();
466
+ $ conversion ->metadata = $ metadata ;
467
+ $ conversion ->created_at = now ();
468
+ $ conversion ->fill ($ attributes );
446
469
447
470
$ conversion ->storeFile (
448
471
file: $ file ,
@@ -458,22 +481,12 @@ public function addConversion(
458
481
) {
459
482
$ existingConversionReplicate ->deleteFile ();
460
483
}
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
-
469
484
} else {
470
485
$ this ->conversions ->push ($ conversion );
471
486
}
472
487
473
- $ definition = $ this ->getConversionDefinition ($ conversionName );
474
-
475
- if ($ onCompleted = $ definition ?->onCompleted) {
476
- $ onCompleted ($ conversion , $ this , $ parent );
488
+ if ($ deleteChildren ) {
489
+ $ this ->deleteChildrenConversions ($ conversionName );
477
490
}
478
491
479
492
event (new MediaConversionAddedEvent ($ conversion ));
0 commit comments