Fix get non-image file size error (#33)

pull/34/head v1.4.10
Luffy 2024-02-29 18:22:18 +08:00 committed by GitHub
parent 530ddf967d
commit ce9cfa44e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 45 additions and 6 deletions

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.4.9
Version: 1.4.10
Author: 沈唁
Author URI: https://qq52o.me
License: Apache2.0
@ -19,7 +19,7 @@ use OSS\Credentials\CredentialsProvider;
use AlibabaCloud\Credentials\Credential;
use OSS\Credentials\StaticCredentialsProvider;
define('OSS_VERSION', '1.4.9');
define('OSS_VERSION', '1.4.10');
define('OSS_BASEFOLDER', plugin_basename(dirname(__FILE__)));
if (!function_exists('get_home_path')) {
@ -109,6 +109,24 @@ function oss_get_bucket_name()
return $oss_opt['bucket'];
}
/**
* @param string $object
* @return array
*/
function oss_get_file_meta($object)
{
try {
$ossClient = oss_get_client();
$bucket = oss_get_bucket_name();
return $ossClient->getObjectMeta($bucket, $object);
} catch (\Throwable $e) {
if (WP_DEBUG) {
echo 'Error Message: ', $e->getMessage(), PHP_EOL, 'Error Code: ', $e->getCode();
}
return ['content-length' => 0];
}
}
/**
* @param string $object
* @param string $file
@ -124,7 +142,7 @@ function oss_file_upload($object, $file, $no_local_file = false)
$ossClient = oss_get_client();
try {
$ossClient->uploadFile($bucket, ltrim($object, '/'), $file);
} catch (Throwable $e) {
} catch (\Throwable $e) {
if (WP_DEBUG) {
echo 'Error Message: ', $e->getMessage(), PHP_EOL, 'Error Code: ', $e->getCode();
}
@ -494,6 +512,23 @@ function oss_custom_image_srcset($sources, $size_array, $image_src, $image_meta,
add_filter('wp_calculate_image_srcset', 'oss_custom_image_srcset', 10, 5);
add_filter('wp_prepare_attachment_for_js', 'oss_wp_prepare_attachment_for_js', 10);
function oss_wp_prepare_attachment_for_js($response)
{
if (empty($response['filesizeInBytes']) || empty($response['filesizeHumanReadable'])) {
$upload_url_path = get_option('upload_url_path');
$upload_path = get_option('upload_path');
$object = str_replace($upload_url_path, $upload_path, $response['url']);
$meta = oss_get_file_meta($object);
if (!empty($meta['content-length'])) {
$response['filesizeInBytes'] = $meta['content-length'];
$response['filesizeHumanReadable'] = size_format($meta['content-length']);
}
}
return $response;
}
add_filter('the_content', 'oss_setting_content_style');
function oss_setting_content_style($content)
{

View File

@ -5,7 +5,7 @@ Tags: oss, 阿里云, 对象存储, aliyun
Requires at least: 4.2
Tested up to: 6.4
Requires PHP: 7.0
Stable tag: 1.4.9
Stable tag: 1.4.10
License: Apache2.0
License URI: http://www.apache.org/licenses/LICENSE-2.0.html
@ -73,6 +73,10 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html
== Changelog ==
= 1.4.10 =
- 修复`不在本地保留备份`时获取不到非图片文件大小
= 1.4.9 =
- 升级 SDK

View File

@ -5,9 +5,9 @@ if (!defined('WP_UNINSTALL_PLUGIN')) {
exit();
}
$obs_options = get_option('oss_options', true);
$options = get_option('oss_options', true);
$upload_url_path = get_option('upload_url_path');
$oss_upload_url_path = esc_attr($obs_options['upload_url_path']);
$oss_upload_url_path = esc_attr($options['upload_url_path']);
//如果现在使用的是OSS的URL则恢复原状
if ($upload_url_path == $oss_upload_url_path) {