QiniuUploadController.php
2.5 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
<?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 2020-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\Request;
use Illuminate\Http\Response;
/**
* Class QiniuUploadController
*
* @category App\Http\Controllers\Service
* @package App\Http\Controllers\Service
* @author Richer <yangzi1028@163.com>
* @date 2020年11月13日 01:26:09
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
class QiniuUploadController extends UploadController
{
/**
* 图片上传
*
* @param Request $request
* @return array
* @throws \Exception
*/
public function store(Request $request)
{
// add by Richer 于 2020年9月22日10:21:07 兼容不同域
$keys = array_keys($request->all());
$field = $request->field ?? 'images';
foreach ($keys as $key) {
if ($key !== 'filetype' && $key !== 'field') {
$field = $key;
}
}
$filetype = $request->filetype ?? 'image';
return parent::upload($request, $field, $filetype);
}
/**
* 上传文件到七牛云服务器
* 1、根据文件的类型,指定不同的目录
* 2、如果是文件数组,需要递归调用方法进行上传
* 3、上传成功后,需要换文件信息保存到本地数据库中,方便前端调用
*
* @param $file
* @return array|bool
* @throws \Exception
*/
public function destroy($file)
{
if (!$response_upload = \Storage::disk('qiniu')->delete($file)) {
return [
'code' => Response::HTTP_UNPROCESSABLE_ENTITY,
'data' => null,
'msg' => '删除文件失败失败',
];
}
// 上传成功,返回文件信息和路径
return ['code' => 0, 'data' => null, 'msg' => '删除文件成功!'];
}
}