CommandToRequestTransformer.php 15 KB

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