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\UploadStorageTrait; |
8
|
|
|
use Validator; |
9
|
|
|
|
10
|
|
|
abstract class BaseFile extends NamedElement |
11
|
|
|
{ |
12
|
|
|
use UploadStorageTrait; |
13
|
|
|
|
14
|
|
|
protected $actionUrl; |
15
|
|
|
|
16
|
|
|
protected $withCredentials = false; |
17
|
|
|
|
18
|
|
|
protected $maxFileSize; |
19
|
|
|
|
20
|
|
|
protected $fileExtensions; |
21
|
|
|
|
22
|
|
|
protected $uploadValidationRules = []; |
23
|
|
|
|
24
|
|
|
protected $uploadValidationMessages = []; |
25
|
|
|
|
26
|
|
|
abstract protected function getDefaultExtensions(); |
27
|
|
|
|
28
|
|
|
public function getActionUrl() |
29
|
|
|
{ |
30
|
|
|
if ($this->actionUrl) { |
31
|
|
|
return $this->actionUrl; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$params = [ |
35
|
|
|
'model' => Admin::component()->getName(), |
36
|
|
|
'field' => $this->getName(), |
37
|
|
|
]; |
38
|
|
|
$params['id'] = null; |
39
|
|
|
if ($this->getModel()->exists) { |
40
|
|
|
$params['id'] = $this->getModel()->getKey(); |
41
|
|
|
} |
42
|
|
|
$params['_token'] = csrf_token(); |
43
|
|
|
|
44
|
|
|
return route('admin.model.upload.file', $params); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function setActionUrl($value) |
48
|
|
|
{ |
49
|
|
|
$this->actionUrl = $value; |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Indicates whether or not cross-site Access-Control requests |
56
|
|
|
* should be made using credentials |
57
|
|
|
* |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function withCredentials() |
61
|
|
|
{ |
62
|
|
|
$this->withCredentials = true; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The maximum size of an uploaded file in kilobytes |
69
|
|
|
* |
70
|
|
|
* @return int |
71
|
|
|
*/ |
72
|
|
|
public function getMaxFileSize() |
73
|
|
|
{ |
74
|
|
|
if ($this->maxFileSize) { |
75
|
|
|
return $this->maxFileSize; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this->getDefaultMaxFileSize(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function getDefaultMaxFileSize() |
82
|
|
|
{ |
83
|
|
|
return UploadedFile::getMaxFilesize() / 1024; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* The maximum size allowed for an uploaded file in kilobytes |
88
|
|
|
* |
89
|
|
|
* @param int $value |
90
|
|
|
* |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
|
|
public function setMaxFileSize(int $value) |
94
|
|
|
{ |
95
|
|
|
$this->maxFileSize = $value; |
96
|
|
|
|
97
|
|
|
$this->addValidationRule('max:' . $this->maxFileSize); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getFileExtensions() |
103
|
|
|
{ |
104
|
|
|
if ($this->fileExtensions) { |
105
|
|
|
return $this->fileExtensions; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->getDefaultExtensions(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* A list of allowable extensions that can be uploaded. |
113
|
|
|
* |
114
|
|
|
* @param string $value |
115
|
|
|
* |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
|
|
public function setFileExtensions(string $value) |
119
|
|
|
{ |
120
|
|
|
$this->fileExtensions = $value; |
121
|
|
|
|
122
|
|
|
$this->addValidationRule('mimes:' . $value); |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function toArray() |
128
|
|
|
{ |
129
|
|
|
return parent::toArray() + [ |
130
|
|
|
'action' => $this->getActionUrl(), |
131
|
|
|
'maxFileSize' => $this->getMaxFileSize(), |
132
|
|
|
'fileExtensions' => $this->getFileExtensions(), |
133
|
|
|
]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Save file to storage |
138
|
|
|
* |
139
|
|
|
* @param \Illuminate\Http\UploadedFile $file |
140
|
|
|
* |
141
|
|
|
* @return array |
142
|
|
|
* @throws \Illuminate\Validation\ValidationException |
143
|
|
|
*/ |
144
|
|
|
public function saveFile(UploadedFile $file) |
145
|
|
|
{ |
146
|
|
|
Validator::validate( |
147
|
|
|
[$this->getName() => $file], |
148
|
|
|
$this->getUploadValidationRules(), |
149
|
|
|
$this->getUploadValidationMessages(), |
150
|
|
|
$this->getUploadValidationTitles() |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
$path = $file->storeAs( |
154
|
|
|
$this->getUploadPath($file), |
155
|
|
|
$this->getUploadFileName($file), |
156
|
|
|
$this->getDisk() |
157
|
|
|
); |
158
|
|
|
|
159
|
|
|
return $this->getFileInfo($path); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get file info(name,path,url) |
164
|
|
|
* |
165
|
|
|
* @param $path |
166
|
|
|
* |
167
|
|
|
* @return array |
168
|
|
|
*/ |
169
|
|
|
protected function getFileInfo($path) |
170
|
|
|
{ |
171
|
|
|
return [ |
172
|
|
|
'name' => substr($path, strrpos($path, '/') + 1), |
173
|
|
|
'path' => $path, |
174
|
|
|
'url' => $this->getFileUrl($path), |
175
|
|
|
]; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function addValidationRule($rule, $message = null) |
179
|
|
|
{ |
180
|
|
|
$uploadRules = [ |
181
|
|
|
'image', |
182
|
|
|
'mimes', |
183
|
|
|
'mimetypes', |
184
|
|
|
'size', |
185
|
|
|
'dimensions', |
186
|
|
|
'max', |
187
|
|
|
'min', |
188
|
|
|
'between', |
189
|
|
|
]; |
190
|
|
|
|
191
|
|
|
if (in_array($this->getValidationRuleName($rule), $uploadRules)) { |
192
|
|
|
return $this->addUploadValidationRule($rule, $message); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return parent::addValidationRule($rule, $message); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
View Code Duplication |
protected function addUploadValidationRule($rule, $message = null) |
|
|
|
|
199
|
|
|
{ |
200
|
|
|
$this->uploadValidationRules[$this->getValidationRuleName($rule)] = $rule; |
201
|
|
|
|
202
|
|
|
if (is_null($message)) { |
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $this->addUploadValidationMessage($rule, $message); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
protected function addUploadValidationMessage($rule, $message) |
210
|
|
|
{ |
211
|
|
|
$key = $this->getName() . '.' . $this->getValidationRuleName($rule); |
212
|
|
|
|
213
|
|
|
$this->uploadValidationMessages[$key] = $message; |
214
|
|
|
|
215
|
|
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
protected function getUploadValidationRules() |
219
|
|
|
{ |
220
|
|
|
$rules = array_merge( |
221
|
|
|
$this->getDefaultUploadValidationRules(), |
222
|
|
|
$this->uploadValidationRules |
223
|
|
|
); |
224
|
|
|
|
225
|
|
|
return [$this->getName() => array_values($rules)]; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Get default validation rules |
230
|
|
|
* |
231
|
|
|
* @return array |
232
|
|
|
*/ |
233
|
|
|
protected function getDefaultUploadValidationRules() |
234
|
|
|
{ |
235
|
|
|
return [ |
236
|
|
|
'bail' => 'bail', |
237
|
|
|
'file' => 'file', |
238
|
|
|
'mimes' => 'mimes:' . $this->getDefaultExtensions(), |
239
|
|
|
'max' => 'max:' . $this->getDefaultMaxFileSize(), |
240
|
|
|
]; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Get validation messages |
245
|
|
|
* |
246
|
|
|
* @return array |
247
|
|
|
*/ |
248
|
|
|
protected function getUploadValidationMessages() |
249
|
|
|
{ |
250
|
|
|
return $this->uploadValidationMessages; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Get validation custom attributes |
255
|
|
|
* |
256
|
|
|
* @return array |
257
|
|
|
*/ |
258
|
|
|
protected function getUploadValidationTitles() |
259
|
|
|
{ |
260
|
|
|
return $this->getValidationTitles(); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.