Skip to content

Commit

Permalink
better addConversion overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Nov 23, 2024
1 parent f6e91ae commit 29cda02
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,19 +397,24 @@ public function addConversion(
?string $disk = null,
): MediaConversion {

if (
$parent &&
! str_contains($conversionName, '.')
) {
/**
* Prefix name with parent if not already done
*/
if ($parent && ! str_contains($conversionName, '.')) {
$conversionName = "{$parent->conversion_name}.{$conversionName}";
}

if ($conversion = $this->getConversion($conversionName)) {
$existingConversion = clone $conversion;
} else {
$existingConversion = null;
$conversion = new MediaConversion;
}
/**
* If the conversion already exists, we are going to overwrite it
*/
$existingConversion = $this->getConversion($conversionName);

/**
* To delete old conversion files, we will use a untouched replicate
*/
$existingConversionReplicate = $existingConversion?->replicate();

$conversion = $existingConversion ?? new MediaConversion;

$conversion->fill([
'conversion_name' => $conversionName,
Expand All @@ -418,15 +423,23 @@ public function addConversion(
'state_set_at' => now(),
]);

$conversion = $conversion->storeFile(
$conversion->storeFile(
file: $file,
destination: $destination ?? $this->makeFreshPath($conversionName),
name: $name,
disk: $disk ?? $this->disk
);

if ($existingConversion) {
$existingConversion->deleteFile();
if ($existingConversionReplicate) {
if (
$existingConversionReplicate->path !== $conversion->path ||
$existingConversionReplicate->disk !== $conversion->disk
) {
$existingConversionReplicate->deleteFile();
}
/**
* Because the conversion has been regenerated, its children are not up to date anymore
*/
$this->deleteChildrenConversions($conversionName);
} else {
$this->conversions->push($conversion);
Expand Down

0 comments on commit 29cda02

Please sign in to comment.