ResultTransformer.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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" || $action == "ImageRepairProcess") {
  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" || $action == "GetPrivateM3U8") {
  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. if ($action == "GetCiService" ) {
  96. $length = intval($result['ContentLength']);
  97. if($length > 0){
  98. $content = $this->geCiContentInfo($result, $length);
  99. $obj = simplexml_load_string($content, "SimpleXMLElement", LIBXML_NOCDATA);
  100. $arr = json_decode(json_encode($obj),true);
  101. $result['CIStatus'] = isset($arr[0]) ? $arr[0] : '';
  102. unset($result['Body']);
  103. }
  104. }
  105. if ($action == "GetOriginProtect" ) {
  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. $arr = json_decode(json_encode($obj),true);
  111. $result['OriginProtectStatus'] = isset($arr[0]) ? $arr[0] : '';
  112. unset($result['Body']);
  113. }
  114. }
  115. if ($action == "GetHotLink" ) {
  116. $length = intval($result['ContentLength']);
  117. if($length > 0){
  118. $content = $this->geCiContentInfo($result, $length);
  119. $obj = simplexml_load_string($content, "SimpleXMLElement", LIBXML_NOCDATA);
  120. $arr = json_decode(json_encode($obj),true);
  121. $result['Hotlink'] = $arr;
  122. unset($result['Body']);
  123. }
  124. }
  125. $xml2JsonActions = array(
  126. 'CreateMediaTranscodeJobs' => 1,
  127. 'CreateMediaJobs' => 1,
  128. 'DescribeMediaJob' => 1,
  129. 'DescribeMediaJobs' => 1,
  130. 'CreateMediaSnapshotJobs' => 1,
  131. 'CreateMediaConcatJobs' => 1,
  132. 'CreateMediaVoiceSeparateJobs' => 1,
  133. 'DescribeMediaVoiceSeparateJob' => 1,
  134. 'UpdateMediaQueue' => 1,
  135. 'CreateMediaSmartCoverJobs' => 1,
  136. 'CreateMediaVideoProcessJobs' => 1,
  137. 'CreateMediaVideoMontageJobs' => 1,
  138. 'CreateMediaAnimationJobs' => 1,
  139. 'CreateMediaPicProcessJobs' => 1,
  140. 'CreateMediaSegmentJobs' => 1,
  141. 'CreateMediaVideoTagJobs' => 1,
  142. 'CreateMediaSuperResolutionJobs' => 1,
  143. 'CreateMediaSDRtoHDRJobs' => 1,
  144. 'CreateMediaDigitalWatermarkJobs' => 1,
  145. 'CreateMediaExtractDigitalWatermarkJobs' => 1,
  146. 'OpticalOcrRecognition' => 1,
  147. 'GetWorkflowInstance' => 1,
  148. 'CreateMediaTranscodeTemplate' => 1,
  149. 'UpdateMediaTranscodeTemplate' => 1,
  150. 'CreateMediaHighSpeedHdTemplate' => 1,
  151. 'UpdateMediaHighSpeedHdTemplate' => 1,
  152. 'CreateMediaAnimationTemplate' => 1,
  153. 'UpdateMediaAnimationTemplate' => 1,
  154. 'CreateMediaConcatTemplate' => 1,
  155. 'UpdateMediaConcatTemplate' => 1,
  156. 'CreateMediaVideoProcessTemplate' => 1,
  157. 'UpdateMediaVideoProcessTemplate' => 1,
  158. 'CreateMediaVideoMontageTemplate' => 1,
  159. 'UpdateMediaVideoMontageTemplate' => 1,
  160. 'CreateMediaVoiceSeparateTemplate' => 1,
  161. 'UpdateMediaVoiceSeparateTemplate' => 1,
  162. 'CreateMediaSuperResolutionTemplate' => 1,
  163. 'UpdateMediaSuperResolutionTemplate' => 1,
  164. 'CreateMediaPicProcessTemplate' => 1,
  165. 'UpdateMediaPicProcessTemplate' => 1,
  166. 'CreateMediaWatermarkTemplate' => 1,
  167. 'UpdateMediaWatermarkTemplate' => 1,
  168. 'CreateInventoryTriggerJob' => 1,
  169. 'DescribeInventoryTriggerJobs' => 1,
  170. 'DescribeInventoryTriggerJob' => 1,
  171. 'CreateMediaNoiseReductionJobs' => 1,
  172. 'CreateMediaQualityEstimateJobs' => 1,
  173. 'CreateMediaStreamExtractJobs' => 1,
  174. );
  175. if (key_exists($action, $xml2JsonActions)) {
  176. $length = intval($result['ContentLength']);
  177. if($length > 0){
  178. $content = $this->geCiContentInfo($result, $length);
  179. $obj = simplexml_load_string($content, "SimpleXMLElement", LIBXML_NOCDATA);
  180. $xmlData = json_decode(json_encode($obj),true);
  181. $result['Response'] = $xmlData;
  182. }
  183. }
  184. return $result;
  185. }
  186. public function geCiContentInfo($result, $length) {
  187. $f = $result['Body'];
  188. $data = "";
  189. while (!$f->eof()) {
  190. $tmp = $f->read($length);
  191. if (empty($tmp)) {
  192. break;
  193. }
  194. $data .= $tmp;
  195. }
  196. return $data;
  197. }
  198. public function getSelectContents($result) {
  199. $f = $result['RawData'];
  200. while (!$f->eof()) {
  201. $data = array();
  202. $tmp = $f->read(4);
  203. if (empty($tmp)) {
  204. break;
  205. }
  206. $total_length = (int)(unpack("N", $tmp)[1]);
  207. $headers_length = (int)(unpack("N", $f->read(4))[1]);
  208. $body_length = $total_length - $headers_length - 16;
  209. $predule_crc = (int)(unpack("N", $f->read(4))[1]);
  210. $headers = array();
  211. for ($offset = 0; $offset < $headers_length;) {
  212. $key_length = (int)(unpack("C", $f->read(1))[1]);
  213. $key = $f->read($key_length);
  214. $head_value_type = (int)(unpack("C", $f->read(1))[1]);
  215. $value_length = (int)(unpack("n", $f->read(2))[1]);
  216. $value = $f->read($value_length);
  217. $offset += 4 + $key_length + $value_length;
  218. if ($key == ":message-type") {
  219. $data['MessageType'] = $value;
  220. }
  221. if ($key == ":event-type") {
  222. $data['EventType'] = $value;
  223. }
  224. if ($key == ":error-code") {
  225. $data['ErrorCode'] = $value;
  226. }
  227. if ($key == ":error-message") {
  228. $data['ErrorMessage'] = $value;
  229. }
  230. }
  231. $body = $f->read($body_length);
  232. $message_crc = (int)(unpack("N", $f->read(4))[1]);
  233. $data['Body'] = $body;
  234. yield $data;
  235. }
  236. }
  237. public function __destruct() {
  238. }
  239. }