AssetController.php
5.2 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
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: kane <chengjin005@163.com>
// +----------------------------------------------------------------------
namespace app\user\controller;
use cmf\controller\AdminBaseController;
use cmf\lib\Upload;
use Qiniu\Auth;
use think\Db;
/**
* 附件上传控制器
* Class Asset
* @package app\asset\controller
*/
class AssetController extends AdminBaseController
{
public function _initialize()
{
$adminId = cmf_get_current_admin_id();
$userId = cmf_get_current_user_id();
if (empty($adminId) && empty($userId)) {
exit("非法上传!");
}
}
/**
* webuploader 上传
*/
public function webuploader()
{
if ($this->request->isPost()) {
$uploader = new Upload();
$result = $uploader->upload();
if ($result === false) {
$this->error($uploader->getError());
} else {
$this->success("上传成功!", '', $result);
}
} else {
$uploadSetting = cmf_get_upload_setting();
$arrFileTypes = [
'image' => ['title' => 'Image files', 'extensions' => $uploadSetting['file_types']['image']['extensions']],
'video' => ['title' => 'Video files', 'extensions' => $uploadSetting['file_types']['video']['extensions']],
'audio' => ['title' => 'Audio files', 'extensions' => $uploadSetting['file_types']['audio']['extensions']],
'file' => ['title' => 'Custom files', 'extensions' => $uploadSetting['file_types']['file']['extensions']]
];
$arrData = $this->request->param();
if (empty($arrData["filetype"])) {
$arrData["filetype"] = "image";
}
$fileType = $arrData["filetype"];
if (array_key_exists($arrData["filetype"], $arrFileTypes)) {
$extensions = $uploadSetting['file_types'][$arrData["filetype"]]['extensions'];
$fileTypeUploadMaxFileSize = $uploadSetting['file_types'][$fileType]['upload_max_filesize'];
} else {
$this->error('上传文件类型配置错误!');
}
$this->assign('filetype', $arrData["filetype"]);
$this->assign('extensions', $extensions);
$this->assign('upload_max_filesize', $fileTypeUploadMaxFileSize * 1024);
$this->assign('upload_max_filesize_mb', intval($fileTypeUploadMaxFileSize / 1024));
$maxFiles = intval($uploadSetting['max_files']);
$maxFiles = empty($maxFiles) ? 20 : $maxFiles;
$chunkSize = intval($uploadSetting['chunk_size']);
$chunkSize = empty($chunkSize) ? 512 : $chunkSize;
$this->assign('max_files', $arrData["multi"] ? $maxFiles : 1);
$this->assign('chunk_size', $chunkSize); //// 单位KB
$this->assign('multi', $arrData["multi"]);
$this->assign('app', $arrData["app"]);
$storage = cmf_get_option('storage');
if ($storage['type'] == 'Qiniu'){
$plugin=Db::name('plugin')->field('config')->where(['name'=>'Qiniu'])->find();
$config=json_decode($plugin['config'],true);
$this->assign('qiniu_config', $config);
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);
$this->assign('token', $token);
return $this->fetch(":webuploader2");
}else{
return $this->fetch(":webuploader");
}
}
}
/**
* @title 获取七牛云TOKEN
* @description 获取七牛云TOKEN
* @author Tiger Yang
* @url /home/public/getQiNiuToken
* @method GET
*
*/
public function getQiNiuToken(){
$plugin=Db::name('plugin')->field('config')->where(['name'=>'Qiniu'])->find();
$config=json_decode($plugin['config'],true);
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);
echo $token;
}
}