UploadController.php
4.4 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
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
namespace api\user\controller;
use cmf\controller\RestUserBaseController;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\Db;
/**
* @title 文件上传
* @description 文件上传
* @package api\wxapp\controller
*/
class UploadController extends RestUserBaseController
{
/**
* @title 上传单个文件
* @description 上传单个文件
* @author Tiger Yang
* @url /user/upload/one
* @method POST
*
* @header name:XX-Token require:1 default: desc:登录标识
* @header name:XX-Device-Type require:0 default:wxapp desc:设备类型
*
* @param name:file type:file require:1 other: desc:上传文件
*/
public function one()
{
$file = $this->request->file('file');
// 移动到框架应用根目录/public/upload/ 目录下
$info = $file->validate([
/*'size' => 15678,*/
'ext' => 'jpg,png,gif'
]);
$fileMd5 = $info->md5();
$fileSha1 = $info->sha1();
$findFile = Db::name("asset")->where('file_md5', $fileMd5)->where('file_sha1', $fileSha1)->find();
if (!empty($findFile)) {
$this->success("上传成功!", ['url' => cmf_get_asset_url($findFile['file_path']), 'filename' => $findFile['filename']]);
}
$info = $info->move(ROOT_PATH . 'public' . DS . 'upload');
if ($info) {
$saveName = $info->getSaveName();
$originalName = $info->getInfo('name');//name,type,size
$fileSize = $info->getInfo('size');
$suffix = $info->getExtension();
$fileKey = $fileMd5 . md5($fileSha1);
$userId = $this->getUserId();
Db::name('asset')->insert([
'user_id' => $userId,
'file_key' => $fileKey,
'filename' => $originalName,
'file_size' => $fileSize,
'file_path' => cmf_get_asset_url($saveName),
'file_md5' => $fileMd5,
'file_sha1' => $fileSha1,
'create_time' => time(),
'suffix' => $suffix
]);
$storage = cmf_get_option('storage');
if (isset($storage['type'])&&$storage['type']=='Qiniu') {
$this->uploadToQiniu($saveName);
}
$this->success("上传成功!", ['url' => cmf_get_asset_url($saveName), 'filename' => $originalName]);
} else {
// 上传失败获取错误信息
$this->error($file->getError());
}
}
/**
* 上传到七牛云
* @param $save_name
* @param bool $is_del_local
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function uploadToQiniu($save_name,$is_del_local=true){
$plugin=Db::name('plugin')->field('config')->where(['name'=>'Qiniu'])->find();
$config=json_decode($plugin['config'],true);
$filePath = './../upload/'.$save_name;
// 上传到七牛后保存的文件名
$key =$save_name;
require_once VENDOR_PATH . 'qiniu/php-sdk/autoload.php';
// 需要填写你的 Access Key 和 Secret Key
$accessKey = $config['accessKey'];
$secretKey = $config['secretKey'];
// 构建鉴权对象
$auth = new Auth($accessKey,$secretKey);
// 要上传的空间
$bucket = $config['bucket'];
$token = $auth->uploadToken($bucket);
// 初始化 UploadManager 对象并进行文件的上传
$uploadMgr = new UploadManager();
// 调用 UploadManager 的 putFile 方法进行文件的上传
list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
if($is_del_local){
unlink($filePath);
}
if ($err !== null) {
return ["err"=>1,"msg"=>$err,"data"=>""];
} else {
return ["err"=>0,"msg"=>"上传完成","data"=>cmf_get_image_url($ret['key'])];
}
}
}