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