Completed
Push — master ( 48ff0c...a157cb )
by wen
03:02
created

File::getFileExts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace Sco\Admin\Form\Elements;
4
5
use Illuminate\Http\UploadedFile;
6
use Storage;
7
8
class File extends Element
9
{
10
    protected $type = 'file';
11
12
    protected $actionUrl;
13
14
    protected $multiSelect = false;
15
16
    protected $multiFile = true;
17
18
    protected $showFileList = true;
19
20
    protected $withCredentials = false;
21
22
    protected $fileSizeLimit = 0;
23
24
    protected $fileUploadsLimit = 0;
25
26
    protected $fileExtensions;
27
28
    protected $listType = 'text';
29
30
    protected $disk;
31
32
    protected $uploadPath;
33
34
    public function getValue()
35
    {
36
        $value = parent::getValue();
37
        if (empty($value)) {
38
            return [];
39
        }
40
        if ($this->isMultiFile()) {
41
            return collect(explode(',', $value))->map(function ($item) {
42
                return $this->getFileUrl($item);
43
            });
44
        }
45
46
        return [
47
            $this->getFileUrl($value),
48
        ];
49
    }
50
51
    public function getActionUrl()
52
    {
53
        if ($this->actionUrl) {
54
            return $this->actionUrl;
55
        }
56
        $model = app('admin.components')->get(get_class($this->getModel()));
57
58
        $params       = [
59
            'model' => $model->getName(),
60
            'field' => $this->getName(),
61
        ];
62
        $params['id'] = null;
63
        if ($this->getModel()->exists) {
64
            $params['id'] = $this->getModel()->getKey();
65
        }
66
        $params['_token'] = csrf_token();
67
68
        return route('admin.model.upload.file', $params);
69
    }
70
71
    public function setActionUrl($value)
72
    {
73
        $this->actionUrl = $value;
74
75
        return $this;
76
    }
77
78
    public function isMultiSelect()
79
    {
80
        return $this->multiSelect;
81
    }
82
83
    public function enableMultiSelect()
84
    {
85
        $this->multiSelect = true;
86
87
        return $this;
88
    }
89
90
    public function isMultiFile()
91
    {
92
        return $this->multiFile;
93
    }
94
95
    public function disableMultiFile()
96
    {
97
        $this->multiFile = false;
98
99
        return $this;
100
    }
101
102
    /**
103
     * Show file list
104
     *
105
     * @return $this
106
     */
107
    public function disableFileList()
108
    {
109
        $this->showFileList = false;
110
111
        return $this;
112
    }
113
114
    /**
115
     * Indicates whether or not cross-site Access-Control requests
116
     * should be made using credentials
117
     *
118
     * @return $this
119
     */
120
    public function withCredentials()
121
    {
122
        $this->withCredentials = true;
123
124
        return $this;
125
    }
126
127
    /**
128
     * The maximum size allowed for a file upload. (KB)
129
     *
130
     * @param int $value
131
     *
132
     * @return $this
133
     */
134
    public function setFileSizeLimit($value)
135
    {
136
        $this->fileSizeLimit = intval($value);
137
        return $this;
138
    }
139
140
    public function getFileExtensions()
141
    {
142
        if ($this->fileExtensions) {
143
            return $this->fileExtensions;
144
        }
145
146
        return config('admin.upload.extensions');
147
    }
148
149
    /**
150
     * A list of allowable extensions that can be uploaded.
151
     *
152
     * @param array|string $value
153
     *
154
     * @return $this
155
     */
156
    public function setFileExtensions($value)
157
    {
158
        $this->fileExtensions = is_array($value) ? $value : explode(',', $value);
159
160
        return $this;
161
    }
162
163
    public function getFileUploadsLimit()
164
    {
165
        if (!$this->isMultiFile()) {
166
            return 1;
167
        }
168
169
        return $this->fileUploadsLimit;
170
    }
171
172
    /**
173
     * The maximum number of files that can be uploaded.
174
     *
175
     * @param int $value
176
     *
177
     * @return $this
178
     */
179
    public function setFileUploadsLimit($value)
180
    {
181
        $this->fileUploadsLimit = intval($value);
182
183
        return $this;
184
    }
185
186
    public function getListType()
187
    {
188
        return $this->listType;
189
    }
190
191
    public function pictureListType()
192
    {
193
        $this->listType = 'picture';
194
195
        return $this;
196
    }
197
198
    public function pictureCardListType()
199
    {
200
        $this->listType = 'picture-card';
201
202
        return $this;
203
    }
204
205
    public function toArray()
206
    {
207
        return parent::toArray() + [
208
                'action'           => $this->getActionUrl(),
209
                'showFileList'     => $this->showFileList,
210
                'multiSelect'      => $this->isMultiSelect(),
211
                'fileSizeLimit'    => $this->fileSizeLimit,
212
                'fileUploadsLimit' => $this->getFileUploadsLimit(),
213
                'fileExtensions'   => $this->getFileExtensions(),
214
                'listType'         => $this->getListType(),
215
            ];
216
    }
217
218
    public function getDisk()
219
    {
220
        if ($this->disk) {
221
            return $this->disk;
222
        }
223
224
        return config('admin.upload.disk', 'public');
225
    }
226
227
    public function setDisk($value)
228
    {
229
        $this->disk = $value;
230
231
        return $this;
232
    }
233
234
    public function getUploadPath()
235
    {
236
        if ($this->uploadPath) {
237
            return $this->uploadPath;
238
        }
239
        return config('admin.upload.directory', 'admin/uploads');
240
    }
241
242
    public function setUploadPath($value)
243
    {
244
        $this->uploadPath = $value;
245
246
        return $this;
247
    }
248
249
    public function saveFile(UploadedFile $file)
250
    {
251
        $path = $file->store($this->getUploadPath(), $this->getDisk());
252
253
        return [
254
            'path' => $path,
255
            'url'  => $this->getFileUrl($path),
256
        ];
257
    }
258
259
    protected function prepareValue($value)
260
    {
261
        if (empty($value) && !is_array($value)) {
262
            return $value;
263
        }
264
265
        if ($this->isMultiFile()) {
266
            return implode(',', $value);
267
        } else {
268
            return $value[0];
269
        }
270
    }
271
272
    protected function getFileUrl($path)
273
    {
274
        return Storage::disk($this->getDisk())->url($path);
275
    }
276
}
277