Skip to content

Commit

Permalink
callback for conversion and collection
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Jan 11, 2025
1 parent 7e1d35c commit 1f47ec0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/Concerns/HasMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ public function addMedia(
force: true,
);

if ($onAdded = $collection?->onAdded) {
$onAdded($media);
}

event(new MediaAddedEvent($media));

return $media;
Expand Down
12 changes: 10 additions & 2 deletions src/Definitions/MediaConversionDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ class MediaConversionDefinition
* @param MediaConversionDefinition[] $conversions
* @param Closure(Media $media, ?MediaConversion $parent, ?string $file, Filesystem $filesystem, SpatieTemporaryDirectory $temporaryDirectory): ?MediaConversion $handle
* @param null|bool|Closure(Media $media, ?MediaConversion $parent): bool $when
* @param null|(Closure(?MediaConversion $conversion, Media $media, ?MediaConversion $parent): void) $onCompleted
*/
public function __construct(
public string $name,
public Closure $handle,
public null|bool|Closure $when = null,
public ?Closure $onCompleted = null,
public bool $immediate = true,
public bool $queued = true,
public ?string $queue = null,
public array $conversions = [],
) {
/** @var array<string, MediaConversionDefinition> $conversions */
$conversions = collect($conversions)->keyBy('name')->toArray();
$conversions = collect($conversions)->keyBy('name')->all();
$this->conversions = $conversions;
}

Expand Down Expand Up @@ -70,7 +72,7 @@ public function dispatch(Media $media, ?MediaConversion $parent): PendingDispatc

public function execute(Media $media, ?MediaConversion $parent): ?MediaConversion
{
return TemporaryDirectory::callback(function ($temporaryDirectory) use ($media, $parent) {
$value = TemporaryDirectory::callback(function ($temporaryDirectory) use ($media, $parent) {

$storage = TemporaryDirectory::storage($temporaryDirectory);

Expand All @@ -84,5 +86,11 @@ public function execute(Media $media, ?MediaConversion $parent): ?MediaConversio
return $this->handle($media, $parent, $copy, $storage, $temporaryDirectory);

});

if ($onCompleted = $this->onCompleted) {
$onCompleted($value, $media, $parent);
}

return $value;
}
}
5 changes: 4 additions & 1 deletion src/MediaCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Elegantly\Media\Definitions\MediaConversionDefinition;
use Elegantly\Media\Models\Media;
use Illuminate\Http\File;
use Illuminate\Http\UploadedFile;

Expand All @@ -13,6 +14,7 @@ class MediaCollection
* @param null|(string[]) $acceptedMimeTypes
* @param null|string|(Closure(): null|string) $fallback
* @param null|(Closure(UploadedFile|File $file): (UploadedFile|File)) $transform
* @param null|(Closure(Media $media): void) $onAdded
* @param MediaConversionDefinition[] $conversions
*/
public function __construct(
Expand All @@ -23,10 +25,11 @@ public function __construct(
public ?string $disk = null,
public null|string|Closure $fallback = null,
public ?Closure $transform = null,
public ?Closure $onAdded = null,
public array $conversions = [],
) {
/** @var array<string, MediaConversionDefinition> $conversions */
$conversions = collect($conversions)->keyBy('name')->toArray();
$conversions = collect($conversions)->keyBy('name')->all();
$this->conversions = $conversions;
}

Expand Down

0 comments on commit 1f47ec0

Please sign in to comment.