134 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			134 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace Qcloud\Cos;
 | 
						|
 | 
						|
use Psr\Http\Message\RequestInterface;
 | 
						|
use Psr\Http\Message\ResponseInterface;
 | 
						|
use GuzzleHttp\Command\CommandInterface;
 | 
						|
use GuzzleHttp\Command\Result;
 | 
						|
 | 
						|
class ResultTransformer {
 | 
						|
    private $config;
 | 
						|
    private $operation;
 | 
						|
 | 
						|
    public function __construct($config, $operation) {
 | 
						|
        $this->config = $config;
 | 
						|
        $this->operation = $operation;
 | 
						|
    }
 | 
						|
 | 
						|
    public function writeDataToLocal(CommandInterface $command, RequestInterface $request, ResponseInterface $response) {
 | 
						|
        $action = $command->getName();
 | 
						|
        if ($action == "GetObject" || $action == "GetSnapshot" || $action == "ImageRepairProcess") {
 | 
						|
            if (isset($command['SaveAs'])) {
 | 
						|
                $fp = fopen($command['SaveAs'], "wb");
 | 
						|
                $stream = $response->getBody();
 | 
						|
                $offset = 0;
 | 
						|
                $partsize = 8192;
 | 
						|
                while (!$stream->eof()) {
 | 
						|
                    $output = $stream->read($partsize);
 | 
						|
                    fseek($fp, $offset);
 | 
						|
                    fwrite($fp, $output);
 | 
						|
                    $offset += $partsize;
 | 
						|
                }
 | 
						|
                fclose($fp);
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    public function metaDataTransformer(CommandInterface $command, ResponseInterface $response, Result $result) {
 | 
						|
        $headers = $response->getHeaders();
 | 
						|
        $metadata = array();
 | 
						|
        foreach ($headers as $key => $value) {
 | 
						|
            if (strpos($key, "x-cos-meta-") === 0) {
 | 
						|
                $metadata[substr($key, 11)] = $value[0];
 | 
						|
            }
 | 
						|
        }
 | 
						|
        if (!empty($metadata)) {
 | 
						|
            $result['Metadata'] = $metadata;
 | 
						|
        }
 | 
						|
        return $result;
 | 
						|
    }
 | 
						|
 | 
						|
    public function extraHeadersTransformer(CommandInterface $command, RequestInterface $request, Result $result) {
 | 
						|
        if ($command['Key'] != null && $result['Key'] == null) {
 | 
						|
            $result['Key'] = $command['Key'];
 | 
						|
        }
 | 
						|
        if ($command['Bucket'] != null && $result['Bucket'] == null) {
 | 
						|
            $result['Bucket'] = $command['Bucket'];
 | 
						|
        }
 | 
						|
        $result['Location'] = $request->getHeader("Host")[0] .  $request->getUri()->getPath();
 | 
						|
        return $result;
 | 
						|
    }
 | 
						|
 | 
						|
    public function selectContentTransformer(CommandInterface $command, Result $result) {
 | 
						|
        $action = $command->getName();
 | 
						|
        if ($action == "SelectObjectContent") {
 | 
						|
            $result['Data'] = $this->getSelectContents($result);
 | 
						|
        }
 | 
						|
        return $result;
 | 
						|
    }
 | 
						|
 | 
						|
    public function ciContentInfoTransformer(CommandInterface $command, Result $result) {
 | 
						|
        $action = $command->getName();
 | 
						|
        if ($action == "ImageInfo" || $action == "ImageExif" || $action == "ImageAve" || $action == "GetPrivateM3U8") {
 | 
						|
            $length = intval($result['ContentLength']);
 | 
						|
            if($length > 0){
 | 
						|
                $result['Data'] = $this->geCiContentInfo($result, $length);
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        if ($action == "PutObject" && isset($command["PicOperations"]) &&  $command["PicOperations"]) {
 | 
						|
            $picOperations = json_decode($command["PicOperations"], true);
 | 
						|
            $picRuleSize = isset($picOperations['rules']) && is_array($picOperations['rules']) ? sizeof($picOperations['rules']) : 0;
 | 
						|
            $length = intval($result['ContentLength']);
 | 
						|
            if($length > 0){
 | 
						|
                $content = $this->geCiContentInfo($result, $length);
 | 
						|
                $obj = simplexml_load_string($content, "SimpleXMLElement", LIBXML_NOCDATA);
 | 
						|
                $xmlData = json_decode(json_encode($obj),true);
 | 
						|
                if ($picRuleSize == 1 && isset($xmlData['ProcessResults']['Object'])){
 | 
						|
                    $tmp = $xmlData['ProcessResults']['Object'];
 | 
						|
                    unset($xmlData['ProcessResults']['Object']);
 | 
						|
                    $xmlData['ProcessResults']['Object'][] = $tmp;
 | 
						|
                }
 | 
						|
                $result['Data'] = $xmlData;
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        if ($action == "GetBucketGuetzli" ) {
 | 
						|
            $length = intval($result['ContentLength']);
 | 
						|
            if($length > 0){
 | 
						|
                $content = $this->geCiContentInfo($result, $length);
 | 
						|
                $obj = simplexml_load_string($content, "SimpleXMLElement", LIBXML_NOCDATA);
 | 
						|
                $arr = json_decode(json_encode($obj),true);
 | 
						|
                $result['GuetzliStatus'] = isset($arr[0]) ? $arr[0] : '';
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        if ($action == "GetCiService" ) {
 | 
						|
            $length = intval($result['ContentLength']);
 | 
						|
            if($length > 0){
 | 
						|
                $content = $this->geCiContentInfo($result, $length);
 | 
						|
                $obj = simplexml_load_string($content, "SimpleXMLElement", LIBXML_NOCDATA);
 | 
						|
                $arr = json_decode(json_encode($obj),true);
 | 
						|
                $result['CIStatus'] = isset($arr[0]) ? $arr[0] : '';
 | 
						|
                unset($result['Body']);
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        if ($action == "GetOriginProtect" ) {
 | 
						|
            $length = intval($result['ContentLength']);
 | 
						|
            if($length > 0){
 | 
						|
                $content = $this->geCiContentInfo($result, $length);
 | 
						|
                $obj = simplexml_load_string($content, "SimpleXMLElement", LIBXML_NOCDATA);
 | 
						|
                $arr = json_decode(json_encode($obj),true);
 | 
						|
                $result['OriginProtectStatus'] = isset($arr[0]) ? $arr[0] : '';
 | 
						|
                unset($result['Body']);
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        if ($action == "GetHotLink" ) {
 | 
						|
            $length = intval($result['ContentLength']);
 | 
						|
            if($length > 0){
 | 
						|
                $content = $this->geCiContentInfo($result, $length);
 | 
						|
                $obj = simplexml_load_string($content, "SimpleXMLElement", LIBXML_NOCDATA);
 | 
						|
                $arr = json |