Completed
Pull Request — master (#468)
by
unknown
13:15
created

FileAdder::toCollectionOnDisk()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 39
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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