Passed
Push — master ( 68835c...74e0f6 )
by Jianhua
05:14
created

NEditorController::isValidVideo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Date: 2019/3/16 Time: 13:42
4
 *
5
 * @author  Eddy <[email protected]>
6
 * @version v1.0.0
7
 */
8
9
namespace App\Http\Controllers\Admin;
10
11
use App\Http\Controllers\Controller;
12
use Illuminate\Http\Request;
13
use Illuminate\Http\UploadedFile;
14
use Illuminate\Support\Facades\Storage;
15
16
class NEditorController extends Controller
17
{
18
    /**
19
     * 基础功能-图片上传
20
     *
21
     * @param Request $request
22
     * @param string $type
23
     * @return array
24
     */
25
    public function serve(Request $request, $type)
26
    {
27
        if (!method_exists(self::class, $type)) {
28
            return [
29
                'code' => 1,
30
                'msg' => '未知操作'
31
            ];
32
        }
33
34
        return call_user_func(self::class . '::' . $type, $request);
35
    }
36
37
    protected function uploadImage(Request $request)
38
    {
39
        if (config('light.image_upload.driver') !== 'local') {
40
            $class = config('light.image_upload.class');
41
            return call_user_func([new $class, 'uploadImage'], $request);
42
        }
43
44
        if (!$request->hasFile('file')) {
45
            return [
46
                'code' => 2,
47
                'msg' => '非法请求'
48
            ];
49
        }
50
        $file = $request->file('file');
51
        if (!$this->isValidImage($file)) {
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type Illuminate\Http\UploadedFile[] and array and null; however, parameter $file of App\Http\Controllers\Adm...troller::isValidImage() does only seem to accept Illuminate\Http\UploadedFile, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
        if (!$this->isValidImage(/** @scrutinizer ignore-type */ $file)) {
Loading history...
52
            return [
53
                'code' => 3,
54
                'msg' => '文件不合要求'
55
            ];
56
        }
57
58
        $result = $file->store(date('Ym'), config('light.neditor.disk'));
59
        if (!$result) {
60
            return [
61
                'code' => 3,
62
                'msg' => '上传失败'
63
            ];
64
        }
65
66
        return [
67
            'code' => 200,
68
            'state' => 'SUCCESS', // 兼容ueditor
69
            'msg' => '',
70
            'url' => Storage::disk(config('light.neditor.disk'))->url($result),
71
        ];
72
    }
73
74
    public function catchImage(Request $request)
75
    {
76
        if (config('light.image_upload.driver') !== 'local') {
77
            $class = config('light.image_upload.class');
78
            return call_user_func([new $class, 'catchImage'], $request);
79
        }
80
81
        $files = (array) $request->post('file');
82
        $urls = [];
83
        foreach ($files as $v) {
84
            $extention = pathinfo(parse_url($v, PHP_URL_PATH), PATHINFO_EXTENSION);
85
            $path = date('Ym') . '/' . md5($v) . '.' . ($extention == '' ? 'jpg' : $extention);
86
            Storage::disk(config('light.neditor.disk'))
87
                ->put($path, file_get_contents($v));
88
            $urls[] = [
89
                'url' => Storage::disk(config('light.neditor.disk'))->url($path),
90
                'source' => $v,
91
                'state' => 'SUCCESS'
92
            ];
93
        }
94
95
        return [
96
           'list' => $urls
97
        ];
98
    }
99
100
    public function uploadVideo(Request $request)
101
    {
102
        if (config('light.image_upload.driver') !== 'local') {
103
            $class = config('light.image_upload.class');
104
            return call_user_func([new $class, 'uploadImage'], $request);
105
        }
106
107
        if (!$request->hasFile('file')) {
108
            return [
109
                'code' => 2,
110
                'msg' => '非法请求'
111
            ];
112
        }
113
        $file = $request->file('file');
114
        if (!$this->isValidVideo($file)) {
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type Illuminate\Http\UploadedFile[] and array and null; however, parameter $file of App\Http\Controllers\Adm...troller::isValidVideo() does only seem to accept Illuminate\Http\UploadedFile, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

114
        if (!$this->isValidVideo(/** @scrutinizer ignore-type */ $file)) {
Loading history...
115
            return [
116
                'code' => 3,
117
                'msg' => '文件不合要求'
118
            ];
119
        }
120
121
        $result = $file->store('video/' . date('Ym'), config('light.neditor.disk'));
122
        if (!$result) {
123
            return [
124
                'code' => 3,
125
                'msg' => '上传失败'
126
            ];
127
        }
128
129
        return [
130
            'code' => 200,
131
            'state' => 'SUCCESS', // 兼容ueditor
132
            'msg' => '',
133
            'url' => Storage::disk(config('light.neditor.disk'))->url($result),
134
        ];
135
    }
136
137
    public function uploadFile(Request $request)
138
    {
139
        if (config('light.image_upload.driver') !== 'local') {
140
            $class = config('light.image_upload.class');
141
            return call_user_func([new $class, 'uploadImage'], $request);
142
        }
143
144
        if (!$request->hasFile('file')) {
145
            return [
146
                'code' => 2,
147
                'msg' => '非法请求'
148
            ];
149
        }
150
        $file = $request->file('file');
151
        if (!$this->isValidFile($file)) {
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type Illuminate\Http\UploadedFile[] and array and null; however, parameter $file of App\Http\Controllers\Adm...ntroller::isValidFile() does only seem to accept Illuminate\Http\UploadedFile, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

151
        if (!$this->isValidFile(/** @scrutinizer ignore-type */ $file)) {
Loading history...
152
            return [
153
                'code' => 3,
154
                'msg' => '文件不合要求'
155
            ];
156
        }
157
158
        $result = $file->store('file/' . date('Ym'), config('light.neditor.disk'));
159
        if (!$result) {
160
            return [
161
                'code' => 3,
162
                'msg' => '上传失败'
163
            ];
164
        }
165
166
        return [
167
            'code' => 200,
168
            'state' => 'SUCCESS', // 兼容ueditor
169
            'msg' => '',
170
            'url' => Storage::disk(config('light.neditor.disk'))->url($result),
171
        ];
172
    }
173
174
    protected function isValidImage(UploadedFile $file)
175
    {
176
        $c = config('light.neditor.upload');
177
        $config = [
178
            'maxSize' => $c['imageMaxSize'],
179
            'AllowFiles' => $c['imageAllowFiles'],
180
        ];
181
182
        return $this->isValidUploadedFile($file, $config);
183
    }
184
185
    protected function isValidVideo(UploadedFile $file)
186
    {
187
        $c = config('light.neditor.upload');
188
        $config = [
189
            'maxSize' => $c['videoMaxSize'],
190
            'AllowFiles' => $c['videoAllowFiles'],
191
        ];
192
193
        return $this->isValidUploadedFile($file, $config);
194
    }
195
196
    protected function isValidFile(UploadedFile $file)
197
    {
198
        $c = config('light.neditor.upload');
199
        $config = [
200
            'maxSize' => $c['fileMaxSize'],
201
            'AllowFiles' => $c['fileAllowFiles'],
202
        ];
203
204
        return $this->isValidUploadedFile($file, $config);
205
    }
206
207
    protected function isValidUploadedFile(UploadedFile $file, array $config)
208
    {
209
        if (!$file->isValid() ||
210
            $file->getSize() > $config['maxSize'] ||
211
            !in_array(
212
                '.' . strtolower($file->getClientOriginalExtension()),
213
                $config['AllowFiles']
214
            ) ||
215
            !in_array(
216
                '.' . strtolower($file->guessExtension()),
217
                $config['AllowFiles']
218
            )
219
        ) {
220
            return false;
221
        }
222
223
        return true;
224
    }
225
}
226