Skip to content

Commit

Permalink
Fix upload and file preview error (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
sy-records authored Oct 20, 2023
1 parent 91e2ded commit cbcbc11
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 72 deletions.
58 changes: 31 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@

## 插件特色

* [x] 可配置是否上传缩略图和是否保留本地备份
* [x] 本地删除可同步删除腾讯云上面的文件
* [x] 支持腾讯云COS存储桶绑定自定义域名
* [x] 支持替换数据库中旧的资源链接地址
* [x] 支持北京、上海、广州、香港、法兰克福等完整地域使用
* [x] 支持同步历史附件到 COS
* [x] 支持验证桶名是否填写正确
* [x] 支持腾讯云数据万象 CI 图片处理
* [x] 支持上传文件自动重命名
* [x] 支持媒体库编辑
* [x] 支持腾讯云数据万象图片极智压缩
* [x] 支持文件预览
* [x] 支持文本内容审核
- [x] 可配置是否上传缩略图和是否保留本地备份
- [x] 本地删除可同步删除腾讯云上面的文件
- [x] 支持腾讯云 COS 存储桶绑定自定义域名
- [x] 支持替换数据库中旧的资源链接地址
- [x] 支持北京、上海、广州、香港、法兰克福等完整地域使用
- [x] 支持同步历史附件到 COS
- [x] 支持验证桶名是否填写正确
- [x] 支持腾讯云数据万象 CI 图片处理
- [x] 支持上传文件自动重命名
- [x] 支持媒体库编辑
- [x] 支持腾讯云数据万象图片极智压缩
- [x] 支持文件预览
- [x] 支持文本内容审核

## 安装

Expand All @@ -44,9 +44,9 @@ GitHub 下载节点:[https://github.com/sy-records/sync-qcloud-cos/releases/la

## 修改配置

* 方法一:在 WordPress 插件管理页面有设置按钮,进行设置
* 方法二:在 WordPress 后台管理左侧导航栏`设置``腾讯云 COS`,点击进入设置页面
* 方法三:在 WordPress 后台管理左侧一级导航栏`腾讯云 COS`,点击进入设置页面
- 方法一:在 WordPress 插件管理页面有设置按钮,进行设置
- 方法二:在 WordPress 后台管理左侧导航栏`设置``腾讯云 COS`,点击进入设置页面
- 方法三:在 WordPress 后台管理左侧一级导航栏`腾讯云 COS`,点击进入设置页面

## Contributors

Expand All @@ -62,32 +62,33 @@ This project exists thanks to all the people who contribute. [[Contributors](htt

2. 使用子账户报错`Cos Error Code: AccessDenied, Status Code: 403`

可以使用子账户,但是 APPID 需要填写为存储桶创建者的ID,而不是子账户的ID。例如下文配置指南中的`1250000000`就是APPID
可以使用子账户,但是 APPID 需要填写为存储桶创建者的 ID,而不是子账户的 ID。例如下文配置指南中的`1250000000`就是 APPID

3. 上传图片提示`图像后期处理失败,请将其缩小到2500像素并重新上传`

1)配置的`存储桶名称`填写错误,正确的配置参照 [配置指南](https://github.com/sy-records/sync-qcloud-cos#%E9%85%8D%E7%BD%AE%E6%8C%87%E5%8D%97) 中的`存储桶名称`

> v1.6.1 版本增强了校验,填写错误会给予提示;同时兼容了桶名称附带`APPID`的情况
2)图片确实像素大于2500px,可以在`functions.php`中加入以下代码禁止大图片裁剪功能
2)图片确实像素大于 2500px,可以在`functions.php`中加入以下代码禁止大图片裁剪功能

```php
add_filter('big_image_size_threshold', '__return_false');
```

4. 从媒体库中删除了图片,但是`COS`中还是存在

原因是在配置页面选择了`不在本地保留备份`因为WordPress机制问题,无法获取对应的文件信息
原因是在配置页面选择了`不在本地保留备份`因为 WordPress 机制问题,无法获取对应的文件信息

5. 在插件中应该如何使用腾讯云数据万象CI
5. 在插件中应该如何使用腾讯云数据万象 CI

参考:[腾讯云对象存储COS + 数据万象CI = 完善的图片解决方案](https://cloud.tencent.com/developer/article/1606153)[腾讯云文档 - 使用图片样式](https://cloud.tencent.com/document/product/436/42214#.E4.BD.BF.E7.94.A8.E5.9B.BE.E7.89.87.E6.A0.B7.E5.BC.8F)
参考:[腾讯云对象存储 COS + 数据万象 CI = 完善的图片解决方案](https://cloud.tencent.com/developer/article/1606153)[腾讯云文档 - 使用图片样式](https://cloud.tencent.com/document/product/436/42214#.E4.BD.BF.E7.94.A8.E5.9B.BE.E7.89.87.E6.A0.B7.E5.BC.8F)

6. 保存配置时报错:`您的站点遇到了致命错误,请查看您的站点的管理电子邮箱来获得指引`

这个问题应该只出现在Windows的机器上,打开`WP_DEBUG`的话会报错:`Fatal error: Uncaught GuzzleHttp\Exception\RequestException: cURL error 60: SSL certificate problem: self signed certificate`,解决方法如下:
这个问题应该只出现在 Windows 的机器上,打开`WP_DEBUG`的话会报错:`Fatal error: Uncaught GuzzleHttp\Exception\RequestException: cURL error 60: SSL certificate problem: self signed certificate`,解决方法如下:

1)从 [https://curl.haxx.se/ca/cacert.pem](https://curl.haxx.se/ca/cacert.pem) 下载最新的cacert.pem
1)从 [https://curl.haxx.se/ca/cacert.pem](https://curl.haxx.se/ca/cacert.pem) 下载最新的 cacert.pem
2)将以下行添加到`php.ini`中,注意修改对应的路径

```ini
Expand All @@ -106,15 +107,18 @@ curl.cainfo=/path/to/cacert.pem

查看详细教程[https://qq52o.me/2722.html](https://qq52o.me/2722.html)

* 存储桶设置
- 存储桶设置

> 访问 [腾讯云控制台](https://console.cloud.tencent.com/cos5/bucket) 创建存储桶,把创建存储桶时要求你填写的**存储桶名称**,把**存储桶名称**填到这里就可以了,没有后面的`-appid`
> `examplebucket-1250000000`,其中`examplebucket`为存储桶名称,`1250000000`为 APPID。
* 存储桶地域
- 存储桶地域

> 选择你创建存储桶时所选的地域即可。
> 如果地域选择`全球加速`,则在COS控制台中开启全球加速服务,具体方法和资费问题请查看 [全球加速官方文档](https://cloud.tencent.com/document/product/436/38866)
> 如果地域选择`全球加速`,则在 COS 控制台中开启全球加速服务,具体方法和资费问题请查看 [全球加速官方文档](https://cloud.tencent.com/document/product/436/38866)
- APP ID、SecretID、SecretKey

* APP ID、SecretID、SecretKey
> APP ID 填写上文存储桶设置中所说的`125000000`即可;也可以访问 [腾讯云控制台](https://console.cloud.tencent.com/cos5/key) 获取 APP ID、SecretID、SecretKey
## 插件截图
Expand Down
13 changes: 13 additions & 0 deletions cos-sdk-v5/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,18 @@
"config": {
"optimize-autoloader": true,
"sort-packages": true
},
"scripts": {
"post-install-cmd": "SyncQcloudCos\\Composer::removeUnusedModels",
"post-update-cmd": "SyncQcloudCos\\Composer::removeUnusedModels"
},
"extra": {
"tencentcloud/monitor": [
"GetMonitorDataRequest.php",
"Instance.php",
"Dimension.php",
"GetMonitorDataResponse.php",
"DataPoint.php"
]
}
}
8 changes: 7 additions & 1 deletion cos-sdk-v5/src/CI/FilePreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,20 @@ public static function checkStatus($client, $bucket)
* 判断是否支持文件预览
*
* @param string $fileUrl
* @param string $urlPrefix
* @return bool
*/
public static function isFileExtensionSupported($fileUrl)
public static function isFileExtensionSupported($fileUrl, $urlPrefix = '')
{
$extension = pathinfo($fileUrl, PATHINFO_EXTENSION);
if (empty($extension)) {
return false;
}

if (!empty($urlPrefix) && strpos($fileUrl, $urlPrefix) === false) {
return false;
}

$supported = [
'pptx', 'ppt', 'pot','potx', 'pps', 'ppsx', 'dps', 'dpt', 'pptm', 'potm', 'ppsm',
'doc', 'dot', 'wps', 'wpt', 'docx', 'dotx', 'docm', 'dotm',
Expand Down
32 changes: 32 additions & 0 deletions cos-sdk-v5/src/Composer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace SyncQcloudCos;

use Composer\Script\Event;

class Composer
{
public static function removeUnusedModels(Event $event)
{
$composer = $event->getComposer();
$extra = $composer->getPackage()->getExtra();
$listedModels = $extra['tencentcloud/monitor'] ?? [];

if ($listedModels) {
$vendorPath = $composer->getConfig()->get('vendor-dir');
$dir = "{$vendorPath}/tencentcloud/monitor/src/TencentCloud/Monitor/V20180724/Models";
$files = scandir($dir);
foreach ($files as $file) {
if ($file === '.' || $file === '..') {
continue;
}
if (!in_array($file, $listedModels)) {
unlink("{$dir}/{$file}");
}
}

$generator = $composer->getAutoloadGenerator();
$generator->dump($composer->getConfig(), $composer->getRepositoryManager()->getLocalRepository(), $composer->getPackage(), $composer->getInstallationManager(), 'composer', true);
}
}
}
34 changes: 20 additions & 14 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Donate link: https://qq52o.me/sponsor.html
Tags: COS, 腾讯云, 对象存储, Tencent, Qcloud
Requires at least: 4.2
Tested up to: 6.3
Requires PHP: 7.0.0
Stable tag: 2.3.2
Requires PHP: 7.0
Stable tag: 2.3.3
License: Apache2.0
License URI: http://www.apache.org/licenses/LICENSE-2.0.html

Expand All @@ -15,8 +15,8 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html

使用腾讯云对象存储服务 COS 作为附件存储空间。(This is a plugin that uses Tencent Cloud Cloud Object Storage for attachments remote saving.)

* 依赖腾讯云COS服务:https://cloud.tencent.com/product/cos
* 使用说明:https://cloud.tencent.com/product/cos/details
- 依赖腾讯云 COS 服务:https://cloud.tencent.com/product/cos
- 使用说明:https://cloud.tencent.com/product/cos/details

## 插件特点

Expand All @@ -40,7 +40,7 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html

[沈唁志](https://qq52o.me "沈唁志")

欢迎加入沈唁的WordPress云存储全家桶QQ交流群:887595381
欢迎加入沈唁的 WordPress 云存储全家桶 QQ 交流群:887595381

== Installation ==

Expand All @@ -65,24 +65,24 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html

= 使用子账户报错:Cos Error Code: AccessDenied, Status Code: 403 =

可以使用子账户,但是 APPID 需要填写为存储桶创建者的ID,而不是子账户的ID。例如[配置指南](https://github.com/sy-records/sync-qcloud-cos#%E9%85%8D%E7%BD%AE%E6%8C%87%E5%8D%97)中的`1250000000`就是APPID
可以使用子账户,但是 APPID 需要填写为存储桶创建者的 ID,而不是子账户的 ID。例如[配置指南](https://github.com/sy-records/sync-qcloud-cos#%E9%85%8D%E7%BD%AE%E6%8C%87%E5%8D%97)中的`1250000000`就是 APPID

= 上传图片提示:图像后期处理失败,请将其缩小到2500像素并重新上传 =
= 上传图片提示:图像后期处理失败,请将其缩小到 2500 像素并重新上传 =

1)配置的`存储桶名称`填写错误,正确的配置参照[配置指南](https://github.com/sy-records/sync-qcloud-cos#%E9%85%8D%E7%BD%AE%E6%8C%87%E5%8D%97)中的`存储桶名称`,v1.6.1 版本增强了校验,填写错误会给予提示;同时兼容了桶名称附带`APPID`的情况
2)图片确实像素大于2500px,可以在`functions.php`中加入以下代码禁止大图片裁剪功能
2)图片确实像素大于 2500px,可以在`functions.php`中加入以下代码禁止大图片裁剪功能

`add_filter('big_image_size_threshold', '__return_false');`

= 在插件中应该如何使用腾讯云数据万象CI =
= 在插件中应该如何使用腾讯云数据万象 CI =

参考:[腾讯云对象存储COS + 数据万象CI = 完善的图片解决方案](https://cloud.tencent.com/developer/article/1606153) 或 [腾讯云文档 - 使用图片样式](https://cloud.tencent.com/document/product/436/42214#.E4.BD.BF.E7.94.A8.E5.9B.BE.E7.89.87.E6.A0.B7.E5.BC.8F)
参考:[腾讯云对象存储 COS + 数据万象 CI = 完善的图片解决方案](https://cloud.tencent.com/developer/article/1606153) 或 [腾讯云文档 - 使用图片样式](https://cloud.tencent.com/document/product/436/42214#.E4.BD.BF.E7.94.A8.E5.9B.BE.E7.89.87.E6.A0.B7.E5.BC.8F)

= 保存配置时报错:您的站点遇到了致命错误,请查看您的站点的管理电子邮箱来获得指引 =

这个问题应该只出现在Windows的机器上,打开`WP_DEBUG`的话会报错:`Fatal error: Uncaught GuzzleHttp\Exception\RequestException: cURL error 60: SSL certificate problem: self signed certificate`,解决方法如下:
这个问题应该只出现在 Windows 的机器上,打开`WP_DEBUG`的话会报错:`Fatal error: Uncaught GuzzleHttp\Exception\RequestException: cURL error 60: SSL certificate problem: self signed certificate`,解决方法如下:

1)从 [https://curl.haxx.se/ca/cacert.pem](https://curl.haxx.se/ca/cacert.pem) 下载最新的cacert.pem
1)从 [https://curl.haxx.se/ca/cacert.pem](https://curl.haxx.se/ca/cacert.pem) 下载最新的 cacert.pem
2)将以下行添加到`php.ini`中,注意修改对应的路径

`curl.cainfo=/path/to/cacert.pem`
Expand All @@ -98,8 +98,14 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html
== Changelog ==

= Stable =
* 优化检测存储桶代码
* 修复文档处理页面的未定义变量

- 增加联系作者页面
- 重构文件预览实现
- 优化同步历史附件代码
- 修复超大图片原图未上传问题
- 修复上传 PDF 等文件格式时的错误
- 修复文件预览页面有重复链接时的错误
- 优化部分代码

= Other =

Expand Down
61 changes: 31 additions & 30 deletions sync-qcloud-cos.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: Sync QCloud COS
Plugin URI: https://qq52o.me/2518.html
Description: 使用腾讯云对象存储服务 COS 作为附件存储空间。(This is a plugin that uses Tencent Cloud Cloud Object Storage for attachments remote saving.)
Version: 2.3.2
Version: 2.3.3
Author: 沈唁
Author URI: https://qq52o.me
License: Apache2.0
Expand All @@ -25,7 +25,7 @@
use SyncQcloudCos\Monitor\Charts;
use SyncQcloudCos\Monitor\DataPoints;

define('COS_VERSION', '2.3.2');
define('COS_VERSION', '2.3.3');
define('COS_PLUGIN_SLUG', 'sync-qcloud-cos');
define('COS_PLUGIN_PAGE', plugin_basename(dirname(__FILE__)) . '%2F' . basename(__FILE__));

Expand Down Expand Up @@ -282,31 +282,33 @@ function cos_upload_attachments($metadata)
*/
function cos_upload_thumbs($metadata)
{
if (empty($metadata['file'])) {
return $metadata;
}

//获取上传路径
$wp_uploads = wp_upload_dir();
$basedir = $wp_uploads['basedir'];
$upload_path = cos_get_option('upload_path');

$cos_options = get_option('cos_options', true);
$no_local_file = esc_attr($cos_options['nolocalsaving']) == 'true';
$no_thumb = esc_attr($cos_options['nothumb']) == 'true';

if (!empty($metadata['file'])) {
// Maybe there is a problem with the old version
$file = $basedir . '/' . $metadata['file'];
$upload_path = cos_get_option('upload_path');
if ($upload_path != '.') {
$path_array = explode($upload_path, $file);
if (count($path_array) >= 2) {
$object = '/' . $upload_path . end($path_array);
}
} else {
$object = '/' . $metadata['file'];
$file = str_replace('./', '', $file);
// Maybe there is a problem with the old version
$file = $basedir . '/' . $metadata['file'];
if ($upload_path != '.') {
$path_array = explode($upload_path, $file);
if (count($path_array) >= 2) {
$object = '/' . $upload_path . end($path_array);
}

cos_file_upload($object, $file, $no_local_file);
} else {
$object = '/' . $metadata['file'];
$file = str_replace('./', '', $file);
}

cos_file_upload($object, $file, $no_local_file);

//得到本地文件夹和远端文件夹
$dirname = dirname($metadata['file']);
$file_path = $dirname != '.' ? "{$basedir}/{$dirname}/" : "{$basedir}/";
Expand Down Expand Up @@ -468,7 +470,7 @@ function cos_read_dir_queue($homePath, $uploadPath)
$foundFiles = [];

while (!$dirsToProcess->isEmpty()) {
[$currentDir, $relativeDir] = $dirsToProcess->dequeue();
list($currentDir, $relativeDir) = $dirsToProcess->dequeue();

foreach (new DirectoryIterator($currentDir) as $fileInfo) {
if ($fileInfo->isDot()) continue;
Expand Down Expand Up @@ -506,15 +508,13 @@ function cos_plugin_action_links($links, $file)
add_filter('plugin_action_links', 'cos_plugin_action_links', 10, 2);

add_filter('the_content', 'cos_setting_content_ci');
add_filter('post_thumbnail_html', 'cos_setting_post_thumbnail_ci', 10, 3);

function cos_setting_content_ci($content)
{
$option = get_option('cos_options');
if (!empty(esc_attr($option['ci_style']))) {
preg_match_all(
'/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim',
$content,
$images
);
preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $images);
if (!empty($images) && isset($images[1])) {
$images[1] = array_unique($images[1]);
foreach ($images[1] as $item) {
Expand All @@ -528,11 +528,17 @@ function cos_setting_content_ci($content)
if (!empty($option['attachment_preview']) && $option['attachment_preview'] == 'on') {
preg_match_all('/<a.*?href="(.*?)".*?\/a>/is', $content, $matches);
if (!empty($matches)) {
[$tags, $links] = $matches;
list($tags, $links) = $matches;
$handledLinks = [];
foreach ($links as $index => $link) {
if (FilePreview::isFileExtensionSupported($link)) {
if (in_array($link, $handledLinks)) {
continue;
}

if (FilePreview::isFileExtensionSupported($link, $option['upload_url_path'])) {
$iframe = '<iframe src="' . $link . '?ci-process=doc-preview&dstType=html" width="100%" allowFullScreen="true" height="800"></iframe>';
$content = str_replace($tags[$index], $iframe, $content);
$handledLinks[] = $link;
}
}
}
Expand All @@ -541,16 +547,11 @@ function cos_setting_content_ci($content)
return $content;
}

add_filter('post_thumbnail_html', 'cos_setting_post_thumbnail_ci', 10, 3);
function cos_setting_post_thumbnail_ci($html, $post_id, $post_image_id)
{
$option = get_option('cos_options');
if (!empty(esc_attr($option['ci_style'])) && has_post_thumbnail()) {
preg_match_all(
'/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim',
$html,
$images
);
preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $html, $images);
if (!empty($images) && isset($images[1])) {
$images[1] = array_unique($images[1]);
foreach ($images[1] as $item) {
Expand Down

0 comments on commit cbcbc11

Please sign in to comment.