Skip to content

Commit

Permalink
Fix get non-image file size error (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
sy-records authored Mar 4, 2024
1 parent b8dd3e6 commit 08853b8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
18 changes: 18 additions & 0 deletions cos-sdk-v5/src/Object/Head.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace SyncQcloudCos\Object;

use Qcloud\Cos\Exception\ServiceResponseException;

class Head
{
public static function getContentLength($client, $bucket, $key)
{
try {
$result = $client->HeadObject(['Bucket' => $bucket, 'Key' => $key]);
return $result['ContentLength'] ?? 0;
} catch (ServiceResponseException $e) {
return 0;
}
}
}
4 changes: 2 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 =

Expand Down
23 changes: 21 additions & 2 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 作为附件存储空间。(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
Expand All @@ -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__));

Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 08853b8

Please sign in to comment.