-
Notifications
You must be signed in to change notification settings - Fork 2
/
Plugin.php
158 lines (124 loc) · 5.55 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
<?php
/**
* 将 Typecho 的附件上传至七牛云存储中。<a href="https://github.com/lichaoxilcx/typecho-Plugin-QiniuFile" target="_blank">源代码参考</a> & <a href="https://portal.qiniu.com/signup?code=3li4q4loavdxu" target="_blank">注册七牛</a>
*
* @package Qiniu File
* @author LiCxi
* @version 1.0.0
* @link http://lichaoxi.com/
* @date 2018-3-30
*/
require __DIR__ . '/sdk/autoload.php';
use \Qiniu\Storage\UploadManager;
use \Qiniu\Auth;
class QiniuFile_Plugin implements Typecho_Plugin_Interface
{
// 激活插件
public static function activate()
{
Typecho_Plugin::factory('Widget_Upload')->uploadHandle = array('QiniuFile_Plugin', 'uploadHandle');
Typecho_Plugin::factory('Widget_Upload')->modifyHandle = array('QiniuFile_Plugin', 'modifyHandle');
Typecho_Plugin::factory('Widget_Upload')->deleteHandle = array('QiniuFile_Plugin', 'deleteHandle');
Typecho_Plugin::factory('Widget_Upload')->attachmentHandle = array('QiniuFile_Plugin', 'attachmentHandle');
return _t('插件已经激活,需先配置七牛的信息!');
}
// 禁用插件
public static function deactivate()
{
return _t('插件已被禁用');
}
// 插件配置面板
public static function config(Typecho_Widget_Helper_Form $form)
{
$bucket = new Typecho_Widget_Helper_Form_Element_Text('bucket', null, null, _t('空间名称:'));
$form->addInput($bucket->addRule('required', _t('“空间名称”不能为空!')));
$accesskey = new Typecho_Widget_Helper_Form_Element_Text('accesskey', null, null, _t('AccessKey:'));
$form->addInput($accesskey->addRule('required', _t('AccessKey 不能为空!')));
$sercetkey = new Typecho_Widget_Helper_Form_Element_Text('sercetkey', null, null, _t('SecretKey:'));
$form->addInput($sercetkey->addRule('required', _t('SecretKey 不能为空!')));
$domain = new Typecho_Widget_Helper_Form_Element_Text('domain', null, 'http://', _t('绑定域名:'), _t('以 http:// 开头,结尾不要加 / !'));
$form->addInput($domain->addRule('required', _t('请填写空间绑定的域名!'))->addRule('url', _t('您输入的域名格式错误!')));
$savepath = new Typecho_Widget_Helper_Form_Element_Text('savepath', null, 'blog/typecho/', _t('保存路径前缀'), _t('请填写保存路径格前缀,以便数据管理和迁移'));
$form->addInput($savepath);
$imgstyle = new Typecho_Widget_Helper_Form_Element_Text('imgstyle', null, '', _t('图片自定义样式名称:'), _t('请到七牛云控制台自定义图片样式,此处填写样式名称'));
$form->addInput($imgstyle);
}
// 个人用户配置面板
public static function personalConfig(Typecho_Widget_Helper_Form $form)
{
}
// 获得插件配置信息
public static function getConfig()
{
return Typecho_Widget::widget('Widget_Options')->plugin('QiniuFile');
}
// 删除文件
public static function deleteFile($filepath)
{
// // 获取插件配置
// $option = self::getConfig();
//
return true;
}
// 上传文件
public static function uploadFile($file, $content = null)
{
// 获取上传文件
if (empty($file['name'])) return false;
// 校验扩展名
$part = explode('.', $file['name']);
$ext = (($length = count($part)) > 1) ? strtolower($part[$length-1]) : '';
if (!Widget_Upload::checkFileType($ext)) return false;
// 获取插件配置
$option = self::getConfig();
$date = new Typecho_Date(Typecho_Widget::widget('Widget_Options')->gmtTime);
// 保存位置
// $savepath = preg_replace(array('/\{year\}/', '/\{month\}/', '/\{day\}/'), array($date->year, $date->month, $date->day), $option->savepath);
// $savename = $savepath . sprintf('%u', crc32(uniqid())) . '.' . $ext;
// if (isset($content))
// {
// $savename = $content['attachment']->path;
// self::deleteFile($savename);
// }
// 上传文件
$filename = $file['tmp_name'];
if (!isset($filename)) return false;
$upManager = new Qiniu\Storage\UploadManager();
$auth = new Qiniu\Auth($option->accesskey, $option->sercetkey);
$token = $auth->uploadToken($option->bucket);
list($ret, $error) = $upManager->putFile($token, $option->savepath . $file['name'], $filename);
if ($error == null)
{
return array
(
'name' => $file['name'],
'path' => $option->savepath . $file['name'].( $option->imgstyle == '' ? '' : '-'.$option->imgstyle ),
'size' => $file['size'],
'type' => $ext,
'mime' => Typecho_Common::mimeContentType($filename)
);
}
else 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();
return Typecho_Common::url($content['attachment']->path, $option->domain);
}
}