Fix missing delete original image

pull/24/head
sy-records 2023-10-18 08:38:01 +08:00
parent 935d8c8807
commit 2b12c92061
No known key found for this signature in database
GPG Key ID: C3BB4FF13CD72ACE
3 changed files with 144 additions and 107 deletions

View File

@ -12,15 +12,15 @@
## 插件特色 ## 插件特色
* [x] 可配置是否上传缩略图和是否保留本地备份 - [x] 可配置是否上传缩略图和是否保留本地备份
* [x] 本地删除可同步删除阿里云对象存储OSS中的文件 - [x] 本地删除可同步删除阿里云对象存储 OSS 中的文件
* [x] 支持阿里云对象存储OSS绑定的用户域名 - [x] 支持阿里云对象存储 OSS 绑定的用户域名
* [x] 支持替换数据库中旧的资源链接地址 - [x] 支持替换数据库中旧的资源链接地址
* [x] 支持阿里云对象存储OSS完整地域使用 - [x] 支持阿里云对象存储 OSS 完整地域使用
* [x] 支持同步历史附件到阿里云对象存储OSS - [x] 支持同步历史附件到阿里云对象存储 OSS
* [x] 支持阿里云OSS图片处理 - [x] 支持阿里云 OSS 图片处理
* [x] 支持上传文件自动重命名 - [x] 支持上传文件自动重命名
* [x] 支持使用RAM操作 - [x] 支持使用 RAM 操作
## 安装 ## 安装
@ -36,16 +36,16 @@ GitHub 下载节点:[https://github.com/sy-records/aliyun-oss-wordpress/releas
## 修改配置 ## 修改配置
* 方法一:在 WordPress 插件管理页面有设置按钮,进行设置 - 方法一:在 WordPress 插件管理页面有设置按钮,进行设置
* 方法二:在 WordPress 后台管理左侧导航栏`设置`下`阿里云OSS设置`,点击进入设置页面 - 方法二:在 WordPress 后台管理左侧导航栏`设置`下`阿里云OSS设置`,点击进入设置页面
## 常见问题 ## 常见问题
* 通过文件URL访问图片无法预览而是以附件形式下载? - 通过文件 URL 访问图片无法预览而是以附件形式下载?
因为 Bucket 是 2019 年 9 月 23 日后创建的,使用默认域名时会自动下载,需要绑定自有域名访问。具体参考[阿里云文档](https://help.aliyun.com/document_detail/142631.html)。 因为 Bucket 是 2019 年 9 月 23 日后创建的,使用默认域名时会自动下载,需要绑定自有域名访问。具体参考[阿里云文档](https://help.aliyun.com/document_detail/142631.html)。
* 如果存在第三方插件或者主题自带上传功能,内容上传到`本地文件夹`(即默认`wp-content/uploads`)中怎么上传到oss中 - 如果存在第三方插件或者主题自带上传功能,内容上传到`本地文件夹`(即默认`wp-content/uploads`)中,怎么上传到 oss 中?
解决方案有两种,推荐使用第二种。 解决方案有两种,推荐使用第二种。

View File

@ -3,7 +3,7 @@
Plugin Name: OSS Aliyun Plugin Name: OSS Aliyun
Plugin URI: https://github.com/sy-records/aliyun-oss-wordpress Plugin URI: https://github.com/sy-records/aliyun-oss-wordpress
Description: 使用阿里云对象存储 OSS 作为附件存储空间。This is a plugin that uses Aliyun Object Storage Service for attachments remote saving. Description: 使用阿里云对象存储 OSS 作为附件存储空间。This is a plugin that uses Aliyun Object Storage Service for attachments remote saving.
Version: 1.4.3 Version: 1.4.4
Author: 沈唁 Author: 沈唁
Author URI: https://qq52o.me Author URI: https://qq52o.me
License: Apache2.0 License: Apache2.0
@ -20,7 +20,7 @@ use OSS\Credentials\CredentialsProvider;
use AlibabaCloud\Credentials\Credential; use AlibabaCloud\Credentials\Credential;
use OSS\Credentials\StaticCredentialsProvider; use OSS\Credentials\StaticCredentialsProvider;
define('OSS_VERSION', '1.4.3'); define('OSS_VERSION', '1.4.4');
define('OSS_BASEFOLDER', plugin_basename(dirname(__FILE__))); define('OSS_BASEFOLDER', plugin_basename(dirname(__FILE__)));
if (!function_exists('get_home_path')) { if (!function_exists('get_home_path')) {
@ -111,9 +111,9 @@ function oss_get_bucket_name()
} }
/** /**
* @param $object * @param string $object
* @param $file * @param string $file
* @param false $no_local_file * @param bool $no_local_file
*/ */
function oss_file_upload($object, $file, $no_local_file = false) function oss_file_upload($object, $file, $no_local_file = false)
{ {
@ -124,7 +124,7 @@ function oss_file_upload($object, $file, $no_local_file = false)
$bucket = oss_get_bucket_name(); $bucket = oss_get_bucket_name();
$ossClient = oss_get_client(); $ossClient = oss_get_client();
try{ try{
$ossClient->uploadFile($bucket, ltrim($object, "/"), $file); $ossClient->uploadFile($bucket, ltrim($object, '/'), $file);
} catch (OssException $e) { } catch (OssException $e) {
if (WP_DEBUG) { if (WP_DEBUG) {
echo 'Error Message: ', $e->getMessage(), PHP_EOL, 'Error Code: ', $e->getCode(); echo 'Error Message: ', $e->getMessage(), PHP_EOL, 'Error Code: ', $e->getCode();
@ -254,8 +254,12 @@ function oss_upload_thumbs($metadata)
//获取上传路径 //获取上传路径
$wp_uploads = wp_upload_dir(); $wp_uploads = wp_upload_dir();
$basedir = $wp_uploads['basedir']; $basedir = $wp_uploads['basedir'];
//获取oss插件的配置信息 //获取oss插件的配置信息
$oss_options = get_option('oss_options', true); $oss_options = get_option('oss_options', true);
$no_local_file = esc_attr($oss_options['nolocalsaving']) == 'true';
$no_thumb = esc_attr($oss_options['nothumb']) == 'true';
if (!empty($metadata['file'])) { if (!empty($metadata['file'])) {
// Maybe there is a problem with the old version // Maybe there is a problem with the old version
$file = $basedir . '/' . $metadata['file']; $file = $basedir . '/' . $metadata['file'];
@ -270,16 +274,9 @@ function oss_upload_thumbs($metadata)
$file = str_replace('./', '', $file); $file = str_replace('./', '', $file);
} }
oss_file_upload($object, $file, esc_attr($oss_options['nolocalsaving']) == 'true'); oss_file_upload($object, $file, $no_local_file);
}
//上传所有缩略图
if (!empty($metadata['sizes'])) {
//是否需要上传缩略图
$nothumb = esc_attr($oss_options['nothumb']) == 'true';
//如果禁止上传缩略图,就不用继续执行了
if ($nothumb) {
return $metadata;
} }
//得到本地文件夹和远端文件夹 //得到本地文件夹和远端文件夹
$dirname = dirname($metadata['file']); $dirname = dirname($metadata['file']);
$file_path = $dirname != '.' ? "{$basedir}/{$dirname}/" : "{$basedir}/"; $file_path = $dirname != '.' ? "{$basedir}/{$dirname}/" : "{$basedir}/";
@ -287,9 +284,18 @@ function oss_upload_thumbs($metadata)
if ($upload_path == '.') { if ($upload_path == '.') {
$file_path = str_replace('./', '', $file_path); $file_path = str_replace('./', '', $file_path);
} }
$object_path = str_replace(get_home_path(), '', $file_path); $object_path = str_replace(get_home_path(), '', $file_path);
if (!empty($metadata['original_image'])) {
oss_file_upload("/{$object_path}{$metadata['original_image']}", "{$file_path}{$metadata['original_image']}", $no_local_file);
}
if ($no_thumb) {
return $metadata;
}
//上传所有缩略图
if (!empty($metadata['sizes'])) {
//there may be duplicated filenames,so .... //there may be duplicated filenames,so ....
foreach ($metadata['sizes'] as $val) { foreach ($metadata['sizes'] as $val) {
//生成object在oss中的存储路径 //生成object在oss中的存储路径
@ -298,7 +304,7 @@ function oss_upload_thumbs($metadata)
$file = $file_path . $val['file']; $file = $file_path . $val['file'];
//执行上传操作 //执行上传操作
oss_file_upload($object, $file, esc_attr($oss_options['nolocalsaving']) == 'true'); oss_file_upload($object, $file, $no_local_file);
} }
} }
return $metadata; return $metadata;
@ -351,11 +357,17 @@ function oss_delete_remote_attachment($post_id)
$deleteObjects[] = str_replace("\\", '/', $file_path); $deleteObjects[] = str_replace("\\", '/', $file_path);
$dirname = dirname($file_path) . '/';
// 超大图原图
if (!empty($meta['original_image'])) {
$deleteObjects[] = ['Key' => str_replace("\\", '/', $dirname . $meta['original_image'])];
}
// 删除缩略图 // 删除缩略图
if (!empty($meta['sizes'])) { if (!empty($meta['sizes'])) {
foreach ($meta['sizes'] as $val) { foreach ($meta['sizes'] as $val) {
$size_file = dirname($file_path) . '/' . $val['file']; $deleteObjects[] = str_replace("\\", '/', $dirname . $val['file']);
$deleteObjects[] = str_replace("\\", '/', $size_file);
} }
} }
@ -772,7 +784,7 @@ function oss_setting_page()
</tr> </tr>
<tr> <tr>
<th><legend>保存/更新选项</legend></th> <th><legend>保存/更新选项</legend></th>
<td><input type="submit" name="submit" class="button button-primary" value="保存更改"/></td> <td><input type="submit" class="button button-primary" value="保存更改"/></td>
</tr> </tr>
</table> </table>
<input type="hidden" name="type" value="oss_set"> <input type="hidden" name="type" value="oss_set">
@ -785,7 +797,7 @@ function oss_setting_page()
</th> </th>
<input type="hidden" name="type" value="aliyun_oss_all"> <input type="hidden" name="type" value="aliyun_oss_all">
<td> <td>
<input type="submit" name="submit" class="button button-secondary" value="开始同步"/> <input type="submit" class="button button-secondary" value="开始同步"/>
<p><b>注意:如果是首次同步,执行时间将会十分十分长(根据你的历史附件数量),有可能会因执行时间过长,页面显示超时或者报错。<br> 所以,建议那些几千上万附件的大神们,考虑官方的 <a target="_blank" rel="nofollow" href="https://help.aliyun.com/knowledge_detail/39628.html">同步工具</a></b></p> <p><b>注意:如果是首次同步,执行时间将会十分十分长(根据你的历史附件数量),有可能会因执行时间过长,页面显示超时或者报错。<br> 所以,建议那些几千上万附件的大神们,考虑官方的 <a target="_blank" rel="nofollow" href="https://help.aliyun.com/knowledge_detail/39628.html">同步工具</a></b></p>
</td> </td>
</tr> </tr>
@ -816,7 +828,7 @@ function oss_setting_page()
</th> </th>
<input type="hidden" name="type" value="aliyun_oss_replace"> <input type="hidden" name="type" value="aliyun_oss_replace">
<td> <td>
<input type="submit" name="submit" class="button button-secondary" value="开始替换"/> <input type="submit" class="button button-secondary" value="开始替换"/>
<p><b>注意:如果是首次替换,请注意备份!此功能会替换文章以及设置的特色图片(题图)等使用的资源链接</b></p> <p><b>注意:如果是首次替换,请注意备份!此功能会替换文章以及设置的特色图片(题图)等使用的资源链接</b></p>
</td> </td>
</tr> </tr>

View File

@ -5,7 +5,7 @@ Tags: oss, 阿里云, 对象存储, aliyun
Requires at least: 4.2 Requires at least: 4.2
Tested up to: 6.3 Tested up to: 6.3
Requires PHP: 7.0.0 Requires PHP: 7.0.0
Stable tag: 1.4.3 Stable tag: 1.4.4
License: Apache2.0 License: Apache2.0
License URI: http://www.apache.org/licenses/LICENSE-2.0.html License URI: http://www.apache.org/licenses/LICENSE-2.0.html
@ -15,7 +15,7 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html
使用阿里云对象存储 OSS 作为附件存储空间。This is a plugin that uses Aliyun Object Storage Service for attachments remote saving. 使用阿里云对象存储 OSS 作为附件存储空间。This is a plugin that uses Aliyun Object Storage Service for attachments remote saving.
* 依赖阿里云OSS服务https://www.aliyun.com/product/oss - 依赖阿里云 OSS 服务https://www.aliyun.com/product/oss
## 插件特点 ## 插件特点
@ -27,7 +27,8 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html
6. 支持同步历史附件到阿里云对象存储 OSS 6. 支持同步历史附件到阿里云对象存储 OSS
7. 支持阿里云 OSS 图片处理 7. 支持阿里云 OSS 图片处理
8. 支持上传文件自动重命名 8. 支持上传文件自动重命名
9. 插件更多详细介绍和安装:[https://github.com/sy-records/aliyun-oss-wordpress](https://github.com/sy-records/aliyun-oss-wordpress) 9. 支持使用 RAM 操作
10. 插件更多详细介绍和安装:[https://github.com/sy-records/aliyun-oss-wordpress](https://github.com/sy-records/aliyun-oss-wordpress)
## 其他插件 ## 其他插件
@ -50,8 +51,8 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html
== Screenshots == == Screenshots ==
1. screenshot-1.png 1. 设置页面
2. screenshot-2.png 2. 数据库同步
== Frequently Asked Questions == == Frequently Asked Questions ==
@ -72,81 +73,105 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html
== Changelog == == Changelog ==
= 1.4.4 =
- 修复超大文件原图上传和删除
= 1.4.3 = = 1.4.3 =
* 修复同步错误
* 更新地域 - 修复同步错误
- 更新地域
= 1.4.2 = = 1.4.2 =
* 优化同步代码逻辑
* 修复 webp 和 heic 格式图片上传缩略图失败问题 - 优化同步代码逻辑
- 修复 webp 和 heic 格式图片上传缩略图失败问题
= 1.4.1 = = 1.4.1 =
* 支持媒体库编辑图片上传
- 支持媒体库编辑图片上传
= 1.4.0 = = 1.4.0 =
* 支持 WordPress 6.3 版本
* 支持 RAM 操作 OSS - 支持 WordPress 6.3 版本
- 支持 RAM 操作 OSS
= 1.3.2 = = 1.3.2 =
* 添加地域
- 添加地域
= 1.3.1 = = 1.3.1 =
* 优化代码
- 优化代码
= 1.3.0 = = 1.3.0 =
* 增加地域
* 优化 isset 判断 - 增加地域
* 优化访问权限 - 优化 isset 判断
* 修复存在同名 path 时截取错误 - 优化访问权限
* 修改 accessKeySecret 类型为 password - 修复存在同名 path 时截取错误
- 修改 accessKeySecret 类型为 password
= 1.2.8 = = 1.2.8 =
* 支持上传文件自动重命名
* 优化图片处理 - 支持上传文件自动重命名
- 优化图片处理
= 1.2.7 = = 1.2.7 =
* 增加地域
- 增加地域
= 1.2.6 = = 1.2.6 =
* 升级 oss sdk
* 修复删除文件的request id异常 - 升级 oss sdk
* 支持 WordPress 5.8 版本 - 修复删除文件的 request id 异常
- 支持 WordPress 5.8 版本
= 1.2.5 = = 1.2.5 =
* 修复当文章图片重复时导致添加多个样式
- 修复当文章图片重复时导致添加多个样式
= 1.2.4 = = 1.2.4 =
* 添加 get_home_path 方法判断
* 支持 WordPress 5.7 版本 - 添加 get_home_path 方法判断
- 支持 WordPress 5.7 版本
= 1.2.3 = = 1.2.3 =
* 支持删除非图片类型文件
- 支持删除非图片类型文件
= 1.2.2 = = 1.2.2 =
* 支持 WordPress 5.6 版本
* 升级 OSS SDK - 支持 WordPress 5.6 版本
* 修复勾选不上传缩略图删除时不会删除已存在的缩略图 - 升级 OSS SDK
- 修复勾选不上传缩略图删除时不会删除已存在的缩略图
= 1.2.1 = = 1.2.1 =
* 支持阿里云OSS图片处理
- 支持阿里云 OSS 图片处理
= 1.2.0 = = 1.2.0 =
* 优化同步上传路径获取
* 修复多站点上传原图失败,缩略图正常问题 - 优化同步上传路径获取
* 优化上传路径获取 - 修复多站点上传原图失败,缩略图正常问题
* 增加数据库题图链接替换 - 优化上传路径获取
- 增加数据库题图链接替换
= 1.1.1 = = 1.1.1 =
* 修复本地文件夹为根目录时路径错误
* 减少一次获取配置代码... - 修复本地文件夹为根目录时路径错误
* 增加回源说明 - 减少一次获取配置代码...
- 增加回源说明
= 1.1.0 = = 1.1.0 =
* 优化删除文件使用删除多个接口
* 修复勾选不在本地保存图片后媒体库显示默认图片问题 - 优化删除文件使用删除多个接口
- 修复勾选不在本地保存图片后媒体库显示默认图片问题
= 1.0.1 = = 1.0.1 =
* 修复勾选不在本地保存图片后媒体库显示默认图片问题
- 修复勾选不在本地保存图片后媒体库显示默认图片问题
= 1.0 = = 1.0 =
* First version
- First version