CommandToRequestTransformer.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. $path = '';
  42. $http_method = $operation['httpMethod'];
  43. $uri = $operation['uri'];
  44. // Hoststyle is used by default
  45. // Pathstyle
  46. if ( $this->config['pathStyle'] != true ) {
  47. if ( isset( $operation['parameters']['Bucket'] ) && $command->hasParam( 'Bucket' ) ) {
  48. $uri = str_replace( '{Bucket}', '', $uri );
  49. }
  50. if ( isset( $operation['parameters']['Key'] ) && $command->hasParam( 'Key' ) ) {
  51. $uri = str_replace( '{/Key*}', encodeKey( $command['Key'] ), $uri );
  52. }
  53. }
  54. if ($this->config['endpoint'] == null) {
  55. $this->config['endpoint'] = "myqcloud.com";
  56. }
  57. $domain_type = '.cos.';
  58. if ($action == 'PutBucketImageStyle' || $action == 'GetBucketImageStyle' || $action == 'DeleteBucketImageStyle'
  59. || $action == 'PutBucketGuetzli' || $action == 'GetBucketGuetzli' || $action == 'DeleteBucketGuetzli') {
  60. $domain_type = '.pic.';
  61. }
  62. $origin_host = $this->config['allow_accelerate'] ?
  63. $bucketname . $domain_type . 'accelerate' . '.' . $this->config['endpoint'] :
  64. $bucketname . $domain_type . $this->config['region'] . '.' . $this->config['endpoint'];
  65. // domain
  66. if ( $this->config['domain'] != null ) {
  67. $origin_host = $this->config['domain'];
  68. }
  69. $host = $origin_host;
  70. if ( $this->config['ip'] != null ) {
  71. $host = $this->config['ip'];
  72. if ( $this->config['port'] != null ) {
  73. $host = $this->config['ip'] . ':' . $this->config['port'];
  74. }
  75. }
  76. $path = $this->config['schema'].'://'. $host . $uri;
  77. $uri = new Uri( $path );
  78. $query = $request->getUri()->getQuery();
  79. if ( $uri->getQuery() != $query && $uri->getQuery() != '' ) {
  80. $query = $uri->getQuery() . '&' . $request->getUri()->getQuery();
  81. }
  82. $uri = $uri->withQuery( $query );
  83. $request = $request->withUri( $uri );
  84. $request = $request->withHeader( 'Host', $origin_host );
  85. return $request;
  86. }
  87. // format upload body
  88. public function uploadBodyTransformer( CommandInterface $command, $request, $bodyParameter = 'Body', $sourceParameter = 'SourceFile' ) {
  89. $operation = $this->operation;
  90. if ( !isset( $operation['parameters']['Body'] ) ) {
  91. return $request;
  92. }
  93. $source = isset( $command[$sourceParameter] ) ? $command[$sourceParameter] : null;
  94. $body = isset( $command[$bodyParameter] ) ? $command[$bodyParameter] : null;
  95. // If a file path is passed in then get the file handle
  96. if ( is_string( $source ) && file_exists( $source ) ) {
  97. $body = fopen( $source, 'rb' );
  98. }
  99. // Prepare the body parameter and remove the source file parameter
  100. if ( null !== $body ) {
  101. return $request;
  102. } else {
  103. throw new InvalidArgumentException(
  104. "You must specify a non-null value for the {$bodyParameter} or {$sourceParameter} parameters." );
  105. }
  106. }
  107. // update md5
  108. public function md5Transformer( CommandInterface $command, $request ) {
  109. $operation = $this->operation;
  110. if ( isset( $operation['data']['contentMd5'] ) ) {
  111. $request = $this->addMd5( $request );
  112. }
  113. if ( isset( $operation['parameters']['ContentMD5'] ) &&
  114. isset( $command['ContentMD5'] ) ) {
  115. $value = $command['ContentMD5'];
  116. if ( $value != false ) {
  117. $request = $this->addMd5( $request );
  118. }
  119. }
  120. return $request;
  121. }
  122. // add Query string
  123. public function queryStringTransformer( CommandInterface $command, $request ) {
  124. $operation = $this->operation;
  125. if ( isset( $command['Params'] ) ) {
  126. $params = $command['Params'];
  127. foreach ( $params as $key => $value ) {
  128. $uri = $request->getUri();
  129. $query = $uri->getQuery();
  130. $uri = $uri->withQuery($query. "&" . urlencode($key) . "=" . $value );
  131. $request = $request->withUri( $uri );
  132. }
  133. }
  134. return $request;
  135. }
  136. // add Header string
  137. public function headerTransformer( CommandInterface $command, $request ) {
  138. $operation = $this->operation;
  139. if ( isset( $command['Headers'] ) ) {
  140. $headers = $command['Headers'];
  141. foreach ( $headers as $key => $value ) {
  142. $request = $request->withHeader( $key, $value);
  143. }
  144. }
  145. return $request;
  146. }
  147. // add meta
  148. public function metadataTransformer( CommandInterface $command, $request ) {
  149. $operation = $this->operation;
  150. if ( isset( $command['Metadata'] ) ) {
  151. $meta = $command['Metadata'];
  152. foreach ( $meta as $key => $value ) {
  153. $request = $request->withHeader( 'x-cos-meta-' . $key, $value );
  154. }
  155. }
  156. $request = headersMap( $command, $request );
  157. return $request;
  158. }
  159. // count md5
  160. private function addMd5( $request ) {
  161. $body = $request->getBody();
  162. if ( $body && $body->getSize() > 0 ) {
  163. $md5 = base64_encode( md5( $body, true ) );
  164. return $request->withHeader( 'Content-MD5', $md5 );
  165. }
  166. return $request;
  167. }
  168. // inventoryId
  169. public function specialParamTransformer( CommandInterface $command, $request ) {
  170. $action = $command->getName();
  171. if ( $action == 'PutBucketInventory' ) {
  172. $id = $command['Id'];
  173. $uri = $request->getUri();
  174. $query = $uri->getQuery();
  175. $uri = $uri->withQuery( $query . '&Id='.$id );
  176. return $request->withUri( $uri );
  177. }
  178. return $request;
  179. }
  180. public function ciParamTransformer( CommandInterface $command, $request ) {
  181. $action = $command->getName();
  182. if ( $action == 'GetObject' ) {
  183. if(str_contains($uri = $request->getUri(), '%21') ) {
  184. $uri = new Uri( str_replace('%21', '!', $uri) );
  185. $request = $request->withUri( $uri );
  186. }
  187. if(isset($command['ImageHandleParam']) && $command['ImageHandleParam']){
  188. $uri = $request->getUri();
  189. $query = $uri->getQuery();
  190. if($query){
  191. $query .= "&" . urlencode($command['ImageHandleParam']);
  192. }else{
  193. $query .= urlencode($command['ImageHandleParam']);
  194. }
  195. $uri = $uri->withQuery($query);
  196. $request = $request->withUri( $uri );
  197. }
  198. }
  199. return $request;
  200. }
  201. public function cosDomain2CiTransformer(CommandInterface $command, $request) {
  202. $action = $command->getName();
  203. if(key_exists($action, array( 'DescribeMediaBuckets' => 1, ))) {
  204. $origin_host = "ci.{$this->config['region']}.myqcloud.com";
  205. $host = $origin_host;
  206. if ($this->config['ip'] != null) {
  207. $host = $this->config['ip'];
  208. if ($this->config['port'] != null) {
  209. $host = $this->config['ip'] . ":" . $this->config['port'];
  210. }
  211. }
  212. $path = $this->config['schema'].'://'. $host . $request->getUri()->getPath();
  213. $uri = new Uri( $path );
  214. $query = $request->getUri()->getQuery();
  215. $uri = $uri->withQuery( $query );
  216. $request = $request->withUri( $uri );
  217. $request = $request->withHeader( 'Host', $origin_host );
  218. return $request;
  219. }
  220. $ciActions = array(
  221. 'DetectText' => 1,
  222. 'CreateMediaTranscodeJobs' => 1,
  223. 'CreateMediaJobs' => 1,
  224. 'DescribeMediaJob' => 1,
  225. 'DescribeMediaJobs' => 1,
  226. 'CreateMediaSnapshotJobs' => 1,
  227. 'CreateMediaConcatJobs' => 1,
  228. 'DetectAudio' => 1,
  229. 'GetDetectAudioResult' => 1,
  230. 'GetDetectTextResult' => 1,
  231. 'DetectVideo' => 1,
  232. 'GetDetectVideoResult' => 1,
  233. 'DetectDocument' => 1,
  234. 'GetDetectDocumentResult' => 1,
  235. 'CreateDocProcessJobs' => 1,
  236. 'DescribeDocProcessQueues' => 1,
  237. 'DescribeDocProcessJob' => 1,
  238. 'GetDescribeDocProcessJobs' => 1,
  239. 'DetectImages' => 1,
  240. 'GetDetectImageResult' => 1,
  241. 'DetectVirus' => 1,
  242. 'GetDetectVirusResult' => 1,
  243. 'CreateMediaVoiceSeparateJobs' => 1,
  244. 'DescribeMediaVoiceSeparateJob' => 1,
  245. 'DetectWebpage' => 1,
  246. 'GetDetectWebpageResult' => 1,
  247. 'DescribeMediaQueues' => 1,
  248. 'UpdateMediaQueue' => 1,
  249. 'CreateMediaSmartCoverJobs' => 1,
  250. 'CreateMediaVideoProcessJobs' => 1,
  251. 'CreateMediaVideoMontageJobs' => 1,
  252. 'CreateMediaAnimationJobs' => 1,
  253. 'CreateMediaPicProcessJobs' => 1,
  254. 'CreateMediaSegmentJobs' => 1,
  255. 'CreateMediaVideoTagJobs' => 1,
  256. 'CreateMediaSuperResolutionJobs' => 1,
  257. 'CreateMediaSDRtoHDRJobs' => 1,
  258. 'CreateMediaDigitalWatermarkJobs' => 1,
  259. 'CreateMediaExtractDigitalWatermarkJobs' => 1,
  260. 'DetectLiveVideo' => 1,
  261. 'CancelLiveVideoAuditing' => 1,
  262. );
  263. if (key_exists($action, $ciActions)) {
  264. $bucketname = $command['Bucket'];
  265. $appId = $this->config['appId'];
  266. if ( $appId != null && endWith( $bucketname, '-'.$appId ) == False ) {
  267. $bucketname = $bucketname.'-'.$appId;
  268. }
  269. $command['Bucket'] = $bucketname;
  270. $domain_type = '.ci.';
  271. $origin_host = $bucketname . $domain_type . $this->config['region'] . '.' . $this->config['endpoint'];
  272. $host = $origin_host;
  273. if ( $this->config['ip'] != null ) {
  274. $host = $this->config['ip'];
  275. if ( $this->config['port'] != null ) {
  276. $host = $this->config['ip'] . ':' . $this->config['port'];
  277. }
  278. }
  279. $path = $this->config['schema'].'://'. $host . $request->getUri()->getPath();
  280. $uri = new Uri( $path );
  281. $query = $request->getUri()->getQuery();
  282. $uri = $uri->withQuery( $query );
  283. $request = $request->withUri( $uri );
  284. $request = $request->withHeader( 'Host', $origin_host );
  285. }
  286. return $request;
  287. }
  288. public function __destruct() {
  289. }
  290. }