-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlugin.php
More file actions
178 lines (157 loc) · 7.31 KB
/
Plugin.php
File metadata and controls
178 lines (157 loc) · 7.31 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
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* typecho附件上传阿里云OSS,基于OSS 1.1.7文档开发
*
* @package 阿里云OSS文件上传
* @author raintao
* @version 1.0.1
* @link https://github.com/rainwsy
*/
class AliOSS_Plugin implements Typecho_Plugin_Interface
{
// 激活插件
public static function activate()
{
Typecho_Plugin::factory('Widget_Upload')->uploadHandle = array(
'AliOSS_Plugin',
'uploadHandle'
);
Typecho_Plugin::factory('Widget_Upload')->modifyHandle = array(
'AliOSS_Plugin',
'modifyHandle'
);
Typecho_Plugin::factory('Widget_Upload')->deleteHandle = array(
'AliOSS_Plugin',
'deleteHandle'
);
Typecho_Plugin::factory('Widget_Upload')->attachmentHandle = array(
'AliOSS_Plugin',
'attachmentHandle'
);
return _t('插件已经激活,请设置OSS信息!');
}
// 禁用插件
public static function deactivate()
{
return _t('插件已被禁用');
}
// 插件配置面板
public static function config(Typecho_Widget_Helper_Form $form)
{
/**
* 节点
*/
$endpointList = array(
"" => _t('请选择所属地域'),
"oss-cn-beijing.aliyuncs.com" => _t('北京'),
"oss-cn-qingdao.aliyuncs.com" => _t('青岛'),
"oss-cn-shenzhen.aliyuncs.com" => _t('深圳'),
"oss-cn-hangzhou.aliyuncs.com" => _t('杭州'),
"oss-cn-shanghai.aliyuncs.com" => _t('上海')
);
$endpoint = new Typecho_Widget_Helper_Form_Element_Select('endpoint', $endpointList, 'oss-cn-beijing.aliyuncs.com', _t('所属地域'), _t('请选择<strong style="color:#C33;">bucket对应节点</strong>,否则无法使用,默认为北京!'));
$form->addInput($endpoint->addRule('required', _t('所属地域 不能为空!')));
$bucket = new Typecho_Widget_Helper_Form_Element_Text('bucket', null, null, _t('Bucket名称:'));
$form->addInput($bucket->addRule('required', _t('“空间名称”不能为空!')));
$accessid = new Typecho_Widget_Helper_Form_Element_Text('accessid', null, null, _t('Access Key ID'), _t('点击<a href="https://ak-console.aliyun.com/#/accesskey">这里</a>查看Access Key ID&Access Key Secret'));
$form->addInput($accessid->addRule('required', _t('AccessID 不能为空!')));
$accesskey = new Typecho_Widget_Helper_Form_Element_Text('accesskey', null, null, _t('Access Key Secret:'));
$form->addInput($accesskey->addRule('required', _t('AccessKey 不能为空!')));
$domain = new Typecho_Widget_Helper_Form_Element_Text('domain', null, null, _t('图片HTTP访问前缀:'), _t('OSS可供外网访问的域名前缀,比如https://cdn.mydomain.com'));
$form->addInput($domain->addRule('required', _t('请填写OSS图片服务的域名前缀!')));
$savepath = new Typecho_Widget_Helper_Form_Element_Text('savepath', null, 'img/{year}/{month}/', _t('图片保存路径格式:'), _t('图片附件保存路径的格式<br />可选参数:{year} 年份、{month} 月份、{day} 日期。<br /><strong style="color:#C33;">因为阿里云图片处理服务不支持故而GIF视为非图片</strong>'));
$form->addInput($savepath->addRule('required', _t('请填写图片保存路径格式!')));
$style = new Typecho_Widget_Helper_Form_Element_Text('style', null, null, _t('图片样式后缀:'), _t('阿里云图片处理服务的后缀,比如@600w.webp'));
$form->addInput($style);
$nonimg_domain = new Typecho_Widget_Helper_Form_Element_Text('nonimg_domain', null, null, _t('非图片HTTP访问前缀:'), _t('OSS可供外网访问的域名前缀,比如https://oss.mydomain.com'));
$form->addInput($nonimg_domain->addRule('required', _t('请填写OSS的域名前缀!')));
$nonimg_savepath = new Typecho_Widget_Helper_Form_Element_Text('nonimg_savepath', null, 'atta/{year}/{month}/', _t('非图片保存路径格式:'), _t('非图片附件保存路径的格式<br />可选参数:{year} 年份、{month} 月份、{day} 日期'));
$form->addInput($nonimg_savepath->addRule('required', _t('请填写非图片保存路径格式!')));
}
// 个人用户配置面板
public static function personalConfig(Typecho_Widget_Helper_Form $form)
{}
// 获得插件配置信息
public static function getConfig()
{
return Typecho_Widget::widget('Widget_Options')->plugin('AliOSS');
}
// 初始化OSS SDK
public static function initSDK()
{
// 引入 SDK
require_once 'sdk.class.php';
}
// 删除文件
public static function deleteFile($filepath)
{
// 获取插件配置
$option = self::getConfig();
self::initSDK();
$obj = new ALIOSS($option->accessid, $option->accesskey, $option->endpoint);
$obj->delete_object($option->bucket, $filepath);
}
public static function isImage($ext)
{
return in_array($ext, array('jpg', 'jpeg', 'bmp', 'png', 'tiff'));
}
// 上传文件
public static function uploadFile($file, $content = null)
{
// 获取上传文件
if (empty($file['name'])) return false;
$option = self::getConfig();
if (!isset($option->accessid)) {
return false;
}
self::initSDK();
$obj = new ALIOSS($option->accessid, $option->accesskey, $option->endpoint);
// 校验扩展名
$part = explode('.', $file['name']);
$ext = (($length = count($part)) > 1) ? strtolower($part[$length - 1]) : '';
if (!Widget_Upload::checkFileType($ext)) return false;
// 保存位置
$savename = str_replace(
array('{year}', '{month}', '{day}'),
array(date('Y'), date('m'), date('d')),
ltrim(self::isImage($ext) ? $option->savepath : $option->nonimg_savepath, '/')
) . sprintf('%u', crc32(uniqid())) . '.' . $ext;
$response = $obj->upload_file_by_file($option->bucket, $savename, $file['tmp_name']);
if ($response->status===200) {
return array(
'name' => $file['name'],
'path' => $savename,
'size' => $file['size'],
'type' => $ext,
'mime' => Typecho_Common::mimeContentType($savename)
);
}
return false;
}
// 上传文件处理函数
public static function uploadHandle($file)
{
return self::uploadFile($file);
}
// 修改文件处理函数
public static function modifyHandle($content, $file)
{
return self::uploadFile($file, $content);
}
// 删除文件
public static function deleteHandle(array $content)
{
self::deleteFile($content['attachment']->path);
}
// 获取实际文件绝对访问路径
public static function attachmentHandle(array $content)
{
$option = self::getConfig();
$isImage = self::isImage($content['attachment']->type);
return Typecho_Common::url(
$content['attachment']->path,
$isImage ? $option->domain : $option->nonimg_domain
) . ($isImage ? $option->style : '');
}
}