Skip to content

Commit

Permalink
better media conversions generation
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Nov 4, 2024
1 parent c6301d2 commit 371564c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 40 deletions.
53 changes: 33 additions & 20 deletions src/Commands/GenerateMediaConversionsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Elegantly\Media\Commands;

use Elegantly\Media\Definitions\MediaConversionDefinition;
use Elegantly\Media\Models\Media;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;
Expand All @@ -28,6 +29,18 @@ public function handle(): int
/** @var string[] $collections */
$collections = (array) $this->option('collections');

$filter = function (MediaConversionDefinition $definition) use ($immediate) {

if (
$immediate === false &&
! $definition->immediate
) {
return false;
}

return true;
};

/**
* @var class-string<Media> $model
*/
Expand All @@ -47,7 +60,7 @@ public function handle(): int

$progress = new Progress('Dispatching Media conversions', $count);

$query->chunkById(5_000, function ($items) use ($progress, $force, $immediate, $conversions) {
$query->chunkById(5_000, function ($items) use ($progress, $force, $conversions, $filter) {

foreach ($items as $media) {
/** @var Media $media */
Expand All @@ -59,26 +72,26 @@ public function handle(): int
);
}
} else {
$media->dispatchConversions(

/**
* Generate missing children conversions
*/
$media->conversions->each(function ($conversion) use ($media, $force, $filter) {
$media->generateConversions(
parent: $conversion,
queued: true,
force: $force,
filter: $filter,
);
});

/**
* Generate missing root conversions
*/
$media->generateConversions(
queued: true,
filter: function ($definition) use ($media, $force, $immediate) {

if (
$immediate === false &&
! $definition->immediate
) {
return false;
}

if (
$force === false &&
$media->hasConversion($definition->name)
) {
return false;
}

return true;
}
force: $force,
filter: $filter,
);
}

Expand Down
5 changes: 3 additions & 2 deletions src/Concerns/HasMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,9 @@ public function addMedia(
);
}

$media->dispatchConversions(
filter: fn ($definition) => $definition->immediate
$media->generateConversions(
filter: fn ($definition) => $definition->immediate,
force: true,
);

return $media;
Expand Down
48 changes: 30 additions & 18 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ public function getChildrenConversionsDefinitions(string $name): array
return $this->getConversionDefinition($name)?->conversions ?? [];
}

/**
* Dispatch any conversion while generating missing parents.
* Will check for 'shouldExecute' definition method.
*/
public function dispatchConversion(
string $conversion,
bool $force = true,
Expand All @@ -264,6 +268,10 @@ public function dispatchConversion(
return null;
}

/**
* Execute any conversion while generating missing parents.
* Will check for 'shouldExecute' definition method.
*/
public function executeConversion(
string $conversion,
bool $force = true,
Expand All @@ -284,6 +292,10 @@ public function executeConversion(
$parent = null;
}

if (! $definition->shouldExecute($this, $parent)) {
return null;
}

return $definition->execute($this, $parent);

}
Expand Down Expand Up @@ -356,9 +368,10 @@ public function replaceConversion(
);
}

$this->dispatchConversions(
$this->generateConversions(
parent: $conversion,
filter: fn ($definition) => $definition->immediate
filter: fn ($definition) => $definition->immediate,
force: true,
);

return $conversion;
Expand Down Expand Up @@ -413,23 +426,27 @@ public function addConversion(
$this->conversions->push($conversion);
}

$this->dispatchConversions(
$this->generateConversions(
parent: $conversion,
filter: fn ($definition) => $definition->immediate
filter: fn ($definition) => $definition->immediate,
force: true,
);

return $conversion;
}

/**
* Execute or dispatch first level conversions based on their definition
*
* @param null|(Closure(MediaConversionDefinition $definition):bool) $filter
* @param ?bool $queued force queueing the conversions
* @return $this
*/
public function dispatchConversions(
public function generateConversions(
?MediaConversion $parent = null,
?Closure $filter = null,
?bool $queued = null,
bool $force = false,
): static {
if ($parent) {
$definitions = $this->getChildrenConversionsDefinitions($parent->conversion_name);
Expand All @@ -443,28 +460,23 @@ public function dispatchConversions(
continue;
}

if (! $definition->shouldExecute(
media: $this,
parent: $parent
)) {
continue;
}
$conversion = $parent ? "{$parent->conversion_name}.{$definition->name}" : $definition->name;

if ($queued ?? $definition->queued) {
$definition->dispatch(
media: $this,
parent: $parent,
$this->dispatchConversion(
conversion: $conversion,
force: $force,
);

} else {
$definition->execute(
media: $this,
parent: $parent
$this->executeConversion(
conversion: $conversion,
force: $force
);
}
}

return $this;

}

/**
Expand Down

0 comments on commit 371564c

Please sign in to comment.