1 | <?php |
||
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) |
||
79 | |||
80 | /** |
||
81 | * @param \Illuminate\Database\Eloquent\Model $subject |
||
82 | * |
||
83 | * @return FileAdder |
||
84 | */ |
||
85 | public function setSubject(Model $subject) |
||
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() |
||
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) |
||
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) |
||
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) |
||
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) |
||
196 | |||
197 | /** |
||
198 | * Set the metadata. |
||
199 | * |
||
200 | * @param array $customProperties |
||
201 | * |
||
202 | * @return $this |
||
203 | */ |
||
204 | public function withCustomProperties(array $customProperties) |
||
210 | |||
211 | /** |
||
212 | * Set properties on the model. |
||
213 | * |
||
214 | * @param array $properties |
||
215 | * |
||
216 | * @return $this |
||
217 | */ |
||
218 | public function withProperties(array $properties) |
||
224 | |||
225 | /** |
||
226 | * Set attributes on the model. |
||
227 | * |
||
228 | * @param array $properties |
||
229 | * |
||
230 | * @return $this |
||
231 | */ |
||
232 | public function withAttributes(array $properties) |
||
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 = '') |
||
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 = '') |
||
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 = '') |
||
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 = '') |
||
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) |
||
372 | |||
373 | /** |
||
374 | * Sanitize the given file name. |
||
375 | * |
||
376 | * @param $fileName |
||
377 | * |
||
378 | * @return string |
||
379 | */ |
||
380 | protected function sanitizeFileName(string $fileName) : string |
||
384 | } |
||
385 |