ResultTransformer.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. 'CreateMediaJobs' => 1,
  98. 'DescribeMediaJob' => 1,
  99. 'DescribeMediaJobs' => 1,
  100. 'CreateMediaSnapshotJobs' => 1,
  101. 'CreateMediaConcatJobs' => 1,
  102. 'CreateMediaVoiceSeparateJobs' => 1,
  103. 'DescribeMediaVoiceSeparateJob' => 1,
  104. );
  105. if (key_exists($action, $xml2JsonActions)) {
  106. $length = intval($result['ContentLength']);
  107. if($length > 0){
  108. $content = $this->geCiContentInfo($result, $length);
  109. $obj = simplexml_load_string($content, "SimpleXMLElement", LIBXML_NOCDATA);
  110. $xmlData = json_decode(json_encode($obj),true);
  111. $result['Response'] = $xmlData;
  112. }
  113. }
  114. return $result;
  115. }
  116. public function geCiContentInfo($result, $length) {
  117. $f = $result['Body'];
  118. $data = "";
  119. while (!$f->eof()) {
  120. $tmp = $f->read($length);
  121. if (empty($tmp)) {
  122. break;
  123. }
  124. $data .= $tmp;
  125. }
  126. return $data;
  127. }
  128. public function getSelectContents($result) {
  129. $f = $result['RawData'];
  130. while (!$f->eof()) {
  131. $data = array();
  132. $tmp = $f->read(4);
  133. if (empty($tmp)) {
  134. break;
  135. }
  136. $total_length = (int)(unpack("N", $tmp)[1]);
  137. $headers_length = (int)(unpack("N", $f->read(4))[1]);
  138. $body_length = $total_length - $headers_length - 16;
  139. $predule_crc = (int)(unpack("N", $f->read(4))[1]);
  140. $headers = array();
  141. for ($offset = 0; $offset < $headers_length;) {
  142. $key_length = (int)(unpack("C", $f->read(1))[1]);
  143. $key = $f->read($key_length);
  144. $head_value_type = (int)(unpack("C", $f->read(1))[1]);
  145. $value_length = (int)(unpack("n", $f->read(2))[1]);
  146. $value = $f->read($value_length);
  147. $offset += 4 + $key_length + $value_length;
  148. if ($key == ":message-type") {
  149. $data['MessageType'] = $value;
  150. }
  151. if ($key == ":event-type") {
  152. $data['EventType'] = $value;
  153. }
  154. if ($key == ":error-code") {
  155. $data['ErrorCode'] = $value;
  156. }
  157. if ($key == ":error-message") {
  158. $data['ErrorMessage'] = $value;
  159. }
  160. }
  161. $body = $f->read($body_length);
  162. $message_crc = (int)(unpack("N", $f->read(4))[1]);
  163. $data['Body'] = $body;
  164. yield $data;
  165. }
  166. }
  167. public function __destruct() {
  168. }
  169. }