Merge pull request #1 from sy-records/fix-no-local-file

Fix no local file
pull/6/head v1.1.0
Luffy 2020-04-11 08:00:01 +08:00 committed by GitHub
commit 7cab19dc0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 27 deletions

View File

@ -33,6 +33,12 @@ Github 下载节点:[https://github.com/sy-records/aliyun-oss-wordpress/releas
* 方法一:在 WordPress 插件管理页面有设置按钮,进行设置
* 方法二:在 WordPress 后台管理左侧导航栏`设置`下`阿里云OSS设置`,点击进入设置页面
## 常见问题
* 通过文件URL访问图片无法预览而是以附件形式下载
因为Bucket是2019年9月23日后创建的使用默认域名时会自动下载需要绑定自有域名访问。具体参考[阿里云文档](https://help.aliyun.com/document_detail/142631.html)。
## 插件截图
![设置页面](screenshot-1.png)

View File

@ -3,7 +3,7 @@
Plugin Name: OSS Aliyun
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.
Version: 1.0.1
Version: 1.1.0
Author: 沈唁
Author URI: https://qq52o.me
License: Apache 2.0
@ -14,7 +14,7 @@ require_once 'sdk/vendor/autoload.php';
use OSS\OssClient;
use OSS\Core\OssException;
define('OSS_VERSION', "1.0.1");
define('OSS_VERSION', "1.1.0");
define('OSS_BASEFOLDER', plugin_basename(dirname(__FILE__)));
// 初始化选项
@ -123,7 +123,7 @@ function oss_delete_local_file($file)
}
/**
* 删除oss中的文件
* 删除oss中的单个文件
* @param $file
* @return bool
*/
@ -134,6 +134,17 @@ function oss_delete_oss_file($file)
$ossClient->deleteObject($bucket, $file);
}
/**
* 批量删除文件
* @param $files
*/
function oss_delete_oss_files(array $files)
{
$bucket = oss_get_bucket_name();
$ossClient = oss_get_client();
$ossClient->deleteObjects($bucket, $files);
}
/**
* 上传附件(包括图片的原图)
*
@ -142,19 +153,32 @@ function oss_delete_oss_file($file)
*/
function oss_upload_attachments($metadata)
{
//生成object在oss中的存储路径
if (get_option('upload_path') == '.') {
//如果含有“./”则去除之
$metadata['file'] = str_replace("./", '', $metadata['file']);
$mime_types = get_allowed_mime_types();
$image_mime_types = array(
$mime_types['jpg|jpeg|jpe'],
$mime_types['gif'],
$mime_types['png'],
$mime_types['bmp'],
$mime_types['tiff|tif'],
$mime_types['ico'],
);
// 例如mp4等格式 上传后根据配置选择是否删除 删除后媒体库会显示默认图片 点开内容是正常的
// 图片在缩略图处理
if (!in_array($metadata['type'], $image_mime_types)) {
//生成object在oss中的存储路径
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; //向上兼容较早的WordPress版本上$metadata['file']存放的是相对路径
//执行上传操作
oss_file_upload('/' . $object, $file, oss_is_delete_local_file());
}
$object = str_replace("\\", '/', $metadata['file']);
$object = str_replace(get_home_path(), '', $object);
//在本地的存储路径
$file = get_home_path() . $object; //向上兼容较早的WordPress版本上$metadata['file']存放的是相对路径
//执行上传操作
oss_file_upload('/' . $object, $file, oss_is_delete_local_file());
return $metadata;
}
@ -169,6 +193,15 @@ if (substr_count($_SERVER['REQUEST_URI'], '/update.php') <= 0) {
*/
function oss_upload_thumbs($metadata)
{
//获取上传路径
$wp_uploads = wp_upload_dir();
$basedir = $wp_uploads['basedir'];
if (isset($metadata['file'])) {
// Maybe there is a problem with the old version
$object ='/' . get_option('upload_path') . '/' . $metadata['file'];
$file = $basedir . '/' . $metadata['file'];
oss_file_upload($object, $file, oss_is_delete_local_file());
}
//上传所有缩略图
if (isset($metadata['sizes']) && count($metadata['sizes']) > 0) {
//获取oss插件的配置信息
@ -179,12 +212,8 @@ function oss_upload_thumbs($metadata)
if ($nothumb) {
return $metadata;
}
//获取上传路径
$wp_uploads = wp_upload_dir();
$basedir = $wp_uploads['basedir'];
$file_dir = $metadata['file'];
//得到本地文件夹和远端文件夹
$file_path = $basedir . '/' . dirname($file_dir) . '/';
$file_path = $basedir . '/' . dirname($metadata['file']) . '/';
if (get_option('upload_path') == '.') {
$file_path = str_replace("\\", '/', $file_path);
$file_path = str_replace(get_home_path() . "./", '', $file_path);
@ -220,26 +249,31 @@ if (substr_count($_SERVER['REQUEST_URI'], '/update.php') <= 0) {
function oss_delete_remote_attachment($post_id) {
$meta = wp_get_attachment_metadata( $post_id );
$oss_options = get_option('oss_options', true);
if (isset($meta['file'])) {
$deleteObjects = [];
// meta['file']的格式为 "2020/01/wp-bg.png"
$upload_path = get_option('upload_path');
if ($upload_path == '') {
$upload_path = 'wp-content/uploads';
}
$file_path = $upload_path . '/' . $meta['file'];
oss_delete_oss_file(str_replace("\\", '/', $file_path));
$deleteObjects[] = str_replace("\\", '/', $file_path);
$oss_options = get_option('oss_options', true);
$is_nothumb = (esc_attr($oss_options['nothumb']) == 'false');
if ($is_nothumb) {
// 删除缩略图
if (isset($meta['sizes']) && count($meta['sizes']) > 0) {
foreach ($meta['sizes'] as $val) {
$size_file = dirname($file_path) . '/' . $val['file'];
oss_delete_oss_file(str_replace("\\", '/', $size_file));
$deleteObjects[] = str_replace("\\", '/', $size_file);
}
}
}
oss_delete_oss_files($deleteObjects);
}
}
add_action('delete_attachment', 'oss_delete_remote_attachment');

View File

@ -5,7 +5,7 @@ Tags: oss, 阿里云, 对象存储, aliyun
Requires at least: 4.2
Tested up to: 5.4
Requires PHP: 5.6.0
Stable tag: 1.0.1
Stable tag: 1.1.0
License: Apache 2.0
License URI: http://www.apache.org/licenses/LICENSE-2.0.html
@ -38,7 +38,7 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html
[沈唁志](https://qq52o.me "沈唁志")
QQ交流群887595381
欢迎加入沈唁的WordPress云存储全家桶QQ交流群887595381
== Installation ==
@ -53,12 +53,20 @@ QQ交流群887595381
== Frequently Asked Questions ==
= 怎么替换文章中之前的旧资源地址链接 =
= 怎么替换文章中之前的旧资源地址链接 =
这个插件已经加上了替换数据库中之前的旧资源地址链接功能,只需要填好对应的链接即可
= 通过文件URL访问图片无法预览而是以附件形式下载 =
因为Bucket是2019年9月23日后创建的使用默认域名时会自动下载需要绑定自有域名访问。具体参考[阿里云文档](https://help.aliyun.com/document_detail/142631.html)。
== Changelog ==
= 1.1.0 =
* 优化删除文件使用删除多个接口
* 修复勾选不在本地保存图片后媒体库显示默认图片问题
= 1.0.1 =
* 修复勾选不在本地保存图片后媒体库显示默认图片问题