Completed
Push — master ( 82f5c2...aef2ab )
by wen
02:50
created

File::getDefaultMaxFileSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Sco\Admin\Form\Elements;
4
5
use Illuminate\Http\UploadedFile;
6
use Illuminate\Support\Str;
7
use Storage;
8
use Validator;
9
10
class File extends NamedElement
11
{
12
    protected $type = 'file';
13
14
    protected $actionUrl;
15
16
    protected $multiSelect = false;
17
18
    protected $multiFile = true;
19
20
    protected $showFileList = true;
21
22
    protected $withCredentials = false;
23
24
    protected $maxFileSize;
25
26
    protected $fileUploadsLimit = 0;
27
28
    protected $fileExtensions;
29
30
    protected $listType = 'text';
31
32
    protected $disk;
33
34
    /**
35
     * @var string|\Closure|null
36
     */
37
    protected $uploadPath;
38
39
    /**
40
     * @var \Closure|null
41
     */
42
    protected $uploadFileNameRule;
43
44
    protected $uploadValidationRules = [];
45
46
    protected $uploadValidationMessages = [];
47
48
    public function getValue()
49
    {
50
        $value = parent::getValue();
51
        if (empty($value)) {
52
            return [];
53
        }
54
        return collect(explode(',', $value))->filter(function ($item) {
55
            return $this->existsFile($item);
56
        })->map(function ($item) {
57
            return $this->getFileInfo($item);
58
        });
59
    }
60
61
    public function getActionUrl()
62
    {
63
        if ($this->actionUrl) {
64
            return $this->actionUrl;
65
        }
66
        $model = app('admin.components')->get(get_class($this->getModel()));
67
68
        $params       = [
69
            'model' => $model->getName(),
70
            'field' => $this->getName(),
71
        ];
72
        $params['id'] = null;
73
        if ($this->getModel()->exists) {
74
            $params['id'] = $this->getModel()->getKey();
75
        }
76
        $params['_token'] = csrf_token();
77
78
        return route('admin.model.upload.file', $params);
79
    }
80
81
    public function setActionUrl($value)
82
    {
83
        $this->actionUrl = $value;
84
85
        return $this;
86
    }
87
88
    public function isMultiSelect()
89
    {
90
        return $this->multiSelect;
91
    }
92
93
    public function enableMultiSelect()
94
    {
95
        $this->multiSelect = true;
96
97
        return $this;
98
    }
99
100
    public function isShowFileList()
101
    {
102
        return $this->showFileList;
103
    }
104
105
    /**
106
     * Disable file list
107
     *
108
     * @return $this
109
     */
110
    public function disableFileList()
111
    {
112
        $this->showFileList = false;
113
114
        return $this;
115
    }
116
117
    /**
118
     * Indicates whether or not cross-site Access-Control requests
119
     * should be made using credentials
120
     *
121
     * @return $this
122
     */
123
    public function withCredentials()
124
    {
125
        $this->withCredentials = true;
126
127
        return $this;
128
    }
129
130
    /**
131
     * The maximum size of an uploaded file in kilobytes
132
     *
133
     * @return int
134
     */
135
    public function getMaxFileSize()
136
    {
137
        if ($this->maxFileSize) {
138
            return $this->maxFileSize;
139
        }
140
141
        return $this->getDefaultMaxFileSize();
142
    }
143
144
    protected function getDefaultMaxFileSize()
145
    {
146
        return UploadedFile::getMaxFilesize() / 1024;
147
    }
148
149
    /**
150
     * The maximum size allowed for an uploaded file in kilobytes
151
     *
152
     * @param int $value
153
     *
154
     * @return $this
155
     */
156
    public function setMaxFileSize($value)
157
    {
158
        $this->maxFileSize = intval($value);
159
160
        $this->addValidationRule('max:' . $this->maxFileSize);
161
162
        return $this;
163
    }
164
165
    public function getFileExtensions()
166
    {
167
        if ($this->fileExtensions) {
168
            return $this->fileExtensions;
169
        }
170
        return $this->getDefaultExtensions();
171
    }
172
173
    /**
174
     * A list of allowable extensions that can be uploaded.
175
     *
176
     * @param string $value
177
     *
178
     * @return $this
179
     */
180
    public function setFileExtensions($value)
181
    {
182
        $this->fileExtensions = $value;
183
184
        $this->addValidationRule('mimes:' . $value);
185
186
        return $this;
187
    }
188
189
    protected function getDefaultExtensions()
190
    {
191
        return config('admin.upload.extensions.file');
192
    }
193
194
    public function getFileUploadsLimit()
195
    {
196
        return $this->fileUploadsLimit;
197
    }
198
199
    /**
200
     * The maximum number of files that can be uploaded.
201
     *
202
     * @param int $value
203
     *
204
     * @return $this
205
     */
206
    public function setFileUploadsLimit($value)
207
    {
208
        $this->fileUploadsLimit = intval($value);
209
210
        return $this;
211
    }
212
213
    public function getListType()
214
    {
215
        return $this->listType;
216
    }
217
218
    public function toArray()
219
    {
220
        return parent::toArray() + [
221
                'action'           => $this->getActionUrl(),
222
                'showFileList'     => $this->isShowFileList(),
223
                'multiSelect'      => $this->isMultiSelect(),
224
                'maxFileSize'      => $this->getMaxFileSize(),
225
                'fileUploadsLimit' => $this->getFileUploadsLimit(),
226
                'fileExtensions'   => $this->getFileExtensions(),
227
                'listType'         => $this->getListType(),
228
            ];
229
    }
230
231
    public function getDisk()
232
    {
233
        if ($this->disk) {
234
            return $this->disk;
235
        }
236
237
        return config('admin.upload.disk', 'public');
238
    }
239
240
    public function setDisk($value)
241
    {
242
        $this->disk = $value;
243
244
        return $this;
245
    }
246
247
    protected function getDefaultUploadPath(UploadedFile $file)
0 ignored issues
show
Unused Code introduced by
The parameter $file is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
248
    {
249
        return config('admin.upload.directory', 'admin/uploads');
250
    }
251
252
    public function getUploadPath(UploadedFile $file)
253
    {
254
        if (!($path = $this->uploadPath)) {
255
            $path = $this->getDefaultUploadPath($file);
256
        }
257
        if (is_callable($path)) {
258
            return call_user_func($path, $file);
259
        }
260
261
        return $path;
262
    }
263
264
    /**
265
     * The path of file save
266
     *
267
     * @param string|\Closure $value
268
     *
269
     * @return $this
270
     */
271
    public function setUploadPath($value)
272
    {
273
        $this->uploadPath = $value;
274
275
        return $this;
276
    }
277
278
    /**
279
     * Get a filename for the upload file.
280
     *
281
     * @param \Illuminate\Http\UploadedFile $file
282
     *
283
     * @return mixed|string
284
     */
285
    public function getUploadFileName(UploadedFile $file)
286
    {
287
        if (is_callable($this->uploadFileNameRule)) {
288
            return call_user_func($this->uploadFileNameRule, $file);
289
        }
290
291
        return $this->getDefaultFileName($file);
292
    }
293
294
    protected function getDefaultFileName(UploadedFile $file)
295
    {
296
        return $file->hashName();
297
    }
298
299
    /**
300
     * Set the generation rule for the filename of the uploaded file
301
     *
302
     * @param \Closure $value
303
     *
304
     * @return $this
305
     */
306
    public function setUploadFileNameRule(\Closure $value)
307
    {
308
        $this->uploadFileNameRule = $value;
309
310
        return $this;
311
    }
312
313
    public function saveFile(UploadedFile $file)
314
    {
315
        Validator::validate(
316
            [$this->getName() => $file],
317
            $this->getUploadValidationRules(),
318
            $this->getUploadValidationMessages(),
319
            $this->getUploadValidationTitles()
320
        );
321
322
        $path = $file->storeAs(
323
            $this->getUploadPath($file),
324
            $this->getUploadFileName($file),
325
            $this->getDisk()
326
        );
327
328
        return $this->getFileInfo($path);
329
    }
330
331
    protected function prepareValue($value)
332
    {
333
        if (empty($value) || !is_array($value)) {
334
            return '';
335
        }
336
        return collect($value)->implode('path', ',');
337
    }
338
339
    protected function existsFile($path)
340
    {
341
        return Storage::disk($this->getDisk())->exists($path);
342
    }
343
344
    protected function getFileUrl($path)
345
    {
346
        return Storage::disk($this->getDisk())->url($path);
347
    }
348
349
    protected function getFileInfo($path)
350
    {
351
        return [
352
            'name' => substr($path, strrpos($path, '/') + 1),
353
            'path' => $path,
354
            'url'  => $this->getFileUrl($path),
355
        ];
356
    }
357
358
    public function addValidationRule($rule, $message = null)
359
    {
360
        $uploadRules = [
361
            'image', 'mimes', 'mimetypes', 'size',
362
            'dimensions', 'max', 'min', 'between'
363
        ];
364
365
        if (in_array($this->getValidationRuleName($rule), $uploadRules)) {
366
            return $this->addUploadValidationRule($rule, $message);
367
        }
368
369
        return parent::addValidationRule($rule, $message);
370
    }
371
372 View Code Duplication
    protected function addUploadValidationRule($rule, $message = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
373
    {
374
        $this->uploadValidationRules[$this->getValidationRuleName($rule)] = $rule;
375
376
        if (is_null($message)) {
377
            return $this;
378
        }
379
        return $this->addUploadValidationMessage($rule, $message);
380
    }
381
382
    protected function addUploadValidationMessage($rule, $message)
383
    {
384
        $key = $this->getName() . '.' . $this->getValidationRuleName($rule);
385
386
        $this->uploadValidationMessages[$key] = $message;
387
388
        return $this;
389
    }
390
391
    protected function getUploadValidationRules()
392
    {
393
        $rules = array_merge(
394
            $this->getDefaultUploadValidationRules(),
395
            $this->uploadValidationRules
396
        );
397
398
        return [$this->getName() => array_values($rules)];
399
    }
400
401
    protected function getDefaultUploadValidationRules()
402
    {
403
        return [
404
            'bail'  => 'bail',
405
            'file'  => 'file',
406
            'mimes' => 'mimes:' . $this->getDefaultExtensions(),
407
            'max'   => 'max:' . $this->getDefaultMaxFileSize(),
408
        ];
409
    }
410
411
    protected function getUploadValidationMessages()
412
    {
413
        return $this->uploadValidationMessages;
414
    }
415
416
    protected function getUploadValidationTitles()
417
    {
418
        return $this->getValidationTitles();
419
    }
420
}
421