Completed
Push — master ( b0673d...012d5c )
by Freek
02:21
created

FileAdder::toMediaLibrary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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