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 syncFileNames(Media $media) |
124
|
|
|
{ |
125
|
|
|
$this->renameMediaFile($media); |
126
|
|
|
|
127
|
|
|
$this->renameConversionFiles($media); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected function renameMediaFile(Media $media) |
131
|
|
|
{ |
132
|
|
|
$newFileName = $media->file_name; |
133
|
|
|
$oldFileName = $media->getOriginal('file_name'); |
134
|
|
|
|
135
|
|
|
$mediaDirectory = $this->getMediaDirectory($media); |
136
|
|
|
|
137
|
|
|
$oldFile = $mediaDirectory.'/'.$oldFileName; |
138
|
|
|
$newFile = $mediaDirectory.'/'.$newFileName; |
139
|
|
|
|
140
|
|
|
$this->filesystem->disk($media->disk)->move($oldFile, $newFile); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
protected function renameConversionFiles(Media $media) |
144
|
|
|
{ |
145
|
|
|
$newFileName = $media->file_name; |
146
|
|
|
$oldFileName = $media->getOriginal('file_name'); |
147
|
|
|
|
148
|
|
|
$conversionDirectory = $this->getConversionDirectory($media); |
149
|
|
|
|
150
|
|
|
$conversionCollection = ConversionCollection::createForMedia($media); |
151
|
|
|
|
152
|
|
|
foreach ($media->getMediaConversionNames() as $conversionName) { |
153
|
|
|
$conversion = $conversionCollection->getByName($conversionName); |
154
|
|
|
|
155
|
|
|
$oldFile = $conversionDirectory.$conversion->getConversionFile($oldFileName); |
156
|
|
|
$newFile = $conversionDirectory.$conversion->getConversionFile($newFileName); |
157
|
|
|
|
158
|
|
|
$this->filesystem->disk($media->disk)->move($oldFile, $newFile); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function getMediaDirectory(Media $media, ?string $type = null) : string |
163
|
|
|
{ |
164
|
|
|
$pathGenerator = PathGeneratorFactory::create(); |
165
|
|
|
|
166
|
|
|
if (! $type) { |
167
|
|
|
$directory = $pathGenerator->getPath($media); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
if ($type === 'conversions') { |
171
|
|
|
$directory = $pathGenerator->getPathForConversions($media); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if ($type === 'responsiveImages') { |
175
|
|
|
$directory = $pathGenerator->getPathForResponsiveImages($media); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if (! in_array($media->getDiskDriverName(), ['s3'], true)) { |
179
|
|
|
$this->filesystem->disk($media->disk)->makeDirectory($directory); |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $directory; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function getConversionDirectory(Media $media) : string |
186
|
|
|
{ |
187
|
|
|
return $this->getMediaDirectory($media, 'conversions'); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function getResponsiveImagesDirectory(Media $media) : string |
191
|
|
|
{ |
192
|
|
|
return $this->getMediaDirectory($media, 'responsiveImages'); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: