ResultTransformer.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace Qcloud\Cos;
  3. use Psr\Http\Message\RequestInterface;
  4. use Psr\Http\Message\ResponseInterface;
  5. use GuzzleHttp\Command\CommandInterface;
  6. use GuzzleHttp\Command\Result;
  7. class ResultTransformer {
  8. private $config;
  9. private $operation;
  10. public function __construct($config, $operation) {
  11. $this->config = $config;
  12. $this->operation = $operation;
  13. }
  14. public function writeDataToLocal(CommandInterface $command, RequestInterface $request, ResponseInterface $response) {
  15. $action = $command->getName();
  16. if ($action == "GetObject" || $action == "GetSnapshot") {
  17. if (isset($command['SaveAs'])) {
  18. $fp = fopen($command['SaveAs'], "wb");
  19. $stream = $response->getBody();
  20. $offset = 0;
  21. $partsize = 8192;
  22. while (!$stream->eof()) {
  23. $output = $stream->read($partsize);
  24. fseek($fp, $offset);
  25. fwrite($fp, $output);
  26. $offset += $partsize;
  27. }
  28. fclose($fp);
  29. }
  30. }
  31. }
  32. public function metaDataTransformer(CommandInterface $command, ResponseInterface $response, Result $result) {
  33. $headers = $response->getHeaders();
  34. $metadata = array();
  35. foreach ($headers as $key => $value) {
  36. if (strpos($key, "x-cos-meta-") === 0) {
  37. $metadata[substr($key, 11)] = $value[0];
  38. }
  39. }
  40. if (!empty($metadata)) {
  41. $result['Metadata'] = $metadata;
  42. }
  43. return $result;
  44. }
  45. public function extraHeadersTransformer(CommandInterface $command, RequestInterface $request, Result $result) {
  46. if ($command['Key'] != null && $result['Key'] == null) {
  47. $result['Key'] = $command['Key'];
  48. }
  49. if ($command['Bucket'] != null && $result['Bucket'] == null) {
  50. $result['Bucket'] = $command['Bucket'];
  51. }
  52. $result['Location'] = $request->getHeader("Host")[0] . $request->getUri()->getPath();
  53. return $result;
  54. }
  55. public function selectContentTransformer(CommandInterface $command, Result $result) {
  56. $action = $command->getName();
  57. if ($action == "SelectObjectContent") {
  58. $result['Data'] = $this->getSelectContents($result);
  59. }
  60. return $result;
  61. }
  62. public function ciContentInfoTransformer(CommandInterface $command, Result $result) {
  63. $action = $command->getName();
  64. if ($action == "ImageInfo" || $action == "ImageExif" || $action == "ImageAve") {
  65. $length = intval($result['ContentLength']);
  66. if($length > 0){
  67. $result['Data'] = $this->geCiContentInfo($result, $length);
  68. }
  69. }
  70. if ($action == "PutObject" && isset($command["PicOperations"]) && $command["PicOperations"]) {
  71. $picOperations = json_decode($command["PicOperations"], true);
  72. $picRuleSize = isset($picOperations['rules']) && is_array($picOperations['rules']) ? sizeof($picOperations['rules']) : 0;
  73. $length = intval($result['ContentLength']);
  74. if($length > 0){
  75. $content = $this->geCiContentInfo($result, $length);
  76. $obj = simplexml_load_string($content, "SimpleXMLElement", LIBXML_NOCDATA);
  77. $xmlData = json_decode(json_encode($obj),true);
  78. if ($picRuleSize == 1 && isset($xmlData['ProcessResults']['Object'])){
  79. $tmp = $xmlData['ProcessResults']['Object'];
  80. unset($xmlData['ProcessResults']['Object']);
  81. $xmlData['ProcessResults']['Object'][] = $tmp;
  82. }
  83. $result['Data'] = $xmlData;
  84. }
  85. }
  86. if ($action == "GetBucketGuetzli" ) {
  87. $length = intval($result['ContentLength']);
  88. if($length > 0){
  89. $content = $this->geCiContentInfo($result, $length);
  90. $obj = simplexml_load_string($content, "SimpleXMLElement", LIBXML_NOCDATA);
  91. $arr = json_decode(json_encode($obj),true);
  92. $result['GuetzliStatus'] = isset($arr[0]) ? $arr[0] : '';
  93. }
  94. }
  95. $xml2JsonActions = array(
  96. 'CreateMediaTranscodeJobs' => 1,
  97. 'CreateMediaSnapshotJobs' => 1,
  98. 'CreateMediaConcatJobs' => 1,
  99. );
  100. if (key_exists($action, $xml2JsonActions)) {
  101. $length = intval($result['ContentLength']);
  102. if($length > 0){
  103. $content = $this->geCiContentInfo($result, $length);
  104. $obj = simplexml_load_string($content, "SimpleXMLElement", LIBXML_NOCDATA);
  105. $xmlData = json_decode(json_encode($obj),true);
  106. $result['Response'] = $xmlData;
  107. }
  108. }
  109. return $result;
  110. }
  111. public function geCiContentInfo($result, $length) {
  112. $f = $result['Body'];
  113. $data = "";
  114. while (!$f->eof()) {
  115. $tmp = $f->read($length);
  116. if (empty($tmp)) {
  117. break;
  118. }
  119. $data .= $tmp;
  120. }
  121. return $data;
  122. }
  123. public function getSelectContents($result) {
  124. $f = $result['RawData'];
  125. while (!$f->eof()) {
  126. $data = array();
  127. $tmp = $f->read(4);
  128. if (empty($tmp)) {
  129. break;
  130. }
  131. $total_length = (int)(unpack("N", $tmp)[1]);
  132. $headers_length = (int)(unpack("N", $f->read(4))[1]);
  133. $body_length = $total_length - $headers_length - 16;
  134. $predule_crc = (int)(unpack("N", $f->read(4))[1]);
  135. $headers = array();
  136. for ($offset = 0; $offset < $headers_length;) {
  137. $key_length = (int)(unpack("C", $f->read(1))[1]);
  138. $key = $f->read($key_length);
  139. $head_value_type = (int)(unpack("C", $f->read(1))[1]);
  140. $value_length = (int)(unpack("n", $f->read(2))[1]);
  141. $value = $f->read($value_length);
  142. $offset += 4 + $key_length + $value_length;
  143. if ($key == ":message-type") {
  144. $data['MessageType'] = $value;
  145. }
  146. if ($key == ":event-type") {
  147. $data['EventType'] = $value;
  148. }
  149. if ($key == ":error-code") {
  150. $data['ErrorCode'] = $value;
  151. }
  152. if ($key == ":error-message") {
  153. $data['ErrorMessage'] = $value;
  154. }
  155. }
  156. $body = $f->read($body_length);
  157. $message_crc = (int)(unpack("N", $f->read(4))[1]);
  158. $data['Body'] = $body;
  159. yield $data;
  160. }
  161. }
  162. public function __destruct() {
  163. }
  164. }