ResultTransformer.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Qcloud\Cos;
  3. use Guzzle\Service\Description\Parameter;
  4. use Guzzle\Service\Description\ServiceDescription;
  5. use GuzzleHttp\HandlerStack;
  6. use Psr\Http\Message\RequestInterface;
  7. use Psr\Http\Message\ResponseInterface;
  8. use Qcloud\Cos\Signature;
  9. use GuzzleHttp\Command\Guzzle\Description;
  10. use GuzzleHttp\Command\Guzzle\GuzzleClient;
  11. use GuzzleHttp\Command\CommandInterface;
  12. use GuzzleHttp\Exception\RequestException;
  13. use GuzzleHttp\Middleware;
  14. use GuzzleHttp\Psr7;
  15. use GuzzleHttp\Psr7\Uri;
  16. use GuzzleHttp\Command\Result;
  17. use InvalidArgumentException;
  18. class ResultTransformer {
  19. private $config;
  20. private $operation;
  21. public function __construct($config, $operation) {
  22. $this->config = $config;
  23. $this->operation = $operation;
  24. }
  25. public function writeDataToLocal(CommandInterface $command, RequestInterface $request, ResponseInterface $response) {
  26. $action = $command->getName();
  27. if ($action == "GetObject") {
  28. if (isset($command['SaveAs'])) {
  29. $fp = fopen($command['SaveAs'], "wb");
  30. $stream = $response->getBody();
  31. $offset = 0;
  32. $partsize = 8192;
  33. while (!$stream->eof()) {
  34. $output = $stream->read($partsize);
  35. fseek($fp, $offset);
  36. fwrite($fp, $output);
  37. $offset += $partsize;
  38. }
  39. fclose($fp);
  40. }
  41. }
  42. }
  43. public function metaDataTransformer(CommandInterface $command, ResponseInterface $response, Result $result) {
  44. $headers = $response->getHeaders();
  45. $metadata = array();
  46. foreach ($headers as $key => $value) {
  47. if (strpos($key, "x-cos-meta-") === 0) {
  48. $metadata[substr($key, 11)] = $value[0];
  49. }
  50. }
  51. if (!empty($metadata)) {
  52. $result['Metadata'] = $metadata;
  53. }
  54. return $result;
  55. }
  56. public function extraHeadersTransformer(CommandInterface $command, RequestInterface $request, Result $result) {
  57. if ($command['Key'] != null && $result['Key'] == null) {
  58. $result['Key'] = $command['Key'];
  59. }
  60. if ($command['Bucket'] != null && $result['Bucket'] == null) {
  61. $result['Bucket'] = $command['Bucket'];
  62. }
  63. $result['Location'] = $request->getHeader("Host")[0] . $request->getUri()->getPath();
  64. return $result;
  65. }
  66. public function selectContentTransformer(CommandInterface $command, Result $result) {
  67. $action = $command->getName();
  68. if ($action == "SelectObjectContent") {
  69. $result['Data'] = $this->getSelectContents($result);
  70. }
  71. return $result;
  72. }
  73. public function getSelectContents($result) {
  74. $f = $result['RawData'];
  75. while (!$f->eof()) {
  76. $data = array();
  77. $tmp = $f->read(4);
  78. if (empty($tmp)) {
  79. break;
  80. }
  81. $totol_length = (int)(unpack("N", $tmp)[1]);
  82. $headers_length = (int)(unpack("N", $f->read(4))[1]);
  83. $body_length = $totol_length - $headers_length - 16;
  84. $predule_crc = (int)(unpack("N", $f->read(4))[1]);
  85. $headers = array();
  86. for ($offset = 0; $offset < $headers_length;) {
  87. $key_length = (int)(unpack("C", $f->read(1))[1]);
  88. $key = $f->read($key_length);
  89. $head_value_type = (int)(unpack("C", $f->read(1))[1]);
  90. $value_length = (int)(unpack("n", $f->read(2))[1]);
  91. $value = $f->read($value_length);
  92. $offset += 4 + $key_length + $value_length;
  93. if ($key == ":message-type") {
  94. $data['MessageType'] = $value;
  95. }
  96. if ($key == ":event-type") {
  97. $data['EventType'] = $value;
  98. }
  99. if ($key == ":error-code") {
  100. $data['ErrorCode'] = $value;
  101. }
  102. if ($key == ":error-message") {
  103. $data['ErrorMessage'] = $value;
  104. }
  105. }
  106. $body = $f->read($body_length);
  107. $message_crc = (int)(unpack("N", $f->read(4))[1]);
  108. $data['Body'] = $body;
  109. yield $data;
  110. }
  111. }
  112. public function __destruct() {
  113. }
  114. }