1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\MediaLibrary\FileAdder; |
4
|
|
|
|
5
|
|
|
use Spatie\MediaLibrary\Media; |
6
|
|
|
use Spatie\MediaLibrary\Helpers\File; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Spatie\MediaLibrary\Filesystem\Filesystem; |
9
|
|
|
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded; |
10
|
|
|
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMedia; |
11
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
12
|
|
|
use Symfony\Component\HttpFoundation\File\File as SymfonyFile; |
13
|
|
|
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\UnknownType; |
14
|
|
|
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\FileIsTooBig; |
15
|
|
|
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\DiskDoesNotExist; |
16
|
|
|
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\FileDoesNotExist; |
17
|
|
|
|
18
|
|
|
class FileAdder |
19
|
|
|
{ |
20
|
|
|
/** @var \Illuminate\Database\Eloquent\Model subject */ |
21
|
|
|
protected $subject; |
22
|
|
|
|
23
|
|
|
/** @var \Spatie\MediaLibrary\Filesystem\Filesystem */ |
24
|
|
|
protected $filesystem; |
25
|
|
|
|
26
|
|
|
/** @var bool */ |
27
|
|
|
protected $preserveOriginal = false; |
28
|
|
|
|
29
|
|
|
/** @var string|\Symfony\Component\HttpFoundation\File\UploadedFile */ |
30
|
|
|
protected $file; |
31
|
|
|
|
32
|
|
|
/** @var array */ |
33
|
|
|
protected $properties = []; |
34
|
|
|
|
35
|
|
|
/** @var array */ |
36
|
|
|
protected $customProperties = []; |
37
|
|
|
|
38
|
|
|
/** @var array */ |
39
|
|
|
protected $manipulations = []; |
40
|
|
|
|
41
|
|
|
/** @var string */ |
42
|
|
|
protected $pathToFile; |
43
|
|
|
|
44
|
|
|
/** @var string */ |
45
|
|
|
protected $fileName; |
46
|
|
|
|
47
|
|
|
/** @var string */ |
48
|
|
|
protected $mediaName; |
49
|
|
|
|
50
|
|
|
/** @var string */ |
51
|
|
|
protected $diskName = ''; |
52
|
|
|
|
53
|
|
|
/** @var null|callable */ |
54
|
|
|
protected $fileNameSanitizer; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param Filesystem $fileSystem |
58
|
|
|
*/ |
59
|
|
|
public function __construct(Filesystem $fileSystem) |
60
|
|
|
{ |
61
|
|
|
$this->filesystem = $fileSystem; |
62
|
|
|
|
63
|
|
|
$this->fileNameSanitizer = function ($fileName) { |
64
|
|
|
return $this->defaultSanitizer($fileName); |
65
|
|
|
}; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param \Illuminate\Database\Eloquent\Model $subject |
70
|
|
|
* |
71
|
|
|
* @return FileAdder |
72
|
|
|
*/ |
73
|
|
|
public function setSubject(Model $subject) |
74
|
|
|
{ |
75
|
|
|
$this->subject = $subject; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/* |
81
|
|
|
* Set the file that needs to be imported. |
82
|
|
|
* |
83
|
|
|
* @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file |
84
|
|
|
* |
85
|
|
|
* @return $this |
86
|
|
|
*/ |
87
|
|
|
public function setFile($file) |
88
|
|
|
{ |
89
|
|
|
$this->file = $file; |
90
|
|
|
|
91
|
|
|
if (is_string($file)) { |
92
|
|
|
$this->pathToFile = $file; |
93
|
|
|
$this->setFileName(pathinfo($file, PATHINFO_BASENAME)); |
94
|
|
|
$this->mediaName = pathinfo($file, PATHINFO_FILENAME); |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($file instanceof UploadedFile) { |
100
|
|
|
$this->pathToFile = $file->getPath().'/'.$file->getFilename(); |
101
|
|
|
$this->setFileName($file->getClientOriginalName()); |
102
|
|
|
$this->mediaName = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME); |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($file instanceof SymfonyFile) { |
108
|
|
|
$this->pathToFile = $file->getPath().'/'.$file->getFilename(); |
109
|
|
|
$this->setFileName(pathinfo($file->getFilename(), PATHINFO_BASENAME)); |
110
|
|
|
$this->mediaName = pathinfo($file->getFilename(), PATHINFO_FILENAME); |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
throw UnknownType::create(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* When adding the file to the media library, the original file |
120
|
|
|
* will be preserved. |
121
|
|
|
* |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
|
|
public function preservingOriginal() |
125
|
|
|
{ |
126
|
|
|
$this->preserveOriginal = true; |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set the name of the media object. |
133
|
|
|
* |
134
|
|
|
* @param string $name |
135
|
|
|
* |
136
|
|
|
* @return $this |
137
|
|
|
*/ |
138
|
|
|
public function usingName(string $name) |
139
|
|
|
{ |
140
|
|
|
return $this->setName($name); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Set the name of the media object. |
145
|
|
|
* |
146
|
|
|
* @param string $name |
147
|
|
|
* |
148
|
|
|
* @return $this |
149
|
|
|
*/ |
150
|
|
|
public function setName(string $name) |
151
|
|
|
{ |
152
|
|
|
$this->mediaName = $name; |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Set the name of the file that is stored on disk. |
159
|
|
|
* |
160
|
|
|
* @param string $fileName |
161
|
|
|
* |
162
|
|
|
* @return $this |
163
|
|
|
*/ |
164
|
|
|
public function usingFileName(string $fileName) |
165
|
|
|
{ |
166
|
|
|
return $this->setFileName($fileName); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Set the name of the file that is stored on disk. |
171
|
|
|
* |
172
|
|
|
* @param string $fileName |
173
|
|
|
* |
174
|
|
|
* @return $this |
175
|
|
|
*/ |
176
|
|
|
public function setFileName(string $fileName) |
177
|
|
|
{ |
178
|
|
|
$this->fileName = $fileName; |
179
|
|
|
|
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Set the metadata. |
185
|
|
|
* |
186
|
|
|
* @param array $customProperties |
187
|
|
|
* |
188
|
|
|
* @return $this |
189
|
|
|
*/ |
190
|
|
|
public function withCustomProperties(array $customProperties) |
191
|
|
|
{ |
192
|
|
|
$this->customProperties = $customProperties; |
193
|
|
|
|
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Set the manipulations. |
199
|
|
|
* |
200
|
|
|
* @param array $manipulations |
201
|
|
|
* |
202
|
|
|
* @return $this |
203
|
|
|
*/ |
204
|
|
|
public function withManipulations(array $manipulations) |
205
|
|
|
{ |
206
|
|
|
$this->manipulations = $manipulations; |
207
|
|
|
|
208
|
|
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Set properties on the model. |
213
|
|
|
* |
214
|
|
|
* @param array $properties |
215
|
|
|
* |
216
|
|
|
* @return $this |
217
|
|
|
*/ |
218
|
|
|
public function withProperties(array $properties) |
219
|
|
|
{ |
220
|
|
|
$this->properties = $properties; |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Set attributes on the model. |
227
|
|
|
* |
228
|
|
|
* @param array $properties |
229
|
|
|
* |
230
|
|
|
* @return $this |
231
|
|
|
*/ |
232
|
|
|
public function withAttributes(array $properties) |
233
|
|
|
{ |
234
|
|
|
return $this->withProperties($properties); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Add the given additional headers when copying the file to a remote filesystem. |
239
|
|
|
* |
240
|
|
|
* @param array $customRemoteHeaders |
241
|
|
|
* |
242
|
|
|
* @return $this |
243
|
|
|
*/ |
244
|
|
|
public function addCustomHeaders(array $customRemoteHeaders) |
245
|
|
|
{ |
246
|
|
|
$this->filesystem->addCustomRemoteHeaders($customRemoteHeaders); |
247
|
|
|
|
248
|
|
|
return $this; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param string $collectionName |
253
|
|
|
* |
254
|
|
|
* @return \Spatie\MediaLibrary\Media |
255
|
|
|
* |
256
|
|
|
* @throws FileCannotBeAdded |
257
|
|
|
* @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
258
|
|
|
*/ |
259
|
|
|
public function toMediaCollectionOnCloudDisk(string $collectionName = 'default') |
260
|
|
|
{ |
261
|
|
|
return $this->toMediaCollection($collectionName, config('filesystems.cloud')); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param string $collectionName |
266
|
|
|
* @param string $diskName |
267
|
|
|
* |
268
|
|
|
* @return \Spatie\MediaLibrary\Media |
269
|
|
|
* |
270
|
|
|
* @throws FileCannotBeAdded |
271
|
|
|
* @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
272
|
|
|
*/ |
273
|
|
|
public function toMediaCollection(string $collectionName = 'default', string $diskName = '') |
274
|
|
|
{ |
275
|
|
|
if (! is_file($this->pathToFile)) { |
276
|
|
|
throw FileDoesNotExist::create($this->pathToFile); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
if (filesize($this->pathToFile) > config('medialibrary.max_file_size')) { |
280
|
|
|
throw FileIsTooBig::create($this->pathToFile); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
$mediaClass = config('medialibrary.media_model'); |
284
|
|
|
$media = new $mediaClass(); |
285
|
|
|
|
286
|
|
|
$media->name = $this->mediaName; |
287
|
|
|
|
288
|
|
|
$this->fileName = ($this->fileNameSanitizer)($this->fileName); |
289
|
|
|
|
290
|
|
|
$media->file_name = $this->fileName; |
291
|
|
|
$media->disk = $this->determineDiskName($diskName); |
292
|
|
|
|
293
|
|
|
$media->collection_name = $collectionName; |
294
|
|
|
|
295
|
|
|
$media->mime_type = File::getMimetype($this->pathToFile); |
296
|
|
|
$media->size = filesize($this->pathToFile); |
297
|
|
|
$media->custom_properties = $this->customProperties; |
298
|
|
|
$media->manipulations = $this->manipulations; |
299
|
|
|
|
300
|
|
|
$media->fill($this->properties); |
301
|
|
|
|
302
|
|
|
$this->attachMedia($media); |
303
|
|
|
|
304
|
|
|
return $media; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @param string $diskName |
309
|
|
|
* |
310
|
|
|
* @return string |
311
|
|
|
* |
312
|
|
|
* @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
313
|
|
|
*/ |
314
|
|
|
protected function determineDiskName(string $diskName) |
315
|
|
|
{ |
316
|
|
|
if ($diskName === '') { |
317
|
|
|
$diskName = config('medialibrary.default_filesystem'); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
if (is_null(config("filesystems.disks.{$diskName}"))) { |
321
|
|
|
throw DiskDoesNotExist::create($diskName); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
return $diskName; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* @param $fileName |
329
|
|
|
* |
330
|
|
|
* @return string |
331
|
|
|
*/ |
332
|
|
|
public function defaultSanitizer(string $fileName): string |
333
|
|
|
{ |
334
|
|
|
return str_replace(['#', '/', '\\'], '-', $fileName); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Sanitize the fileName of the file using a callable. |
339
|
|
|
* |
340
|
|
|
* @param callable $fileNameSanitizer |
341
|
|
|
* |
342
|
|
|
* @return $this |
343
|
|
|
*/ |
344
|
|
|
public function sanitizingFileName(callable $fileNameSanitizer) |
345
|
|
|
{ |
346
|
|
|
$this->fileNameSanitizer = $fileNameSanitizer; |
347
|
|
|
|
348
|
|
|
return $this; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @param Media $media |
353
|
|
|
*/ |
354
|
|
|
protected function attachMedia(Media $media) |
355
|
|
|
{ |
356
|
|
|
if (! $this->subject->exists) { |
357
|
|
|
$this->subject->prepareToAttachMedia($media, $this); |
358
|
|
|
|
359
|
|
|
$class = get_class($this->subject); |
360
|
|
|
|
361
|
|
|
$class::created(function ($model) { |
362
|
|
|
$model->processUnattachedMedia(function (Media $media, FileAdder $fileAdder) use ($model) { |
363
|
|
|
$this->processMediaItem($model, $media, $fileAdder); |
|
|
|
|
364
|
|
|
}); |
365
|
|
|
}); |
366
|
|
|
|
367
|
|
|
return; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
$this->processMediaItem($this->subject, $media, $this); |
|
|
|
|
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* @param HasMedia $model |
375
|
|
|
* @param Media $media |
376
|
|
|
* @param FileAdder $fileAdder |
377
|
|
|
*/ |
378
|
|
|
protected function processMediaItem(HasMedia $model, Media $media, self $fileAdder) |
379
|
|
|
{ |
380
|
|
|
$model->media()->save($media); |
381
|
|
|
|
382
|
|
|
$this->filesystem->add($fileAdder->pathToFile, $media, $fileAdder->fileName); |
383
|
|
|
|
384
|
|
|
if (! $fileAdder->preserveOriginal) { |
385
|
|
|
unlink($fileAdder->pathToFile); |
386
|
|
|
} |
387
|
|
|
} |
388
|
|
|
} |
389
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: