|
2 | 2 |
|
3 | 3 | namespace Qcloud\Cos; |
4 | 4 |
|
5 | | -use Guzzle\Service\Description\Parameter; |
6 | | -use Guzzle\Service\Description\ServiceDescription; |
7 | | -use GuzzleHttp\HandlerStack; |
8 | 5 | use Psr\Http\Message\RequestInterface; |
9 | | -use Psr\Http\Message\ResponseInterface; |
10 | | -use Qcloud\Cos\Signature; |
11 | | -use GuzzleHttp\Command\Guzzle\Description; |
12 | | -use GuzzleHttp\Command\Guzzle\GuzzleClient; |
13 | 6 | use GuzzleHttp\Command\CommandInterface; |
14 | | -use GuzzleHttp\Exception\RequestException; |
15 | | -use GuzzleHttp\Middleware; |
16 | | -use GuzzleHttp\Psr7; |
17 | 7 | use GuzzleHttp\Psr7\Uri; |
18 | 8 | use InvalidArgumentException; |
19 | 9 |
|
20 | | - |
21 | 10 | class CommandToRequestTransformer { |
22 | 11 | private $config; |
23 | 12 | private $operation; |
24 | 13 |
|
25 | | - public function __construct($config ,$operation) { |
| 14 | + public function __construct( $config, $operation ) { |
26 | 15 | $this->config = $config; |
27 | 16 | $this->operation = $operation; |
28 | 17 | } |
29 | 18 |
|
30 | 19 | // format bucket style |
31 | | - public function bucketStyleTransformer(CommandInterface $command, RequestInterface $request) { |
| 20 | + |
| 21 | + public function bucketStyleTransformer( CommandInterface $command, RequestInterface $request ) { |
32 | 22 | $action = $command->getName(); |
33 | 23 | if ($action == 'ListBuckets') { |
34 | | - return $request->withUri(new Uri($this->config['schema']."://service.cos.myqcloud.com/")); |
| 24 | + $uri = "service.cos.myqcloud.com"; |
| 25 | + |
| 26 | + if ($this->config['endpoint'] != null) { |
| 27 | + $uri = $this->config['endpoint']; |
| 28 | + } |
| 29 | + if ($this->config['domain'] != null) { |
| 30 | + $uri = $this->config['domain']; |
| 31 | + } |
| 32 | + if ($this->config['ip'] != null) { |
| 33 | + $uri = $this->config['ip']; |
| 34 | + if ($this->config['port'] != null) { |
| 35 | + $uri = $this->config['ip'] . ":" . $this->config['port']; |
| 36 | + } |
| 37 | + } |
| 38 | + return $request->withUri(new Uri($this->config['schema']."://". $uri. "/")); |
35 | 39 | } |
36 | 40 | $operation = $this->operation; |
37 | 41 | $bucketname = $command['Bucket']; |
38 | 42 |
|
39 | 43 | $appId = $this->config['appId']; |
40 | | - if ($appId != null && endWith($bucketname, '-'.$appId) == False) |
41 | | - { |
| 44 | + if ( $appId != null && endWith( $bucketname, '-'.$appId ) == False ) { |
42 | 45 | $bucketname = $bucketname.'-'.$appId; |
43 | 46 | } |
44 | 47 | $command['Bucket'] = $bucketname; |
45 | | - $path = ''; |
| 48 | + $path = ''; |
| 49 | + |
46 | 50 | $http_method = $operation['httpMethod']; |
47 | 51 | $uri = $operation['uri']; |
48 | | - |
| 52 | + |
49 | 53 | // Hoststyle is used by default |
50 | 54 | // Pathstyle |
51 | | - if ($this->config['pathStyle'] != true) { |
52 | | - if (isset($operation['parameters']['Bucket']) && $command->hasParam('Bucket')) { |
53 | | - $uri = str_replace("{Bucket}", '', $uri); |
54 | | - } |
55 | | - if (isset($operation['parameters']['Key']) && $command->hasParam('Key')) { |
56 | | - $uri = str_replace("{/Key*}", encodeKey($command['Key']), $uri); |
| 55 | + if ( $this->config['pathStyle'] != true ) { |
| 56 | + if ( isset( $operation['parameters']['Bucket'] ) && $command->hasParam( 'Bucket' ) ) { |
| 57 | + $uri = str_replace( '{Bucket}', '', $uri ); |
| 58 | + } |
| 59 | + |
| 60 | + if ( isset( $operation['parameters']['Key'] ) && $command->hasParam( 'Key' ) ) { |
| 61 | + $uri = str_replace( '{/Key*}', encodeKey( $command['Key'] ), $uri ); |
57 | 62 | } |
58 | 63 | } |
| 64 | + if ($this->config['endpoint'] == null) { |
| 65 | + $this->config['endpoint'] = "myqcloud.com"; |
| 66 | + } |
59 | 67 | $origin_host = $bucketname. '.cos.' . $this->config['region'] . '.' . $this->config['endpoint']; |
60 | 68 | // domain |
61 | | - if ($this->config['domain'] != null) { |
| 69 | + if ( $this->config['domain'] != null ) { |
62 | 70 | $origin_host = $this->config['domain']; |
63 | 71 | } |
64 | 72 | $host = $origin_host; |
65 | | - if ($this->config['ip'] != null) { |
| 73 | + if ( $this->config['ip'] != null ) { |
66 | 74 | $host = $this->config['ip']; |
67 | | - if ($this->config['port'] != null) { |
68 | | - $host = $this->config['ip'] . ":" . $this->config['port']; |
| 75 | + if ( $this->config['port'] != null ) { |
| 76 | + $host = $this->config['ip'] . ':' . $this->config['port']; |
69 | 77 | } |
70 | 78 | } |
71 | 79 |
|
72 | | - |
73 | 80 | $path = $this->config['schema'].'://'. $host . $uri; |
74 | | - $uri = new Uri($path); |
| 81 | + $uri = new Uri( $path ); |
75 | 82 | $query = $request->getUri()->getQuery(); |
76 | | - if ($uri->getQuery() != $query && $uri->getQuery() != "") { |
77 | | - $query = $uri->getQuery() . "&" . $request->getUri()->getQuery(); |
| 83 | + if ( $uri->getQuery() != $query && $uri->getQuery() != '' ) { |
| 84 | + $query = $uri->getQuery() . '&' . $request->getUri()->getQuery(); |
78 | 85 | } |
79 | | - $uri = $uri->withQuery($query); |
80 | | - $request = $request->withUri($uri); |
81 | | - $request = $request->withHeader('Host', $origin_host); |
| 86 | + $uri = $uri->withQuery( $query ); |
| 87 | + $request = $request->withUri( $uri ); |
| 88 | + $request = $request->withHeader( 'Host', $origin_host ); |
82 | 89 | return $request; |
83 | 90 | } |
84 | 91 |
|
85 | 92 | // format upload body |
86 | | - public function uploadBodyTransformer(CommandInterface $command, $request, $bodyParameter = 'Body', $sourceParameter = 'SourceFile') { |
87 | | - |
| 93 | + |
| 94 | + public function uploadBodyTransformer( CommandInterface $command, $request, $bodyParameter = 'Body', $sourceParameter = 'SourceFile' ) { |
| 95 | + |
88 | 96 | $operation = $this->operation; |
89 | | - if (!isset($operation['parameters']['Body'])) { |
| 97 | + if ( !isset( $operation['parameters']['Body'] ) ) { |
90 | 98 | return $request; |
91 | 99 | } |
92 | | - $source = isset($command[$sourceParameter]) ? $command[$sourceParameter] : null; |
93 | | - $body = isset($command[$bodyParameter]) ? $command[$bodyParameter] : null; |
| 100 | + $source = isset( $command[$sourceParameter] ) ? $command[$sourceParameter] : null; |
| 101 | + $body = isset( $command[$bodyParameter] ) ? $command[$bodyParameter] : null; |
94 | 102 | // If a file path is passed in then get the file handle |
95 | | - if (is_string($source) && file_exists($source)) { |
96 | | - $body = fopen($source, 'rb'); |
| 103 | + if ( is_string( $source ) && file_exists( $source ) ) { |
| 104 | + $body = fopen( $source, 'rb' ); |
97 | 105 | } |
98 | 106 | // Prepare the body parameter and remove the source file parameter |
99 | | - if (null !== $body) { |
| 107 | + if ( null !== $body ) { |
100 | 108 | return $request; |
101 | 109 | } else { |
102 | 110 | throw new InvalidArgumentException( |
103 | | - "You must specify a non-null value for the {$bodyParameter} or {$sourceParameter} parameters."); |
| 111 | + "You must specify a non-null value for the {$bodyParameter} or {$sourceParameter} parameters." ); |
| 112 | + } |
104 | 113 | } |
105 | | - } |
106 | 114 |
|
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); |
| 115 | + // update md5 |
| 116 | + |
| 117 | + public function md5Transformer( CommandInterface $command, $request ) { |
| 118 | + $operation = $this->operation; |
| 119 | + if ( isset( $operation['data']['contentMd5'] ) ) { |
| 120 | + $request = $this->addMd5( $request ); |
| 121 | + } |
| 122 | + if ( isset( $operation['parameters']['ContentMD5'] ) && |
| 123 | + isset( $command['ContentMD5'] ) ) { |
| 124 | + $value = $command['ContentMD5']; |
| 125 | + if ( $value === true ) { |
| 126 | + $request = $this->addMd5( $request ); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return $request; |
112 | 131 | } |
113 | | - if (isset($operation['parameters']['ContentMD5']) && |
114 | | - isset($command['ContentMD5'])) { |
115 | | - $value = $command['ContentMD5']; |
116 | | - if ($value === true) { |
117 | | - $request = $this->addMd5($request); |
| 132 | + |
| 133 | + // add meta |
| 134 | + |
| 135 | + public function metadataTransformer( CommandInterface $command, $request ) { |
| 136 | + $operation = $this->operation; |
| 137 | + if ( isset( $command['Metadata'] ) ) { |
| 138 | + $meta = $command['Metadata']; |
| 139 | + foreach ( $meta as $key => $value ) { |
| 140 | + $request = $request->withHeader( 'x-cos-meta-' . $key, $value ); |
| 141 | + } |
118 | 142 | } |
| 143 | + $request = headersMap( $command, $request ); |
| 144 | + |
| 145 | + return $request; |
119 | 146 | } |
120 | 147 |
|
121 | | - return $request; |
122 | | - } |
| 148 | + // count md5 |
123 | 149 |
|
124 | | - // add meta |
125 | | - public function metadataTransformer(CommandInterface $command, $request) { |
126 | | - $operation = $this->operation; |
127 | | - if (isset($command['Metadata'])) { |
128 | | - $meta = $command['Metadata']; |
129 | | - foreach ($meta as $key => $value) { |
130 | | - $request = $request->withHeader('x-cos-meta-' . $key, $value); |
| 150 | + private function addMd5( $request ) { |
| 151 | + $body = $request->getBody(); |
| 152 | + if ( $body && $body->getSize() > 0 ) { |
| 153 | + $md5 = base64_encode( md5( $body, true ) ); |
| 154 | + return $request->withHeader( 'Content-MD5', $md5 ); |
131 | 155 | } |
| 156 | + return $request; |
132 | 157 | } |
133 | | - $request = headersMap($command, $request); |
134 | | - return $request; |
135 | | - } |
136 | 158 |
|
137 | | - // count md5 |
138 | | - private function addMd5($request) { |
139 | | - $body = $request->getBody(); |
140 | | - if ($body && $body->getSize() > 0) { |
141 | | - $md5 = base64_encode(md5($body, true)); |
142 | | - return $request->withHeader('Content-MD5', $md5); |
| 159 | + // inventoryId |
| 160 | + |
| 161 | + public function specialParamTransformer( CommandInterface $command, $request ) { |
| 162 | + $action = $command->getName(); |
| 163 | + if ( $action == 'PutBucketInventory' ) { |
| 164 | + $id = $command['Id']; |
| 165 | + $uri = $request->getUri(); |
| 166 | + $query = $uri->getQuery(); |
| 167 | + $uri = $uri->withQuery( $query . '&Id='.$id ); |
| 168 | + return $request->withUri( $uri ); |
| 169 | + } |
| 170 | + return $request; |
143 | 171 | } |
144 | | - return $request; |
145 | | - } |
146 | 172 |
|
147 | | - // inventoryId |
148 | | - public function specialParamTransformer(CommandInterface $command, $request) { |
149 | | - $action = $command->getName(); |
150 | | - if ($action == 'PutBucketInventory') { |
151 | | - $id = $command['Id']; |
152 | | - $uri = $request->getUri(); |
153 | | - $query = $uri->getQuery(); |
154 | | - $uri = $uri->withQuery($query . "&Id=".$id); |
155 | | - return $request->withUri($uri); |
| 173 | + public function __destruct() { |
156 | 174 | } |
157 | | - return $request; |
158 | | - } |
159 | 175 |
|
160 | | - public function __destruct() { |
161 | 176 | } |
162 | | - |
163 | | -} |
|
0 commit comments