StorageRepository.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\repositories\system;
  12. use app\common\dao\system\SystemStorageDao;
  13. use app\common\repositories\BaseRepository;
  14. use app\common\repositories\system\config\ConfigValueRepository;
  15. use crmeb\services\UploadService;
  16. use FormBuilder\Factory\Elm;
  17. use think\exception\ValidateException;
  18. use think\facade\Db;
  19. use think\facade\Route;
  20. use think\queue\command\Retry;
  21. class StorageRepository extends BaseRepository
  22. {
  23. protected $service;
  24. public function __construct(SystemStorageDao $dao, UploadService $service)
  25. {
  26. $this->dao = $dao;
  27. $this->service = $service;
  28. }
  29. /**
  30. * 获取服务的类型
  31. *
  32. * 本方法通过调用内部服务对象的getType方法,来获取服务的类型。
  33. * 该方法的存在是为了提供一个统一的接口,用于外部代码获取当前服务的类型信息,
  34. * 而不需要直接与内部服务对象交互。
  35. *
  36. * @return string 返回服务的类型
  37. */
  38. public function getType()
  39. {
  40. return $this->service->getType();
  41. }
  42. /**
  43. * 获取指定类型的前缀
  44. *
  45. * 本方法通过调用服务类的静态方法getPrefix来获取特定类型的数据前缀。
  46. * 这种设计模式的使用允许灵活地从不同的服务类中获取配置或数据前缀,从而提高了代码的可扩展性和可维护性。
  47. *
  48. * @param int $type 类型标识,默认为1。不同的类型标识可以用于获取不同数据或配置的前缀,这取决于服务类的具体实现。
  49. * @return string 返回指定类型的前缀。具体前缀的格式和含义由服务类的实现决定。
  50. */
  51. public function getPrefix($type = 1)
  52. {
  53. return $this->service::getPrefix($type);
  54. }
  55. /**
  56. * 获取区域列表
  57. *
  58. * 根据给定的条件和分页信息,从数据库中检索区域列表。此方法主要用于支持分页查询,以及根据条件筛选结果。
  59. *
  60. * @param array $where 查询条件,包含类型在内的各种过滤条件。
  61. * @param int $page 当前页码,用于分页查询。
  62. * @param int $limit 每页显示的记录数,用于分页查询。
  63. * @return array 返回包含总数和列表数据的数组。
  64. */
  65. public function lstRegion(array $where, int $page, int $limit)
  66. {
  67. // 根据类型获取前缀,用于构建访问密钥的名称
  68. $prefix = $this->service::getPrefix($where['type']);
  69. // 构建访问密钥的完整名称
  70. $accessKey = $prefix . 'accessKey';
  71. // 通过系统配置获取访问密钥,并将其添加到查询条件中
  72. $where['access_key'] = systemConfig($accessKey);
  73. // 根据条件执行查询
  74. $query = $this->dao->getSearch($where);
  75. // 统计查询结果的总数
  76. $count = $query->count();
  77. // 获取当前页码的分页数据
  78. $list = $query->page($page, $limit)->select();
  79. // 返回包含总数和列表数据的数组
  80. return compact('count', 'list');
  81. }
  82. /**
  83. * 根据类型获取配置信息
  84. *
  85. * 本函数旨在通过指定的类型从数据库中检索相应的配置信息。它尝试从配置表中选取类型匹配、状态启用且未被删除的配置项。
  86. * 如果找到符合条件的配置,则返回该配置的详细信息;如果未找到,则返回一个空的配置数组。
  87. *
  88. * @param int $type 配置的类型标识。用于指定查询哪种类型的配置信息。
  89. * @return array 包含配置名称、区域、域名和CDN信息的数组。如果未找到匹配的配置,则所有字段均为空字符串。
  90. */
  91. public function getConfig(int $type, $accessKey)
  92. {
  93. // 初始化一个包含默认空值的配置数组
  94. $res = ['name' => '', 'region' => '', 'domain' => '', 'cdn' => ''];
  95. try {
  96. // 尝试从数据库中根据类型、状态和删除标记获取配置项
  97. $config = $this->dao->getWhere(['access_key' => $accessKey,'type' => $type, 'status' => 1, 'is_del' => 0]);
  98. // 如果成功获取到配置信息,则返回该配置的详细数组
  99. if ($config) {
  100. return ['name' => $config->name, 'region' => $config->region, 'domain' => $config->domain, 'cdn' => $config->cdn];
  101. }
  102. } catch (\Throwable $e) {
  103. // 捕获并处理任何在尝试获取配置信息过程中抛出的异常
  104. }
  105. // 如果未找到匹配的配置或发生异常,则返回初始化的默认配置数组
  106. return $res;
  107. }
  108. /**
  109. * 根据类型生成存储服务配置表单
  110. *
  111. * 本函数用于生成一个表单,该表单根据传入的类型(如腾讯云、京东云等)
  112. * 动态配置相应的访问密钥(AccessKey和SecretKey)以及其他必要的配置项。
  113. * 通过这种方式,用户可以方便地配置和管理不同存储服务的访问凭证。
  114. *
  115. * @param string $type 存储服务类型,决定表单中需要显示的具体配置项。
  116. * @return \EasyAdmin\ui\Elm|\FormBuilder\Form
  117. */
  118. public function form($type)
  119. {
  120. // 通过服务类获取指定类型存储服务的前缀
  121. $prefix = $this->service::getPrefix($type);
  122. // 根据前缀和键名构造访问密钥的配置键名
  123. //获取配置
  124. //accessKey
  125. $accessKey = $prefix . 'accessKey';
  126. //secretKey
  127. $secretKey = $prefix . 'secretKey';
  128. // 创建表单对象,并设置表单提交的URL
  129. $form = Elm::createForm(Route::buildUrl('systemStorageUpdate')->build());
  130. // 定义表单规则,包括隐藏字段upload_type和必填的AccessKey、SecretKey输入字段
  131. $rule = [
  132. Elm::hidden('upload_type', $type),
  133. Elm::input('accessKey', 'AccessKey:', systemConfig($accessKey))->placeholder('请输入accessKey')->required(),
  134. Elm::input('secretKey', 'SecretKey:', systemConfig($secretKey))->placeholder('请输入secretKey')->required(),
  135. ];
  136. // 根据类型动态添加特定的配置项,如腾讯云的APPID,京东云的storageRegion
  137. if ($type == $this->service::STORAGE_TENGXUN) {
  138. $rule[] = Elm::input('tengxun_appid', 'APPID:', systemConfig('tengxun_appid'))->placeholder('请输入APPID')->required();
  139. }
  140. if ($type == $this->service::STORAGE_JINGDONG) {
  141. $rule[] = Elm::input('jd_storageRegion', 'storageRegion:', systemConfig('jd_storageRegion'))->placeholder('请输入storageRegion')->required();
  142. }
  143. // 设置表单的规则,即定义了表单中显示的字段及其验证规则
  144. $form->setRule($rule);
  145. // 设置表单标题
  146. return $form->setTitle('配置');
  147. }
  148. /**
  149. * 创建区域表单
  150. * 用于系统中创建云存储区域的表单生成,根据不同的存储类型配置不同的表单字段。
  151. *
  152. * @param int $type 存储类型,决定表单的具体配置。
  153. * @return \EasyWeChat\Kernel\Messages\Form 创建的表单实例。
  154. */
  155. public function createRegionForm(int $type)
  156. {
  157. // 创建上传服务实例,根据类型配置不同的上传策略
  158. $upload = UploadService::create($type);
  159. // 根据类型获取配置前缀,用于拼接配置项的键名
  160. $prefix = $this->service::getPrefix($type);
  161. // 拼接accessKey和secretKey的键名,并获取对应的配置值
  162. $accessKey = $prefix . 'accessKey';
  163. $secretKey = $prefix . 'secretKey';
  164. // 从系统配置中获取accessKey、secretKey和tengxun_appid的值
  165. $config = systemConfig([$accessKey, $secretKey, 'tengxun_appid']);
  166. // 构建表单提交的URL,并创建表单实例
  167. $form = Elm::createForm(Route::buildUrl('systemStorageCreateRegion', ['type' => $type])->build());
  168. // 初始化表单规则配置
  169. $ruleConfig = [];
  170. // 如果accessKey未配置,则添加accessKey和secretKey的输入字段
  171. if (!$config[$accessKey]) {
  172. $ruleConfig = [
  173. Elm::input('accessKey', 'AccessKey:', $config[$accessKey])->placeholder('请输入accessKey')->required(),
  174. Elm::input('secretKey', 'SecretKey:', $config[$secretKey])->placeholder('请输入secretKey')->required(),
  175. ];
  176. }
  177. // 如果是腾讯云存储类型且未配置appid,则添加appid的输入字段
  178. if ($type == $this->service::STORAGE_TENGXUN && !$config['tengxun_appid']) {
  179. $ruleConfig[] = Elm::input('tengxun_appid', 'APPID:')->placeholder('请输入APPID')->required();
  180. }
  181. // 定义表单的基本规则,包括空间名称、区域和读写权限的选择
  182. $rule = [
  183. Elm::input('name', '空间名称')->required()->min(5),
  184. Elm::select('region', '空间区域')->options($upload->getRegion())->required(),
  185. Elm::radio('acl', '读写权限', 'public-read')->options([
  186. ['label' => '公共读(推荐)', 'value' => 'public-read'],
  187. ['label' => '公共读写', 'value' => 'public-read-write'],
  188. ])->required(),
  189. ];
  190. // 将之前的规则配置合并到基本规则中
  191. $rule = array_merge($ruleConfig, $rule);
  192. // 设置表单的验证规则
  193. $form->setRule($rule);
  194. // 设置表单的标题
  195. $form->setTitle('添加云空间');
  196. // 返回构建好的表单实例
  197. return $form;
  198. }
  199. /**
  200. * 编辑域名表单
  201. *
  202. * 本函数用于生成编辑存储空间域名的表单。通过传入的ID,获取相应的存储空间信息,
  203. * 并基于这些信息构建一个包含域名和CDN域名输入字段的表单。
  204. *
  205. * @param int $id 存储空间的ID,用于获取特定存储空间的信息。
  206. * @return \Phalcon\Forms\Form 表单实例,包含用于编辑域名的输入字段和表单提交的URL。
  207. */
  208. public function editDomainForm($id)
  209. {
  210. // 根据ID获取存储空间的信息
  211. $storage = $this->dao->get($id);
  212. // 构建表单提交的URL
  213. $formAction = Route::buildUrl('systemStorageUpdateDomain', ['id' => $id])->build();
  214. // 创建表单实例
  215. $form = Elm::createForm($formAction);
  216. // 定义表单规则,包含域名和CDN域名的输入字段
  217. $rule = [
  218. Elm::input('domain', '空间域名', $storage['domain']),
  219. Elm::input('cdn', 'cdn域名', $storage['cdn']),
  220. ];
  221. // 设置表单的验证规则
  222. $form->setRule($rule);
  223. // 设置表单标题
  224. return $form->setTitle('配置');
  225. }
  226. /**
  227. * 修改空间域名
  228. * @param int $id
  229. * @param string $domain
  230. * @param array $data
  231. * @return bool
  232. * @author Qinii
  233. * @day 2024/3/13
  234. */
  235. public function updateDomain(int $id, string $domain, array $data = [])
  236. {
  237. $info = $this->dao->get($id);
  238. if (!$info) {
  239. throw new ValidateException('数据不存在');
  240. }
  241. if ($info->domain != $domain) {
  242. $info->domain = $domain;
  243. $upload = UploadService::create($info->type);
  244. //是否添加过域名不存在需要绑定域名
  245. $domainList = $upload->getDomian($info->name, $info->region);
  246. $domainParse = parse_url($domain);
  247. if (false === $domainParse) {
  248. throw new ValidateException('域名输入有误');
  249. }
  250. if (!in_array($domainParse['host'], $domainList)) {
  251. //绑定域名到云储存桶
  252. $res = $upload->bindDomian($info->name, $domain, $info->region);
  253. if (false === $res) {
  254. throw new ValidateException($upload->getError());
  255. }
  256. }
  257. //七牛云需要通过接口获取cname
  258. if (2 === ((int)$info->type)) {
  259. $resDomain = $upload->getDomianInfo($domain);
  260. $info->cname = $resDomain['cname'] ?? '';
  261. }
  262. $info->save();
  263. }
  264. if ($info->cdn != $data['cdn']) {
  265. $info->cdn = $data['cdn'];
  266. $info->save();
  267. }
  268. return true;
  269. }
  270. /**
  271. * 选择使用某个存储空间
  272. * @param $id
  273. * @param $info
  274. * @return mixed|\think\response\Json
  275. * @author Qinii
  276. * @day 2024/3/13
  277. */
  278. public function status($id, $info)
  279. {
  280. //设置跨域规则
  281. try {
  282. $upload = UploadService::create($info->type);
  283. $res = $upload->setBucketCors($info->name, $info->region);
  284. if (false === $res) {
  285. return app('json')->fail($upload->getError());
  286. }
  287. } catch (\Throwable $e) {
  288. }
  289. //修改状态
  290. return Db::transaction(function () use ($id, $info) {
  291. $this->dao->getSearch(['type' => $info->type])->update(['status' => 0]);
  292. $info->status = 1;
  293. $info->save();
  294. });
  295. }
  296. /**
  297. * 删除存储空间
  298. * @param int $id
  299. * @return bool
  300. * @author Qinii
  301. * @day 2024/3/13
  302. */
  303. public function deleteRegion(int $id)
  304. {
  305. $storageInfo = $this->dao->getSearch(['is_del' => 0, 'id' => $id])->find();
  306. if (!$storageInfo) {
  307. throw new ValidateException('数据不存在');
  308. }
  309. if ($storageInfo->status) {
  310. throw new ValidateException('存储空间使用中不能删除');
  311. }
  312. try {
  313. $upload = UploadService::create($storageInfo->type);
  314. $res = $upload->deleteBucket($storageInfo->name, $storageInfo->region);
  315. if (false === $res) {
  316. throw new ValidateException($upload->getError());
  317. }
  318. } catch (\Throwable $e) {
  319. throw new ValidateException($e->getMessage());
  320. }
  321. $storageInfo->delete();
  322. return true;
  323. }
  324. /**
  325. * 添加存储空间
  326. * @param int $type
  327. * @param array $data
  328. * @return bool
  329. * @author Qinii
  330. * @day 2024/3/13
  331. */
  332. public function createRegion(int $type, array $data, array $params)
  333. {
  334. $prefix = $this->service::getPrefix($type);
  335. $access_key = '';
  336. if ($params && $params['accessKey']){
  337. $access_key = $params['accessKey'];
  338. $secretKey = $params['secretKey'];
  339. unset($params['accessKey'],$params['secretKey']);
  340. $params[$prefix.'accessKey'] = $access_key;
  341. $params[$prefix.'secretKey'] = $secretKey;
  342. app()->make(ConfigValueRepository::class)->setFormData($params,0);
  343. }
  344. $access_key = $access_key ?: systemConfig($prefix . 'accessKey');
  345. $data['type'] = $type;
  346. $count = $this->dao->getWhereCount(['name' => $data['name'], 'access_key' => $access_key]);
  347. if ($count) throw new ValidateException('空间名称已存在');
  348. $upload = UploadService::create($type);
  349. $res = $upload->createBucket($data['name'], $data['region'], $data['acl']);
  350. if (false === $res) {
  351. throw new ValidateException($upload->getError());
  352. }
  353. if ($type === $this->service::STORAGE_ALIYUN) {
  354. $data['region'] = $this->getReagionHost($type, $data['region']);
  355. }
  356. $data['domain'] = $this->getDomain($type, $data['name'], $data['region'], systemConfig('tengxun_appid'));
  357. if ($type !== $this->service::STORAGE_QINIU) {
  358. $data['cname'] = $data['domain'];
  359. }
  360. $data['access_key'] = $access_key;
  361. if ($type === $this->service::STORAGE_TENGXUN) {
  362. $data['name'] = $data['name'].'-'.systemConfig('tengxun_appid');
  363. }
  364. $this->dao->create($data);
  365. $this->setDefualtUse($type,$access_key);
  366. return true;
  367. }
  368. /**
  369. * 同步存储空间
  370. * @param $type
  371. * @return bool
  372. * @author Qinii
  373. * @day 2024/3/14
  374. */
  375. public function synchRegion(int $type)
  376. {
  377. $upload = $this->service::create($type);
  378. $list = $upload->listbuckets();
  379. $data = [];
  380. if ($list) {
  381. $prefix = $this->service::getPrefix($type);
  382. $access_key = systemConfig($prefix . 'accessKey');
  383. $data = $this->{$prefix . 'sync_region'}($access_key, $list);
  384. }
  385. if ($data) {
  386. $this->dao->insertAll($data);
  387. $this->setDefualtUse($type,$access_key);
  388. }
  389. return true;
  390. }
  391. public function setDefualtUse(int $type,$access_key)
  392. {
  393. $config = $this->dao->getSearch([])->where(['type' => $type,'is_del' => 0,'access_key' => $access_key])->order('status DESC,create_time DESC')->find();
  394. if (!$config['status']) {
  395. $config->status = 1;
  396. $config->save();
  397. }
  398. }
  399. /**
  400. * 同步存储空间-七牛
  401. * @param $access_key
  402. * @param $list
  403. * @return array
  404. * @author Qinii
  405. * @day 2024/3/15
  406. */
  407. public function qiniu_sync_region($access_key, $list)
  408. {
  409. $data = [];
  410. $namesArray = [];
  411. foreach ($list as $item) {
  412. array_push($namesArray, $item['id']);
  413. if (!$this->dao->getWhereCount(['name' => $item['id'], 'access_key' => $access_key])) {
  414. $data[] = [
  415. 'type' => $this->service::STORAGE_QINIU,
  416. 'access_key' => $access_key,
  417. 'name' => $item['id'],
  418. 'region' => $item['region'],
  419. 'acl' => $item['private'] == 0 ? 'public-read' : 'private',
  420. 'status' => 0,
  421. 'domain' => $this->getDomain($this->service::STORAGE_QINIU,$item['id'],''),
  422. ];
  423. }
  424. }
  425. $removeList = $this->dao->getSearch([])->where([
  426. 'type' => $this->service::STORAGE_QINIU,
  427. 'access_key' => $access_key
  428. ])->whereNotIn('name', $namesArray)->delete();
  429. return $data;
  430. }
  431. /**
  432. * 同步存储空间-阿里
  433. * @param $access_key
  434. * @param $list
  435. * @return array
  436. * @author Qinii
  437. * @day 2024/3/15
  438. */
  439. public function sync_region($access_key, $list)
  440. {
  441. $data = [];
  442. $type = $this->service::STORAGE_ALIYUN;
  443. $namesArray = [];
  444. foreach ($list as $item) {
  445. array_push($namesArray, $item['name']);
  446. if (!$this->dao->getWhereCount(['name' => $item['name'], 'access_key' => $access_key])) {
  447. $region = $this->getReagionHost($type, $item['location']);
  448. $data[] = [
  449. 'type' => $type,
  450. 'access_key' => $access_key,
  451. 'name' => $item['name'],
  452. 'region' => $region,
  453. 'acl' => 'public-read',
  454. 'domain' => $this->getDomain($type, $item['name'], $region),
  455. 'status' => 0,
  456. ];
  457. }
  458. }
  459. $removeList = $this->dao->getSearch([])->where([
  460. 'type' => $type,
  461. 'access_key' => $access_key
  462. ])->whereNotIn('name', $namesArray)->delete();
  463. return $data;
  464. }
  465. /**
  466. * 同步存储空间-腾讯
  467. * @param $access_key
  468. * @param $list
  469. * @return array
  470. * @author Qinii
  471. * @day 2024/3/15
  472. */
  473. public function tengxun_sync_region($access_key, $list)
  474. {
  475. if (isset($list['Name'])) {
  476. $newlist = $list;
  477. $list = [];
  478. $list[] = $newlist;
  479. }
  480. $data = [];
  481. $namesArray = [];
  482. foreach ($list as $item) {
  483. array_push($namesArray, $item['Name']);
  484. $res = $this->dao->getWhereCount(['name' => $item['Name'], 'access_key' => $access_key]);
  485. if (!$res) {
  486. $data[] = [
  487. 'type' => $this->service::STORAGE_TENGXUN,
  488. 'access_key' => $access_key,
  489. 'name' => $item['Name'],
  490. 'region' => $item['Location'],
  491. 'acl' => 'public-read',
  492. 'status' => 0,
  493. 'domain' => systemConfig('tengxun_appid') ? $this->getDomain($this->service::STORAGE_TENGXUN, $item['Name'], $item['Location']) : '',
  494. ];
  495. }
  496. }
  497. $removeList = $this->dao->getSearch([])->where([
  498. 'type' => $this->service::STORAGE_TENGXUN,
  499. 'access_key' => $access_key
  500. ])->whereNotIn('name', $namesArray)->delete();
  501. return $data;
  502. }
  503. /**
  504. * 同步存储空间-华为
  505. * @param $access_key
  506. * @param $list
  507. * @return array
  508. * @author Qinii
  509. * @day 2024/3/15
  510. */
  511. public function obs_sync_region($access_key, $list)
  512. {
  513. if (isset($list['Name']) && !empty($list['Name'])) {
  514. $newlist = $list;
  515. $list = [];
  516. $list[] = $newlist;
  517. }
  518. $data = [];
  519. $namesArray = [];
  520. foreach ($list as $item) {
  521. array_push($namesArray, $item['Name']);
  522. if (!$this->dao->getWhereCount(['name' => $item['Name'], 'access_key' => $access_key])) {
  523. $data[] = [
  524. 'type' => $this->service::STORAGE_HUAWEI,
  525. 'access_key' => $access_key,
  526. 'name' => $item['Name'],
  527. 'region' => $item['Location'],
  528. 'acl' => 'public-read',
  529. 'status' => 0,
  530. 'domain' => $this->getDomain($this->service::STORAGE_HUAWEI, $item['Name'], $item['Location']),
  531. ];
  532. }
  533. }
  534. $removeList = $this->dao->getSearch([])->where([
  535. 'type' => $this->service::STORAGE_HUAWEI,
  536. 'access_key' => $access_key
  537. ])->whereNotIn('name', $namesArray)->delete();
  538. return $data;
  539. }
  540. /**
  541. * 同步存储空间-京东
  542. * @param $access_key
  543. * @param $list
  544. * @return array
  545. * @author Qinii
  546. * @day 2024/3/15
  547. */
  548. public function jdoss_sync_region($access_key, $list)
  549. {
  550. $list = $list['Buckets'];
  551. $data = [];
  552. $namesArray = [];
  553. $location = explode('.', $list['@metadata']['effectiveUri'])[1] ?? 'cn-north-1';
  554. foreach ($list as $item) {
  555. array_push($namesArray, $item['Name']);
  556. if (!$this->dao->getWhereCount(['name' => $item['Name'], 'access_key' => $access_key])) {
  557. $data[] = [
  558. 'type' => $this->service::STORAGE_JINGDONG,
  559. 'access_key' => $access_key,
  560. 'name' => $item['Name'],
  561. 'region' => $location,
  562. 'acl' => 'public-read',
  563. 'status' => 0,
  564. 'domain' => $this->getDomain($this->service::STORAGE_JINGDONG, $item['Name'], $location),
  565. ];
  566. }
  567. }
  568. $removeList = $this->dao->getSearch([])->where([
  569. 'type' => $this->service::STORAGE_JINGDONG,
  570. 'access_key' => $access_key
  571. ])->whereNotIn('name', $namesArray)->delete();
  572. return $data;
  573. }
  574. /**
  575. * 同步存储空间-天翼
  576. * @param $access_key
  577. * @param $list
  578. * @return array
  579. * @author Qinii
  580. * @day 2024/3/15
  581. */
  582. public function ctoss_sync_region($access_key, $list)
  583. {
  584. if (isset($list['Name'])) {
  585. $newlist = $list;
  586. $list = [];
  587. $list[] = $newlist;
  588. }
  589. $namesArray = [];
  590. $data = [];
  591. foreach ($list as $item) {
  592. array_push($namesArray, $item['Name']);
  593. if (!$this->dao->getWhereCount(['name' => $item['Name'], 'access_key' => $access_key])) {
  594. $data[] = [
  595. 'type' => $this->service::STORAGE_TIANYI,
  596. 'access_key' => $access_key,
  597. 'name' => $item['Name'],
  598. 'region' => $item['Location'],
  599. 'acl' => 'public-read',
  600. 'status' => 0,
  601. 'domain' => $this->getDomain($this->service::STORAGE_TIANYI, $item['Name'], $item['Location']),
  602. ];
  603. }
  604. }
  605. $removeList = $this->dao->getSearch([])->where([
  606. 'type' => $this->service::STORAGE_JINGDONG,
  607. 'access_key' => $access_key
  608. ])->whereNotIn('name', $namesArray)->delete();
  609. return $data;
  610. }
  611. /**
  612. * 同步存储空间-UC
  613. * @param $access_key
  614. * @param $list
  615. * @return bool
  616. * @author Qinii
  617. * @day 2024/3/15
  618. */
  619. public function uc_sync_region($access_key, $list)
  620. {
  621. $data = [];
  622. $namesArray = [];
  623. if($list && !empty($list)){
  624. foreach ($list as $item){
  625. array_push($namesArray, $item['BucketName']);
  626. if (!$this->dao->getWhereCount(['name' => $item['BucketName'], 'access_key' => $access_key])) {
  627. $data[] = [
  628. 'type' => $this->service::STORAGE_UCLOUD,
  629. 'access_key' => $access_key,
  630. 'name' => $item['BucketName'],
  631. 'region' => $item['Region'],
  632. 'acl' => $item['Type'],
  633. 'status' => 0,
  634. 'domain' => $this->getDomain($this->service::STORAGE_UCLOUD, $item['BucketName'], $item['Region']),
  635. ];
  636. }
  637. }
  638. }
  639. $removeList = $this->dao->getSearch([])->where([
  640. 'type' => $this->service::STORAGE_UCLOUD,
  641. 'access_key' => $access_key
  642. ])->whereNotIn('name', $namesArray)->delete();
  643. return $data;
  644. }
  645. /**
  646. * 根据存储服务类型和提供的信息构造存储服务的域名。
  647. *
  648. * 本函数用于根据不同的存储服务类型(如阿里云、腾讯云等),构造对应服务的域名。
  649. * 通过传入存储服务的类型、名称、区域和可选的应用程序ID,生成用于访问存储服务的URL。
  650. *
  651. * @param int $type 存储服务的类型,对应于服务常量中的定义(如ALIYUN、TENGXUN等)。
  652. * @param string $name 存储服务的名称,用于构成域名的一部分。
  653. * @param string $reagion 存储服务所在的区域,用于构成域名的一部分。
  654. * @param string $appid 应用程序的ID,对于某些存储服务(如腾讯云),可以用于区分不同的应用程序实例。
  655. * @return string 返回构造的存储服务域名。
  656. */
  657. public function getDomain(int $type, string $name, string $reagion, string $appid = '')
  658. {
  659. $domainName = '';
  660. // 根据存储服务类型选择不同的域名构造方式
  661. switch ($type) {
  662. case $this->service::STORAGE_QINIU:
  663. $domianList = $this->service::create($this->service::STORAGE_QINIU)->getDomian($name);
  664. $domainName = $domianList[count($domianList) - 1] ?? '';
  665. break;
  666. case $this->service::STORAGE_ALIYUN:
  667. // 阿里云对象存储域名格式
  668. $domainName = 'https://' . $name . '.' . $reagion;
  669. break;
  670. case $this->service::STORAGE_TENGXUN:
  671. // 腾讯云对象存储域名格式,支持appid作为域名的一部分
  672. $domainName = 'https://' . $name . ($appid ? '-' . $appid : '') . '.cos.' . $reagion . '.myqcloud.com';
  673. break;
  674. case $this->service::STORAGE_JINGDONG:
  675. // 京东云对象存储域名格式
  676. $domainName = 'https://' . $name . '.s3.' . $reagion . '.jdcloud-oss.com';
  677. break;
  678. case $this->service::STORAGE_HUAWEI:
  679. // 华为云对象存储域名格式
  680. $domainName = 'https://' . $name . '.obs.' . $reagion . '.myhuaweicloud.com';
  681. break;
  682. case $this->service::STORAGE_TIANYI:
  683. // 天翼云对象存储域名格式
  684. $domainName = 'https://' . $name . '.obs.' . $reagion . '.ctyun.cn';
  685. break;
  686. case $this->service::STORAGE_UCLOUD:
  687. // 优刻得对象存储域名格式
  688. $domainName = 'https://' . $name .'.' . $reagion .'.ufileos.com';
  689. break;
  690. }
  691. return $domainName;
  692. }
  693. /**
  694. * 根据上传类型和区域名称获取对应的区域主机地址。
  695. *
  696. * 本函数旨在通过上传服务获取特定类型上传所对应的区域主机列表,
  697. * 然后在这些列表中查找包含指定区域名称的主机地址,最后返回该地址。
  698. * 如果找不到匹配的主机地址,则返回空字符串。
  699. *
  700. * @param int $type 上传类型,用于确定上传服务和对应的区域列表。
  701. * @param string $reagion 指定的区域名称,用于在区域列表中查找匹配的主机地址。
  702. * @return string 匹配的主机地址,如果找不到则返回空字符串。
  703. */
  704. public function getReagionHost(int $type, string $reagion)
  705. {
  706. // 创建上传服务实例,参数$type用于指定上传类型。
  707. $upload = UploadService::create($type);
  708. // 通过上传服务实例获取区域列表。
  709. $reagionList = $upload->getRegion();
  710. // 遍历区域列表,查找包含指定区域名称的主机地址。
  711. foreach ($reagionList as $item) {
  712. // 如果当前项的值包含指定的区域名称,则返回该主机地址。
  713. if (strstr($item['value'], $reagion) !== false) {
  714. return $item['value'];
  715. }
  716. }
  717. // 如果遍历完毕没有找到匹配的主机地址,则返回空字符串。
  718. return '';
  719. }
  720. /**
  721. * 获取指定类型的域名列表
  722. *
  723. * 本函数用于查询并返回指定上传类型对应的域名列表。如果未指定类型,则根据系统配置的默认上传类型获取。
  724. * 主要用于支持多域名配置下的文件上传服务,使得可以灵活切换不同的域名进行文件访问。
  725. *
  726. * @param int|null $type 上传类型标识,null表示使用系统默认上传类型。
  727. * @return array 域名列表,如果没有找到任何域名则返回空数组。
  728. */
  729. public function domains(?int $type)
  730. {
  731. // 如果未指定上传类型,则使用系统配置的默认上传类型
  732. $type = $type ?: (systemConfig('upload_type') ?: 1);
  733. // 查询数据库,获取指定类型且未被删除的域名列表
  734. $domain = $this->dao->getSearch([])->where(['type' => $type,'is_del' => 0])->where('domain','>',0)->column('domain');
  735. // 返回查询结果,如果结果为空则返回空数组
  736. return $domain ?: [];
  737. }
  738. }