This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Spatie\MediaLibrary; |
||
| 4 | |||
| 5 | use Illuminate\Contracts\Bus\Dispatcher; |
||
| 6 | use Illuminate\Support\Facades\File; |
||
|
0 ignored issues
–
show
|
|||
| 7 | use Illuminate\Support\Str; |
||
| 8 | use Spatie\MediaLibrary\Conversion\Conversion; |
||
| 9 | use Spatie\MediaLibrary\Conversion\ConversionCollection; |
||
| 10 | use Spatie\MediaLibrary\Events\ConversionHasBeenCompleted; |
||
| 11 | use Spatie\MediaLibrary\Events\ConversionWillStart; |
||
| 12 | use Spatie\MediaLibrary\Filesystem\Filesystem; |
||
| 13 | use Spatie\MediaLibrary\Helpers\File as MediaLibraryFileHelper; |
||
| 14 | use Spatie\MediaLibrary\Helpers\ImageFactory; |
||
| 15 | use Spatie\MediaLibrary\Helpers\TemporaryDirectory; |
||
| 16 | use Spatie\MediaLibrary\ImageGenerators\ImageGenerator; |
||
| 17 | use Spatie\MediaLibrary\Jobs\PerformConversions; |
||
| 18 | use Spatie\MediaLibrary\Models\Media; |
||
| 19 | use Spatie\MediaLibrary\ResponsiveImages\ResponsiveImageGenerator; |
||
| 20 | use Storage; |
||
| 21 | |||
| 22 | class FileManipulator |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Create all derived files for the given media. |
||
| 26 | * |
||
| 27 | * @param \Spatie\MediaLibrary\Models\Media $media |
||
| 28 | * @param array $only |
||
| 29 | * @param bool $onlyMissing |
||
| 30 | */ |
||
| 31 | public function createDerivedFiles(Media $media, array $only = [], bool $onlyMissing = false) |
||
| 32 | { |
||
| 33 | $profileCollection = ConversionCollection::createForMedia($media); |
||
| 34 | |||
| 35 | if (! empty($only)) { |
||
| 36 | $profileCollection = $profileCollection->filter(function ($collection) use ($only) { |
||
| 37 | return in_array($collection->getName(), $only); |
||
| 38 | }); |
||
| 39 | } |
||
| 40 | |||
| 41 | $this->performConversions( |
||
| 42 | $profileCollection->getNonQueuedConversions($media->collection_name), |
||
| 43 | $media, |
||
| 44 | $onlyMissing |
||
| 45 | ); |
||
| 46 | |||
| 47 | $queuedConversions = $profileCollection->getQueuedConversions($media->collection_name); |
||
| 48 | |||
| 49 | if ($queuedConversions->isNotEmpty()) { |
||
| 50 | $this->dispatchQueuedConversions($media, $queuedConversions, $onlyMissing); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Perform the given conversions for the given media. |
||
| 56 | * |
||
| 57 | * @param \Spatie\MediaLibrary\Conversion\ConversionCollection $conversions |
||
| 58 | * @param \Spatie\MediaLibrary\Models\Media $media |
||
| 59 | * @param bool $onlyMissing |
||
| 60 | */ |
||
| 61 | public function performConversions(ConversionCollection $conversions, Media $media, bool $onlyMissing = false) |
||
| 62 | { |
||
| 63 | if ($conversions->isEmpty()) { |
||
| 64 | return; |
||
| 65 | } |
||
| 66 | |||
| 67 | $imageGenerator = $this->determineImageGenerator($media); |
||
| 68 | |||
| 69 | if (! $imageGenerator) { |
||
| 70 | return; |
||
| 71 | } |
||
| 72 | |||
| 73 | $temporaryDirectory = TemporaryDirectory::create(); |
||
| 74 | |||
| 75 | $copiedOriginalFile = app(Filesystem::class)->copyFromMediaLibrary( |
||
| 76 | $media, |
||
| 77 | $temporaryDirectory->path(Str::random(16).'.'.$media->extension) |
||
| 78 | ); |
||
| 79 | |||
| 80 | $conversions |
||
| 81 | ->reject(function (Conversion $conversion) use ($onlyMissing, $media) { |
||
| 82 | $relativePath = $media->getPath($conversion->getName()); |
||
| 83 | |||
| 84 | $rootPath = config('filesystems.disks.'.$media->disk.'.root'); |
||
| 85 | |||
| 86 | if ($rootPath) { |
||
| 87 | $relativePath = str_replace($rootPath, '', $relativePath); |
||
| 88 | } |
||
| 89 | |||
| 90 | return $onlyMissing && Storage::disk($media->disk)->exists($relativePath); |
||
| 91 | }) |
||
| 92 | ->each(function (Conversion $conversion) use ($media, $imageGenerator, $copiedOriginalFile) { |
||
| 93 | event(new ConversionWillStart($media, $conversion, $copiedOriginalFile)); |
||
| 94 | |||
| 95 | $copiedOriginalFile = $imageGenerator->convert($copiedOriginalFile, $conversion); |
||
|
0 ignored issues
–
show
Consider using a different name than the imported variable
$copiedOriginalFile, or did you forget to import by reference?
It seems like you are assigning to a variable which was imported through a For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope. Change not visible in outer-scope$x = 1;
$callable = function() use ($x) {
$x = 2; // Not visible in outer scope. If you would like this, how
// about using a different variable name than $x?
};
$callable();
var_dump($x); // integer(1)
Change visible in outer-scope$x = 1;
$callable = function() use (&$x) {
$x = 2;
};
$callable();
var_dump($x); // integer(2)
Loading history...
|
|||
| 96 | |||
| 97 | $manipulationResult = $this->performManipulations($media, $conversion, $copiedOriginalFile); |
||
| 98 | |||
| 99 | $newFileName = $conversion->getConversionFile($media->file_name); |
||
| 100 | |||
| 101 | $renamedFile = MediaLibraryFileHelper::renameInDirectory($manipulationResult, $newFileName); |
||
| 102 | |||
| 103 | if ($conversion->shouldGenerateResponsiveImages()) { |
||
| 104 | app(ResponsiveImageGenerator::class)->generateResponsiveImagesForConversion( |
||
| 105 | $media, |
||
| 106 | $conversion, |
||
| 107 | $renamedFile |
||
| 108 | ); |
||
| 109 | } |
||
| 110 | |||
| 111 | app(Filesystem::class)->copyToMediaLibrary($renamedFile, $media, 'conversions'); |
||
| 112 | |||
| 113 | $media->markAsConversionGenerated($conversion->getName(), true); |
||
| 114 | |||
| 115 | event(new ConversionHasBeenCompleted($media, $conversion)); |
||
| 116 | }); |
||
| 117 | |||
| 118 | $temporaryDirectory->delete(); |
||
| 119 | } |
||
| 120 | |||
| 121 | public function performManipulations(Media $media, Conversion $conversion, string $imageFile): string |
||
| 122 | { |
||
| 123 | if ($conversion->getManipulations()->isEmpty()) { |
||
| 124 | return $imageFile; |
||
| 125 | } |
||
| 126 | |||
| 127 | $conversionTempFile = pathinfo($imageFile, PATHINFO_DIRNAME).'/'.Str::random(16) |
||
| 128 | .$conversion->getName() |
||
| 129 | .'.' |
||
| 130 | .$media->extension; |
||
| 131 | |||
| 132 | File::copy($imageFile, $conversionTempFile); |
||
| 133 | |||
| 134 | $supportedFormats = ['jpg', 'pjpg', 'png', 'gif']; |
||
| 135 | if ($conversion->shouldKeepOriginalImageFormat() && in_array($media->extension, $supportedFormats)) { |
||
| 136 | $conversion->format($media->extension); |
||
|
0 ignored issues
–
show
The method
format() does not exist on Spatie\MediaLibrary\Conversion\Conversion. Did you maybe mean keepOriginalImageFormat()?
This check marks calls to methods that do not seem to exist on an object. This is most likely the result of a method being renamed without all references to it being renamed likewise. Loading history...
|
|||
| 137 | } |
||
| 138 | |||
| 139 | ImageFactory::load($conversionTempFile) |
||
| 140 | ->manipulate($conversion->getManipulations()) |
||
| 141 | ->save(); |
||
| 142 | |||
| 143 | return $conversionTempFile; |
||
| 144 | } |
||
| 145 | |||
| 146 | protected function dispatchQueuedConversions(Media $media, ConversionCollection $queuedConversions, bool $onlyMissing = false) |
||
| 147 | { |
||
| 148 | $performConversionsJobClass = config('medialibrary.jobs.perform_conversions', PerformConversions::class); |
||
| 149 | |||
| 150 | $job = new $performConversionsJobClass($queuedConversions, $media, $onlyMissing); |
||
| 151 | |||
| 152 | if ($customQueue = config('medialibrary.queue_name')) { |
||
| 153 | $job->onQueue($customQueue); |
||
| 154 | } |
||
| 155 | |||
| 156 | app(Dispatcher::class)->dispatch($job); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @param \Spatie\MediaLibrary\Models\Media $media |
||
| 161 | * |
||
| 162 | * @return \Spatie\MediaLibrary\ImageGenerators\ImageGenerator|null |
||
| 163 | */ |
||
| 164 | public function determineImageGenerator(Media $media) |
||
| 165 | { |
||
| 166 | return $media->getImageGenerators() |
||
| 167 | ->map(function (string $imageGeneratorClassName) { |
||
| 168 | return app($imageGeneratorClassName); |
||
| 169 | }) |
||
| 170 | ->first(function (ImageGenerator $imageGenerator) use ($media) { |
||
| 171 | return $imageGenerator->canConvert($media); |
||
| 172 | }); |
||
| 173 | } |
||
| 174 | } |
||
| 175 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: