Skip to content

Commit

Permalink
support for force
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Oct 23, 2024
1 parent 07f2e1f commit 1cf1e6d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
12 changes: 4 additions & 8 deletions src/Commands/GenerateMediaConversionsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,10 @@ public function handle(): int
/** @var Media $media */
if (! empty($conversions)) {
foreach ($conversions as $conversion) {
if (
$force === false &&
$media->hasConversion($conversion)
) {
continue;
}

$media->dispatchConversion($conversion);
$media->dispatchConversion(
conversion: $conversion,
force: $force
);
}
} else {
$media->dispatchConversions(
Expand Down
51 changes: 36 additions & 15 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Elegantly\Media\Events\MediaFileStoredEvent;
use Elegantly\Media\FileDownloaders\FileDownloader;
use Elegantly\Media\Helpers\File;
use Elegantly\Media\Jobs\MediaConversionJob;
use Elegantly\Media\TemporaryDirectory;
use Elegantly\Media\Traits\HasUuid;
use Exception;
Expand Down Expand Up @@ -240,30 +241,41 @@ public function getChildrenConversionsDefinitions(string $name): array
return $this->getConversionDefinition($name)?->conversions ?? [];
}

public function dispatchConversion(string $conversion): ?PendingDispatch
{
public function dispatchConversion(
string $conversion,
bool $force = true,
): ?PendingDispatch {
if (
$force === false &&
$this->hasConversion($conversion)
) {
return null;
}

if ($definition = $this->getConversionDefinition($conversion)) {
$parent = $this->getParentConversion($conversion);

if ($definition->shouldExecute($this, $parent)) {
return $definition->dispatch($this, $parent);
}
return dispatch(new MediaConversionJob(
media: $this,
conversion: $conversion
));

}

return null;
}

public function getOrExecuteConversion(string $name): ?MediaConversion
{
if ($conversion = $this->getConversion($name)) {
return $conversion;
}
public function executeConversion(
string $conversion,
bool $force = true,
): ?MediaConversion {

return $this->executeConversion($name);
}
if (
$force === false &&
$this->hasConversion($conversion)
) {
return null;
}

public function executeConversion(string $conversion): ?MediaConversion
{
if ($definition = $this->getConversionDefinition($conversion)) {

if (str_contains($conversion, '.')) {
Expand All @@ -279,6 +291,15 @@ public function executeConversion(string $conversion): ?MediaConversion
return null;
}

public function getOrExecuteConversion(string $name): ?MediaConversion
{
if ($conversion = $this->getConversion($name)) {
return $conversion;
}

return $this->executeConversion($name);
}

public function getConversion(string $name): ?MediaConversion
{
return $this->conversions->firstWhere('conversion_name', $name);
Expand Down

0 comments on commit 1cf1e6d

Please sign in to comment.