1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Form\Elements; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\UploadedFile; |
6
|
|
|
use Sco\Admin\Facades\Admin; |
7
|
|
|
use Sco\Admin\Traits\StorageTrait; |
8
|
|
|
use Validator; |
9
|
|
|
|
10
|
|
|
class File extends NamedElement |
11
|
|
|
{ |
12
|
|
|
use StorageTrait; |
13
|
|
|
|
14
|
|
|
protected $type = 'file'; |
15
|
|
|
|
16
|
|
|
protected $actionUrl; |
17
|
|
|
|
18
|
|
|
protected $multiSelect = false; |
19
|
|
|
|
20
|
|
|
//protected $multiFile = true; |
|
|
|
|
21
|
|
|
|
22
|
|
|
protected $showFileList = true; |
23
|
|
|
|
24
|
|
|
protected $withCredentials = false; |
25
|
|
|
|
26
|
|
|
protected $maxFileSize; |
27
|
|
|
|
28
|
|
|
protected $fileUploadsLimit = 0; |
29
|
|
|
|
30
|
|
|
protected $fileExtensions; |
31
|
|
|
|
32
|
|
|
protected $listType = 'text'; |
33
|
|
|
|
34
|
|
|
protected $uploadValidationRules = []; |
35
|
|
|
|
36
|
|
|
protected $uploadValidationMessages = []; |
37
|
|
|
|
38
|
|
|
public function getValue() |
39
|
|
|
{ |
40
|
|
|
$value = parent::getValue(); |
41
|
|
|
if (empty($value)) { |
42
|
|
|
return []; |
43
|
|
|
} |
44
|
|
|
return collect(explode(',', $value))->filter(function ($item) { |
45
|
|
|
return $this->existsFile($item); |
46
|
|
|
})->map(function ($item) { |
47
|
|
|
return $this->getFileInfo($item); |
48
|
|
|
}); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getActionUrl() |
52
|
|
|
{ |
53
|
|
|
if ($this->actionUrl) { |
54
|
|
|
return $this->actionUrl; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$params = [ |
58
|
|
|
'model' => Admin::component()->getName(), |
59
|
|
|
'field' => $this->getName(), |
60
|
|
|
]; |
61
|
|
|
$params['id'] = null; |
62
|
|
|
if ($this->getModel()->exists) { |
63
|
|
|
$params['id'] = $this->getModel()->getKey(); |
64
|
|
|
} |
65
|
|
|
$params['_token'] = csrf_token(); |
66
|
|
|
|
67
|
|
|
return route('admin.model.upload.file', $params); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function setActionUrl($value) |
71
|
|
|
{ |
72
|
|
|
$this->actionUrl = $value; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function isMultiSelect() |
78
|
|
|
{ |
79
|
|
|
return $this->multiSelect; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function enableMultiSelect() |
83
|
|
|
{ |
84
|
|
|
$this->multiSelect = true; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function isShowFileList() |
90
|
|
|
{ |
91
|
|
|
return $this->showFileList; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Disable file list |
96
|
|
|
* |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
|
|
public function disableFileList() |
100
|
|
|
{ |
101
|
|
|
$this->showFileList = false; |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Indicates whether or not cross-site Access-Control requests |
108
|
|
|
* should be made using credentials |
109
|
|
|
* |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
|
|
public function withCredentials() |
113
|
|
|
{ |
114
|
|
|
$this->withCredentials = true; |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* The maximum size of an uploaded file in kilobytes |
121
|
|
|
* |
122
|
|
|
* @return int |
123
|
|
|
*/ |
124
|
|
|
public function getMaxFileSize() |
125
|
|
|
{ |
126
|
|
|
if ($this->maxFileSize) { |
127
|
|
|
return $this->maxFileSize; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $this->getDefaultMaxFileSize(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
protected function getDefaultMaxFileSize() |
134
|
|
|
{ |
135
|
|
|
return UploadedFile::getMaxFilesize() / 1024; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* The maximum size allowed for an uploaded file in kilobytes |
140
|
|
|
* |
141
|
|
|
* @param int $value |
142
|
|
|
* |
143
|
|
|
* @return $this |
144
|
|
|
*/ |
145
|
|
|
public function setMaxFileSize($value) |
146
|
|
|
{ |
147
|
|
|
$this->maxFileSize = intval($value); |
148
|
|
|
|
149
|
|
|
$this->addValidationRule('max:' . $this->maxFileSize); |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function getFileExtensions() |
155
|
|
|
{ |
156
|
|
|
if ($this->fileExtensions) { |
157
|
|
|
return $this->fileExtensions; |
158
|
|
|
} |
159
|
|
|
return $this->getDefaultExtensions(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* A list of allowable extensions that can be uploaded. |
164
|
|
|
* |
165
|
|
|
* @param string $value |
166
|
|
|
* |
167
|
|
|
* @return $this |
168
|
|
|
*/ |
169
|
|
|
public function setFileExtensions($value) |
170
|
|
|
{ |
171
|
|
|
$this->fileExtensions = $value; |
172
|
|
|
|
173
|
|
|
$this->addValidationRule('mimes:' . $value); |
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
protected function getDefaultExtensions() |
179
|
|
|
{ |
180
|
|
|
return config('admin.upload.extensions.file'); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function getFileUploadsLimit() |
184
|
|
|
{ |
185
|
|
|
return $this->fileUploadsLimit; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* The maximum number of files that can be uploaded. |
190
|
|
|
* |
191
|
|
|
* @param int $value |
192
|
|
|
* |
193
|
|
|
* @return $this |
194
|
|
|
*/ |
195
|
|
|
public function setFileUploadsLimit($value) |
196
|
|
|
{ |
197
|
|
|
$this->fileUploadsLimit = intval($value); |
198
|
|
|
|
199
|
|
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function getListType() |
203
|
|
|
{ |
204
|
|
|
return $this->listType; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function toArray() |
208
|
|
|
{ |
209
|
|
|
return parent::toArray() + [ |
210
|
|
|
'action' => $this->getActionUrl(), |
211
|
|
|
'showFileList' => $this->isShowFileList(), |
212
|
|
|
'multiSelect' => $this->isMultiSelect(), |
213
|
|
|
'maxFileSize' => $this->getMaxFileSize(), |
214
|
|
|
'fileUploadsLimit' => $this->getFileUploadsLimit(), |
215
|
|
|
'fileExtensions' => $this->getFileExtensions(), |
216
|
|
|
'listType' => $this->getListType(), |
217
|
|
|
]; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Save file to storage |
222
|
|
|
* |
223
|
|
|
* @param \Illuminate\Http\UploadedFile $file |
224
|
|
|
* |
225
|
|
|
* @return array |
226
|
|
|
* @throws \Illuminate\Validation\ValidationException |
227
|
|
|
*/ |
228
|
|
|
public function saveFile(UploadedFile $file) |
229
|
|
|
{ |
230
|
|
|
Validator::validate( |
231
|
|
|
[$this->getName() => $file], |
232
|
|
|
$this->getUploadValidationRules(), |
233
|
|
|
$this->getUploadValidationMessages(), |
234
|
|
|
$this->getUploadValidationTitles() |
235
|
|
|
); |
236
|
|
|
|
237
|
|
|
$path = $file->storeAs( |
238
|
|
|
$this->getUploadPath($file), |
239
|
|
|
$this->getUploadFileName($file), |
240
|
|
|
$this->getDisk() |
241
|
|
|
); |
242
|
|
|
|
243
|
|
|
return $this->getFileInfo($path); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
protected function prepareValue($value) |
247
|
|
|
{ |
248
|
|
|
if (empty($value) || !is_array($value)) { |
249
|
|
|
return ''; |
250
|
|
|
} |
251
|
|
|
return collect($value)->implode('path', ','); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Get file info(name path url) |
256
|
|
|
* |
257
|
|
|
* @param $path |
258
|
|
|
* |
259
|
|
|
* @return array |
260
|
|
|
*/ |
261
|
|
|
protected function getFileInfo($path) |
262
|
|
|
{ |
263
|
|
|
return [ |
264
|
|
|
'name' => substr($path, strrpos($path, '/') + 1), |
265
|
|
|
'path' => $path, |
266
|
|
|
'url' => $this->getFileUrl($path), |
267
|
|
|
]; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
public function addValidationRule($rule, $message = null) |
271
|
|
|
{ |
272
|
|
|
$uploadRules = [ |
273
|
|
|
'image', 'mimes', 'mimetypes', 'size', |
274
|
|
|
'dimensions', 'max', 'min', 'between', |
275
|
|
|
]; |
276
|
|
|
|
277
|
|
|
if (in_array($this->getValidationRuleName($rule), $uploadRules)) { |
278
|
|
|
return $this->addUploadValidationRule($rule, $message); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
return parent::addValidationRule($rule, $message); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
View Code Duplication |
protected function addUploadValidationRule($rule, $message = null) |
|
|
|
|
285
|
|
|
{ |
286
|
|
|
$this->uploadValidationRules[$this->getValidationRuleName($rule)] = $rule; |
287
|
|
|
|
288
|
|
|
if (is_null($message)) { |
289
|
|
|
return $this; |
290
|
|
|
} |
291
|
|
|
return $this->addUploadValidationMessage($rule, $message); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
protected function addUploadValidationMessage($rule, $message) |
295
|
|
|
{ |
296
|
|
|
$key = $this->getName() . '.' . $this->getValidationRuleName($rule); |
297
|
|
|
|
298
|
|
|
$this->uploadValidationMessages[$key] = $message; |
299
|
|
|
|
300
|
|
|
return $this; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
protected function getUploadValidationRules() |
304
|
|
|
{ |
305
|
|
|
$rules = array_merge( |
306
|
|
|
$this->getDefaultUploadValidationRules(), |
307
|
|
|
$this->uploadValidationRules |
308
|
|
|
); |
309
|
|
|
|
310
|
|
|
return [$this->getName() => array_values($rules)]; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Get default validation rules |
315
|
|
|
* |
316
|
|
|
* @return array |
317
|
|
|
*/ |
318
|
|
|
protected function getDefaultUploadValidationRules() |
319
|
|
|
{ |
320
|
|
|
return [ |
321
|
|
|
'bail' => 'bail', |
322
|
|
|
'file' => 'file', |
323
|
|
|
'mimes' => 'mimes:' . $this->getDefaultExtensions(), |
324
|
|
|
'max' => 'max:' . $this->getDefaultMaxFileSize(), |
325
|
|
|
]; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Get validation messages |
330
|
|
|
* |
331
|
|
|
* @return array |
332
|
|
|
*/ |
333
|
|
|
protected function getUploadValidationMessages() |
334
|
|
|
{ |
335
|
|
|
return $this->uploadValidationMessages; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Get validation custom attributes |
340
|
|
|
* |
341
|
|
|
* @return array |
342
|
|
|
*/ |
343
|
|
|
protected function getUploadValidationTitles() |
344
|
|
|
{ |
345
|
|
|
return $this->getValidationTitles(); |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.