|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Qcloud\Cos; |
| 4 | + |
| 5 | +use Guzzle\Service\Description\Parameter; |
| 6 | +use Guzzle\Service\Description\ServiceDescription; |
| 7 | +use GuzzleHttp\HandlerStack; |
| 8 | +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 | +use GuzzleHttp\Command\CommandInterface; |
| 14 | +use GuzzleHttp\Exception\RequestException; |
| 15 | +use GuzzleHttp\Middleware; |
| 16 | +use GuzzleHttp\Psr7; |
| 17 | +use GuzzleHttp\Psr7\Uri; |
| 18 | +use InvalidArgumentException; |
| 19 | + |
| 20 | + |
| 21 | +class CommandToRequestTransformer { |
| 22 | + private $config; |
| 23 | + private $operation; |
| 24 | + |
| 25 | + public function __construct($config ,$operation) { |
| 26 | + $this->config = $config; |
| 27 | + $this->operation = $operation; |
| 28 | + } |
| 29 | + |
| 30 | + // format bucket style |
| 31 | + public function bucketStyleTransformer(CommandInterface $command, RequestInterface $request) { |
| 32 | + $action = $command->getName(); |
| 33 | + if ($action == 'ListBuckets') { |
| 34 | + return $request->withUri(new Uri($this->config['schema']."://service.cos.myqcloud.com/")); |
| 35 | + } |
| 36 | + $operation = $this->operation; |
| 37 | + $bucketname = $command['Bucket']; |
| 38 | + |
| 39 | + $appId = $this->config['appId']; |
| 40 | + if ($appId != null && endWith($bucketname, '-'.$appId) == False) |
| 41 | + { |
| 42 | + $bucketname = $bucketname.'-'.$appId; |
| 43 | + } |
| 44 | + $command['Bucket'] = $bucketname; |
| 45 | + $path = ''; |
| 46 | + $http_method = $operation['httpMethod']; |
| 47 | + $uri = $operation['uri']; |
| 48 | + |
| 49 | + // Hoststyle is used by default |
| 50 | + // 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); |
| 57 | + } |
| 58 | + } |
| 59 | + $origin_host = $bucketname. '.cos.' . $this->config['region'] . '.' . $this->config['endpoint']; |
| 60 | + // domain |
| 61 | + if ($this->config['domain'] != null) { |
| 62 | + $origin_host = $this->config['domain']; |
| 63 | + } |
| 64 | + $host = $origin_host; |
| 65 | + if ($this->config['ip'] != null) { |
| 66 | + $host = $this->config['ip']; |
| 67 | + if ($this->config['port'] != null) { |
| 68 | + $host = $this->config['ip'] . ":" . $this->config['port']; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + $path = $this->config['schema'].'://'. $host . $uri; |
| 74 | + $uri = new Uri($path); |
| 75 | + $query = $request->getUri()->getQuery(); |
| 76 | + if ($uri->getQuery() != $query && $uri->getQuery() != "") { |
| 77 | + $query = $uri->getQuery() . "&" . $request->getUri()->getQuery(); |
| 78 | + } |
| 79 | + $uri = $uri->withQuery($query); |
| 80 | + $request = $request->withUri($uri); |
| 81 | + $request = $request->withHeader('Host', $origin_host); |
| 82 | + return $request; |
| 83 | + } |
| 84 | + |
| 85 | + // format upload body |
| 86 | + public function uploadBodyTransformer(CommandInterface $command, $request, $bodyParameter = 'Body', $sourceParameter = 'SourceFile') { |
| 87 | + |
| 88 | + $operation = $this->operation; |
| 89 | + if (!isset($operation['parameters']['Body'])) { |
| 90 | + return $request; |
| 91 | + } |
| 92 | + $source = isset($command[$sourceParameter]) ? $command[$sourceParameter] : null; |
| 93 | + $body = isset($command[$bodyParameter]) ? $command[$bodyParameter] : null; |
| 94 | + // 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'); |
| 97 | + } |
| 98 | + // Prepare the body parameter and remove the source file parameter |
| 99 | + if (null !== $body) { |
| 100 | + return $request; |
| 101 | + } else { |
| 102 | + throw new InvalidArgumentException( |
| 103 | + "You must specify a non-null value for the {$bodyParameter} or {$sourceParameter} parameters."); |
| 104 | + } |
| 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 === true) { |
| 117 | + $request = $this->addMd5($request); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return $request; |
| 122 | + } |
| 123 | + |
| 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); |
| 131 | + } |
| 132 | + } |
| 133 | + return $request; |
| 134 | + } |
| 135 | + |
| 136 | + // count md5 |
| 137 | + private function addMd5($request) { |
| 138 | + $body = $request->getBody(); |
| 139 | + if ($body && $body->getSize() > 0) { |
| 140 | + $md5 = base64_encode(md5($body, true)); |
| 141 | + return $request->withHeader('Content-MD5', $md5); |
| 142 | + } |
| 143 | + return $request; |
| 144 | + } |
| 145 | + |
| 146 | + // inventoryId |
| 147 | + public function specialParamTransformer(CommandInterface $command, $request) { |
| 148 | + $action = $command->getName(); |
| 149 | + if ($action == 'PutBucketInventory') { |
| 150 | + $id = $command['Id']; |
| 151 | + $uri = $request->getUri(); |
| 152 | + $query = $uri->getQuery(); |
| 153 | + $uri = $uri->withQuery($query . "&Id=".$id); |
| 154 | + return $request->withUri($uri); |
| 155 | + } |
| 156 | + return $request; |
| 157 | + } |
| 158 | + |
| 159 | + public function __destruct() { |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments