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\Filesystem; |
||
| 4 | |||
| 5 | use Spatie\MediaLibrary\Helpers\File; |
||
| 6 | use Spatie\MediaLibrary\Models\Media; |
||
| 7 | use Spatie\MediaLibrary\FileManipulator; |
||
| 8 | use Illuminate\Contracts\Filesystem\Factory; |
||
| 9 | use Spatie\MediaLibrary\Events\MediaHasBeenAdded; |
||
| 10 | use Spatie\MediaLibrary\Conversion\ConversionCollection; |
||
| 11 | use Spatie\MediaLibrary\PathGenerator\PathGeneratorFactory; |
||
| 12 | |||
| 13 | class Filesystem |
||
| 14 | { |
||
| 15 | /** @var \Illuminate\Contracts\Filesystem\Factory */ |
||
| 16 | protected $filesystem; |
||
| 17 | |||
| 18 | /** @var array */ |
||
| 19 | protected $customRemoteHeaders = []; |
||
| 20 | |||
| 21 | public function __construct(Factory $filesystem) |
||
| 22 | { |
||
| 23 | $this->filesystem = $filesystem; |
||
| 24 | } |
||
| 25 | |||
| 26 | public function add(string $file, Media $media, ?string $targetFileName = null) |
||
| 27 | { |
||
| 28 | $this->copyToMediaLibrary($file, $media, null, $targetFileName); |
||
| 29 | |||
| 30 | event(new MediaHasBeenAdded($media)); |
||
| 31 | |||
| 32 | app(FileManipulator::class)->createDerivedFiles($media); |
||
| 33 | } |
||
| 34 | |||
| 35 | public function copyToMediaLibrary(string $pathToFile, Media $media, ?string $type = null, ?string $targetFileName = null) |
||
| 36 | { |
||
| 37 | $destinationFileName = $targetFileName ?: pathinfo($pathToFile, PATHINFO_BASENAME); |
||
| 38 | |||
| 39 | $destination = $this->getMediaDirectory($media, $type).$destinationFileName; |
||
| 40 | |||
| 41 | $file = fopen($pathToFile, 'r'); |
||
| 42 | |||
| 43 | if ($media->getDiskDriverName() === 'local') { |
||
| 44 | $this->filesystem |
||
| 45 | ->disk($media->disk) |
||
| 46 | ->put($destination, $file); |
||
| 47 | |||
| 48 | fclose($file); |
||
| 49 | |||
| 50 | return; |
||
| 51 | } |
||
| 52 | |||
| 53 | $this->filesystem |
||
| 54 | ->disk($media->disk) |
||
| 55 | ->put($destination, $file, $this->getRemoteHeadersForFile($pathToFile, $media->getCustomHeaders())); |
||
| 56 | |||
| 57 | if (is_resource($file)) { |
||
| 58 | fclose($file); |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | public function addCustomRemoteHeaders(array $customRemoteHeaders) |
||
| 63 | { |
||
| 64 | $this->customRemoteHeaders = $customRemoteHeaders; |
||
| 65 | } |
||
| 66 | |||
| 67 | public function getRemoteHeadersForFile(string $file, array $mediaCustomHeaders = []) : array |
||
| 68 | { |
||
| 69 | $mimeTypeHeader = ['ContentType' => File::getMimeType($file)]; |
||
| 70 | |||
| 71 | $extraHeaders = config('medialibrary.remote.extra_headers'); |
||
| 72 | |||
| 73 | return array_merge($mimeTypeHeader, $extraHeaders, $this->customRemoteHeaders, $mediaCustomHeaders); |
||
| 74 | } |
||
| 75 | |||
| 76 | public function getStream(Media $media) |
||
| 77 | { |
||
| 78 | $sourceFile = $this->getMediaDirectory($media).'/'.$media->file_name; |
||
| 79 | |||
| 80 | return $this->filesystem->disk($media->disk)->readStream($sourceFile); |
||
| 81 | } |
||
| 82 | |||
| 83 | public function copyFromMediaLibrary(Media $media, string $targetFile): string |
||
| 84 | { |
||
| 85 | touch($targetFile); |
||
| 86 | |||
| 87 | $stream = $this->getStream($media); |
||
| 88 | |||
| 89 | $targetFileStream = fopen($targetFile, 'a'); |
||
| 90 | |||
| 91 | while (! feof($stream)) { |
||
| 92 | $chunk = fread($stream, 1024); |
||
| 93 | fwrite($targetFileStream, $chunk); |
||
| 94 | } |
||
| 95 | |||
| 96 | fclose($stream); |
||
| 97 | |||
| 98 | fclose($targetFileStream); |
||
| 99 | |||
| 100 | return $targetFile; |
||
| 101 | } |
||
| 102 | |||
| 103 | public function removeAllFiles(Media $media) |
||
| 104 | { |
||
| 105 | $mediaDirectory = $this->getMediaDirectory($media); |
||
| 106 | |||
| 107 | $conversionsDirectory = $this->getMediaDirectory($media, 'conversions'); |
||
| 108 | |||
| 109 | $responsiveImagesDirectory = $this->getMediaDirectory($media, 'responsiveImages'); |
||
| 110 | |||
| 111 | collect([$mediaDirectory, $conversionsDirectory, $responsiveImagesDirectory]) |
||
| 112 | |||
| 113 | ->each(function ($directory) use ($media) { |
||
| 114 | $this->filesystem->disk($media->disk)->deleteDirectory($directory); |
||
| 115 | }); |
||
| 116 | } |
||
| 117 | |||
| 118 | public function removeFile(Media $media, string $path) |
||
| 119 | { |
||
| 120 | $this->filesystem->disk($media->disk)->delete($path); |
||
| 121 | } |
||
| 122 | |||
| 123 | public function removeResponsiveImages(Media $media, string $conversionName = 'medialibrary_original') |
||
| 124 | { |
||
| 125 | $responsiveImagesDirectory = $this->getResponsiveImagesDirectory($media); |
||
| 126 | |||
| 127 | $allFilePaths = $this->filesystem->allFiles($responsiveImagesDirectory); |
||
| 128 | |||
| 129 | $responsiveImagePaths = array_filter( |
||
| 130 | $allFilePaths, |
||
| 131 | function (string $path) use ($conversionName) { |
||
| 132 | return str_contains($path, $conversionName); |
||
|
0 ignored issues
–
show
|
|||
| 133 | } |
||
| 134 | ); |
||
| 135 | |||
| 136 | $this->filesystem->delete($responsiveImagePaths); |
||
| 137 | } |
||
| 138 | |||
| 139 | public function syncFileNames(Media $media) |
||
| 140 | { |
||
| 141 | $this->renameMediaFile($media); |
||
| 142 | |||
| 143 | $this->renameConversionFiles($media); |
||
| 144 | } |
||
| 145 | |||
| 146 | protected function renameMediaFile(Media $media) |
||
| 147 | { |
||
| 148 | $newFileName = $media->file_name; |
||
| 149 | $oldFileName = $media->getOriginal('file_name'); |
||
| 150 | |||
| 151 | $mediaDirectory = $this->getMediaDirectory($media); |
||
| 152 | |||
| 153 | $oldFile = $mediaDirectory.'/'.$oldFileName; |
||
| 154 | $newFile = $mediaDirectory.'/'.$newFileName; |
||
| 155 | |||
| 156 | $this->filesystem->disk($media->disk)->move($oldFile, $newFile); |
||
| 157 | } |
||
| 158 | |||
| 159 | protected function renameConversionFiles(Media $media) |
||
| 160 | { |
||
| 161 | $newFileName = $media->file_name; |
||
| 162 | $oldFileName = $media->getOriginal('file_name'); |
||
| 163 | |||
| 164 | $conversionDirectory = $this->getConversionDirectory($media); |
||
| 165 | |||
| 166 | $conversionCollection = ConversionCollection::createForMedia($media); |
||
| 167 | |||
| 168 | foreach ($media->getMediaConversionNames() as $conversionName) { |
||
| 169 | $conversion = $conversionCollection->getByName($conversionName); |
||
| 170 | |||
| 171 | $oldFile = $conversionDirectory.$conversion->getConversionFile($oldFileName); |
||
| 172 | $newFile = $conversionDirectory.$conversion->getConversionFile($newFileName); |
||
| 173 | |||
| 174 | $disk = $this->filesystem->disk($media->disk); |
||
| 175 | |||
| 176 | // A media conversion file might be missing, waiting to be generated, failed etc. |
||
| 177 | if (! $disk->exists($oldFile)) { |
||
| 178 | continue; |
||
| 179 | } |
||
| 180 | |||
| 181 | $disk->move($oldFile, $newFile); |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | public function getMediaDirectory(Media $media, ?string $type = null) : string |
||
| 186 | { |
||
| 187 | $pathGenerator = PathGeneratorFactory::create(); |
||
| 188 | |||
| 189 | if (! $type) { |
||
| 190 | $directory = $pathGenerator->getPath($media); |
||
| 191 | } |
||
| 192 | |||
| 193 | if ($type === 'conversions') { |
||
| 194 | $directory = $pathGenerator->getPathForConversions($media); |
||
| 195 | } |
||
| 196 | |||
| 197 | if ($type === 'responsiveImages') { |
||
| 198 | $directory = $pathGenerator->getPathForResponsiveImages($media); |
||
| 199 | } |
||
| 200 | |||
| 201 | if (! in_array($media->getDiskDriverName(), ['s3'], true)) { |
||
| 202 | $this->filesystem->disk($media->disk)->makeDirectory($directory); |
||
| 203 | } |
||
| 204 | |||
| 205 | return $directory; |
||
| 206 | } |
||
| 207 | |||
| 208 | public function getConversionDirectory(Media $media) : string |
||
| 209 | { |
||
| 210 | return $this->getMediaDirectory($media, 'conversions'); |
||
| 211 | } |
||
| 212 | |||
| 213 | public function getResponsiveImagesDirectory(Media $media) : string |
||
| 214 | { |
||
| 215 | return $this->getMediaDirectory($media, 'responsiveImages'); |
||
| 216 | } |
||
| 217 | } |
||
| 218 |
This function has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.