Completed
Push — master ( b9e2d3...5b20cd )
by Freek
02:00
created

FileAdder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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