Completed
Push — master ( 0380c2...02776d )
by wen
56:13
created

File::getMaxFileSize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
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 Illuminate\Support\Str;
7
use Storage;
8
9
class File extends NamedElement
10
{
11
    protected $type = 'file';
12
13
    protected $actionUrl;
14
15
    protected $multiSelect = false;
16
17
    protected $multiFile = true;
18
19
    protected $showFileList = true;
20
21
    protected $withCredentials = false;
22
23
    protected $maxFileSize;
24
25
    protected $fileUploadsLimit = 0;
26
27
    protected $fileExtensions;
28
29
    protected $listType = 'text';
30
31
    protected $disk;
32
33
    /**
34
     * @var string|\Closure|null
35
     */
36
    protected $uploadPath;
37
38
    /**
39
     * @var \Closure|null
40
     */
41
    protected $uploadFileNameRule;
42
43
    public function getValue()
44
    {
45
        $value = parent::getValue();
46
        if (empty($value)) {
47
            return [];
48
        }
49
        return collect(explode(',', $value))->filter(function ($item) {
50
            return $this->existsFile($item);
51
        })->map(function ($item) {
52
            return [
53
                'path' => $item,
54
                'url' => $this->getFileUrl($item),
55
            ];
56
        });
57
    }
58
59
    public function getActionUrl()
60
    {
61
        if ($this->actionUrl) {
62
            return $this->actionUrl;
63
        }
64
        $model = app('admin.components')->get(get_class($this->getModel()));
65
66
        $params       = [
67
            'model' => $model->getName(),
68
            'field' => $this->getName(),
69
        ];
70
        $params['id'] = null;
71
        if ($this->getModel()->exists) {
72
            $params['id'] = $this->getModel()->getKey();
73
        }
74
        $params['_token'] = csrf_token();
75
76
        return route('admin.model.upload.file', $params);
77
    }
78
79
    public function setActionUrl($value)
80
    {
81
        $this->actionUrl = $value;
82
83
        return $this;
84
    }
85
86
    public function isMultiSelect()
87
    {
88
        return $this->multiSelect;
89
    }
90
91
    public function enableMultiSelect()
92
    {
93
        $this->multiSelect = true;
94
95
        return $this;
96
    }
97
98
    /**
99
     * Show file list
100
     *
101
     * @return $this
102
     */
103
    public function disableFileList()
104
    {
105
        $this->showFileList = false;
106
107
        return $this;
108
    }
109
110
    /**
111
     * Indicates whether or not cross-site Access-Control requests
112
     * should be made using credentials
113
     *
114
     * @return $this
115
     */
116
    public function withCredentials()
117
    {
118
        $this->withCredentials = true;
119
120
        return $this;
121
    }
122
123
    /**
124
     * The maximum size of an uploaded file in bytes
125
     * If didn't set maximum size, return maximum size as configured in php.ini.
126
     *
127
     * @return int
128
     */
129
    public function getMaxFileSize()
130
    {
131
        if ($this->maxFileSize) {
132
            return $this->maxFileSize;
133
        }
134
        return UploadedFile::getMaxFilesize();
135
    }
136
137
    /**
138
     * The maximum size allowed for an uploaded file in bytes
139
     *
140
     * @param int $value
141
     *
142
     * @return $this
143
     */
144
    public function setMaxFileSize($value)
145
    {
146
        $this->maxFileSize = intval($value);
147
148
        return $this;
149
    }
150
151
    public function getFileExtensions()
152
    {
153
        if ($this->fileExtensions) {
154
            return $this->fileExtensions;
155
        }
156
157
        return $this->getDefaultExtensions();
158
    }
159
160
    /**
161
     * A list of allowable extensions that can be uploaded.
162
     *
163
     * @param string $value
164
     *
165
     * @return $this
166
     */
167
    public function setFileExtensions($value)
168
    {
169
        $this->fileExtensions = $value;
170
171
        return $this;
172
    }
173
174
    protected function getDefaultExtensions()
175
    {
176
        return config('admin.upload.extensions.file');
177
    }
178
179
    public function getFileUploadsLimit()
180
    {
181
        return $this->fileUploadsLimit;
182
    }
183
184
    /**
185
     * The maximum number of files that can be uploaded.
186
     *
187
     * @param int $value
188
     *
189
     * @return $this
190
     */
191
    public function setFileUploadsLimit($value)
192
    {
193
        $this->fileUploadsLimit = intval($value);
194
195
        return $this;
196
    }
197
198
    public function getListType()
199
    {
200
        return $this->listType;
201
    }
202
203
    public function toArray()
204
    {
205
        return parent::toArray() + [
206
                'action'           => $this->getActionUrl(),
207
                'showFileList'     => $this->showFileList,
208
                'multiSelect'      => $this->isMultiSelect(),
209
                'maxFileSize'      => $this->getMaxFileSize(),
210
                'fileUploadsLimit' => $this->getFileUploadsLimit(),
211
                'fileExtensions'   => $this->getFileExtensions(),
212
                'listType'         => $this->getListType(),
213
            ];
214
    }
215
216
    public function getDisk()
217
    {
218
        if ($this->disk) {
219
            return $this->disk;
220
        }
221
222
        return config('admin.upload.disk', 'public');
223
    }
224
225
    public function setDisk($value)
226
    {
227
        $this->disk = $value;
228
229
        return $this;
230
    }
231
232
    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...
233
    {
234
        return config('admin.upload.directory', 'admin/uploads');
235
    }
236
237
    public function getUploadPath(UploadedFile $file)
238
    {
239
        if (!($path = $this->uploadPath)) {
240
            $path = $this->getDefaultUploadPath($file);
241
        }
242
        if (is_callable($path)) {
243
            return call_user_func($path, $file);
244
        }
245
246
        return $path;
247
    }
248
249
    /**
250
     * The path of file save
251
     *
252
     * @param string|\Closure $value
253
     *
254
     * @return $this
255
     */
256
    public function setUploadPath($value)
257
    {
258
        $this->uploadPath = $value;
259
260
        return $this;
261
    }
262
263
    public function getUploadFileName(UploadedFile $file)
264
    {
265
        if (is_callable($this->uploadFileNameRule)) {
266
            return call_user_func($this->uploadFileNameRule, $file);
267
        }
268
269
        return $this->getDefaultFileName($file);
270
    }
271
272
    protected function getDefaultFileName(UploadedFile $file)
273
    {
274
        $hash = Str::random(40);
275
        return $hash . '.' . $file->guessExtension();
276
    }
277
278
    public function setUploadFileNameRule(\Closure $value)
279
    {
280
        $this->uploadFileNameRule = $value;
281
282
        return $this;
283
    }
284
285
    public function saveFile(UploadedFile $file)
286
    {
287
        $path = $file->storeAs(
288
            $this->getUploadPath($file),
289
            $this->getUploadFileName($file),
290
            $this->getDisk()
291
        );
292
293
        return [
294
            'path' => $path,
295
            'url'  => $this->getFileUrl($path),
296
        ];
297
    }
298
299
    protected function prepareValue($value)
300
    {
301
        if (empty($value) || !is_array($value)) {
302
            return '';
303
        }
304
        return collect($value)->implode('path', ',');
305
    }
306
307
    protected function existsFile($path)
308
    {
309
        return Storage::disk($this->getDisk())->exists($path);
310
    }
311
312
    protected function getFileUrl($path)
313
    {
314
        return Storage::disk($this->getDisk())->url($path);
315
    }
316
}
317