Skip to content

Commit

Permalink
1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
AyagawaSeirin committed Jan 20, 2020
1 parent 7d2a216 commit d8381ed
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 25 deletions.
82 changes: 62 additions & 20 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @package UploadGithubForTypecho
* @author AyagawaSeirin
* @link https://qwq.best/
* @version 1.0.1
* @version 1.0.2
* @dependence 1.0-*
*
*/
Expand All @@ -15,6 +15,7 @@ class UploadGithubForTypecho_Plugin implements Typecho_Plugin_Interface
//上传文件目录
const UPLOAD_DIR = '/usr/uploads';


/**
* 插件激活接口
*/
Expand Down Expand Up @@ -76,7 +77,7 @@ public static function config(Typecho_Widget_Helper_Form $form)
async: true,
type: "GET",
success: function (data) {
var now = "1.0.1";
var now = "1.0.2";
var newest = data[0][\'tag_name\'];
if(newest == null){
notice = "检查更新失败,请手动访问插件项目地址获取更新。";
Expand All @@ -98,7 +99,7 @@ public static function config(Typecho_Widget_Helper_Form $form)
';
$desc1 = new Typecho_Widget_Helper_Form_Element_Text('desc1', NULL, '', _t('插件使用说明:'),_t("
$desc1 = new Typecho_Widget_Helper_Form_Element_Text('desc1', NULL, '', _t('插件使用说明:'), _t("
<ol>
<li>本插件用于将文章附件(如图片)上传至您的(公开的)Github的仓库中,并使用jsDelivr访问仓库文件达到优化文件访问速度的目的。了解jsDelivr应用于博客中的优势,您可以<a href='https://qwq.best/dev/113.html' target='_blank'>点击这里</a>。<br></li>
<li>项目地址:<a href='https://github.com/AyagawaSeirin/UploadGithubForTypecho' target='_blank'>https://github.com/AyagawaSeirin/UploadGithubForTypecho</a><br></li>
Expand Down Expand Up @@ -149,8 +150,10 @@ public static function uploadHandle($file)
$options = Typecho_Widget::widget('Widget_Options')->plugin('UploadGithubForTypecho');
//获取文件名
$date = new Typecho_Date($options->gmtTime);
$fileDir = self::getUploadDir() . '/' . $date->year . '/' . $date->month;
$fileDir_relatively = self::getUploadDir(true) . '/' . $date->year . '/' . $date->month;
$fileDir = self::getUploadDir(false) . '/' . $date->year . '/' . $date->month;
$fileName = sprintf('%u', crc32(uniqid())) . '.' . $ext;
$path_relatively = $fileDir_relatively . '/' . $fileName;
$path = $fileDir . '/' . $fileName;
//获得上传文件
$uploadfile = self::getUploadFile($file);
Expand Down Expand Up @@ -187,16 +190,21 @@ public static function uploadHandle($file)
curl_close($ch);

/* 写到本地文件 */
if (!is_dir($fileDir)){
mkdir($fileDir,0777,true);
if (!is_dir($fileDir)) {
if (self::makeUploadDir($fileDir)) {
file_put_contents($path, $fileContent);
} else {
//目录创建失败,不写文件
}
} else {
file_put_contents($path, $fileContent);
}
file_put_contents(__TYPECHO_ROOT_DIR__ . $path, $fileContent);


//返回相对存储路径
return array(
'name' => $file['name'],
'path' => $path,
'path' => $path_relatively,
'size' => $file['size'],
'type' => $ext,
'mime' => @Typecho_Common::mimeContentType($path)
Expand Down Expand Up @@ -231,8 +239,8 @@ public static function modifyHandle($content, $file)
$fileContent = file_get_contents($uploadfile);

//判断仓库内相对路径
$filename = __TYPECHO_ROOT_DIR__. $content['attachment']->path;//本地文件绝对路径
$github_path = $options->githubDirectory . str_replace(self::getUploadDir(),"",$content['attachment']->path);
$filename = __TYPECHO_ROOT_DIR__ . $content['attachment']->path;//本地文件绝对路径
$github_path = $options->githubDirectory . str_replace(self::getUploadDir(), "", $content['attachment']->path);

//获取文件sha
$header = array(
Expand All @@ -245,13 +253,13 @@ public static function modifyHandle($content, $file)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = json_decode(curl_exec($ch),true);
$output = json_decode(curl_exec($ch), true);
curl_close($ch);
$sha = $output['sha'];

/* 更新Github仓库内文件 */
$data = array(
"message" => "Update file " . str_replace(self::getUploadDir(),"",$content['attachment']->path),
"message" => "Update file " . str_replace(self::getUploadDir(), "", $content['attachment']->path),
"content" => base64_encode($fileContent),
"sha" => $sha,
);
Expand Down Expand Up @@ -304,8 +312,8 @@ public static function deleteHandle(array $content)
$options = Typecho_Widget::widget('Widget_Options')->plugin('UploadGithubForTypecho');

//判断仓库内相对路径
$filename = __TYPECHO_ROOT_DIR__. $content['attachment']->path;
$github_path = $options->githubDirectory . str_replace(self::getUploadDir(),"",$content['attachment']->path);
$filename = __TYPECHO_ROOT_DIR__ . $content['attachment']->path;
$github_path = $options->githubDirectory . str_replace(self::getUploadDir(), "", $content['attachment']->path);

//获取文件sha
$header = array(
Expand All @@ -318,7 +326,7 @@ public static function deleteHandle(array $content)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = json_decode(curl_exec($ch),true);
$output = json_decode(curl_exec($ch), true);
curl_close($ch);
$sha = $output['sha'];

Expand Down Expand Up @@ -373,7 +381,7 @@ public static function attachmentHandle($content)
//获取设置参数
$options = Typecho_Widget::widget('Widget_Options')->plugin('UploadGithubForTypecho');
$latest = "";
if($options->urlType == "latest"){
if ($options->urlType == "latest") {
$latest = "@latest";
}
return Typecho_Common::url($content['attachment']->path, "https://cdn.jsdelivr.net/gh/" . $options->githubUser . "/" . $options->githubRepo . $latest);
Expand All @@ -395,12 +403,17 @@ private static function getSafeName(&$name)
/**
* 获取文件上传目录
*/
private static function getUploadDir()
private static function getUploadDir($relatively = true)
{
if (defined('__TYPECHO_UPLOAD_DIR__')) {
return __TYPECHO_UPLOAD_DIR__;
if ($relatively) {
if (defined('__TYPECHO_UPLOAD_DIR__')) {
return __TYPECHO_UPLOAD_DIR__;
} else {
return self::UPLOAD_DIR;
}
} else {
return self::UPLOAD_DIR;
return Typecho_Common::url(defined('__TYPECHO_UPLOAD_DIR__') ? __TYPECHO_UPLOAD_DIR__ : self::UPLOAD_DIR,
defined('__TYPECHO_UPLOAD_ROOT_DIR__') ? __TYPECHO_UPLOAD_ROOT_DIR__ : __TYPECHO_ROOT_DIR__);
}
}

Expand All @@ -411,4 +424,33 @@ private static function getUploadFile($file)
{
return isset($file['tmp_name']) ? $file['tmp_name'] : (isset($file['bytes']) ? $file['bytes'] : (isset($file['bits']) ? $file['bits'] : ''));
}

/**
* 创建上传路径
*/
private static function makeUploadDir($path)
{
$path = preg_replace("/\\\+/", '/', $path);
$current = rtrim($path, '/');
$last = $current;

while (!is_dir($current) && false !== strpos($path, '/')) {
$last = $current;
$current = dirname($current);
}

if ($last == $current) {
return true;
}

if (!@mkdir($last)) {
return false;
}

$stat = @stat($last);
$perms = $stat['mode'] & 0007777;
@chmod($last, $perms);

return self::makeUploadDir($path);
}
}
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ jsDelivr为融合CDN,在全球分布750余节点(包括中国),并Github
本插件利用此服务来加速文章附件(图片等)访问速度。
关于jsDelivr运用于博客的优势本文不再赘述,具体请[访问这里](https://qwq.best/dev/113.html "访问这里")。<br>

## 版本记录
v1.0.1 - 2019.1.20<br>
v1.0.0 - 2019.1.20<br>
## 最新版本
1.0.2 - 2020.01.20
(因为出了各种BUG,一次更新好几个版本...)

## 安装插件
项目地址:<https://github.com/AyagawaSeirin/UploadGithubForTypecho><br>
Expand All @@ -36,8 +36,9 @@ Github仓库内的上传目录:必填,附件上传到的仓库内目录位

## 使用说明
插件**不会验证配置的正确性**,请**自行确认配置信息正确**,否则不能正常使用。<br>
插件会**替换所有之前上传的文件的链接**,若启用插件前存在已上传的文件,**请自行将其上传至仓库相同目录中以保证正常显示**;同时,禁用插件也会导致链接恢复。插件在上传文件同时也会在本地相应位置留下文件,所以禁用插件后文件链接恢复**不用担心**文件不存在问题。<br>
注意:由于CDN缓存问题,修改文件后访问链接可能仍然是旧文件,所以**建议删掉旧文件再上传新文件****不建议使用修改文件功能**。jsDelivr刷新缓存功能暂未推出,推出后本插件会及时更新。<br>
插件会**替换所有之前上传的文件的链接**,若启用插件前存在已上传的文件,**请自行将其上传至仓库相同目录中以保证正常显示**;同时,禁用插件也会导致链接恢复。<br>
**由于Linux权限管理问题,将文件写入本地时可能出现无法创建目录的情况,导致文件无法正常写入。请给予uploads目录777权限`chmod -R 777 runtime`**<br>
由于CDN缓存问题,修改文件后访问链接可能仍然是旧文件,所以**建议删掉旧文件再上传新文件****不建议使用修改文件功能**。jsDelivr刷新缓存功能暂未推出,推出后本插件会及时更新。<br>
在插件设置页面会自动检查更新,若检查失败请手动前往项目地址检查更新。<br>

## 版权声明
Expand Down

0 comments on commit d8381ed

Please sign in to comment.