|
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)) { |
|
|
|
|
|
|
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
|
|
|
protected function isValidImage(UploadedFile $file) |
|
101
|
|
|
{ |
|
102
|
|
|
if (!$file->isValid() || |
|
103
|
|
|
$file->getSize() > config('light.neditor.upload.imageMaxSize') || |
|
104
|
|
|
!in_array( |
|
105
|
|
|
'.' . strtolower($file->getClientOriginalExtension()), |
|
106
|
|
|
config('light.neditor.upload.imageAllowFiles') |
|
107
|
|
|
) |
|
108
|
|
|
) { |
|
109
|
|
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return true; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|