CommandToRequestTransformer.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. "&" . $key . "=" . $value );
  131. $request = $request->withUri( $uri );
  132. }
  133. }
  134. return $request;
  135. }
  136. // add meta
  137. public function metadataTransformer( CommandInterface $command, $request ) {
  138. $operation = $this->operation;
  139. if ( isset( $command['Metadata'] ) ) {
  140. $meta = $command['Metadata'];
  141. foreach ( $meta as $key => $value ) {
  142. $request = $request->withHeader( 'x-cos-meta-' . $key, $value );
  143. }
  144. }
  145. $request = headersMap( $command, $request );
  146. return $request;
  147. }
  148. // count md5
  149. private function addMd5( $request ) {
  150. $body = $request->getBody();
  151. if ( $body && $body->getSize() > 0 ) {
  152. $md5 = base64_encode( md5( $body, true ) );
  153. return $request->withHeader( 'Content-MD5', $md5 );
  154. }
  155. return $request;
  156. }
  157. // inventoryId
  158. public function specialParamTransformer( CommandInterface $command, $request ) {
  159. $action = $command->getName();
  160. if ( $action == 'PutBucketInventory' ) {
  161. $id = $command['Id'];
  162. $uri = $request->getUri();
  163. $query = $uri->getQuery();
  164. $uri = $uri->withQuery( $query . '&Id='.$id );
  165. return $request->withUri( $uri );
  166. }
  167. return $request;
  168. }
  169. public function ciParamTransformer( CommandInterface $command, $request ) {
  170. $action = $command->getName();
  171. if ( $action == 'GetObject' ) {
  172. if(str_contains($uri = $request->getUri(), '%21') ) {
  173. $uri = new Uri( str_replace('%21', '!', $uri) );
  174. $request = $request->withUri( $uri );
  175. }
  176. if(isset($command['ImageHandleParam']) && $command['ImageHandleParam']){
  177. $uri = $request->getUri();
  178. $query = $uri->getQuery();
  179. if($query){
  180. $query .= "&" . urlencode($command['ImageHandleParam']);
  181. }else{
  182. $query .= urlencode($command['ImageHandleParam']);
  183. }
  184. $uri = $uri->withQuery($query);
  185. $request = $request->withUri( $uri );
  186. }
  187. }
  188. return $request;
  189. }
  190. public function cosDomain2CiTransformer(CommandInterface $command, $request) {
  191. $action = $command->getName();
  192. $ciActions = array(
  193. 'DetectText' => 1,
  194. 'CreateMediaTranscodeJobs' => 1,
  195. 'CreateMediaSnapshotJobs' => 1,
  196. 'CreateMediaConcatJobs' => 1,
  197. 'DetectAudio' => 1,
  198. 'GetDetectAudioResult' => 1,
  199. 'GetDetectTextResult' => 1,
  200. 'DetectVideo' => 1,
  201. 'GetDetectVideoResult' => 1,
  202. 'DetectDocument' => 1,
  203. 'GetDetectDocumentResult' => 1,
  204. 'CreateDocProcessJobs' => 1,
  205. 'DescribeDocProcessQueues' => 1,
  206. 'DescribeDocProcessJob' => 1,
  207. 'GetDescribeDocProcessJobs' => 1,
  208. 'DetectImages' => 1,
  209. );
  210. if (key_exists($action, $ciActions)) {
  211. $bucketname = $command['Bucket'];
  212. $appId = $this->config['appId'];
  213. if ( $appId != null && endWith( $bucketname, '-'.$appId ) == False ) {
  214. $bucketname = $bucketname.'-'.$appId;
  215. }
  216. $command['Bucket'] = $bucketname;
  217. $domain_type = '.ci.';
  218. $origin_host = $bucketname . $domain_type . $this->config['region'] . '.' . $this->config['endpoint'];
  219. $host = $origin_host;
  220. if ( $this->config['ip'] != null ) {
  221. $host = $this->config['ip'];
  222. if ( $this->config['port'] != null ) {
  223. $host = $this->config['ip'] . ':' . $this->config['port'];
  224. }
  225. }
  226. $path = $this->config['schema'].'://'. $host . $request->getUri()->getPath();
  227. $uri = new Uri( $path );
  228. $query = $request->getUri()->getQuery();
  229. $uri = $uri->withQuery( $query );
  230. $request = $request->withUri( $uri );
  231. $request = $request->withHeader( 'Host', $origin_host );
  232. }
  233. return $request;
  234. }
  235. public function __destruct() {
  236. }
  237. }