Merge branch 'v5.1.0dev' of https://gitee.com/ZhongBangKeJi/CRMEB into v5.1.0dev

This commit is contained in:
From-wh 2023-06-08 18:16:20 +08:00
commit d1363eaf6e
7 changed files with 304 additions and 129 deletions

View File

@ -166,7 +166,10 @@ class SystemStorage extends AuthController
//设置跨域规则
try {
$upload = UploadService::init($info->type);
$upload->setBucketCors($info->name, $info->region);
$res = $upload->setBucketCors($info->name, $info->region);
if (false === $res) {
return app('json')->fail($upload->getError());
}
} catch (\Throwable $e) {
}
@ -199,6 +202,7 @@ class SystemStorage extends AuthController
public function updateDomain($id)
{
$domain = $this->request->post('domain', '');
$cdn = $this->request->post('cdn', '');
$data = $this->request->postMore([
['pri', ''],
['ca', '']
@ -214,7 +218,7 @@ class SystemStorage extends AuthController
// return app('json')->fail('域名为HTTPS访问时,必须填写证书');
// }
$this->services->updateDomain($id, $domain);
$this->services->updateDomain($id, $domain, ['cdn' => $cdn]);
return app('json')->success(100001);
}

View File

@ -95,6 +95,7 @@ class UploadService
$config['uploadUrl'] = $res['domain'];
$config['storageName'] = $res['name'];
$config['storageRegion'] = $res['region'];
$config['cdn'] = $res['cdn'];
}
$thumb = SystemConfigService::more(['thumb_big_height', 'thumb_big_width', 'thumb_mid_height', 'thumb_mid_width', 'thumb_small_height', 'thumb_small_width',]);

View File

@ -368,6 +368,29 @@ class SystemStorageServices extends BaseServices
}
}
break;
case 5:// cos 京东云
case 6:// cos 华为云
case 7:// cos 天翼云
$upload = UploadService::init($type);
$list = $upload->listbuckets();
$config = $this->getStorageConfig($type);
foreach ($list as $item) {
if (!$this->dao->count(['name' => $item['Name'], 'access_key' => $config['accessKey']])) {
$data[] = [
'type' => $type,
'access_key' => $config['accessKey'],
'name' => $item['Name'],
'region' => $item['Location'],
'acl' => 'public-read',
'status' => 0,
'domain' => $this->getDomain($type, $item['Name'], $item['Location']),
'is_delete' => 0,
'add_time' => strtotime($item['CreationDate']),
'update_time' => time()
];
}
}
break;
}
if ($data) {
$this->dao->saveAll($data);
@ -413,6 +436,15 @@ class SystemStorageServices extends BaseServices
case 4:// cos 腾讯云
$domainName = 'https://' . $name . ($appid ? '-' . $appid : '') . '.cos.' . $reagion . '.myqcloud.com';
break;
case 5:// cos 京东云
$domainName = 'https://' . $name . '.cos.' . $reagion . '.jdcloud-oss.com';
break;
case 6:// cos 华为云
$domainName = 'https://' . $name . '.cos.' . $reagion . '.myhuaweicloud.com';
break;
case 7:// cos 天翼云
$domainName = 'https://' . $name . '.cos.' . $reagion . '.ctyun.cn';
break;
}
return $domainName;
}
@ -425,11 +457,11 @@ class SystemStorageServices extends BaseServices
*/
public function getConfig(int $type)
{
$res = ['name' => '', 'region' => '', 'domain' => ''];
$res = ['name' => '', 'region' => '', 'domain' => '', 'cdn' => ''];
try {
$config = $this->dao->get(['type' => $type, 'status' => 1, 'is_delete' => 0]);
if ($config) {
return ['name' => $config->name, 'region' => $config->region, 'domain' => $config->domain];
return ['name' => $config->name, 'region' => $config->region, 'domain' => $config->domain, 'cdn' => $config->cdn];
}
} catch (\Throwable $e) {
}
@ -445,9 +477,10 @@ class SystemStorageServices extends BaseServices
*/
public function getUpdateDomainForm(int $id)
{
$domain = $this->dao->value(['id' => $id], 'domain');
$storage = $this->dao->get(['id' => $id], ['domain', 'cdn']);
$rule = [
FormBuilder::input('domain', '空间域名', $domain),
FormBuilder::input('domain', '空间域名', $storage['domain']),
FormBuilder::input('cdn', 'cdn域名', $storage['cdn']),
];
return create_form('修改空间域名', $rule, '/system/config/storage/domain/' . $id);
}
@ -488,7 +521,11 @@ class SystemStorageServices extends BaseServices
$resDomain = $upload->getDomianInfo($domain);
$info->cname = $resDomain['cname'] ?? '';
}
return $info->save();
$info->save();
}
if ($info->cdn != $data['cdn']) {
$info->cdn = $data['cdn'];
$info->save();
}
$this->cacheDriver()->clear();

View File

@ -175,10 +175,8 @@ class Client extends BaseClient
*/
public function putObject(string $key, $body, string $contentType = 'image/jpeg')
{
$url = $this->uploadUrl ?: $this->getRequestUrl($this->bucketName, $this->region);
$header = [
'Host' => $url,
'Host' => $this->getRequestUrl($this->bucketName, $this->region),
'Content-Type' => $contentType,
'Content-Length' => strlen($body),
];
@ -222,7 +220,7 @@ class Client extends BaseClient
public function listBuckets()
{
$header = [
'Host' => $this->getRequestUrl($this->bucketName, $this->region),
'Host' => $this->getRequestUrl('', $this->region),
];
$res = $this->request('https://' . $header['Host'] . '/', 'GET', [], []);
return $this->response($res);
@ -237,6 +235,30 @@ class Client extends BaseClient
return $this->response($res);
}
/**
* 设置桶的策略
* @param string $bucket
* @param string $region
* @param array $data
* @return mixed
*
* @date 2023/06/08
* @author yyw
*/
public function putPolicy(string $bucket, string $region, array $data)
{
$header = [
'Host' => $this->getRequestUrl($bucket, $region),
"Content-Type" => "application/json"
];
$res = $this->request('https://' . $header['Host'] . '/?policy', 'PUT', [
'bucket' => $bucket,
'json' => $data
], $header);
return $this->response($res);
}
/**
* 创建桶
* @param string $bucket
@ -252,10 +274,12 @@ class Client extends BaseClient
$header = [
'x-obs-acl' => $acl,
'Host' => $this->getRequestUrl($bucket, $region),
"Content-Type" => "application/xml"
];
$xml = "<CreateBucketConfiguration><Location>{$region}</Location></CreateBucketConfiguration>";
$res = $this->request('https://' . $header['Host'] . '/', 'PUT', [
'Location' => $region
'bucket' => $bucket,
'body' => $xml
], $header);
return $this->response($res);
@ -333,12 +357,14 @@ class Client extends BaseClient
*/
public function putBucketCors(string $bucket, string $region, array $data = [])
{
$xml = $this->xmlBuild($data, 'CORSConfiguration', 'CORSRule');
$header = [
'Host' => $this->getRequestUrl($bucket, $region),
'content-md5' => base64_encode(md5(json_encode($data), true))
'Content-Type' => 'application/xml',
'Content-Length' => strlen($xml),
'Content-MD5' => base64_encode(md5($xml, true))
];
$xml = $this->xmlBuild($data, 'CORSConfiguration', 'CORSRule');
$res = $this->request('https://' . $header['Host'] . '/cors ', 'PUT', [
$res = $this->request('https://' . $header['Host'] . '/?cors', 'PUT', [
'bucket' => $bucket,
'body' => $xml
], $header);
@ -346,6 +372,27 @@ class Client extends BaseClient
return $this->response($res);
}
/**
* 删除跨域
* @param string $bucket
* @param string $region
* @return mixed
*
* @date 2023/06/08
* @author yyw
*/
public function deleteBucketCors(string $bucket, string $region)
{
$header = [
'Host' => $this->getRequestUrl($bucket, $region),
];
$res = $this->request('https://' . $header['Host'] . '/?cors', 'DELETE', [
'bucket' => $bucket,
], $header);
return $this->response($res);
}
/**
* @param $res
* @return mixed
@ -366,19 +413,22 @@ class Client extends BaseClient
* @param string $bucket
* @param string $region
* @return string
* @author 等风来
* @email 136327134@qq.com
* @date 2023/5/18
*
* @date 2023/06/08
* @author yyw
*/
protected function getRequestUrl(string $bucket, string $region)
protected function getRequestUrl(string $bucket = '', string $region = '')
{
if ($this->type == 'hw') {
return $bucket . '.obs.' . $region . '.myhuaweicloud.com'; // 华为
$url = '.myhuaweicloud.com'; // 华为
} else {
return $bucket . '.obs.' . $region . '.ctyun.cn'; // 天翼
$url = $region . '.ctyun.cn'; // 天翼
}
if ($bucket) {
return $bucket . '.obs.' . $region . $url;
} else {
return 'obs.' . $region . $url;
}
}
/**

View File

@ -147,7 +147,7 @@ class Jdoss extends BaseUpload
'Key' => $key,
'SourceFile' => $fileHandle->getRealPath()
]);
if (!isset($uploadInfo['info']['url'])) {
if (!isset($uploadInfo['ObjectURL'])) {
return $this->setError('Upload failure');
}
$this->fileInfo->uploadInfo = $uploadInfo;
@ -157,7 +157,7 @@ class Jdoss extends BaseUpload
$this->fileInfo->filePathWater = $this->water($this->fileInfo->filePath);
$this->authThumb && $this->thumb($this->fileInfo->filePath);
return $this->fileInfo;
} catch (UploadException $e) {
} catch (\Throwable $e) {
return $this->setError($e->getMessage());
}
}
@ -185,7 +185,7 @@ class Jdoss extends BaseUpload
$this->fileInfo->filePathWater = $this->water($this->fileInfo->filePath);
$this->thumb($this->fileInfo->filePath);
return $this->fileInfo;
} catch (UploadException $e) {
} catch (\Throwable $e) {
return $this->setError($e->getMessage());
}
}
@ -207,7 +207,7 @@ class Jdoss extends BaseUpload
{
try {
$res = $this->app()->listBuckets();
return $res['Buckets']['Bucket'] ?? [];
return $res['ListAllMyBucketsResult']['Buckets'] ?? [];
} catch (\Throwable $e) {
return [];
}
@ -290,7 +290,7 @@ class Jdoss extends BaseUpload
{
try {
$this->storageRegion = $region;
$res = $this->app()->getBucketWebsite([
$res = $this->app()->getBucketPolicy([
'Bucket' => $name
]);
return $res['DomainName'] ?? [];
@ -429,33 +429,4 @@ class Jdoss extends BaseUpload
}
return $waterPath;
}
public function setError(?string $error = null)
{
$this->error = $error ?: '未知错误';
if (env('APP_DEBUG')) {
throw new UploadException($this->xmlToArr($this->error));
} else {
return false;
}
}
function xmlToArr($errorXml)
{
$pattern = '/<statusCode>(\d+)<\/statusCode><Code>(.*?)<\/Code><Message>(.*?)<\/Message><Resource>(.*?)<\/Resource><RequestId>(.*?)<\/RequestId>/';
preg_match($pattern, $errorXml, $matches);
$error = [
'statusCode' => $matches[1],
'Code' => $matches[2],
'Message' => $matches[3],
'Resource' => $matches[4],
'RequestId' => $matches[5]
];
return $error;
}
}

View File

@ -6,6 +6,7 @@ use crmeb\exceptions\AdminException;
use crmeb\exceptions\UploadException;
use crmeb\services\upload\extend\obs\Client as TyClient;
use crmeb\services\upload\BaseUpload;
use GuzzleHttp\Psr7\Utils;
class Obs extends BaseUpload
{
@ -70,7 +71,8 @@ class Obs extends BaseUpload
}
$key = $this->saveFileName($fileHandle->getRealPath(), $fileHandle->getOriginalExtension());
$body = $fileHandle->getRealPath();
$body = fopen($fileHandle->getRealPath(), 'rb');
$body = (string)Utils::streamFor($body);
} else {
$key = $file;
$body = $fileContent;
@ -78,7 +80,7 @@ class Obs extends BaseUpload
$key = $this->getUploadPath($key);
try {
$uploadInfo = $this->app()->putObject($key, $body);
$uploadInfo = $this->app()->putObject($key, $body, 'application/octet-stream');
$this->fileInfo->uploadInfo = $uploadInfo;
$this->fileInfo->realName = $fileHandle->getOriginalName();
$this->fileInfo->filePath = ($this->cdn ?: $this->uploadUrl) . '/' . $key;
@ -134,17 +136,16 @@ class Obs extends BaseUpload
$this->handle = new TyClient([
'accessKey' => $this->accessKey,
'secretKey' => $this->secretKey,
'region' => $this->storageRegion ?: 'oos-hazz',
'region' => $this->storageRegion ?: 'cn-north-1',
'bucket' => $this->storageName,
'uploadUrl' => $this->uploadUrl
]);
return $this->handle;
}
public function listbuckets(string $region, bool $line = false, bool $shared = false)
public function listbuckets(string $region = null, bool $line = false, bool $shared = false)
{
try {
$this->storageRegion = $region;
$res = $this->app()->listBuckets();
return $res['Buckets']['Bucket'] ?? [];
} catch (\Throwable $e) {
@ -164,6 +165,19 @@ class Obs extends BaseUpload
//创建桶
try {
$app->createBucket($name, $region, $acl);
$data = [
'Statement' => [
'Sid' => '公共读' . $name,
'Effect' => 'Allow',
'Principal' => [
'ID' => ['*']
],
'Action' => ['HeadBucket', 'GetBucketLocation', 'ListBucketVersions', 'GetObject', 'RestoreObject', 'GetObjectVersion'],
'Resource' => [$name, $name . '/*']
]
];
$app->putPolicy($name, $region, $data);
} catch (\Throwable $e) {
return $this->setError('COS:' . $e->getMessage());
}
@ -190,7 +204,7 @@ class Obs extends BaseUpload
try {
$res = $this->app()->GetBucketDomain($name, $region);
if ($res) {
$domainRules = $res->toArray()['ListBucketCustomDomainsResult '];
$domainRules = $res->toArray()['ListBucketCustomDomainsResult'];
return array_column($domainRules, 'DomainName');
} else {
return [];
@ -205,17 +219,13 @@ class Obs extends BaseUpload
{
$parseDomin = parse_url($domain);
try {
$this->app()->putBucketDomain($name, '', [
$this->app()->putBucketDomain($name, $region, [
'domainname' => $parseDomin['host'],
]);
return true;
} catch (\Throwable $e) {
if ($message = $this->setMessage($e->getMessage())) {
return $this->setError($message);
}
return $this->setError($e->getMessage());
}
return false;
}
public function setBucketCors(string $name, string $region)

View File

@ -5,6 +5,7 @@ namespace crmeb\services\upload\storage;
use crmeb\exceptions\AdminException;
use crmeb\services\upload\extend\obs\Client as TyClient;
use crmeb\services\upload\BaseUpload;
use GuzzleHttp\Psr7\Utils;
class Tyoss extends BaseUpload
{
@ -69,7 +70,8 @@ class Tyoss extends BaseUpload
}
$key = $this->saveFileName($fileHandle->getRealPath(), $fileHandle->getOriginalExtension());
$body = $fileHandle->getRealPath();
$body = fopen($fileHandle->getRealPath(), 'rb');
$body = (string)Utils::streamFor($body);
} else {
$key = $file;
$body = $fileContent;
@ -77,7 +79,7 @@ class Tyoss extends BaseUpload
$key = $this->getUploadPath($key);
try {
$uploadInfo = $this->app()->putObject($key, $body);
$uploadInfo = $this->app()->putObject($key, $body, 'application/octet-stream');
$this->fileInfo->uploadInfo = $uploadInfo;
$this->fileInfo->realName = $fileHandle->getOriginalName();
$this->fileInfo->filePath = ($this->cdn ?: $this->uploadUrl) . '/' . $key;
@ -133,7 +135,7 @@ class Tyoss extends BaseUpload
$this->handle = new TyClient([
'accessKey' => $this->accessKey,
'secretKey' => $this->secretKey,
'region' => $this->storageRegion ?: 'oos-hazz',
'region' => $this->storageRegion ?: 'cn-qhxn1',
'bucket' => $this->storageName,
'uploadUrl' => $this->uploadUrl,
'type' => 'ty'
@ -141,10 +143,9 @@ class Tyoss extends BaseUpload
return $this->handle;
}
public function listbuckets(string $region, bool $line = false, bool $shared = false)
public function listbuckets(string $region = null, bool $line = false, bool $shared = false)
{
try {
$this->storageRegion = $region;
$res = $this->app()->listBuckets();
return $res['Buckets']['Bucket'] ?? [];
} catch (\Throwable $e) {
@ -163,79 +164,156 @@ class Tyoss extends BaseUpload
$app = $this->app();
//创建桶
try {
$res = $app->createBucket($name, $region, $acl);
$app->createBucket($name, $region, $acl);
$data = [
'Statement' => [
'Sid' => '公共读' . $name,
'Effect' => 'Allow',
'Principal' => [
'ID' => ['*']
],
'Action' => ['HeadBucket', 'GetBucketLocation', 'ListBucketVersions', 'GetObject', 'RestoreObject', 'GetObjectVersion'],
'Resource' => [$name, $name . '/*']
]
];
$app->putPolicy($name, $region, $data);
} catch (\Throwable $e) {
return $this->setError('COS:' . $e->getMessage());
}
return $res;
return true;
}
public function getRegion()
{
return [
[
'value' => 'oos-hazz',
'value' => 'cn-gz1',
'label' => '贵州'
],
[
'value' => 'cn-fz1',
'label' => '福州'
],
[
'value' => 'cn-hz1',
'label' => '杭州'
],
[
'value' => 'cn-sz1',
'label' => '深圳'
],
[
'value' => 'cn-gdgz1',
'label' => '广州'
],
[
'value' => 'cn-jssz1',
'label' => '苏州'
],
[
'value' => 'cn-sh1',
'label' => '上海'
],
[
'value' => 'cn-ahwh1',
'label' => '芜湖'
],
[
'value' => 'cn-bj1',
'label' => '北京'
],
[
'value' => 'cn-sccd1',
'label' => '成都'
],
[
'value' => 'cn-hazz1',
'label' => '郑州'
],
[
'value' => 'oos-lnsy',
'label' => '沈阳'
'value' => 'cn-hncs1',
'label' => '长沙'
],
[
'value' => 'oos-sccd',
'label' => '四川成都'
'value' => 'cn-gxnn1',
'label' => '南宁'
],
[
'value' => 'oos-xjwlmq',
'label' => '乌鲁木齐'
'value' => 'cn-jxnc1',
'label' => '南昌'
],
[
'value' => 'oos-xjwlmq',
'label' => '乌鲁木齐'
],
[
'value' => 'oos-gslz',
'label' => '甘肃兰州'
],
[
'value' => 'oos-sdqd',
'label' => '山东青岛'
],
[
'value' => 'oos-gzgy',
'label' => '贵州贵阳'
],
[
'value' => 'oos-hbwh',
'label' => '湖北武汉'
],
[
'value' => 'oos-xzls',
'label' => '西藏拉萨'
],
[
'value' => 'oos-hbwh',
'label' => '湖北武汉'
],
[
'value' => 'oos-ahwh',
'label' => '安徽芜湖'
],
[
'value' => 'oos-gdsz',
'label' => '广东深圳'
],
[
'value' => 'oos-jssz',
'label' => '江苏苏州'
],
[
'value' => 'oos-sh2',
'label' => '上海2'
'value' => 'cn-sdqd1',
'label' => '青岛'
],
[
'value' => 'cn-snxy1',
'label' => '西安2'
'label' => '咸阳'
],
[
'value' => 'cn-xjcj1',
'label' => '新疆'
],
[
'value' => 'cn-ynkm1',
'label' => '昆明'
],
[
'value' => 'cn-hihk1',
'label' => '海口'
],
[
'value' => 'cn-hbwh1',
'label' => '武汉'
],
[
'value' => 'cn-cq1',
'label' => '重庆'
],
[
'value' => 'cn-qhxn1',
'label' => '西宁'
],
[
'value' => 'cn-gslz1',
'label' => '兰州'
],
[
'value' => 'cn-nxyc1',
'label' => '银川'
],
[
'value' => 'cn-sxty1',
'label' => '太原'
],
[
'value' => 'cn-hesjz1',
'label' => '石家庄'
],
[
'value' => 'cn-tj1',
'label' => '天津'
],
[
'value' => 'cn-jlcc1',
'label' => '长春'
],
[
'value' => 'cn-hlhrb1',
'label' => '哈尔滨'
],
[
'value' => 'cn-nmhh1',
'label' => '内蒙古'
],
[
'value' => 'cn-lnsy1',
'label' => '沈阳'
],
[
'value' => 'cn-north1',
'label' => '华北'
]
];
}
@ -250,20 +328,44 @@ class Tyoss extends BaseUpload
}
}
public function getDomian($name, $region)
{
try {
$res = $this->app()->GetBucketDomain($name, $region);
if ($res) {
$domainRules = $res->toArray()['ListBucketCustomDomainsResult'];
return array_column($domainRules, 'DomainName');
} else {
return [];
}
} catch (\Throwable $e) {
}
return [];
}
public function bindDomian(string $name, string $domain, string $region = null)
{
// TODO: Implement bindDomian() method.
$parseDomin = parse_url($domain);
try {
$this->app()->putBucketDomain($name, $region, [
'domainname' => $parseDomin['host'],
]);
return true;
} catch (\Throwable $e) {
return $this->setError($e->getMessage());
}
}
public function setBucketCors(string $name, string $region)
{
try {
$res = $this->app()->PutBucketCors($name, $region, [
$this->app()->PutBucketCors($name, $region, [
'AllowedHeaders' => ['*'],
'AllowedMethods' => ['PUT', 'GET', 'POST', 'DELETE', 'HEAD'],
'AllowedOrigins' => ['*'],
'ExposeHeaders' => ['ETag', 'Content-Length', 'x-cos-request-id'],
'MaxAgeSeconds' => 12
'MaxAgeSeconds' => 100
]);
return true;
} catch (\Throwable $e) {