CommandToRequestTransformer.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <?php
  2. namespace Qcloud\Cos;
  3. use http\Exception\BadUrlException;
  4. use Psr\Http\Message\RequestInterface;
  5. use GuzzleHttp\Command\CommandInterface;
  6. use GuzzleHttp\Psr7\Uri;
  7. use InvalidArgumentException;
  8. use Psr\Http\Message\UriInterface;
  9. class CommandToRequestTransformer {
  10. private $config;
  11. private $operation;
  12. public function __construct( $config, $operation ) {
  13. $this->config = $config;
  14. $this->operation = $operation;
  15. }
  16. // format bucket style
  17. public function bucketStyleTransformer( CommandInterface $command, RequestInterface $request ) {
  18. $action = $command->getName();
  19. if ($action == 'ListBuckets') {
  20. $uri = "service.cos.myqcloud.com";
  21. if ($this->config['endpoint'] != null) {
  22. $uri = $this->config['endpoint'];
  23. }
  24. if ($this->config['domain'] != null) {
  25. $uri = $this->config['domain'];
  26. }
  27. if ($this->config['ip'] != null) {
  28. $uri = $this->config['ip'];
  29. if ($this->config['port'] != null) {
  30. $uri = $this->config['ip'] . ":" . $this->config['port'];
  31. }
  32. }
  33. return $request->withUri(new Uri($this->config['schema']."://". $uri. "/"));
  34. }
  35. $operation = $this->operation;
  36. $bucketname = $command['Bucket'];
  37. $appId = $this->config['appId'];
  38. if ( $appId != null && endWith( $bucketname, '-'.$appId ) == false ) {
  39. $bucketname = $bucketname.'-'.$appId;
  40. }
  41. $command['Bucket'] = $bucketname;
  42. $uri = $operation['uri'];
  43. // Hoststyle is used by default
  44. // Pathstyle
  45. if ( $this->config['pathStyle'] != true ) {
  46. if ( isset( $operation['parameters']['Bucket'] ) && $command->hasParam( 'Bucket' ) ) {
  47. $uri = str_replace( '{Bucket}', '', $uri );
  48. }
  49. if ( isset( $operation['parameters']['Key'] ) && $command->hasParam( 'Key' ) ) {
  50. $uri = str_replace( '{/Key*}', encodeKey( $command['Key'] ), $uri );
  51. }
  52. }
  53. if ($this->config['endpoint'] == null) {
  54. $this->config['endpoint'] = "myqcloud.com";
  55. }
  56. $domain_type = '.cos.';
  57. if ($action == 'PutBucketImageStyle' || $action == 'GetBucketImageStyle' || $action == 'DeleteBucketImageStyle'
  58. || $action == 'PutBucketGuetzli' || $action == 'GetBucketGuetzli' || $action == 'DeleteBucketGuetzli'
  59. || $action == 'BindCiService' || $action == 'GetCiService' || $action == 'UnBindCiService'
  60. || $action == 'GetHotLink' || $action == 'AddHotLink'
  61. || $action == 'OpenOriginProtect' || $action == 'GetOriginProtect' || $action == 'CloseOriginProtect'
  62. || $action == 'OpenImageSlim' || $action == 'GetImageSlim' || $action == 'CloseImageSlim' ) {
  63. $domain_type = '.pic.';
  64. }
  65. $origin_host = $this->config['allow_accelerate'] ?
  66. $bucketname . $domain_type . 'accelerate' . '.' . $this->config['endpoint'] :
  67. $bucketname . $domain_type . $this->config['region'] . '.' . $this->config['endpoint'];
  68. // domain
  69. if ( $this->config['domain'] != null ) {
  70. $origin_host = $this->config['domain'];
  71. }
  72. $host = $origin_host;
  73. if ( $this->config['ip'] != null ) {
  74. $host = $this->config['ip'];
  75. if ( $this->config['port'] != null ) {
  76. $host = $this->config['ip'] . ':' . $this->config['port'];
  77. }
  78. }
  79. $path = $this->config['schema'].'://'. $host . $uri;
  80. $uri = new Uri( $path );
  81. $query = $request->getUri()->getQuery();
  82. if ( $uri->getQuery() != $query && $uri->getQuery() != '' ) {
  83. $query = $uri->getQuery() . '&' . $request->getUri()->getQuery();
  84. }
  85. $uri = $uri->withQuery( $query );
  86. $request = $request->withUri( $uri );
  87. $request = $request->withHeader( 'Host', $origin_host );
  88. return $request;
  89. }
  90. // format upload body
  91. public function uploadBodyTransformer( CommandInterface $command, $request, $bodyParameter = 'Body', $sourceParameter = 'SourceFile' ) {
  92. $operation = $this->operation;
  93. if ( !isset( $operation['parameters']['Body'] ) ) {
  94. return $request;
  95. }
  96. $source = isset( $command[$sourceParameter] ) ? $command[$sourceParameter] : null;
  97. $body = isset( $command[$bodyParameter] ) ? $command[$bodyParameter] : null;
  98. // If a file path is passed in then get the file handle
  99. if ( is_string( $source ) && file_exists( $source ) ) {
  100. $body = fopen( $source, 'rb' );
  101. }
  102. // Prepare the body parameter and remove the source file parameter
  103. if ( null !== $body ) {
  104. return $request;
  105. } else {
  106. throw new InvalidArgumentException("You must specify a non-null value for the {$bodyParameter} or {$sourceParameter} parameters.");
  107. }
  108. }
  109. // update md5
  110. public function md5Transformer( CommandInterface $command, $request ) {
  111. $operation = $this->operation;
  112. if ( isset( $operation['data']['contentMd5'] ) ) {
  113. $request = $this->addMd5( $request );
  114. }
  115. if ( isset( $operation['parameters']['ContentMD5'] ) &&
  116. isset( $command['ContentMD5'] ) ) {
  117. $value = $command['ContentMD5'];
  118. if ( $value != false ) {
  119. $request = $this->addMd5( $request );
  120. }
  121. }
  122. return $request;
  123. }
  124. // add Query string
  125. public function queryStringTransformer( CommandInterface $command, $request ) {
  126. $operation = $this->operation;
  127. if ( isset( $command['Params'] ) ) {
  128. $params = $command['Params'];
  129. foreach ( $params as $key => $value ) {
  130. $uri = $request->getUri();
  131. $query = $uri->getQuery();
  132. $uri = $uri->withQuery($query. "&" . urlencode($key) . "=" . $value );
  133. $request = $request->withUri( $uri );
  134. }
  135. }
  136. return $request;
  137. }
  138. // add Header string
  139. public function headerTransformer( CommandInterface $command, $request ) {
  140. $operation = $this->operation;
  141. if ( isset( $command['Headers'] ) ) {
  142. $headers = $command['Headers'];
  143. foreach ( $headers as $key => $value ) {
  144. $request = $request->withHeader( $key, $value);
  145. }
  146. }
  147. return $request;
  148. }
  149. // add meta
  150. public function metadataTransformer( CommandInterface $command, $request ) {
  151. $operation = $this->operation;
  152. if ( isset( $command['Metadata'] ) ) {
  153. $meta = $command['Metadata'];
  154. foreach ( $meta as $key => $value ) {
  155. $request = $request->withHeader( 'x-cos-meta-' . $key, $value );
  156. }
  157. }
  158. $request = headersMap( $command, $request );
  159. return $request;
  160. }
  161. // count md5
  162. private function addMd5( $request ) {
  163. $body = $request->getBody();
  164. if ( $body && $body->getSize() > 0 ) {
  165. $md5 = base64_encode( md5( $body, true ) );
  166. return $request->withHeader( 'Content-MD5', $md5 );
  167. }
  168. return $request;
  169. }
  170. // inventoryId
  171. public function specialParamTransformer( CommandInterface $command, $request ) {
  172. $action = $command->getName();
  173. if ( $action == 'PutBucketInventory' ) {
  174. $id = $command['Id'];
  175. $uri = $request->getUri();
  176. $query = $uri->getQuery();
  177. $uri = $uri->withQuery( $query . '&Id='.$id );
  178. return $request->withUri( $uri );
  179. }
  180. return $request;
  181. }
  182. public function ciParamTransformer( CommandInterface $command, $request ) {
  183. $action = $command->getName();
  184. if ( $action == 'GetObject' ) {
  185. if(str_contains($uri = $request->getUri(), '%21') ) {
  186. $uri = new Uri( str_replace('%21', '!', $uri) );
  187. $request = $request->withUri( $uri );
  188. }
  189. if(isset($command['ImageHandleParam']) && $command['ImageHandleParam']){
  190. $uri = $request->getUri();
  191. $query = $uri->getQuery();
  192. if($query){
  193. $query .= "&" . urlencode($command['ImageHandleParam']);
  194. }else{
  195. $query .= urlencode($command['ImageHandleParam']);
  196. }
  197. $uri = $uri->withQuery($query);
  198. $request = $request->withUri( $uri );
  199. }
  200. }
  201. return $request;
  202. }
  203. public function cosDomain2CiTransformer(CommandInterface $command, $request) {
  204. $action = $command->getName();
  205. if(key_exists($action, array(
  206. 'DescribeMediaBuckets' => 1,
  207. 'DescribeDocProcessBuckets' =>1,
  208. 'GetPicBucketList' =>1,
  209. 'GetAiBucketList' =>1,
  210. ))) {
  211. $origin_host = "ci.{$this->config['region']}.myqcloud.com";
  212. $host = $origin_host;
  213. if ($this->config['ip'] != null) {
  214. $host = $this->config['ip'];
  215. if ($this->config['port'] != null) {
  216. $host = $this->config['ip'] . ":" . $this->config['port'];
  217. }
  218. }
  219. $path = $this->config['schema'].'://'. $host . $request->getUri()->getPath();
  220. $uri = new Uri( $path );
  221. $query = $request->getUri()->getQuery();
  222. $uri = $uri->withQuery( $query );
  223. $request = $request->withUri( $uri );
  224. $request = $request->withHeader( 'Host', $origin_host );
  225. return $request;
  226. }
  227. $ciActions = array(
  228. 'DetectText' => 1,
  229. 'CreateMediaTranscodeJobs' => 1,
  230. 'CreateMediaJobs' => 1,
  231. 'DescribeMediaJob' => 1,
  232. 'DescribeMediaJobs' => 1,
  233. 'CreateMediaSnapshotJobs' => 1,
  234. 'CreateMediaConcatJobs' => 1,
  235. 'DetectAudio' => 1,
  236. 'GetDetectAudioResult' => 1,
  237. 'GetDetectTextResult' => 1,
  238. 'DetectVideo' => 1,
  239. 'GetDetectVideoResult' => 1,
  240. 'DetectDocument' => 1,
  241. 'GetDetectDocumentResult' => 1,
  242. 'CreateDocProcessJobs' => 1,
  243. 'DescribeDocProcessQueues' => 1,
  244. 'DescribeDocProcessJob' => 1,
  245. 'GetDescribeDocProcessJobs' => 1,
  246. 'DetectImages' => 1,
  247. 'GetDetectImageResult' => 1,
  248. 'DetectVirus' => 1,
  249. 'GetDetectVirusResult' => 1,
  250. 'CreateMediaVoiceSeparateJobs' => 1,
  251. 'DescribeMediaVoiceSeparateJob' => 1,
  252. 'DetectWebpage' => 1,
  253. 'GetDetectWebpageResult' => 1,
  254. 'DescribeMediaQueues' => 1,
  255. 'UpdateMediaQueue' => 1,
  256. 'CreateMediaSmartCoverJobs' => 1,
  257. 'CreateMediaVideoProcessJobs' => 1,
  258. 'CreateMediaVideoMontageJobs' => 1,
  259. 'CreateMediaAnimationJobs' => 1,
  260. 'CreateMediaPicProcessJobs' => 1,
  261. 'CreateMediaSegmentJobs' => 1,
  262. 'CreateMediaVideoTagJobs' => 1,
  263. 'CreateMediaSuperResolutionJobs' => 1,
  264. 'CreateMediaSDRtoHDRJobs' => 1,
  265. 'CreateMediaDigitalWatermarkJobs' => 1,
  266. 'CreateMediaExtractDigitalWatermarkJobs' => 1,
  267. 'DetectLiveVideo' => 1,
  268. 'CancelLiveVideoAuditing' => 1,
  269. 'TriggerWorkflow' => 1,
  270. 'GetWorkflowInstances' => 1,
  271. 'GetWorkflowInstance' => 1,
  272. 'CreateMediaSnapshotTemplate' => 1,
  273. 'UpdateMediaSnapshotTemplate' => 1,
  274. 'CreateMediaTranscodeTemplate' => 1,
  275. 'UpdateMediaTranscodeTemplate' => 1,
  276. 'CreateMediaHighSpeedHdTemplate' => 1,
  277. 'UpdateMediaHighSpeedHdTemplate' => 1,
  278. 'CreateMediaAnimationTemplate' => 1,
  279. 'UpdateMediaAnimationTemplate' => 1,
  280. 'CreateMediaConcatTemplate' => 1,
  281. 'UpdateMediaConcatTemplate' => 1,
  282. 'CreateMediaVideoProcessTemplate' => 1,
  283. 'UpdateMediaVideoProcessTemplate' => 1,
  284. 'CreateMediaVideoMontageTemplate' => 1,
  285. 'UpdateMediaVideoMontageTemplate' => 1,
  286. 'CreateMediaVoiceSeparateTemplate' => 1,
  287. 'UpdateMediaVoiceSeparateTemplate' => 1,
  288. 'CreateMediaSuperResolutionTemplate' => 1,
  289. 'UpdateMediaSuperResolutionTemplate' => 1,
  290. 'CreateMediaPicProcessTemplate' => 1,
  291. 'UpdateMediaPicProcessTemplate' => 1,
  292. 'CreateMediaWatermarkTemplate' => 1,
  293. 'UpdateMediaWatermarkTemplate' => 1,
  294. 'DescribeMediaTemplates' => 1,
  295. 'DescribeWorkflow' => 1,
  296. 'DeleteWorkflow' => 1,
  297. 'CreateInventoryTriggerJob' => 1,
  298. 'DescribeInventoryTriggerJobs' => 1,
  299. 'DescribeInventoryTriggerJob' => 1,
  300. 'CancelInventoryTriggerJob' => 1,
  301. 'CreateMediaNoiseReductionJobs' => 1,
  302. 'ImageSearchOpen' => 1,
  303. 'UpdateDocProcessQueue' => 1,
  304. 'CreateMediaQualityEstimateJobs' => 1,
  305. 'CreateMediaStreamExtractJobs' => 1,
  306. 'OpenFileProcessService' => 1,
  307. 'GetFileProcessQueueList' => 1,
  308. 'UpdateFileProcessQueue' => 1,
  309. 'CreateFileHashCodeJobs' => 1,
  310. 'GetFileHashCodeResult' => 1,
  311. 'CreateFileUncompressJobs' => 1,
  312. 'GetFileUncompressResult' => 1,
  313. 'CreateFileCompressJobs' => 1,
  314. 'GetFileCompressResult' => 1,
  315. 'CreateM3U8PlayListJobs' => 1,
  316. 'GetPicQueueList' => 1,
  317. 'UpdatePicQueue' => 1,
  318. 'OpenAiService' => 1,
  319. 'GetAiQueueList' => 1,
  320. 'UpdateAiQueue' => 1,
  321. 'CreateMediaTranscodeProTemplate' => 1,
  322. 'UpdateMediaTranscodeProTemplate' => 1,
  323. 'CreateVoiceTtsTemplate' => 1,
  324. 'UpdateVoiceTtsTemplate' => 1,
  325. 'CreateMediaSmartCoverTemplate' => 1,
  326. 'UpdateMediaSmartCoverTemplate' => 1,
  327. 'CreateVoiceSpeechRecognitionTemplate' => 1,
  328. 'UpdateVoiceSpeechRecognitionTemplate' => 1,
  329. 'CreateVoiceTtsJobs' => 1,
  330. 'CreateAiTranslationJobs' => 1,
  331. 'CreateVoiceSpeechRecognitionJobs' => 1,
  332. 'CreateAiWordsGeneralizeJobs' => 1,
  333. 'CreateMediaVideoEnhanceJobs' => 1,
  334. 'CreateMediaVideoEnhanceTemplate' => 1,
  335. 'UpdateMediaVideoEnhanceTemplate' => 1,
  336. );
  337. if (key_exists($action, $ciActions)) {
  338. // 万象接口需要https,http方式报错
  339. if($this->config['schema'] !== 'https') {
  340. $e = new Exception\CosException('CI request schema must be "https", instead of "http"');
  341. $e->setExceptionCode('Invalid Argument');
  342. throw $e;
  343. }
  344. $bucketname = $command['Bucket'];
  345. $appId = $this->config['appId'];
  346. if ( $appId != null && endWith( $bucketname, '-'.$appId ) == false ) {
  347. $bucketname = $bucketname.'-'.$appId;
  348. }
  349. $command['Bucket'] = $bucketname;
  350. $domain_type = '.ci.';
  351. $origin_host = $bucketname . $domain_type . $this->config['region'] . '.' . $this->config['endpoint'];
  352. $host = $origin_host;
  353. if ( $this->config['ip'] != null ) {
  354. $host = $this->config['ip'];
  355. if ( $this->config['port'] != null ) {
  356. $host = $this->config['ip'] . ':' . $this->config['port'];
  357. }
  358. }
  359. $path = $this->config['schema'].'://'. $host . $request->getUri()->getPath();
  360. $uri = new Uri( $path );
  361. $query = $request->getUri()->getQuery();
  362. $uri = $uri->withQuery( $query );
  363. $request = $request->withUri( $uri );
  364. $request = $request->withHeader( 'Host', $origin_host );
  365. }
  366. return $request;
  367. }
  368. public function __destruct() {
  369. }
  370. }