From 71924b0397a18ae34b024de7c2e715a038cc08ec Mon Sep 17 00:00:00 2001 From: Wisp X <1591788658@qq.com> Date: Wed, 23 Mar 2022 17:00:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=85=BE=E8=AE=AF=E4=BA=91?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E5=AE=A1=E6=A0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Enums/Scan/TencentOption.php | 21 +++++++++ app/Http/Requests/Admin/GroupRequest.php | 17 ++++++-- app/Services/ImageService.php | 50 ++++++++++++++++++++-- config/convention.php | 12 +++++- resources/views/admin/group/add.blade.php | 35 ++++++++++++--- resources/views/admin/group/edit.blade.php | 33 ++++++++++++-- 6 files changed, 151 insertions(+), 17 deletions(-) create mode 100644 app/Enums/Scan/TencentOption.php diff --git a/app/Enums/Scan/TencentOption.php b/app/Enums/Scan/TencentOption.php new file mode 100644 index 00000000..9a308c92 --- /dev/null +++ b/app/Enums/Scan/TencentOption.php @@ -0,0 +1,21 @@ + ['exclude_if:configs.is_enable_scan,false', 'in:aliyun'], + 'configs.scan_configs.driver' => ['exclude_if:configs.is_enable_scan,false', 'in:tencent,aliyun,baidu'], + 'configs.scan_configs.drivers.tencent.endpoint' => [$requiredIfReview('tencent')], + 'configs.scan_configs.drivers.tencent.secret_id' => [$requiredIfReview('tencent')], + 'configs.scan_configs.drivers.tencent.secret_key' => [$requiredIfReview('tencent')], + 'configs.scan_configs.drivers.tencent.region' => [$requiredIfReview('tencent')], + 'configs.scan_configs.drivers.tencent.biz_type' => '', + 'configs.scan_configs.drivers.aliyun.access_key_id' => [$requiredIfReview('aliyun')], 'configs.scan_configs.drivers.aliyun.access_key_secret' => [$requiredIfReview('aliyun')], - 'configs.scan_configs.drivers.aliyun.biz_type' => [$requiredIfReview('aliyun')], 'configs.scan_configs.drivers.aliyun.region_id' => [$requiredIfReview('aliyun')], + 'configs.scan_configs.drivers.aliyun.biz_type' => '', 'configs.scan_configs.drivers.aliyun.scenes' => [$requiredIfReview('aliyun'), 'array'], 'configs.is_enable_original_protection' => 'boolean', @@ -110,10 +116,15 @@ class GroupRequest extends FormRequest 'configs.is_enable_scan' => '是否启用图片审核', 'configs.scanned_action' => '图片审核动作', 'configs.scan_configs.driver' => '图片审核驱动', + 'configs.scan_configs.drivers.tencent.endpoint' => 'Endpoint', + 'configs.scan_configs.drivers.tencent.secret_id' => 'SecretId', + 'configs.scan_configs.drivers.tencent.secret_key' => 'SecretKey', + 'configs.scan_configs.drivers.tencent.region' => '地域节点', + 'configs.scan_configs.drivers.tencent.biz_type' => '业务场景', 'configs.scan_configs.drivers.aliyun.access_key_id' => 'AccessKeyId', 'configs.scan_configs.drivers.aliyun.access_key_secret' => 'AccessKeySecret', - 'configs.scan_configs.drivers.aliyun.biz_type' => '场景名称', 'configs.scan_configs.drivers.aliyun.region_id' => '地域节点', + 'configs.scan_configs.drivers.aliyun.biz_type' => '场景名称', 'configs.scan_configs.drivers.aliyun.scenes' => '审核场景', 'configs.is_enable_original_protection' => '是否启用原图保护功能', diff --git a/app/Services/ImageService.php b/app/Services/ImageService.php index f3d440eb..55c31073 100644 --- a/app/Services/ImageService.php +++ b/app/Services/ImageService.php @@ -8,6 +8,7 @@ use App\Enums\ConfigKey; use App\Enums\GroupConfigKey; use App\Enums\ImagePermission; use App\Enums\Scan\AliyunOption; +use App\Enums\Scan\TencentOption; use App\Enums\Strategy\CosOption; use App\Enums\Strategy\FtpOption; use App\Enums\Strategy\KodoOption; @@ -54,6 +55,11 @@ use League\Flysystem\WebDAV\WebDAVAdapter; use Overtrue\Flysystem\Cos\CosAdapter; use Overtrue\Flysystem\Qiniu\QiniuAdapter; use Sabre\DAV\Client; +use TencentCloud\Common\Credential; +use TencentCloud\Common\Profile\ClientProfile; +use TencentCloud\Common\Profile\HttpProfile; +use TencentCloud\Ims\V20201229\ImsClient; +use TencentCloud\Ims\V20201229\Models\ImageModerationRequest; use WispX\Flysystem\Upyun\UpyunAdapter; use Zing\Flysystem\Oss\OssAdapter; use OSS\OssClient; @@ -211,7 +217,12 @@ class ImageService // 图片检测,跳过 tif 以及 psd 格式 if ($configs->get(GroupConfigKey::IsEnableScan) && ! in_array($extension, ['psd', 'tif'])) { $scanConfigs = $configs->get(GroupConfigKey::ScanConfigs); - if ($this->scan($scanConfigs['driver'], collect($scanConfigs['drivers'][$scanConfigs['driver']]), $image)) { + if ($this->scan( + driver: $scanConfigs['driver'], + configs: collect($scanConfigs['drivers'][$scanConfigs['driver']]), + image: $image, + file: $file, + )) { // 标记 or 删除 if ($configs->get(GroupConfigKey::ScannedAction) === 'delete') { $image->delete(); @@ -354,14 +365,47 @@ class ImageService * @param $driver * @param Collection $configs * @param Image $image + * @param UploadedFile $file * @return bool true=违规 * @throws UploadException */ - public function scan($driver, Collection $configs, Image $image): bool + public function scan($driver, Collection $configs, Image $image, UploadedFile $file): bool { $flag = false; try { + if ($driver === 'tencent') { + // 图片大小不得超过 5mb + if ($file->getSize() >= 5242880) { + return false; + } + + $cred = new Credential($configs->get(TencentOption::SecretId), $configs->get(TencentOption::SecretKey)); + $httpProfile = new HttpProfile(); + $httpProfile->setEndpoint($configs->get(TencentOption::Endpoint)); + $clientProfile = new ClientProfile(); + $clientProfile->setHttpProfile($httpProfile); + $client = new ImsClient($cred, $configs->get(TencentOption::Region), $clientProfile); + $req = new ImageModerationRequest(); + $params = [ + "FileContent" => base64_encode($file->getContent()), + ]; + if ($configs->get(TencentOption::BizType)) { + $params['BizType'] = $configs->get(TencentOption::BizType); + } + $req->fromJsonString(json_encode($params)); + $resp = $client->ImageModeration($req); + + if ($resp->getSuggestion() === 'Block') { + $flag = true; + } + } + if ($driver === 'aliyun') { + // 20 mb以内、宽高不超过 30000px + if ($file->getSize() >= 20971520 || $image->width >= 30000 || $image->height >= 30000) { + return false; + } + AlibabaCloud::accessKeyClient( $configs->get(AliyunOption::AccessKeyId), $configs->get(AliyunOption::AccessKeySecret), @@ -386,7 +430,7 @@ class ImageService } } - if ($driver === 'tencent') { + if ($driver === 'baidu') { } } catch (\Throwable $e) { diff --git a/config/convention.php b/config/convention.php index 0dbeb1eb..ddfe900e 100644 --- a/config/convention.php +++ b/config/convention.php @@ -6,13 +6,14 @@ use App\Enums\ConfigKey; use App\Enums\GroupConfigKey; use App\Enums\Mail\SmtpOption; use App\Enums\Scan\AliyunOption; +use App\Enums\Scan\TencentOption; use App\Enums\Watermark\FontOption; use App\Enums\Watermark\ImageOption; return [ 'app' => [ ConfigKey::AppName => 'Lsky Pro', - ConfigKey::AppVersion => 'V 2.0.1', + ConfigKey::AppVersion => 'V 2.0.2', ConfigKey::SiteKeywords => 'Lsky Pro,lsky,兰空图床', ConfigKey::SiteDescription => 'Lsky Pro, Your photo album on the cloud.', ConfigKey::SiteNotice => '', @@ -45,8 +46,15 @@ return [ GroupConfigKey::IsEnableOriginalProtection => 0, GroupConfigKey::ScannedAction => 'mark', // in mark or delete GroupConfigKey::ScanConfigs => [ - 'driver' => 'aliyun', + 'driver' => 'tencent', 'drivers' => [ + 'tencent' => [ + TencentOption::Endpoint => 'ims.tencentcloudapi.com', + TencentOption::SecretId => '', + TencentOption::SecretKey => '', + TencentOption::Region => '', + TencentOption::BizType => '' + ], 'aliyun' => [ AliyunOption::AccessKeyId => '', AliyunOption::AccessKeySecret => '', diff --git a/resources/views/admin/group/add.blade.php b/resources/views/admin/group/add.blade.php index b79ff419..bbecb021 100644 --- a/resources/views/admin/group/add.blade.php +++ b/resources/views/admin/group/add.blade.php @@ -116,9 +116,34 @@