Skip to content

Commit 1cf1e6d

Browse files
committed
support for force
1 parent 07f2e1f commit 1cf1e6d

File tree

2 files changed

+40
-23
lines changed

2 files changed

+40
-23
lines changed

src/Commands/GenerateMediaConversionsCommand.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,10 @@ public function handle(): int
5353
/** @var Media $media */
5454
if (! empty($conversions)) {
5555
foreach ($conversions as $conversion) {
56-
if (
57-
$force === false &&
58-
$media->hasConversion($conversion)
59-
) {
60-
continue;
61-
}
62-
63-
$media->dispatchConversion($conversion);
56+
$media->dispatchConversion(
57+
conversion: $conversion,
58+
force: $force
59+
);
6460
}
6561
} else {
6662
$media->dispatchConversions(

src/Models/Media.php

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Elegantly\Media\Events\MediaFileStoredEvent;
1313
use Elegantly\Media\FileDownloaders\FileDownloader;
1414
use Elegantly\Media\Helpers\File;
15+
use Elegantly\Media\Jobs\MediaConversionJob;
1516
use Elegantly\Media\TemporaryDirectory;
1617
use Elegantly\Media\Traits\HasUuid;
1718
use Exception;
@@ -240,30 +241,41 @@ public function getChildrenConversionsDefinitions(string $name): array
240241
return $this->getConversionDefinition($name)?->conversions ?? [];
241242
}
242243

243-
public function dispatchConversion(string $conversion): ?PendingDispatch
244-
{
244+
public function dispatchConversion(
245+
string $conversion,
246+
bool $force = true,
247+
): ?PendingDispatch {
248+
if (
249+
$force === false &&
250+
$this->hasConversion($conversion)
251+
) {
252+
return null;
253+
}
254+
245255
if ($definition = $this->getConversionDefinition($conversion)) {
246-
$parent = $this->getParentConversion($conversion);
247256

248-
if ($definition->shouldExecute($this, $parent)) {
249-
return $definition->dispatch($this, $parent);
250-
}
257+
return dispatch(new MediaConversionJob(
258+
media: $this,
259+
conversion: $conversion
260+
));
261+
251262
}
252263

253264
return null;
254265
}
255266

256-
public function getOrExecuteConversion(string $name): ?MediaConversion
257-
{
258-
if ($conversion = $this->getConversion($name)) {
259-
return $conversion;
260-
}
267+
public function executeConversion(
268+
string $conversion,
269+
bool $force = true,
270+
): ?MediaConversion {
261271

262-
return $this->executeConversion($name);
263-
}
272+
if (
273+
$force === false &&
274+
$this->hasConversion($conversion)
275+
) {
276+
return null;
277+
}
264278

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

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

294+
public function getOrExecuteConversion(string $name): ?MediaConversion
295+
{
296+
if ($conversion = $this->getConversion($name)) {
297+
return $conversion;
298+
}
299+
300+
return $this->executeConversion($name);
301+
}
302+
282303
public function getConversion(string $name): ?MediaConversion
283304
{
284305
return $this->conversions->firstWhere('conversion_name', $name);

0 commit comments

Comments
 (0)