This commit is contained in:
20
OssForTypecho/LICENSE
Normal file
20
OssForTypecho/LICENSE
Normal file
@@ -0,0 +1,20 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Hugh Jiang
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
366
OssForTypecho/Plugin.php
Normal file
366
OssForTypecho/Plugin.php
Normal file
@@ -0,0 +1,366 @@
|
||||
<?php
|
||||
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
|
||||
|
||||
/**
|
||||
* 阿里云OSS上传插件(Typecho)【<a href="https://github.com/typecho-fans/plugins" target="_blank">TF</a>社区维护版】
|
||||
*
|
||||
* @package OssForTypecho
|
||||
* @author 权那他, Charmeryl
|
||||
* @version 1.0.2
|
||||
* @link https://github.com/typecho-fans/plugins/tree/master/OssForTypecho
|
||||
* @dependence 14.10.10-*
|
||||
*/
|
||||
|
||||
class OssForTypecho_Plugin implements Typecho_Plugin_Interface {
|
||||
//上传文件目录
|
||||
const UPLOAD_DIR = '/usr/uploads' ;
|
||||
|
||||
/**
|
||||
* 激活插件方法,如果激活失败,直接抛出异常
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
* @throws Typecho_Plugin_Exception
|
||||
*/
|
||||
public static function activate() {
|
||||
Typecho_Plugin::factory('Widget_Upload')->uploadHandle = array('OssForTypecho_Plugin', 'uploadHandle');
|
||||
Typecho_Plugin::factory('Widget_Upload')->modifyHandle = array('OssForTypecho_Plugin', 'modifyHandle');
|
||||
Typecho_Plugin::factory('Widget_Upload')->deleteHandle = array('OssForTypecho_Plugin', 'deleteHandle');
|
||||
Typecho_Plugin::factory('Widget_Upload')->attachmentHandle = array('OssForTypecho_Plugin', 'attachmentHandle');
|
||||
Typecho_Plugin::factory('Widget_Upload')->attachmentDataHandle = array('OssForTypecho_Plugin', 'attachmentDataHandle');
|
||||
return _t('插件已激活,请前往设置');
|
||||
}
|
||||
|
||||
/**
|
||||
* 禁用插件方法,如果禁用失败,直接抛出异常
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @return void
|
||||
* @throws Typecho_Plugin_Exception
|
||||
*/
|
||||
public static function deactivate(){
|
||||
return _t('插件已禁用');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取插件配置面板
|
||||
*
|
||||
* @access public
|
||||
* @param Typecho_Widget_Helper_Form $form 配置面板
|
||||
* @return void
|
||||
*/
|
||||
public static function config(Typecho_Widget_Helper_Form $form) {
|
||||
$desc = new Typecho_Widget_Helper_Form_Element_Text('desc', NULL, '', _t('插件使用说明:'),
|
||||
_t('<ol>
|
||||
<li>插件基于阿里云aliyun-oss-php-sdk开发,若发现插件不可用,请到本插件 <a target="_blank" href="https://github.com/typecho-fans/plugins/tree/master/OssForTypecho">GitHub发布地址</a> 检查是否有更新,或者提交Issues。<br></li>
|
||||
<li>在阿里云 <a target="_blank" href="https://usercenter.console.aliyun.com/#/manage/ak">AccessKey管理控制台</a> 页面里获取AccessKeyID与AccessKeySecret。<br></li>
|
||||
<li>插件不会验证配置的正确性,请自行确认配置信息正确,否则不能正常使用。<br></li>
|
||||
<li>插件会替换所有之前上传的文件的链接,若启用插件前存在已上传的数据,请自行将其上传至OSS相同目录中以保证正常显示;同时,禁用插件也会导致链接恢复,也请自行将数据下载至相同目录中。<br></li>
|
||||
</ol>'));
|
||||
$form->addInput($desc);
|
||||
|
||||
$acid = new Typecho_Widget_Helper_Form_Element_Text('acid',
|
||||
NULL, '',
|
||||
_t('AccessKeyId:'));
|
||||
$form->addInput($acid->addRule('required', _t('AccessId不能为空!')));
|
||||
|
||||
$ackey = new Typecho_Widget_Helper_Form_Element_Text('ackey',
|
||||
NULL, '',
|
||||
_t('AccessKeySecret:'));
|
||||
$form->addInput($ackey->addRule('required', _t('AccessKey不能为空!')));
|
||||
|
||||
$region = new Typecho_Widget_Helper_Form_Element_Select('region',
|
||||
array('oss-cn-hangzhou' => '华东 1 (杭州)', 'oss-cn-shanghai' => '华东 2 (上海)', 'oss-cn-qingdao' => '华北 1 (青岛)',
|
||||
'oss-cn-beijing' => '华北 2 (北京)', 'oss-cn-zhangjiakou' => '华北 3 (张家口)', 'oss-cn-huhehaote' => '华北 5 (呼和浩特)',
|
||||
'oss-cn-shenzhen' => '华南 1 (深圳)', 'oss-cn-hongkong' => '香港', 'oss-us-west-1' => '美国西部 1 (硅谷)',
|
||||
'oss-us-east-1' => '美国东部 1 (弗吉尼亚)', 'oss-ap-southeast-1' => '亚太东南 1 (新加坡)',
|
||||
'oss-ap-southeast-2' => '亚太东南 2 (悉尼)', 'oss-ap-southeast-3' => '亚太东南 3 (吉隆坡)',
|
||||
'oss-ap-southeast-5' => '亚太东南 5 (雅加达)', 'oss-ap-northeast-1' => '亚太东北 1 (日本)',
|
||||
'oss-ap-south-1' => '亚太南部 1 (孟买)', 'oss-eu-central-1' => '欧洲中部 1 (法兰克福)',
|
||||
'oss-me-east-1' => '中东东部 1 (迪拜)'),
|
||||
'oss-cn-hangzhou', _t('存储区域:')
|
||||
);
|
||||
$form->addInput($region);
|
||||
|
||||
$suffix = new Typecho_Widget_Helper_Form_Element_Radio('suffix', array('.aliyuncs.com' => '外网', '-internal.aliyuncs.com' => '内网'),
|
||||
'.aliyuncs.com', _t('节点访问方式:'), _t('阿里云主机选择内网方式可节省上传流量'));
|
||||
$form->addInput($suffix);
|
||||
|
||||
$bucket = new Typecho_Widget_Helper_Form_Element_Text('bucket',
|
||||
NULL, '',
|
||||
_t('Bucket名称:'));
|
||||
$form->addInput($bucket->addRule('required', _t('Bucket名称不能为空!')));
|
||||
|
||||
$domain = new Typecho_Widget_Helper_Form_Element_Text('domain',
|
||||
NULL, '',
|
||||
_t('Bucket自定义域名:'),
|
||||
_t('可使用自定义域名(留空则使用默认域名)<br>例如:http://oss.example.com(需加上前面的 http:// 或 https://)'));
|
||||
$form->addInput($domain);
|
||||
|
||||
$imgstyle = new Typecho_Widget_Helper_Form_Element_Text('imgstyle', null, '', _t('分隔符+图片处理样式名:'), _t('填写<a href="https://oss.console.aliyun.com/bucket" target="_blank">Bucket设置</a>数据处理-图片处理中建立的规则名称(前面加分隔符)如-test'));
|
||||
$form->addInput($imgstyle);
|
||||
|
||||
echo '<script>
|
||||
window.onload = function()
|
||||
{
|
||||
document.getElementsByName("desc")[0].type = "hidden";
|
||||
}
|
||||
</script>';
|
||||
}
|
||||
|
||||
/**
|
||||
* 个人用户的配置面板
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
//获取扩展名
|
||||
$ext = self::getSafeName($file['name']);
|
||||
//判定是否是允许的文件类型
|
||||
if (!Widget_Upload::checkFileType($ext) || Typecho_Common::isAppEngine()) {
|
||||
return false;
|
||||
}
|
||||
//获取设置参数
|
||||
$options = Typecho_Widget::widget('Widget_Options')->plugin('OssForTypecho');
|
||||
//获取文件名
|
||||
$date = new Typecho_Date($options->gmtTime);
|
||||
$fileDir = self::getUploadDir() . '/' . $date->year . '/' . $date->month;
|
||||
$fileName = sprintf('%u', crc32(uniqid())) . '.' . $ext;
|
||||
$path = $fileDir . '/' . $fileName;
|
||||
//获得上传文件
|
||||
$uploadfile = self::getUploadFile($file);
|
||||
//如果没有临时文件,则退出
|
||||
if (!isset($uploadfile)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* 上传到OSS */
|
||||
//初始化OSS
|
||||
$ossClient = self::OssInit();
|
||||
try {
|
||||
if (isset($file['tmp_name'])) {
|
||||
$result = $ossClient->uploadFile($options->bucket, substr($path, 1), $uploadfile);
|
||||
} else {
|
||||
$result = $ossClient->putObject($options->bucket, substr($path, 1), $uploadfile);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
print_r($e);
|
||||
return false;
|
||||
}
|
||||
|
||||
//HeYabin Add, 添加文件读写权限设定
|
||||
$acl = "public-read";
|
||||
try {
|
||||
$ossClient->putObjectAcl($options->bucket,substr($path,1), $acl);
|
||||
} catch (Exception $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!isset($file['size'])){
|
||||
$fileInfo = $result['info'];
|
||||
$file['size'] = $fileInfo['size_upload'];
|
||||
}
|
||||
|
||||
//返回相对存储路径
|
||||
return array(
|
||||
'name' => $file['name'],
|
||||
'path' => $path,
|
||||
'size' => $file['size'],
|
||||
'type' => $ext,
|
||||
'mime' => (isset($file['tmp_name']) ? Typecho_Common::mimeContentType($file['tmp_name']) : $file['mime'])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改文件处理函数
|
||||
*
|
||||
* @access public
|
||||
* @param array $content 老文件
|
||||
* @param array $file 新上传的文件
|
||||
* @return mixed
|
||||
*/
|
||||
public static function modifyHandle($content, $file) {
|
||||
if (empty($file['name'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//获取扩展名
|
||||
$ext = self::getSafeName($file['name']);
|
||||
//判定是否是允许的文件类型
|
||||
if ($content['attachment']->type != $ext || Typecho_Common::isAppEngine()) {
|
||||
return false;
|
||||
}
|
||||
//获取设置参数
|
||||
$options = Typecho_Widget::widget('Widget_Options')->plugin('OssForTypecho');
|
||||
//获取文件路径
|
||||
$path = $content['attachment']->path;
|
||||
//获得上传文件
|
||||
$uploadfile = self::getUploadFile($file);
|
||||
//如果没有临时文件,则退出
|
||||
if (!isset($uploadfile)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* 上传到OSS */
|
||||
//初始化OSS
|
||||
$ossClient = self::OssInit();
|
||||
try {
|
||||
$result = $ossClient->uploadFile($options->bucket, substr($path,1), $uploadfile);
|
||||
} catch (Exception $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//HeYabin Add, 添加文件读写权限设定
|
||||
$acl = "public-read";
|
||||
try {
|
||||
$ossClient->putObjectAcl($options->bucket,substr($path,1), $acl);
|
||||
} catch (Exception $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!isset($file['size'])){
|
||||
$fileInfo = $result['info'];
|
||||
$file['size'] = $fileInfo['size_upload'];
|
||||
}
|
||||
|
||||
//返回相对存储路径
|
||||
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) {
|
||||
//获取设置参数
|
||||
$options = Typecho_Widget::widget('Widget_Options')->plugin('OssForTypecho');
|
||||
//初始化COS
|
||||
$ossClient = self::OssInit();
|
||||
try {
|
||||
$ossClient->deleteObject($options->bucket, substr($content['attachment']->path,1));
|
||||
} catch (Exception $e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实际文件绝对访问路径
|
||||
*
|
||||
* @access public
|
||||
* @param array $content 文件相关信息
|
||||
* @return string
|
||||
*/
|
||||
public static function attachmentHandle(array $content) {
|
||||
//获取设置参数
|
||||
$options = Typecho_Widget::widget('Widget_Options')->plugin('OssForTypecho');
|
||||
return Typecho_Common::url($content['attachment']->path, self::getDomain()).$options->imgstyle;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实际文件数据
|
||||
*
|
||||
* @access public
|
||||
* @param array $content
|
||||
* @return string
|
||||
*/
|
||||
public static function attachmentDataHandle($content) {
|
||||
//获取设置参数
|
||||
$options = Typecho_Widget::widget('Widget_Options')->plugin('OssForTypecho');
|
||||
$ossClient = self::OssInit();
|
||||
return $ossClient->getObjectMeta($options->bucket, substr($content['attachment']->path,1));
|
||||
}
|
||||
|
||||
/**
|
||||
* OSS初始化
|
||||
*
|
||||
* @access public
|
||||
* @return object
|
||||
*/
|
||||
public static function OssInit() {
|
||||
$options = Typecho_Widget::widget('Widget_Options')->plugin('OssForTypecho');
|
||||
$endpoint = 'http://' . $options->region . $options->suffix;
|
||||
require_once 'aliyun-oss-php-sdk-2.3.1.phar';
|
||||
return new OSS\OssClient($options->acid, $options->ackey, $endpoint);
|
||||
}
|
||||
|
||||
/**
|
||||
*获取文件上传目录
|
||||
* @access private
|
||||
* @return string
|
||||
*/
|
||||
private static function getUploadDir() {
|
||||
if(defined('__TYPECHO_UPLOAD_DIR__'))
|
||||
{
|
||||
return __TYPECHO_UPLOAD_DIR__;
|
||||
}
|
||||
else{
|
||||
return self::UPLOAD_DIR;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上传文件
|
||||
*
|
||||
* @param array $file 上传的文件
|
||||
* @access private
|
||||
* @return string
|
||||
*/
|
||||
private static function getUploadFile($file) {
|
||||
return isset($file['tmp_name']) ? $file['tmp_name'] : (isset($file['bytes']) ? $file['bytes'] : (isset($file['bits']) ? $file['bits'] : ''));
|
||||
}
|
||||
|
||||
/**
|
||||
*获取文件上传目录
|
||||
* @access private
|
||||
* @return string
|
||||
*/
|
||||
private static function getDomain() {
|
||||
$options = Typecho_Widget::widget('Widget_Options')->plugin('OssForTypecho');
|
||||
$domain = $options->domain;
|
||||
if(empty($domain)) $domain = 'https://' . $options->bucket . '.' . $options->region . '.aliyuncs.com';
|
||||
return $domain;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取安全的文件名
|
||||
*
|
||||
* @param string $name
|
||||
* @static
|
||||
* @access private
|
||||
* @return string
|
||||
*/
|
||||
private static function getSafeName(&$name) {
|
||||
$name = str_replace(array('"', '<', '>'), '', $name);
|
||||
$name = str_replace('\\', '/', $name);
|
||||
$name = false === strpos($name, '/') ? ('a' . $name) : str_replace('/', '/a', $name);
|
||||
$info = pathinfo($name);
|
||||
$name = substr($info['basename'], 1);
|
||||
return isset($info['extension']) ? strtolower($info['extension']) : '';
|
||||
}
|
||||
|
||||
}
|
||||
78
OssForTypecho/README.md
Normal file
78
OssForTypecho/README.md
Normal file
@@ -0,0 +1,78 @@
|
||||
<a href="https://typecho-fans.github.io">
|
||||
<img src="https://typecho-fans.github.io/text-logo.svg" alt="TF Logo" title="Typecho Fans开源作品社区" align="right" height="100" />
|
||||
</a>
|
||||
|
||||
OssForTypecho v1.0.2 - 社区维护版
|
||||
======================
|
||||
<h4 align="center">—— 附件使用阿里云储存OSS插件,支持流式上传/图片样式等。</h4>
|
||||
|
||||
<p align="center">
|
||||
<a href="#使用说明">使用说明</a> •
|
||||
<a href="#版本历史">版本历史</a> •
|
||||
<a href="#贡献作者">贡献作者</a> •
|
||||
<a href="#附注链接">附注/链接</a> •
|
||||
<a href="#授权协议">授权协议</a>
|
||||
</p>
|
||||
|
||||
---
|
||||
|
||||
## 使用说明
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<td>
|
||||
|
||||
###### OssForTypecho是基于Typecho附件功能的阿里云储存插件。除基本的文件附件上传、修改和删除功能,还支持自定义域名和HTTPS,可在Typecho 1.x版本下运行。
|
||||
|
||||
**使用方法**:
|
||||
##### 1. 下载本插件,放在 `usr/plugins/` 目录中,确保文件夹名为 OssForTypecho;
|
||||
##### 2. 激活插件,填写AccessKeyId、AccessKeySecret、Bucket名称、域名等配置;
|
||||
##### 3. 保存设置,使用附件上传功能即可看到效果。
|
||||
|
||||
**注意事项**:
|
||||
* ##### 请先在阿里云[用户信息管理](https://usercenter.console.aliyun.com/#/manage/ak)处获取AccessKeyID与AccessKeySecret(如有必要可使用RAM子账户密钥);
|
||||
* ##### 插件会替换所有之前上传的文件的链接,若启用插件前存在已上传的数据,请自行将其上传至OSS相同目录中以保证正常显示;同时,禁用插件也会导致链接恢复,也请自行将数据下载至相同目录中;
|
||||
* ##### 插件不会验证配置的正确性,请自行确认配置信息正确,否则不能正常使用。
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
## 版本历史
|
||||
|
||||
* v1.0.2 (20-6-26 [@jzwalk](https://github.com/jzwalk))
|
||||
* 增加附件url带**图片处理样式**后缀功能,合并1个衍生版改动:
|
||||
* 使用**2.3.1版SDK**,补全地域名称([@dragonflylee](https://github.com/dragonflylee))。
|
||||
* 增加**数据流上传**方式支持XMLRPC([@kraity](https://github.com/kraity));
|
||||
* 修复原版无法同步删除文件bug,自动设置读写权限([@AaronHoEng](https://github.com/AaronHoEng))。
|
||||
* v1.0.1 (18-08-08 [@jqjiang819](https://github.com/jqjiang819))
|
||||
* 升级OSS-PHP-SDK,使用phar包。
|
||||
* v1.0.0 (18-04-05 [@jqjiang819](https://github.com/jqjiang819))
|
||||
* 首次发布并上传至GitHub的Typecho-Fans目录,产生社区维护版。
|
||||
|
||||
## 贡献作者
|
||||
|
||||
[](https://github.com/jzwalk) | [](https://github.com/dragonflylee) | [](https://github.com/kraity) | [<img src="https://avatars1.githubusercontent.com/u/29192241?v=3" alt="AaronHoEng" height="100" />](https://github.com/AaronHoEng) | [](https://github.com/jqjiang819)
|
||||
:---:|:---:|:---:|:---:|:---:
|
||||
[jzwalk](https://github.com/jzwalk) (2020) | [dragonflylee](https://github.com/dragonflylee) (2019) | [kraity](https://github.com/kraity) (2019) | [AaronHoEng](https://github.com/AaronHoEng) (2019) | [jqjiang819](https://github.com/jqjiang819) (2018)
|
||||
|
||||
*为避免作者栏显示过长,插件信息仅选取登记2个署名,如有异议可协商修改。
|
||||
|
||||
## 附注/链接
|
||||
|
||||
本社区维护版已包含以下各版本功能并做优化调整:
|
||||
|
||||
* [Fork版(dragonflylee)](https://github.com/dragonflylee/typecho-plugin-ossfile) - 更新SDK和地域名称。
|
||||
* [Issues代码段(kraity)](https://github.com/jqjiang819/typecho-plugin-ossfile/issues/6) - 加入流上传方式支持。
|
||||
* [Issues代码段(AaronHoEng)](https://github.com/jqjiang819/typecho-plugin-ossfile/issues/3) - 解决删除问题,增加权限设置。
|
||||
* [原版](https://github.com/jqjiang819/typecho-plugin-ossfile) - 实现附件功能整合。
|
||||
|
||||
欢迎社区成员继续贡献代码参与更新。
|
||||
|
||||
另有基于原版的二次开发版插件[OssImg](https://github.com/v03413/Typecho_Plugins/tree/master/OssImg)可供参考。
|
||||
|
||||
## 授权协议
|
||||
|
||||
沿用原作声明的[MIT](https://github.com/jqjiang819/typecho-plugin-ossfile/blob/master/LICENSE)开源协议。(要求提及出处。)
|
||||
|
||||
> OssForTypecho附带MIT许可证,原作者保留著作权及相关权利。 © [Charmeryl](https://github.com/jqjiang819)
|
||||
BIN
OssForTypecho/aliyun-oss-php-sdk-2.3.1.phar
Normal file
BIN
OssForTypecho/aliyun-oss-php-sdk-2.3.1.phar
Normal file
Binary file not shown.
Reference in New Issue
Block a user