UploadController.php
12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 上传基类:
+----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Http\Controllers\Service
* @package App\Http\Controllers\Service
* @author Richer <yangzi1028@163.com>
* @date 2020年11月13日 01:26:09
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Http\Controllers\Service;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;
use Ramsey\Uuid\Uuid;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\File;
// 引入鉴权类
use Qiniu\Auth;
// 引入上传类
use Qiniu\Storage\UploadManager;
/**
* Class UploadController
*
* @category App\Http\Controllers\Service
* @package App\Http\Controllers\Service
* @author Richer <yangzi1028@163.com>
* @date 2020年11月13日 01:26:09
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
class UploadController extends Controller
{
protected $file_size_max = 10 * 1024 * 1024;// 上传文件最大为10M
protected $message = 'success';
/**
* 上传
*
* @param Request $request
* @param string $field
* @param string $filetype
*
* @return array
* @throws \Exception
*/
public function upload($request, $field = 'file', $filetype = 'image')
{
Log::info("请求");
Log::info($request->file($field));
Log::info($request->ceshi);
//文件获取:取特定的参数名,可以改为配置参数
$files = $request->file($field) ?? $request->ceshi;
//无上传文件抛出错误信息
if (empty($files)) {
return [
'code' => Response::HTTP_BAD_REQUEST,
'data' => null,
'msg' => '文件不存在!'
];
}
// 验证文件是否为正确的文件类型类型
if (!$verify = $this->verifyFile($files, $filetype)) {
return [
'code' => Response::HTTP_UNPROCESSABLE_ENTITY,
'data' => null,
'msg' => $this->message,
];
}
// 上传文件到七牛云:可以根据系统配置设置上传到七牛云还是本地
if (!$return = $this->uploadToLocalServer($files, $filetype)) {
return [
'code' => Response::HTTP_BAD_REQUEST,
'data' => null,
'msg' => $this->message,
];
}
// 上传成功,返回文件信息和路径
return ['code' => 0, 'data' => $return, 'msg' => 'success'];
}
/**
* 检测上传的文件是否有效的文件类型
*
* @param $file
* @param string $filetype
* @return bool
*/
public function verifyFile($file, $filetype = 'image')
{
if (is_array($file)) {
foreach ($file as $key => $vo) {
if (is_array($vo)) {
return $this->verifyFile($vo, $filetype);
} else {
if (!$isValid = $vo->isValid()) {
$this->message ='第'.($key+1) .'个文件无效!';
return false;
}
}
// 判断文件类型
if (!$this->verifyFileType($vo, $key, $filetype)) {
return false;
}
if (!$this->verifyFileSize($vo, $key)) {
return false;
}
// return $this->verifyFile($vo, $filetype);
}
} else {
if (!$isValid = $file->isValid()) {
$this->message ='文件无效!';
return false;
}
// 判断文件类型
if (!$this->verifyFileType($file, 0, $filetype)) {
return false;
}
// 判断文件大小
if (!$this->verifyFileSize($file)) {
return false;
}
}
return true;
}
/**
* 验证文件大小是否正确
*
* @param $file
* @param int $index
* @param string $filetype
*
* @return bool
*/
public function verifyFileType($file, $index = 0, $filetype = 'image')
{
// 如果是全部的文件类型,则不进行类型的判断
if ($filetype === 'all') {
return true;
}
$fileMimeType = 'image/';
// 上传文件的类型
$mimeType = $file->getClientMimeType();
// 上传文件的原始文件名
$originalName = $file->getClientOriginalName();
switch ($filetype) {
case 'image':
if (!Str::startsWith($mimeType, $fileMimeType) && !is_image($originalName)) {
$this->message ='第'.($index+1) .'个文件不是有效的图片文件!';
return false;
}
break;
case 'video':
$fileMimeType = 'video/';
if (!Str::startsWith($mimeType, $fileMimeType) && !is_video($originalName)) {
$this->message ='第'.($index+1) .'个文件不是有效的视频文件!';
return false;
}
break;
}
return true;
}
/**
* 验证文件大小是否正确
*
* @param $file
* @param int $index
* @return bool
*/
public function verifyFileSize($file, $index = 0)
{
// if ($size = $file->getClientSize() > $this->file_size_max) { Laravel5.5
if ($size = $file->getSize() > $this->file_size_max) { // Laravel 8.0
$this->message ='第'.($index+1) .'个文件大小超过' .$this->file_size_max /(1024*1024) .'M!';
return false;
}
return true;
}
/**
* 上传文件到七牛云服务器
* 1、根据文件的类型,指定不同的目录
* 2、如果是文件数组,需要递归调用方法进行上传
* 3、上传成功后,需要换文件信息保存到本地数据库中,方便前端调用
*
* @param $file
* @param string $filetype
* @return array|bool
* @throws \Exception
*/
public function uploadToQiniu($file, $filetype = 'image')
{
$return = [];
$dir = 'images';
if ($filetype == 'video') {
$dir = 'videos';
} elseif ($filetype == 'all') {
$dir = 'files';
}
if (is_array($file)) {
foreach ($file as $vo) {
// 上传文件到七牛云
$return[] = $this->uploadToQiniu($vo, $filetype);
}
} else {
// 获取原始文件名
$original_name = $file->getClientOriginalName();
// 获取后缀名
$extension = $file->extension();
// 生成新的文件名
$name = Uuid::uuid1(). '.' . $extension;
if (request('is_original_name') == 1) {
$name = $original_name;
}
$return['name'] = $name;
$return['original_name'] = $original_name;
// 存储图片路径
$file_name = '/'.$dir.'/' . date('Ymd') .'/'.$name;
if (!$response_upload = \Storage::disk('qiniu')->put($file_name, fopen($file, 'r+'))) {
$this->message ='上传到七牛云失败';
return false;
}
$full_path = config('filesystems.disks.qiniu.host'). $file_name;
// add By Richer 于 2019年11月29日16:18:42 上传成功后,将图片同时保存到本地,将文件的信息保存到本地,方便前端调用
//
// $client = new \GuzzleHttp\Client();
// $response = $client->get('http://clouddn.www.xxx.com/images/20191129/e6b5db40-1285-11ea-a5ae-fdd6f4a9db1b.jpeg');
// \Log::info(json_encode($response));
// $content = json_decode($response->getBody()->getContents());
// \Log::info(json_encode($content));
if (is_image($file_name)) {
$image_size = getimagesize($file);
//StoreFileAfterUploadToQiniu::dispatch($name, $original_name, $file_name, $full_path, $image_size, $extension, filesize($file));
}
$return['path'] = $file_name;
$return['full_path'] = $full_path;
}
return $return;
}
/**
* 上传文件到本地服务器
*
* @param $file
* @param string $filetype
* @return array
* @throws \Exception
*/
public function uploadToLocalServer($file, $filetype = 'image')
{
//获取存入数据库的相对路径
$public_path = '/storage/upload/' . date('Ymd');
$storage_path = '/app/public/upload/' . date('Ymd');
$return = [];
//服务器真是路径
if (!File::exists($real_path = storage_path($storage_path))) {
File::makeDirectory($real_path, 755, true, true);
}
if (is_array($file)) {
foreach ($file as $vo) {
// 上传文件到七牛云
$return[] = $this->uploadToLocalServer($vo, $filetype);
}
} else {
// 获取原始文件名
$original_name = $file->getClientOriginalName();
//临时文件的绝对路径
$realPath = $file->getRealPath();
// 获取后缀名
$extension = $file->extension();
//设置文件路径和名称
$name = Uuid::uuid1();
$return['name'] = $name;
$return['path'] = date('Ymd') .'/' .$name. '.' . $extension;
$return['original_name'] = $original_name;
$result = Storage::disk($filetype)->put($return['path'], file_get_contents($realPath)); // 文件名称 , 内容
if ($result) {
$return['full_path'] = Storage::disk($filetype)->url($return['path']);
return $return;
} else {
return false;
}
$image = Image::make($file)->save($real_path . '/' . $name . '.' . $extension);
$path = $public_path . '/' . $name . '.' . $extension;
if (request()->filled('width') || request()->filled('height')) {
$image->resize(
request()->input('width'),
request()->input('height'),
function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
}
);
$path = $public_path . '/' . $name . '.thumb.' . $extension;
$image->save($real_path . '/' . $name . '.thumb.' . $extension);
}
$return['path'] = $path;
$return['full_path'] = config('app.url').$path;
}
return $return;
}
// /**
// * 上传图片
// *
// * @param Request $request
// * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|Response
// */
// public function img($originalName, $realPath, $path)
// {
// Storage::disk('img')->put($originalName, file_get_contents($realPath)); // 文件名称 , 内容
// $file = $request->file('file'); // 获取上传的文件
//
//// 获取文件相关信息
//
// $originalName = $file->getClientOriginalName(); // 文件原名
//
// $realPath = $file->getRealPath(); //临时文件的绝对路径
//
//
// return response(['message' => '上传成功', 'url' => '/uploads/img/'.$originalName], 201);
//
// } else {
// return response(['message' => '请重新上传'], 400);
//
// }
//
// }
}