|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Javaabu\Mediapicker\Conversions; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\Storage; |
| 6 | +use Illuminate\Support\Str; |
| 7 | +use Javaabu\Mediapicker\Contracts\Attachment; |
| 8 | +use Javaabu\Mediapicker\Conversions\Jobs\PerformAttachmentConversionsJob; |
| 9 | +use Spatie\MediaLibrary\Conversions\Actions\PerformConversionAction; |
| 10 | +use Spatie\MediaLibrary\Conversions\Conversion; |
| 11 | +use Spatie\MediaLibrary\Conversions\FileManipulator; |
| 12 | +use Spatie\MediaLibrary\Conversions\ImageGenerators\ImageGeneratorFactory; |
| 13 | +use Spatie\MediaLibrary\Conversions\Jobs\PerformConversionsJob; |
| 14 | +use Spatie\MediaLibrary\MediaCollections\Filesystem; |
| 15 | +use Spatie\MediaLibrary\MediaCollections\Models\Media; |
| 16 | +use Spatie\MediaLibrary\ResponsiveImages\Jobs\GenerateResponsiveImagesJob; |
| 17 | +use Spatie\MediaLibrary\Support\TemporaryDirectory; |
| 18 | + |
| 19 | +class MediaManipulator extends FileManipulator |
| 20 | +{ |
| 21 | + public function createDerivedAttachmentFiles( |
| 22 | + Attachment $attachment, |
| 23 | + array $onlyConversionNames = [], |
| 24 | + bool $onlyMissing = false, |
| 25 | + bool $withResponsiveImages = false, |
| 26 | + bool $queueAll = false, |
| 27 | + ): void { |
| 28 | + $media = $attachment->media; |
| 29 | + |
| 30 | + if (! $this->canConvertMedia($media)) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + [$queuedConversions, $conversions] = AttachmentConversionCollection::createForAttachment($attachment) |
| 35 | + ->filter(function (Conversion $conversion) use ($onlyConversionNames) { |
| 36 | + if (count($onlyConversionNames) === 0) { |
| 37 | + return true; |
| 38 | + } |
| 39 | + |
| 40 | + return in_array($conversion->getName(), $onlyConversionNames); |
| 41 | + }) |
| 42 | + ->filter(fn (Conversion $conversion) => $conversion->shouldBePerformedOn($attachment->collection_name)) |
| 43 | + ->partition(fn (Conversion $conversion) => $queueAll || $conversion->shouldBeQueued()); |
| 44 | + |
| 45 | + $this |
| 46 | + ->performAttachmentConversions($conversions, $attachment, $onlyMissing) |
| 47 | + ->dispatchQueuedAttachmentConversions($attachment, $queuedConversions, $onlyMissing); |
| 48 | + } |
| 49 | + |
| 50 | + public function performAttachmentConversions( |
| 51 | + AttachmentConversionCollection $conversions, |
| 52 | + Attachment $attachment, |
| 53 | + bool $onlyMissing = false |
| 54 | + ): self { |
| 55 | + $media = $attachment->media; |
| 56 | + |
| 57 | + $conversions = $conversions |
| 58 | + ->when( |
| 59 | + $onlyMissing, |
| 60 | + fn (AttachmentConversionCollection $conversions) => $conversions->reject(function (Conversion $conversion) use ($attachment, $media) { |
| 61 | + $relativePath = $attachment->getPath($conversion->getName()); |
| 62 | + |
| 63 | + if ($rootPath = config("filesystems.disks.{$media->disk}.root")) { |
| 64 | + $relativePath = str_replace($rootPath, '', $relativePath); |
| 65 | + } |
| 66 | + |
| 67 | + return Storage::disk($media->disk)->exists($relativePath); |
| 68 | + }) |
| 69 | + ); |
| 70 | + |
| 71 | + if ($conversions->isEmpty()) { |
| 72 | + return $this; |
| 73 | + } |
| 74 | + |
| 75 | + $temporaryDirectory = TemporaryDirectory::create(); |
| 76 | + |
| 77 | + $copiedOriginalFile = app(Filesystem::class)->copyFromMediaLibrary( |
| 78 | + $media, |
| 79 | + $temporaryDirectory->path(Str::random(32).'.'.$media->extension) |
| 80 | + ); |
| 81 | + |
| 82 | + $conversions->each(fn (Conversion $conversion) => (new PerformConversionAction)->execute($conversion, $media, $copiedOriginalFile)); |
| 83 | + |
| 84 | + $temporaryDirectory->delete(); |
| 85 | + |
| 86 | + return $this; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @return $this |
| 91 | + */ |
| 92 | + protected function dispatchQueuedAttachmentConversions( |
| 93 | + Attachment $attachment, |
| 94 | + AttachmentConversionCollection $conversions, |
| 95 | + bool $onlyMissing = false |
| 96 | + ): self { |
| 97 | + if ($conversions->isEmpty()) { |
| 98 | + return $this; |
| 99 | + } |
| 100 | + |
| 101 | + $performConversionsJobClass = config( |
| 102 | + 'mediapicker.jobs.perform_conversions', |
| 103 | + PerformAttachmentConversionsJob::class |
| 104 | + ); |
| 105 | + |
| 106 | + /** @var PerformAttachmentConversionsJob $job */ |
| 107 | + $job = (new $performConversionsJobClass($conversions, $attachment, $onlyMissing)) |
| 108 | + ->onConnection(config('media-library.queue_connection_name')) |
| 109 | + ->onQueue(config('media-library.queue_name')); |
| 110 | + |
| 111 | + config('media-library.queue_conversions_after_database_commit') |
| 112 | + ? dispatch($job)->afterCommit() |
| 113 | + : dispatch($job); |
| 114 | + |
| 115 | + return $this; |
| 116 | + } |
| 117 | + |
| 118 | + protected function canConvertMedia(Media $media): bool |
| 119 | + { |
| 120 | + $imageGenerator = ImageGeneratorFactory::forMedia($media); |
| 121 | + |
| 122 | + return $imageGenerator ? true : false; |
| 123 | + } |
| 124 | +} |
0 commit comments