diff --git a/cos-sdk-v5/src/Object/Head.php b/cos-sdk-v5/src/Object/Head.php new file mode 100644 index 0000000..b25e4d1 --- /dev/null +++ b/cos-sdk-v5/src/Object/Head.php @@ -0,0 +1,18 @@ +HeadObject(['Bucket' => $bucket, 'Key' => $key]); + return $result['ContentLength'] ?? 0; + } catch (ServiceResponseException $e) { + return 0; + } + } +} diff --git a/readme.txt b/readme.txt index ab3b5d8..b18ff9a 100644 --- a/readme.txt +++ b/readme.txt @@ -5,7 +5,7 @@ Tags: COS, 腾讯云, 对象存储, Tencent, Qcloud Requires at least: 4.2 Tested up to: 6.4 Requires PHP: 7.2 -Stable tag: 2.5.2 +Stable tag: 2.5.3 License: Apache2.0 License URI: http://www.apache.org/licenses/LICENSE-2.0.html @@ -100,7 +100,7 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html = Stable = -- Support gif for image slim +- Fix get non-image file size error = Other = diff --git a/sync-qcloud-cos.php b/sync-qcloud-cos.php index fb3ff21..c73e9c1 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.2 +Version: 2.5.3 Author: 沈唁 Author URI: https://qq52o.me License: Apache2.0 @@ -25,8 +25,9 @@ use SyncQcloudCos\ErrorCode; use SyncQcloudCos\Monitor\Charts; use SyncQcloudCos\Monitor\DataPoints; +use SyncQcloudCos\Object\Head; -define('COS_VERSION', '2.5.2'); +define('COS_VERSION', '2.5.3'); define('COS_PLUGIN_SLUG', 'sync-qcloud-cos'); define('COS_PLUGIN_PAGE', plugin_basename(dirname(__FILE__)) . '%2F' . basename(__FILE__)); @@ -614,6 +615,24 @@ function cos_plugin_action_links($links, $file) add_filter('the_content', 'cos_setting_content_ci'); add_filter('post_thumbnail_html', 'cos_setting_post_thumbnail_ci', 10, 3); add_filter('wp_calculate_image_srcset', 'cos_custom_image_srcset', 10, 5); +add_filter('wp_prepare_attachment_for_js', 'cos_wp_prepare_attachment_for_js'); + +function cos_wp_prepare_attachment_for_js($response) +{ + if (empty($response['filesizeInBytes']) || empty($response['filesizeHumanReadable'])) { + $cos_options = get_option('cos_options', true); + $upload_url_path = esc_attr($cos_options['upload_url_path']); + $upload_path = get_option('upload_path'); + $object = str_replace($upload_url_path, $upload_path, $response['url']); + $contentLength = Head::getContentLength(cos_get_client($cos_options), cos_get_bucket_name($cos_options), $object); + if (!empty($contentLength)) { + $response['filesizeInBytes'] = $contentLength; + $response['filesizeHumanReadable'] = size_format($contentLength); + } + } + + return $response; +} function cos_custom_image_srcset($sources, $size_array, $image_src, $image_meta, $attachment_id) {