Skip to content

Commit

Permalink
Merge pull request #3721 from patrickomeara/dont-perform-conversions-…
Browse files Browse the repository at this point in the history
…if-not-needed

Don't copy the file if no conversion is needed
  • Loading branch information
timvandijck authored Nov 8, 2024
2 parents c0c22fd + 49a81d6 commit 1c476bb
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/Conversions/FileManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ public function performConversions(
Media $media,
bool $onlyMissing = false
): self {
$conversions = $conversions
->when(
$onlyMissing,
fn (ConversionCollection $conversions) => $conversions->reject(function (Conversion $conversion) use ($media) {
$relativePath = $media->getPath($conversion->getName());

if ($rootPath = config("filesystems.disks.{$media->disk}.root")) {
$relativePath = str_replace($rootPath, '', $relativePath);
}

return Storage::disk($media->disk)->exists($relativePath);
})
);

if ($conversions->isEmpty()) {
return $this;
}
Expand All @@ -57,19 +71,7 @@ public function performConversions(
$temporaryDirectory->path(Str::random(32).'.'.$media->extension)
);

$conversions
->reject(function (Conversion $conversion) use ($onlyMissing, $media) {
$relativePath = $media->getPath($conversion->getName());

if ($rootPath = config("filesystems.disks.{$media->disk}.root")) {
$relativePath = str_replace($rootPath, '', $relativePath);
}

return $onlyMissing && Storage::disk($media->disk)->exists($relativePath);
})
->each(function (Conversion $conversion) use ($media, $copiedOriginalFile) {
(new PerformConversionAction)->execute($conversion, $media, $copiedOriginalFile);
});
$conversions->each(fn (Conversion $conversion) => (new PerformConversionAction)->execute($conversion, $media, $copiedOriginalFile));

$temporaryDirectory->delete();

Expand Down

0 comments on commit 1c476bb

Please sign in to comment.