forked from Moidea/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.php
284 lines (246 loc) · 8.89 KB
/
Plugin.php
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
<?php
/**
* <a href="http://developer.baidu.com/bae/bcs/bucket/" target="_blank">Baidu App Engine</a> 附件上传插件
*
* @category system
* @package BaeUpload
* @author doudou
* @version 1.0.1
* @link https://github.com/doudoutime
* @date 2014-1-5
*/
class BaeUpload_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
Typecho_Plugin::factory('Widget_Upload')->uploadHandle = array('BaeUpload_Plugin', 'uploadHandle');
Typecho_Plugin::factory('Widget_Upload')->modifyHandle = array('BaeUpload_Plugin', 'modifyHandle');
Typecho_Plugin::factory('Widget_Upload')->deleteHandle = array('BaeUpload_Plugin', 'deleteHandle');
Typecho_Plugin::factory('Widget_Upload')->attachmentHandle = array('BaeUpload_Plugin', 'attachmentHandle');
Typecho_Plugin::factory('Widget_Upload')->attachmentDataHandle = array('BaeUpload_Plugin', 'attachmentDataHandle');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate(){}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$ak = new Typecho_Widget_Helper_Form_Element_Text('ak',
NULL, '',
_t('API Key'),
_t('<a href="http://developer.baidu.com/console" target="_blank">获取API Key</a>'));
$form->addInput($ak);
$sk = new Typecho_Widget_Helper_Form_Element_Text('sk',
NULL, '',
_t('Secret Key'),
_t('<a href="http://developer.baidu.com/console" target="_blank">获取Secret Key</a>'));
$form->addInput($sk);
$bucketName = new Typecho_Widget_Helper_Form_Element_Text('bucket',
NULL, '',
_t('Bucket名称'),
_t(''));
$form->addInput($bucketName);
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 上传文件处理函数
*
* @access public
* @param array $file 上传的文件
* @return mixed
*/
public static function uploadHandle($file)
{
if (empty($file['name'])) {
return false;
}
$fileName = preg_split("(\/|\\|:)", $file['name']);
$file['name'] = array_pop($fileName);
//获取扩展名
$ext = '';
$part = explode('.', $file['name']);
if (($length = count($part)) > 1) {
$ext = strtolower($part[$length - 1]);
}
if (!Widget_Upload::checkFileType($ext)) {
return false;
}
$options = Typecho_Widget::widget('Widget_Options');
$date = new Typecho_Date($options->gmtTime);
//构建路径
$path = Widget_Upload::UPLOAD_PATH . '/' . $date->year . '/' . $date->month;
//获取文件名
$fileName = sprintf('%u', crc32(uniqid())) . '.' . $ext;
$path = $path . '/' . $fileName;
$bcs = self::bcsInit();
$bucket = $options->plugin('BaeUpload')->bucket;
//空日志记录函数
function bcs_log(){}
if (isset($file['tmp_name'])) {
//移动上传文件
if (!$bcs->create_object($bucket, $path, $file['tmp_name'], array('acl'=>BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_READ, BaiduBCS::IMPORT_BCS_LOG_METHOD=>'bcs_log'))->isOK()) {
return false;
}
} else if (isset($file['bits'])) {
//直接写入文件
if (!$bcs->create_object_by_content($bucket, $path, $file['bits'], array('acl'=>BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_READ, BaiduBCS::IMPORT_BCS_LOG_METHOD=>'bcs_log'))->isOK()) {
return false;
}
} else {
return false;
}
//设置文件Content-Type
$bcs->set_object_meta($bucket, $path, array('Content-Type'=>BCS_MimeTypes::get_mimetype($ext)), array(BaiduBCS::IMPORT_BCS_LOG_METHOD=>'bcs_log'));
if (!isset($file['size'])) {
$file['size'] = $bcs->get_object_info($bucket, $path, array(BaiduBCS::IMPORT_BCS_LOG_METHOD=>'bcs_log'))->header['Content-Length'];
}
//返回相对存储路径
return array(
'name' => $file['name'],
'path' => $path,
'size' => $file['size'],
'type' => $ext,
'mime' => Typecho_Common::mimeContentType($path)
);
}
/**
* 修改文件处理函数
*
* @access public
* @param array $content 老文件
* @param array $file 新上传的文件
* @return mixed
*/
public static function modifyHandle($content, $file)
{
if (empty($file['name'])) {
return false;
}
$fileName = preg_split("(\/|\\|:)", $file['name']);
$file['name'] = array_pop($fileName);
//获取扩展名
$ext = '';
$part = explode('.', $file['name']);
if (($length = count($part)) > 1) {
$ext = strtolower($part[$length - 1]);
}
if ($content['attachment']->type != $ext) {
return false;
}
//获取文件名
$fileName = $content['attachment']->path;
$path = $fileName;
$bcs = self::bcsInit();
$options = Typecho_Widget::widget('Widget_Options');
$bucket = $options->plugin('BaeUpload')->bucket;
//空日志记录函数
function bcs_log(){}
if (isset($file['tmp_name'])) {
//移动上传文件
if (!$bcs->create_object($bucket, $path, $file['tmp_name'], array('acl'=>BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_READ, BaiduBCS::IMPORT_BCS_LOG_METHOD=>'bcs_log'))->isOK()) {
return false;
}
} else if (isset($file['bits'])) {
//直接写入文件
if (!$bcs->create_object_by_content($bucket, $path, $file['bits'], array('acl'=>BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_READ, BaiduBCS::IMPORT_BCS_LOG_METHOD=>'bcs_log'))->isOK()) {
return false;
}
} else {
return false;
}
//设置文件Content-Type
$bcs->set_object_meta($bucket, $path, array('Content-Type'=>BCS_MimeTypes::get_mimetype($ext)), array(BaiduBCS::IMPORT_BCS_LOG_METHOD=>'bcs_log'));
if (!isset($file['size'])) {
$file['size'] = $bcs->get_object_info($bucket, $path, array(BaiduBCS::IMPORT_BCS_LOG_METHOD=>'bcs_log'))->header['Content-Length'];
}
//返回相对存储路径
return array(
'name' => $content['attachment']->name,
'path' => $content['attachment']->path,
'size' => $file['size'],
'type' => $content['attachment']->type,
'mime' => $content['attachment']->mime
);
}
/**
* 删除文件
*
* @access public
* @param array $content 文件相关信息
* @return string
*/
public static function deleteHandle(array $content)
{
$bcs = self::bcsInit();
$bucket = Helper::options()->plugin('BaeUpload')->bucket;
//空日志记录函数
function bcs_log(){}
return $bcs->delete_object($bucket, $content['attachment']->path, array(BaiduBCS::IMPORT_BCS_LOG_METHOD=>'bcs_log'))->isOK();
}
/**
* 获取实际文件绝对访问路径
*
* @access public
* @param array $content 文件相关信息
* @return string
*/
public static function attachmentHandle(array $content)
{
//不采用bcs获取还签名的url
$bucket = Helper::options()->plugin('BaeUpload')->bucket;
return Typecho_Common::url($content['attachment']->path, 'http://bcs.duapp.com/' . $bucket);
}
/**
* 获取实际文件数据
*
* @access public
* @param array $content
* @return string
*/
public static function attachmentDataHandle(array $content)
{
$bcs = self::bcsInit();
$bucket = Helper::options()->plugin('BaeUpload')->bucket;
//空日志记录函数
function bcs_log(){}
return $bcs->get_object($bucket, $content['attachment']->path, array(BaiduBCS::IMPORT_BCS_LOG_METHOD=>'bcs_log'))->body;
}
/**
* bcs初始化
*
* @access public
* @return object
*/
public static function bcsInit()
{
$options = Typecho_Widget::widget('Widget_Options')->plugin('BaeUpload');
require_once 'SDK/bcs.class.php';
return new BaiduBCS($options->ak, $options->sk);
}
}