mirror of https://github.com/vtrois/kratos
feat: add object storage function
parent
00660e8755
commit
786bb69098
|
@ -32,6 +32,9 @@ require get_template_directory() . '/inc/theme-shortcode.php';
|
|||
// 添加导航目录
|
||||
require get_template_directory() . '/inc/theme-navwalker.php';
|
||||
|
||||
// 对象存储配置
|
||||
require get_template_directory() . '/inc/theme-dogecloud.php';
|
||||
|
||||
// SMTP 配置
|
||||
require get_template_directory() . '/inc/theme-smtp.php';
|
||||
|
||||
|
|
|
@ -16,6 +16,20 @@ jQuery(document).ready(function($) {
|
|||
$('#section-'+id+'_links').show();
|
||||
}
|
||||
|
||||
jQuery('#g_cos').click(function() {
|
||||
jQuery('#section-g_cos_bucketname').fadeToggle(400);
|
||||
jQuery('#section-g_cos_url').fadeToggle(400);
|
||||
jQuery('#section-g_cos_accesskey').fadeToggle(400);
|
||||
jQuery('#section-g_cos_secretkey').fadeToggle(400);
|
||||
});
|
||||
|
||||
if (jQuery('#g_cos:checked').val() !== undefined) {
|
||||
jQuery('#section-g_cos_bucketname').show();
|
||||
jQuery('#section-g_cos_url').show();
|
||||
jQuery('#section-g_cos_accesskey').show();
|
||||
jQuery('#section-g_cos_secretkey').show();
|
||||
}
|
||||
|
||||
jQuery('#g_cdn').click(function() {
|
||||
jQuery('#section-g_cdn_n3').fadeToggle(400);
|
||||
});
|
||||
|
|
|
@ -0,0 +1,151 @@
|
|||
<?php
|
||||
/**
|
||||
* dogecloud 对象存储
|
||||
* @author Seaton Jiang <seaton@vtrois.com>
|
||||
* @license MIT License
|
||||
* @version 2020.03.17
|
||||
*/
|
||||
|
||||
if (kratos_option('g_cos', false)) {
|
||||
function dogcloud_upload($object, $file, $mime){
|
||||
if (!@file_exists($file)) {
|
||||
return false;
|
||||
}
|
||||
if (@file_exists($file)) {
|
||||
$accessKey = kratos_option('g_cos_accesskey');
|
||||
$secretKey = kratos_option('g_cos_secretkey');
|
||||
$bucket = kratos_option('g_cos_bucketname');
|
||||
|
||||
$filesize = fileSize($file);
|
||||
$file = fopen($file, 'rb');
|
||||
|
||||
$signStr = "/oss/upload/put.json?bucket=$bucket&key=$object" . "\n" . "";
|
||||
$sign = hash_hmac('sha1', $signStr, $secretKey);
|
||||
$authorization = "TOKEN " . $accessKey . ":" . $sign;
|
||||
|
||||
$url = "https://api.dogecloud.com/oss/upload/put.json?bucket=$bucket&key=$object";
|
||||
$headers = array("Host: api.dogecloud.com", "Content-Type: $mime", "Authorization: $authorization");
|
||||
|
||||
$curl = curl_init();
|
||||
curl_setopt_array($curl, array(
|
||||
CURLOPT_URL => $url,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_PUT => true,
|
||||
CURLOPT_TIMEOUT => 0,
|
||||
CURLOPT_FOLLOWLOCATION => true,
|
||||
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
|
||||
CURLOPT_CUSTOMREQUEST => "PUT",
|
||||
CURLOPT_INFILE => $file,
|
||||
CURLOPT_INFILESIZE => $filesize,
|
||||
CURLOPT_HTTPHEADER => $headers,
|
||||
));
|
||||
$response = curl_exec($curl);
|
||||
curl_close($curl);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 上传附件
|
||||
function dogecloud_upload_attachments($metadata)
|
||||
{
|
||||
if (get_option('upload_path') == '.') {
|
||||
$metadata['file'] = str_replace("./", '', $metadata['file']);
|
||||
}
|
||||
|
||||
$object = str_replace("\\", '/', $metadata['file']);
|
||||
$object = str_replace(get_home_path(), '', $object);
|
||||
$file = get_home_path() . $object;
|
||||
$object = str_replace("wp-content/uploads/", '', $object);
|
||||
$mime = $metadata['type'];
|
||||
|
||||
dogcloud_upload('/' . $object, $file, $mime);
|
||||
|
||||
return $metadata;
|
||||
}
|
||||
|
||||
if (substr_count($_SERVER['REQUEST_URI'], '/update.php') <= 0) {
|
||||
add_filter('wp_handle_upload', 'dogecloud_upload_attachments', 50);
|
||||
}
|
||||
|
||||
// 上传缩略图
|
||||
function dogecloud_upload_thumbs($metadata)
|
||||
{
|
||||
if (isset($metadata['sizes']) && count($metadata['sizes']) > 0) {
|
||||
$wp_uploads = wp_upload_dir();
|
||||
$basedir = $wp_uploads['basedir'];
|
||||
$file_dir = $metadata['file'];
|
||||
$file_path = $basedir . '/' . dirname($file_dir) . '/';
|
||||
if (get_option('upload_path') == '.') {
|
||||
$file_path = str_replace("\\", '/', $file_path);
|
||||
$file_path = str_replace(get_home_path() . "./", '', $file_path);
|
||||
} else {
|
||||
$file_path = str_replace("\\", '/', $file_path);
|
||||
}
|
||||
$object_path = str_replace(get_home_path(), '', $file_path);
|
||||
foreach ($metadata['sizes'] as $val) {
|
||||
$object = '/' . $object_path . $val['file'];
|
||||
$object = str_replace("wp-content/uploads/", '', $object);
|
||||
$file = $file_path . $val['file'];
|
||||
$mime = $metadata['type'];
|
||||
|
||||
dogcloud_upload('/' . $object, $file, $mime);
|
||||
}
|
||||
}
|
||||
return $metadata;
|
||||
}
|
||||
|
||||
if (substr_count($_SERVER['REQUEST_URI'], '/update.php') <= 0) {
|
||||
add_filter('wp_generate_attachment_metadata', 'dogecloud_upload_thumbs', 100);
|
||||
}
|
||||
|
||||
// 删除文件
|
||||
function dogecloud_delete_remote_file($file)
|
||||
{
|
||||
$accessKey = kratos_option('g_cos_accesskey');
|
||||
$secretKey = kratos_option('g_cos_secretkey');
|
||||
$bucket = kratos_option('g_cos_bucketname');
|
||||
|
||||
$file = str_replace("\\", '/', $file);
|
||||
$file = str_replace(get_home_path(), '', $file);
|
||||
$del_file_path = str_replace("wp-content/uploads/", '', $file);
|
||||
$del_file_body = "[\"$del_file_path\"]";
|
||||
|
||||
$signStr = "/oss/file/delete.json?bucket=$bucket" . "\n" . $del_file_body;
|
||||
$sign = hash_hmac('sha1', $signStr, $secretKey);
|
||||
$authorization = "TOKEN " . $accessKey . ":" . $sign;
|
||||
|
||||
$url = "https://api.dogecloud.com/oss/file/delete.json?bucket=$bucket";
|
||||
$headers = array('Host' => 'api.dogecloud.com', 'Content-Type' => 'application/json', 'Authorization' => $authorization);
|
||||
|
||||
$request = new WP_Http;
|
||||
$result = $request->request($url, array('method' => 'POST', 'body' => $del_file_body, 'headers' => $headers));
|
||||
|
||||
return $file;
|
||||
}
|
||||
add_action('wp_delete_file', 'dogecloud_delete_remote_file', 100);
|
||||
|
||||
// 修改图片地址
|
||||
function custom_upload_dir($uploads)
|
||||
{
|
||||
$upload_path = '';
|
||||
$upload_url_path = kratos_option('g_cos_url');
|
||||
|
||||
if (empty($upload_path) || 'wp-content/uploads' == $upload_path) {
|
||||
$uploads['basedir'] = WP_CONTENT_DIR . '/uploads';
|
||||
} elseif (0 !== strpos($upload_path, ABSPATH)) {
|
||||
$uploads['basedir'] = path_join(ABSPATH, $upload_path);
|
||||
} else {
|
||||
$uploads['basedir'] = $upload_path;
|
||||
}
|
||||
|
||||
$uploads['path'] = $uploads['basedir'] . $uploads['subdir'];
|
||||
|
||||
if ($upload_url_path) {
|
||||
$uploads['baseurl'] = $upload_url_path;
|
||||
$uploads['url'] = $uploads['baseurl'] . $uploads['subdir'];
|
||||
}
|
||||
return $uploads;
|
||||
}
|
||||
add_filter('upload_dir', 'custom_upload_dir');
|
||||
}
|
|
@ -108,6 +108,43 @@ function kratos_options()
|
|||
'type' => 'checkbox',
|
||||
);
|
||||
|
||||
$options[] = array(
|
||||
'name' => __('媒体资源托管', 'kratos'),
|
||||
'desc' => __('是否将媒体资源托管到 DogeCloud 存储', 'kratos'),
|
||||
'id' => 'g_cos',
|
||||
'std' => '0',
|
||||
'type' => 'checkbox',
|
||||
);
|
||||
|
||||
$options[] = array(
|
||||
'name' => __('存储空间名称', 'kratos'),
|
||||
'id' => 'g_cos_bucketname',
|
||||
'class' => 'hidden',
|
||||
'type' => 'text',
|
||||
);
|
||||
|
||||
$options[] = array(
|
||||
'name' => __('加速域名', 'kratos'),
|
||||
'id' => 'g_cos_url',
|
||||
'placeholder' => __('例如:https://cdn.xxx.com', 'kratos'),
|
||||
'class' => 'hidden',
|
||||
'type' => 'text',
|
||||
);
|
||||
|
||||
$options[] = array(
|
||||
'name' => __('AccessKey', 'kratos'),
|
||||
'id' => 'g_cos_accesskey',
|
||||
'class' => 'hidden',
|
||||
'type' => 'text',
|
||||
);
|
||||
|
||||
$options[] = array(
|
||||
'name' => __('SecretKey', 'kratos'),
|
||||
'id' => 'g_cos_secretkey',
|
||||
'class' => 'hidden',
|
||||
'type' => 'password',
|
||||
);
|
||||
|
||||
$options[] = array(
|
||||
'name' => __('Gutenberg 编辑器', 'kratos'),
|
||||
'desc' => __('开启 Gutenberg 编辑器', 'kratos'),
|
||||
|
|
Loading…
Reference in New Issue