1 | <?php |
||
18 | class FileAdder |
||
19 | { |
||
20 | /** @var \Illuminate\Database\Eloquent\Model subject */ |
||
21 | protected $subject; |
||
22 | |||
23 | /** @var \Spatie\MediaLibrary\Filesystem\Filesystem */ |
||
24 | protected $filesystem; |
||
25 | |||
26 | /** @var bool */ |
||
27 | protected $preserveOriginal = false; |
||
28 | |||
29 | /** @var string|\Symfony\Component\HttpFoundation\File\UploadedFile */ |
||
30 | protected $file; |
||
31 | |||
32 | /** @var array */ |
||
33 | protected $properties = []; |
||
34 | |||
35 | /** @var array */ |
||
36 | protected $customProperties = []; |
||
37 | |||
38 | /** @var string */ |
||
39 | protected $pathToFile; |
||
40 | |||
41 | /** @var string */ |
||
42 | protected $fileName; |
||
43 | |||
44 | /** @var string */ |
||
45 | protected $mediaName; |
||
46 | |||
47 | /** @var string */ |
||
48 | protected $diskName = ''; |
||
49 | |||
50 | /** |
||
51 | * @param Filesystem $fileSystem |
||
52 | */ |
||
53 | public function __construct(Filesystem $fileSystem) |
||
57 | |||
58 | /** |
||
59 | * @param \Illuminate\Database\Eloquent\Model $subject |
||
60 | * |
||
61 | * @return FileAdder |
||
62 | */ |
||
63 | public function setSubject(Model $subject) |
||
69 | |||
70 | /* |
||
71 | * Set the file that needs to be imported. |
||
72 | * |
||
73 | * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file |
||
74 | * |
||
75 | * @return $this |
||
76 | */ |
||
77 | public function setFile($file) |
||
107 | |||
108 | /** |
||
109 | * When adding the file to the media library, the original file |
||
110 | * will be preserved. |
||
111 | * |
||
112 | * @return $this |
||
113 | */ |
||
114 | public function preservingOriginal() |
||
120 | |||
121 | /** |
||
122 | * Set the name of the media object. |
||
123 | * |
||
124 | * @param string $name |
||
125 | * |
||
126 | * @return $this |
||
127 | */ |
||
128 | public function usingName(string $name) |
||
132 | |||
133 | /** |
||
134 | * Set the name of the media object. |
||
135 | * |
||
136 | * @param string $name |
||
137 | * |
||
138 | * @return $this |
||
139 | */ |
||
140 | public function setName(string $name) |
||
146 | |||
147 | /** |
||
148 | * Set the name of the file that is stored on disk. |
||
149 | * |
||
150 | * @param string $fileName |
||
151 | * |
||
152 | * @return $this |
||
153 | */ |
||
154 | public function usingFileName(string $fileName) |
||
158 | |||
159 | /** |
||
160 | * Set the name of the file that is stored on disk. |
||
161 | * |
||
162 | * @param string $fileName |
||
163 | * |
||
164 | * @return $this |
||
165 | */ |
||
166 | public function setFileName(string $fileName) |
||
172 | |||
173 | /** |
||
174 | * Set the metadata. |
||
175 | * |
||
176 | * @param array $customProperties |
||
177 | * |
||
178 | * @return $this |
||
179 | */ |
||
180 | public function withCustomProperties(array $customProperties) |
||
186 | |||
187 | /** |
||
188 | * Set properties on the model. |
||
189 | * |
||
190 | * @param array $properties |
||
191 | * |
||
192 | * @return $this |
||
193 | */ |
||
194 | public function withProperties(array $properties) |
||
200 | |||
201 | /** |
||
202 | * Set attributes on the model. |
||
203 | * |
||
204 | * @param array $properties |
||
205 | * |
||
206 | * @return $this |
||
207 | */ |
||
208 | public function withAttributes(array $properties) |
||
212 | |||
213 | /** |
||
214 | * Add the given additional headers when copying the file to a remote filesystem. |
||
215 | * |
||
216 | * @param array $customRemoteHeaders |
||
217 | * |
||
218 | * @return $this |
||
219 | */ |
||
220 | public function addCustomHeaders(array $customRemoteHeaders) |
||
226 | |||
227 | /** |
||
228 | * @param string $collectionName |
||
229 | * |
||
230 | * @return \Spatie\MediaLibrary\Media |
||
231 | * |
||
232 | * @throws FileCannotBeAdded |
||
233 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
234 | */ |
||
235 | public function toMediaCollectionOnCloudDisk(string $collectionName = 'default') |
||
236 | { |
||
237 | return $this->toMediaCollection($collectionName, config('filesystems.cloud')); |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @param string $collectionName |
||
242 | * @param string $diskName |
||
243 | * |
||
244 | * @return \Spatie\MediaLibrary\Media |
||
245 | * |
||
246 | * @throws FileCannotBeAdded |
||
247 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
248 | */ |
||
249 | public function toMediaCollection(string $collectionName = 'default', string $diskName = '') |
||
279 | |||
280 | /** |
||
281 | * @param string $diskName |
||
282 | * |
||
283 | * @return string |
||
284 | * |
||
285 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
286 | */ |
||
287 | protected function determineDiskName(string $diskName) |
||
299 | |||
300 | /** |
||
301 | * @param $fileName |
||
302 | * |
||
303 | * @return string |
||
304 | */ |
||
305 | protected function sanitizeFileName(string $fileName): string |
||
309 | |||
310 | /** |
||
311 | * Set the fileName of the file using a callable. |
||
312 | * |
||
313 | * @param callable $function |
||
314 | * |
||
315 | * @return $this |
||
316 | */ |
||
317 | public function sanitizingFileName(callable $function) |
||
323 | |||
324 | /** |
||
325 | * @param Media $media |
||
326 | */ |
||
327 | protected function attachMedia(Media $media) |
||
345 | |||
346 | /** |
||
347 | * @param HasMedia $model |
||
348 | * @param Media $media |
||
349 | * @param FileAdder $fileAdder |
||
350 | */ |
||
351 | protected function processMediaItem(HasMedia $model, Media $media, FileAdder $fileAdder) |
||
361 | } |
||
362 |