Jdoss.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. <?php
  2. namespace crmeb\services\upload\storage;
  3. use Aws\Acm\Exception\AcmException;
  4. use crmeb\exceptions\AdminException;
  5. use crmeb\exceptions\UploadException;
  6. use crmeb\basic\BaseUpload;
  7. use GuzzleHttp\Psr7\Utils;
  8. use think\exception\ValidateException;
  9. /**
  10. * 京东云COS文件上传
  11. * Class Jdoss
  12. * @package crmeb\services\upload\storage
  13. */
  14. class Jdoss extends BaseUpload
  15. {
  16. /**
  17. * 应用id
  18. * @var string
  19. */
  20. protected $appid;
  21. /**
  22. * accessKey
  23. * @var mixed
  24. */
  25. protected $accessKey;
  26. /**
  27. * secretKey
  28. * @var mixed
  29. */
  30. protected $secretKey;
  31. /**
  32. * 句柄
  33. * @var S3Client
  34. */
  35. protected $handle;
  36. /**
  37. * 空间域名 Domain
  38. * @var mixed
  39. */
  40. protected $uploadUrl;
  41. /**
  42. * 存储空间名称 公开空间
  43. * @var mixed
  44. */
  45. protected $storageName;
  46. /**
  47. * COS使用 所属地域
  48. * @var mixed|null
  49. */
  50. protected $storageRegion;
  51. /**
  52. * @var string
  53. */
  54. protected $cdn;
  55. /**
  56. * 水印位置
  57. * @var string[]
  58. */
  59. protected $position = [
  60. '1' => '1',//:左上
  61. '2' => '2',//:中上
  62. '3' => '3',//:右上
  63. '4' => '4',//:左中
  64. '5' => '5',//:中部
  65. '6' => '6',//:右中
  66. '7' => '7',//:左下
  67. '8' => '8',//:中下
  68. '9' => '9',//:右下
  69. ];
  70. /**
  71. * 初始化
  72. * @param array $config
  73. * @return mixed|void
  74. */
  75. public function initialize(array $config)
  76. {
  77. parent::initialize($config);
  78. $this->accessKey = $config['accessKey'] ?? null;
  79. $this->secretKey = $config['secretKey'] ?? null;
  80. $this->uploadUrl = $this->checkUploadUrl($config['uploadUrl'] ?? '') ?: sys_config('site_url');
  81. $this->storageName = $config['storageName'] ?? null;
  82. $this->storageRegion = $config['storageRegion'] ?? null;
  83. $this->waterConfig['watermark_text_font'] = 'simfang仿宋.ttf';
  84. }
  85. /**
  86. * @return \crmeb\services\upload\extend\jdoss\Client
  87. */
  88. protected function app()
  89. {
  90. if (!$this->accessKey || !$this->secretKey) {
  91. throw new UploadException('Please configure accessKey and secretKey');
  92. }
  93. $this->handle = new \crmeb\services\upload\extend\jdoss\Client([
  94. 'accessKey' => $this->accessKey,
  95. 'secretKey' => $this->secretKey,
  96. ]);
  97. return $this->handle;
  98. }
  99. /**
  100. * 上传图片
  101. * @param string $file
  102. * @return array|bool|mixed
  103. */
  104. public function move(string $file = 'file')
  105. {
  106. $fileHandle = app()->request->file($file);
  107. if (!$fileHandle) {
  108. return $this->setError('上传的文件不存在');
  109. }
  110. if ($this->validate) {
  111. try {
  112. $error = [
  113. $file . '.filesize' => 'Upload filesize error',
  114. $file . '.fileExt' => 'Upload fileExt error',
  115. $file . '.fileMime' => 'Upload fileMine error'
  116. ];
  117. validate([$file => $this->validate], $error)->check([$file => $fileHandle]);
  118. } catch (ValidateException $e) {
  119. return $this->setError($e->getMessage());
  120. }
  121. }
  122. $key = $this->saveFileName($fileHandle->getRealPath(), $fileHandle->getOriginalExtension());
  123. $key = $this->getUploadPath($key);
  124. $body = fopen($fileHandle->getRealPath(), 'rb');
  125. $body = (string)Utils::streamFor($body);
  126. try {
  127. $uploadInfo = $this->app()->putObject($this->storageName, $this->storageRegion, $key, [
  128. 'body' => $body
  129. ]);
  130. if (!$uploadInfo) {
  131. return $this->setError('Upload failure');
  132. }
  133. $this->fileInfo->uploadInfo = $uploadInfo;
  134. $this->fileInfo->realName = $fileHandle->getOriginalName();
  135. $this->fileInfo->filePath = ($this->cdn ?: $this->uploadUrl) . '/' . $key;
  136. $this->fileInfo->fileName = $key;
  137. $this->fileInfo->filePathWater = $this->water($this->fileInfo->filePath);
  138. $this->authThumb && $this->thumb($this->fileInfo->filePath);
  139. return $this->fileInfo;
  140. } catch (\Throwable $e) {
  141. return $this->setError($e->getMessage());
  142. }
  143. }
  144. /**
  145. * 文件流上传
  146. * @param string $fileContent
  147. * @param string|null $key
  148. * @return bool|mixed
  149. */
  150. public function stream(string $fileContent, string $key = null)
  151. {
  152. try {
  153. if (!$key) {
  154. $key = $this->saveFileName();
  155. }
  156. $key = $this->getUploadPath($key);
  157. $fileContent = (string)Utils::streamFor($fileContent);
  158. $uploadInfo = $this->app()->putObject($this->storageName, $this->storageRegion, $key, [
  159. 'body' => $fileContent
  160. ]);
  161. if (!$uploadInfo) {
  162. return $this->setError('Upload failure');
  163. }
  164. $this->fileInfo->uploadInfo = $uploadInfo;
  165. $this->fileInfo->realName = $key;
  166. $this->fileInfo->filePath = ($this->cdn ?: $this->uploadUrl) . '/' . $key;
  167. $this->fileInfo->fileName = $key;
  168. $this->fileInfo->filePathWater = $this->water($this->fileInfo->filePath);
  169. $this->thumb($this->fileInfo->filePath);
  170. return $this->fileInfo;
  171. } catch (\Throwable $e) {
  172. return $this->setError($e->getMessage());
  173. }
  174. }
  175. /**
  176. * 删除
  177. * @param string $key
  178. * @return array|bool|\crmeb\services\upload\extend\cos\SimpleXMLElement|mixed
  179. */
  180. public function delete(string $key)
  181. {
  182. try {
  183. return $this->app()->deleteObject($this->storageName, $this->storageRegion, $key);
  184. } catch (\Throwable $e) {
  185. return $this->setError($e->getMessage());
  186. }
  187. }
  188. /**
  189. * @param string $region
  190. * @param bool $line
  191. * @param bool $shared
  192. * @return array|\Aws\Result
  193. */
  194. public function listbuckets(string $region = 'cn-north-1', bool $line = false, bool $shared = false)
  195. {
  196. try {
  197. $res = $this->app()->listBuckets();
  198. return $res['Buckets']['Bucket'] ?? [];
  199. } catch (\Throwable $e) {
  200. return [];
  201. }
  202. }
  203. /**
  204. * 创建桶
  205. * @param string $name
  206. * @param string $region
  207. * @param string $acl
  208. * @return array|\AsyncAws\S3\Result\CreateBucketOutput|bool|\crmeb\services\upload\extend\cos\SimpleXMLElement
  209. */
  210. public function createBucket(string $name, string $region = '', string $acl = 'public-read')
  211. {
  212. $regionData = $this->getRegion();
  213. $regionData = array_column($regionData, 'value');
  214. if (!in_array($region, $regionData)) {
  215. return $this->setError('COS:无效的区域!');
  216. }
  217. $this->storageRegion = $region;
  218. $app = $this->app();
  219. //检测桶
  220. try {
  221. $app->headBucket($name, $region);
  222. } catch (\Throwable $e) {
  223. //桶不存在返回404
  224. if (strstr('404', $e->getMessage())) {
  225. return $this->setError('JDOSS:' . $e->getMessage());
  226. }
  227. }
  228. //创建桶
  229. try {
  230. $res = $app->createBucket($name, $region, $acl);
  231. } catch (\Throwable $e) {
  232. return $this->setError('JDOSS:' . $e->getMessage());
  233. }
  234. return $res;
  235. }
  236. /**
  237. * 删除桶
  238. * @param string $name
  239. * @param string $region
  240. * @return bool
  241. */
  242. public function deleteBucket(string $name, string $region = '')
  243. {
  244. try {
  245. $this->storageRegion = $region;
  246. $this->app()->deleteBucket($name, $region);
  247. return true;
  248. } catch (AcmException $e) {
  249. return $this->setError($e->getMessage());
  250. }
  251. }
  252. public function getRegion()
  253. {
  254. return [
  255. [
  256. 'value' => 'cn-north-1',
  257. 'label' => '华北-北京'
  258. ],
  259. [
  260. 'value' => 'cn-east-1',
  261. 'label' => '华东-宿迁'
  262. ],
  263. [
  264. 'value' => 'cn-east-2',
  265. 'label' => '华东-上海'
  266. ],
  267. [
  268. 'value' => 'cn-south-1',
  269. 'label' => '华南-广州'
  270. ]
  271. ];
  272. }
  273. public function getDomian(string $name, string $region = null)
  274. {
  275. try {
  276. $this->storageRegion = $region;
  277. $res = $this->app()->getBucketPolicy([
  278. 'Bucket' => $name
  279. ]);
  280. return $res['DomainName'] ?? [];
  281. } catch (\Throwable $e) {
  282. return $this->setError($e->getMessage());
  283. }
  284. }
  285. public function bindDomian(string $name, string $domain, string $region = null)
  286. {
  287. try {
  288. $this->storageRegion = $region;
  289. $this->app()->putBucketWebsite([
  290. 'Bucket' => $name,
  291. 'WebsiteConfiguration' => [
  292. 'RedirectAllRequestsTo' => [
  293. 'HostName' => $domain,
  294. 'Protocol' => 'http'
  295. ]
  296. ]
  297. ]);
  298. return true;
  299. } catch (\Throwable $e) {
  300. return $this->setError($e->getMessage());
  301. }
  302. }
  303. public function setBucketCors(string $name, string $region)
  304. {
  305. $this->storageRegion = $region;
  306. try {
  307. $this->app()->putBucketCors($name, $region,
  308. ['CORSConfiguration' => [ // REQUIRED
  309. 'CORSRules' => [ // REQUIRED
  310. [
  311. 'AllowedHeaders' => ['*'],
  312. 'AllowedMethods' => ['POST', 'GET', 'PUT', 'DELETE', 'HEAD'], // REQUIRED
  313. 'AllowedOrigins' => ['*'], // REQUIRED
  314. 'ExposeHeaders' => ['Etag'],
  315. 'MaxAgeSeconds' => 0
  316. ],
  317. ],
  318. ]
  319. ]);
  320. return true;
  321. } catch (\Throwable $e) {
  322. return $this->setError($e->getMessage());
  323. }
  324. }
  325. /**
  326. * 获取OSS上传密钥
  327. * @return mixed|void
  328. */
  329. public function getTempKeys($key = '', $path = '', $contentType = '', $expires = '+10 minutes')
  330. {
  331. try {
  332. $app = $this->app();
  333. $host = $app->getRequestUrl() . '/' . $this->storageName;
  334. $url = 'https://' . $host . '/' . $key;
  335. $params = $this->getTempKeysParam($host, $url);
  336. $param = [];
  337. foreach ($params as $k => $value) {
  338. $param[] = $k . '=' . $value;
  339. }
  340. return [
  341. 'upload_url' => $url . '?' . implode('&', $param),
  342. 'type' => 'JDOSS',
  343. 'url' => $this->uploadUrl . '/' . $key
  344. ];
  345. } catch (\Throwable $e) {
  346. return $this->setError($e->getMessage());
  347. }
  348. }
  349. /**
  350. * 获取上传需要参数
  351. * @param string $host
  352. * @return array
  353. */
  354. public function getTempKeysParam(string $host, string $url)
  355. {
  356. $amzDate = gmdate('Ymd\THis\Z');
  357. $sdt = substr($amzDate, 0, 8);
  358. $credentialScope = $sdt . '/' . $this->storageRegion . '/' . 's3' . '/aws4_request';
  359. $clientHeader = [
  360. 'Host' => $host,
  361. 'X-Amz-Content-Sha256' => 'UNSIGNED-PAYLOAD',
  362. 'X-Amz-Date' => $amzDate
  363. ];
  364. $param = [
  365. 'X-Amz-Content-Sha256' => 'UNSIGNED-PAYLOAD',
  366. 'X-Amz-Algorithm' => 'AWS4-HMAC-SHA256',
  367. 'X-Amz-Credential' => $this->accessKey . '/' . $credentialScope,
  368. 'X-Amz-Date' => $amzDate,
  369. 'X-Amz-SignedHeaders' => 'host;X-Amz-Content-Sha25;X-Amz-Date',
  370. 'X-Amz-Expires' => 600
  371. ];
  372. [$canonicalRequest, $signedHeaders] = $this->app()->createCanonicalRequest($url, 'GET', $clientHeader, ['query' => $param]);
  373. $stringToSign = "AWS4-HMAC-SHA256\n" . $amzDate . "\n" . $credentialScope . "\n" . hash('sha256', $canonicalRequest);
  374. $key = $this->getSigningKey($sdt, $this->storageRegion, 's3', $this->accessKey);
  375. $signature = hash_hmac('sha256', $stringToSign, $key);
  376. $param['X-Amz-Signature'] = $signature;
  377. return $param;
  378. }
  379. /**
  380. * 生成key
  381. * @param $shortDate
  382. * @param $region
  383. * @param $service
  384. * @param $secretKey
  385. * @return false|string
  386. */
  387. public function getSigningKey($shortDate, $region, $service, $secretKey)
  388. {
  389. $kSecret = 'AWS4' . $secretKey;
  390. $kDate = hash_hmac('sha256', $shortDate, $kSecret, true);
  391. $kRegion = hash_hmac('sha256', $region, $kDate, true);
  392. $kService = hash_hmac('sha256', $service, $kRegion, true);
  393. return hash_hmac('sha256', 'aws4_request', $kService, true);
  394. }
  395. /**
  396. * 缩略图
  397. * @param string $filePath
  398. * @param string $fileName
  399. * @param string $type
  400. * @return array|mixed
  401. */
  402. public function thumb(string $filePath = '', string $fileName = '', string $type = 'all')
  403. {
  404. $filePath = $this->getFilePath($filePath);
  405. $data = ['big' => $filePath, 'mid' => $filePath, 'small' => $filePath];
  406. $this->fileInfo->filePathBig = $this->fileInfo->filePathMid = $this->fileInfo->filePathSmall = $this->fileInfo->filePathWater = $filePath;
  407. if ($filePath) {
  408. $config = $this->thumbConfig;
  409. foreach ($this->thumb as $v) {
  410. if ($type == 'all' || $type == $v) {
  411. $height = 'thumb_' . $v . '_height';
  412. $width = 'thumb_' . $v . '_width';
  413. $key = 'filePath' . ucfirst($v);
  414. //x-oss-process=img/s/200/300
  415. if (sys_config('image_thumbnail_status', 1) && isset($config[$height]) && isset($config[$width]) && $config[$height] && $config[$width]) {
  416. $this->fileInfo->$key = $filePath . '?x-oss-process=img/s' . $config[$width] . '/' . $config[$height];
  417. $this->fileInfo->$key = $this->water($this->fileInfo->$key);
  418. $data[$v] = $this->fileInfo->$key;
  419. } else {
  420. $this->fileInfo->$key = $this->water($this->fileInfo->$key);
  421. $data[$v] = $this->fileInfo->$key;
  422. }
  423. }
  424. }
  425. }
  426. return $data;
  427. }
  428. /**
  429. * 水印
  430. * @param string $filePath
  431. * @return mixed|string
  432. */
  433. public function water(string $filePath = '')
  434. {
  435. $filePath = $this->getFilePath($filePath);
  436. $waterConfig = $this->waterConfig;
  437. $waterPath = $filePath;
  438. if ($waterConfig['image_watermark_status'] && $filePath) {
  439. if (strpos($filePath, '?x-oss-process') === false) {
  440. $filePath .= '?x-oss-process=img';
  441. }
  442. switch ($waterConfig['watermark_type']) {
  443. case 1://图片
  444. if (!$waterConfig['watermark_image']) {
  445. throw new AdminException('请先配置水印图片');
  446. }
  447. //x-oss-process=img/wmi/wk/ZG93bmxvYWRzOmxvZ28ucG5n/ws/100
  448. $waterPath = $filePath .= '/wmi/wk/' . base64_encode($waterConfig['watermark_image']) . '/wd/' . $waterConfig['watermark_opacity'] . '/wp/' . ($this->position[$waterConfig['watermark_position']] ?? '1') . '/wdx/' . $waterConfig['watermark_x'] . '/wdy/' . $waterConfig['watermark_y'];
  449. break;
  450. case 2://文字
  451. if (!$waterConfig['watermark_text']) {
  452. throw new AdminException('请先配置水印文字');
  453. }
  454. //?x-oss-process=img/wmt/wt/5Lqs5Lic5LqR
  455. $waterConfig['watermark_text_color'] = str_replace('#', '', $waterConfig['watermark_text_color']);
  456. $waterPath = $filePath .= '/wmt/wt/' . base64_encode($waterConfig['watermark_text']) . '/wc/' . $waterConfig['watermark_text_color'] . '/ws/' . $waterConfig['watermark_text_size'] . '/wp/' . ($this->position[$waterConfig['watermark_position']] ?? 'nw') . '/wdx/' . $waterConfig['watermark_x'] . '/wdy/' . $waterConfig['watermark_y'] . '/wr/' . $waterConfig['watermark_text_angle'];
  457. break;
  458. }
  459. }
  460. return $waterPath;
  461. }
  462. /**
  463. * 获取视频封面图
  464. * @param string $filePath
  465. * @param string $type
  466. * @param int $time
  467. * @return array
  468. */
  469. public function videoCoverImage(string $filePath = '', string $type = 'all', int $time = 1)
  470. {
  471. $data = ['big' => $filePath, 'mid' => $filePath, 'small' => $filePath];
  472. $this->fileInfo->filePathBig = $this->fileInfo->filePathMid = $this->fileInfo->filePathSmall = $this->fileInfo->filePathWater = $filePath;
  473. if ($filePath) {
  474. //?x-oss-process=video/snapshot,t_7000,f_jpg,w_800,h_600,m_fast
  475. foreach ($this->thumb as $v) {
  476. if ($type == 'all' || $type == $v) {
  477. $height = 600;
  478. $width = 400;
  479. $key = 'filePath' . ucfirst($v);
  480. $this->fileInfo->$key = $filePath . '?x-oss-process=video/snapshot,t_' . ($time * 1000) . ',f_jpg,w_' . $width . ',h_' . $height . ',m_fast';
  481. $data[$v] = $this->fileInfo->$key;
  482. }
  483. }
  484. }
  485. return $data;
  486. }
  487. }