ResultTransformer.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. if ($this->config['locationWithScheme']) {
  54. $result['Location'] = $this->config['scheme'] . '://' . $result['Location'];
  55. }
  56. return $result;
  57. }
  58. public function selectContentTransformer(CommandInterface $command, Result $result) {
  59. $action = $command->getName();
  60. if ($action == "SelectObjectContent") {
  61. $result['Data'] = $this->getSelectContents($result);
  62. }
  63. return $result;
  64. }
  65. public function ciContentInfoTransformer(CommandInterface $command, Result $result) {
  66. $action = $command->getName();
  67. if ($action == "ImageInfo" || $action == "ImageExif" || $action == "ImageAve" || $action == "GetPrivateM3U8") {
  68. $length = intval($result['ContentLength']);
  69. if($length > 0){
  70. $result['Data'] = $this->geCiContentInfo($result, $length);
  71. }
  72. }
  73. if ($action == "PutObject" && isset($command["PicOperations"]) && $command["PicOperations"]) {
  74. $picOperations = json_decode($command["PicOperations"], true);
  75. $picRuleSize = isset($picOperations['rules']) && is_array($picOperations['rules']) ? sizeof($picOperations['rules']) : 0;
  76. $length = intval($result['ContentLength']);
  77. if($length > 0){
  78. $content = $this->geCiContentInfo($result, $length);
  79. if (version_compare(PHP_VERSION, '8.0.0', '<')) {
  80. libxml_disable_entity_loader(true);
  81. }
  82. $obj = simplexml_load_string($content, "SimpleXMLElement", LIBXML_NOCDATA);
  83. $xmlData = json_decode(json_encode($obj),true);
  84. if ($picRuleSize == 1 && isset($xmlData['ProcessResults']['Object'])){
  85. $tmp = $xmlData['ProcessResults']['Object'];
  86. unset($xmlData['ProcessResults']['Object']);
  87. $xmlData['ProcessResults']['Object'][] = $tmp;
  88. }
  89. $result['Data'] = $xmlData;
  90. }
  91. }
  92. if ($action == "GetBucketGuetzli") {
  93. $length = intval($result['ContentLength']);
  94. if($length > 0){
  95. $content = $this->geCiContentInfo($result, $length);
  96. if (version_compare(PHP_VERSION, '8.0.0', '<')) {
  97. libxml_disable_entity_loader(true);
  98. }
  99. $obj = simplexml_load_string($content, "SimpleXMLElement", LIBXML_NOCDATA);
  100. $arr = json_decode(json_encode($obj),true);
  101. $result['GuetzliStatus'] = isset($arr[0]) ? $arr[0] : '';
  102. }
  103. }
  104. if ($action == "GetCiService") {
  105. $length = intval($result['ContentLength']);
  106. if($length > 0){
  107. $content = $this->geCiContentInfo($result, $length);
  108. if (version_compare(PHP_VERSION, '8.0.0', '<')) {
  109. libxml_disable_entity_loader(true);
  110. }
  111. $obj = simplexml_load_string($content, "SimpleXMLElement", LIBXML_NOCDATA);
  112. $arr = json_decode(json_encode($obj),true);
  113. $result['CIStatus'] = isset($arr[0]) ? $arr[0] : '';
  114. unset($result['Body']);
  115. }
  116. }
  117. if ($action == "GetOriginProtect") {
  118. $length = intval($result['ContentLength']);
  119. if($length > 0){
  120. $content = $this->geCiContentInfo($result, $length);
  121. if (version_compare(PHP_VERSION, '8.0.0', '<')) {
  122. libxml_disable_entity_loader(true);
  123. }
  124. $obj = simplexml_load_string($content, "SimpleXMLElement", LIBXML_NOCDATA);
  125. $arr = json_decode(json_encode($obj),true);
  126. $result['OriginProtectStatus'] = isset($arr[0]) ? $arr[0] : '';
  127. unset($result['Body']);
  128. }
  129. }
  130. if ($action == "GetHotLink") {
  131. $length = intval($result['ContentLength']);
  132. if($length > 0){
  133. $content = $this->geCiContentInfo($result, $length);
  134. if (version_compare(PHP_VERSION, '8.0.0', '<')) {
  135. libxml_disable_entity_loader(true);
  136. }
  137. $obj = simplexml_load_string($content, "SimpleXMLElement", LIBXML_NOCDATA);
  138. $arr = json_decode(json_encode($obj),true);
  139. $result['Hotlink'] = $arr;
  140. unset($result['Body']);
  141. }
  142. }
  143. if ($action == "AutoTranslationBlockProcess") {
  144. $length = intval($result['ContentLength']);
  145. if($length > 0){
  146. $content = $this->geCiContentInfo($result, $length);
  147. if (version_compare(PHP_VERSION, '8.0.0', '<')) {
  148. libxml_disable_entity_loader(true);
  149. }
  150. $obj = simplexml_load_string($content, "SimpleXMLElement", LIBXML_NOCDATA);
  151. $arr = json_decode(json_encode($obj),true);
  152. $result['TranslationResult'] = isset($arr[0]) ? $arr[0] : '';
  153. unset($result['Body']);
  154. }
  155. }
  156. $xml2JsonActions = array(
  157. 'CreateMediaTranscodeJobs' => 1,
  158. 'CreateMediaJobs' => 1,
  159. 'DescribeMediaJob' => 1,
  160. 'DescribeMediaJobs' => 1,
  161. 'CreateMediaSnapshotJobs' => 1,
  162. 'CreateMediaConcatJobs' => 1,
  163. 'CreateMediaVoiceSeparateJobs' => 1,
  164. 'DescribeMediaVoiceSeparateJob' => 1,
  165. 'UpdateMediaQueue' => 1,
  166. 'CreateMediaSmartCoverJobs' => 1,
  167. 'CreateMediaVideoProcessJobs' => 1,
  168. 'CreateMediaVideoMontageJobs' => 1,
  169. 'CreateMediaAnimationJobs' => 1,
  170. 'CreateMediaPicProcessJobs' => 1,
  171. 'CreateMediaSegmentJobs' => 1,
  172. 'CreateMediaVideoTagJobs' => 1,
  173. 'CreateMediaSuperResolutionJobs' => 1,
  174. 'CreateMediaSDRtoHDRJobs' => 1,
  175. 'CreateMediaDigitalWatermarkJobs' => 1,
  176. 'CreateMediaExtractDigitalWatermarkJobs' => 1,
  177. 'GetWorkflowInstance' => 1,
  178. 'CreateMediaTranscodeTemplate' => 1,
  179. 'UpdateMediaTranscodeTemplate' => 1,
  180. 'CreateMediaHighSpeedHdTemplate' => 1,
  181. 'UpdateMediaHighSpeedHdTemplate' => 1,
  182. 'CreateMediaAnimationTemplate' => 1,
  183. 'UpdateMediaAnimationTemplate' => 1,
  184. 'CreateMediaConcatTemplate' => 1,
  185. 'UpdateMediaConcatTemplate' => 1,
  186. 'CreateMediaVideoProcessTemplate' => 1,
  187. 'UpdateMediaVideoProcessTemplate' => 1,
  188. 'CreateMediaVideoMontageTemplate' => 1,
  189. 'UpdateMediaVideoMontageTemplate' => 1,
  190. 'CreateMediaVoiceSeparateTemplate' => 1,
  191. 'UpdateMediaVoiceSeparateTemplate' => 1,
  192. 'CreateMediaSuperResolutionTemplate' => 1,
  193. 'UpdateMediaSuperResolutionTemplate' => 1,
  194. 'CreateMediaPicProcessTemplate' => 1,
  195. 'UpdateMediaPicProcessTemplate' => 1,
  196. 'CreateMediaWatermarkTemplate' => 1,
  197. 'UpdateMediaWatermarkTemplate' => 1,
  198. 'CreateInventoryTriggerJob' => 1,
  199. 'DescribeInventoryTriggerJobs' => 1,
  200. 'DescribeInventoryTriggerJob' => 1,
  201. 'CreateMediaNoiseReductionJobs' => 1,
  202. 'CreateMediaQualityEstimateJobs' => 1,
  203. 'CreateMediaStreamExtractJobs' => 1,
  204. );
  205. if (key_exists($action, $xml2JsonActions)) {
  206. $length = intval($result['ContentLength']);
  207. if($length > 0){
  208. $content = $this->geCiContentInfo($result, $length);
  209. if (version_compare(PHP_VERSION, '8.0.0', '<')) {
  210. libxml_disable_entity_loader(true);
  211. }
  212. $obj = simplexml_load_string($content, "SimpleXMLElement", LIBXML_NOCDATA);
  213. $xmlData = json_decode(json_encode($obj),true);
  214. $result['Response'] = $xmlData;
  215. unset($result['Body']);
  216. }
  217. }
  218. return $result;
  219. }
  220. public function geCiContentInfo($result, $length) {
  221. $f = $result['Body'];
  222. $data = "";
  223. while (!$f->eof()) {
  224. $tmp = $f->read($length);
  225. if (empty($tmp)) {
  226. break;
  227. }
  228. $data .= $tmp;
  229. }
  230. return $data;
  231. }
  232. public function getSelectContents($result) {
  233. $f = $result['RawData'];
  234. while (!$f->eof()) {
  235. $data = array();
  236. $tmp = $f->read(4);
  237. if (empty($tmp)) {
  238. break;
  239. }
  240. $total_length = (int)(unpack("N", $tmp)[1]);
  241. $headers_length = (int)(unpack("N", $f->read(4))[1]);
  242. $body_length = $total_length - $headers_length - 16;
  243. $predule_crc = (int)(unpack("N", $f->read(4))[1]);
  244. $headers = array();
  245. for ($offset = 0; $offset < $headers_length;) {
  246. $key_length = (int)(unpack("C", $f->read(1))[1]);
  247. $key = $f->read($key_length);
  248. $head_value_type = (int)(unpack("C", $f->read(1))[1]);
  249. $value_length = (int)(unpack("n", $f->read(2))[1]);
  250. $value = $f->read($value_length);
  251. $offset += 4 + $key_length + $value_length;
  252. if ($key == ":message-type") {
  253. $data['MessageType'] = $value;
  254. }
  255. if ($key == ":event-type") {
  256. $data['EventType'] = $value;
  257. }
  258. if ($key == ":error-code") {
  259. $data['ErrorCode'] = $value;
  260. }
  261. if ($key == ":error-message") {
  262. $data['ErrorMessage'] = $value;
  263. }
  264. }
  265. $body = $f->read($body_length);
  266. $message_crc = (int)(unpack("N", $f->read(4))[1]);
  267. $data['Body'] = $body;
  268. yield $data;
  269. }
  270. }
  271. public function __destruct() {
  272. }
  273. }