From 203fea7df3404dbd3436b51e4e3a2b61ca6dfbd6 Mon Sep 17 00:00:00 2001 From: Luffy <52o@qq52o.cn> Date: Sat, 17 Aug 2024 08:52:08 +0800 Subject: [PATCH] Support upload to subdirectories (#72) --- README.md | 3 ++- cos-commands.php | 1 - readme.txt | 7 +++--- sync-qcloud-cos.php | 60 ++++++++++++++++++++++++++++++++------------- 4 files changed, 49 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 3eeaa22..eba7247 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,8 @@ - [x] 支持文本内容审核 - [x] 支持原图保护 - [x] 支持数据监控 -- [x] 支持 `wp-cli` 命令上传文件 +- [x] 支持使用 `wp-cli` 命令上传/删除文件 +- [x] 支持上传文件到存储桶子目录 ## 安装 diff --git a/cos-commands.php b/cos-commands.php index 96db97c..08253f6 100644 --- a/cos-commands.php +++ b/cos-commands.php @@ -6,7 +6,6 @@ class COS_CLI_Commands { - /** * 同步文件夹到 COS * diff --git a/readme.txt b/readme.txt index 8dccb11..d8faa2e 100644 --- a/readme.txt +++ b/readme.txt @@ -5,7 +5,7 @@ Tags: COS, 腾讯云, 对象存储, Tencent, Qcloud Requires at least: 4.6 Tested up to: 6.6 Requires PHP: 7.2 -Stable tag: 2.5.8 +Stable tag: 2.6.0 License: Apache2.0 License URI: http://www.apache.org/licenses/LICENSE-2.0.html @@ -35,7 +35,8 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html 13. 支持文本内容审核 14. 支持原图保护 15. 支持数据监控 -16. 支持 `wp-cli` 命令上传文件 +16. 支持使用 `wp-cli` 命令上传/删除文件 +17. 支持上传文件到存储桶子目录 插件更多详细介绍和安装:[https://github.com/sy-records/sync-qcloud-cos](https://github.com/sy-records/sync-qcloud-cos) @@ -103,7 +104,7 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html = Stable = -- Use wp_get_mime_types instead of get_allowed_mime_types. +- Support upload to subdirectories. = Other = diff --git a/sync-qcloud-cos.php b/sync-qcloud-cos.php index 4b8f5a8..b7105bc 100644 --- a/sync-qcloud-cos.php +++ b/sync-qcloud-cos.php @@ -3,7 +3,7 @@ Plugin Name: Sync QCloud COS Plugin URI: https://qq52o.me/2518.html Description: 使用腾讯云对象存储服务 COS 作为附件存储空间。(Using Tencent Cloud Object Storage Service COS as Attachment Storage Space.) -Version: 2.5.8 +Version: 2.6.0 Author: 沈唁 Author URI: https://qq52o.me License: Apache2.0 @@ -27,7 +27,7 @@ use SyncQcloudCos\Monitor\DataPoints; use SyncQcloudCos\Object\Head; -define('COS_VERSION', '2.5.8'); +define('COS_VERSION', '2.6.0'); define('COS_PLUGIN_SLUG', 'sync-qcloud-cos'); define('COS_PLUGIN_PAGE', plugin_basename(dirname(__FILE__)) . '%2F' . basename(__FILE__)); @@ -52,6 +52,7 @@ function cos_set_options() 'nothumb' => 'false', // 是否上传缩略图 'nolocalsaving' => 'false', // 是否保留本地备份 'delete_options' => 'true', + 'upload_subdirectory' => '', 'upload_url_path' => '', // URL前缀 'update_file_name' => 'false', // 是否重命名文件名 'ci_style' => '', @@ -255,11 +256,15 @@ function cos_file_upload($object, $filename, $no_local_file = false) if (!@file_exists($filename)) { return false; } - $bucket = cos_get_bucket_name(); + $options = get_option('cos_options', true); + $bucket = cos_get_bucket_name($options); try { $file = fopen($filename, 'rb'); if ($file) { - $cosClient = cos_get_client(); + if (!empty($options['upload_subdirectory'])) { + $object = '/' . esc_attr($options['upload_subdirectory']) . $object; + } + $cosClient = cos_get_client($options); $cosClient->upload($bucket, $object, $file); if (is_resource($file)) { @@ -293,7 +298,7 @@ function cos_is_delete_local_file() /** * 删除本地文件 * - * @param $file + * @param string $file * @return bool */ function cos_delete_local_file($file) @@ -317,12 +322,16 @@ function cos_delete_local_file($file) /** * 删除cos中的单个文件 - * @param $file + * @param string $file */ function cos_delete_cos_file($file) { - $bucket = cos_get_bucket_name(); - $cosClient = cos_get_client(); + $options = get_option('cos_options', true); + $bucket = cos_get_bucket_name($options); + $cosClient = cos_get_client($options); + if (!empty($options['upload_subdirectory'])) { + $file = esc_attr($options['upload_subdirectory']) . '/' . ltrim($file, '/'); + } $cosClient->deleteObject(['Bucket' => $bucket, 'Key' => $file]); } @@ -332,13 +341,17 @@ function cos_delete_cos_file($file) */ function cos_delete_cos_files(array $files) { + $options = get_option('cos_options', true); + $subdirectory = !empty($options['upload_subdirectory']) ? esc_attr($options['upload_subdirectory']) . '/' : ''; + $deleteObjects = []; foreach ($files as $file) { - $deleteObjects[] = ['Key' => str_replace(["\\", './'], ['/', ''], $file)]; + $fileKey = str_replace(["\\", './'], ['/', ''], $subdirectory . $file); + $deleteObjects[] = ['Key' => $fileKey]; } - $bucket = cos_get_bucket_name(); - $cosClient = cos_get_client(); + $bucket = cos_get_bucket_name($options); + $cosClient = cos_get_client($options); $cosClient->deleteObjects(['Bucket' => $bucket, 'Objects' => $deleteObjects]); } @@ -486,9 +499,6 @@ function cos_delete_remote_attachment($post_id) // 获取图片类附件的meta信息 $meta = wp_get_attachment_metadata($post_id); $upload_path = cos_get_option('upload_path'); - if ($upload_path == '') { - $upload_path = 'wp-content/uploads'; - } if (!empty($meta['file'])) { $deleteObjects = []; @@ -523,19 +533,22 @@ function cos_delete_remote_attachment($post_id) // 获取链接删除 $link = wp_get_attachment_url($post_id); if ($link) { + $cos_options = get_option('cos_options', true); + $subdirectory = !empty($cos_options['upload_subdirectory']) ? '/' . esc_attr($cos_options['upload_subdirectory']) : ''; if ($upload_path != '.') { $file_info = explode($upload_path, $link); if (count($file_info) >= 2) { - cos_delete_cos_file($upload_path . end($file_info)); + $file = $subdirectory . $upload_path . end($file_info); } } else { - $cos_options = get_option('cos_options', true); $cos_upload_url = esc_attr($cos_options['upload_url_path']); $file_info = explode($cos_upload_url, $link); if (count($file_info) >= 2) { - cos_delete_cos_file(end($file_info)); + $file = $subdirectory . end($file_info); } } + + cos_delete_cos_file($file); } } } @@ -1479,6 +1492,7 @@ function cos_setting_page() //仅用于插件卸载时比较使用 $options['upload_url_path'] = isset($_POST['upload_url_path']) ? sanitize_text_field(stripslashes($_POST['upload_url_path'])) : ''; + $options['upload_subdirectory'] = isset($_POST['upload_subdirectory']) ? sanitize_text_field(trim($_POST['upload_subdirectory'], '/')) : ''; $options['ci_style'] = isset($_POST['ci_style']) ? sanitize_text_field($_POST['ci_style']) : ''; $options['update_file_name'] = isset($_POST['update_file_name']) ? sanitize_text_field($_POST['update_file_name']) : 'false'; $options['origin_protect'] = isset($_POST['origin_protect']) ? sanitize_text_field($_POST['origin_protect']) : 'off'; @@ -1576,6 +1590,7 @@ function cos_setting_page() $check_origin_protect = esc_attr($cos_options['origin_protect'] ?? 'off') !== 'off' ? 'checked="checked"' : ''; $cos_update_file_name = esc_attr($cos_options['update_file_name']); + $cos_upload_subdirectory = esc_attr($cos_options['upload_subdirectory'] ?? ''); $protocol = cos_get_url_scheme(); @@ -1735,6 +1750,17 @@ function cos_setting_page()

3)如果需要使用 独立域名 ,直接将 {cos域名} 替换为 独立域名 即可。

+ + + 上传至子目录 + + + + +

如果需要多个站点共用一个存储桶时设置,例如:sub1sub1/sub2(注意不要以“/”开头和结尾),支持多级;

+

如果设置了上传至子目录,需要在 URL前缀 中增加对应的子目录,例如:{cos域名}/{子目录}/{本地文件夹}

+ + 图片处理样式