| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 | <?phpnamespace Upload\Lib\View;use Dever;Dever::apply('sdk/qiniu', 'upload');class Qiniu{    private $client;    private $token;    private $auth;    private $bucket;	public function token($config, $upload, $file = null)    {        $cur = time();        //if (!$config['token'] || ($config['token_endtime'] && $cur > $config['token_endtime'])) {        $this->auth = new \Qiniu\Auth($config['appkey'], $config['appsecret']);        if (true) {            # 暂时没用到callback            $policy = array(                'callbackUrl' => Dever::url('yun.callback?config=' . $config['id'], 'upload'),                'callbackBody' => 'filename=$(fname)&key=$(key)&filesize=$(fsize)&width=$(imageInfo.width)&height=$(imageInfo.height)'            );            $token = $this->auth->uploadToken($upload['bucket'], $file, 3600);            $up['token'] = $token;            $up['token_endtime'] = $cur + 3600 - 60;            $up['where_id'] = $config['id'];            Dever::db('upload/yun')->update($up);        } else {            $token = $config['token'];        }                $domain = 'http://up-z1.qiniup.com/';        return array('qiniu', $token, $domain, $upload['bucket']);    }    public function callback()	{		$body = file_get_contents('php://input');		Dever::log($body, 'qiniu_callback');		$body = json_decode($body, true);		return $body;	}    # 连接    public function connect($config, $upload, $file = null)    {        list($type, $token, $domain, $bucket) = $this->token($config, $upload, $file);        $this->token = $token;        $this->bucket = $bucket;        return $this;    }    # 删除文件    public function delete($file, $bucket = false)    {        $bucket = $bucket ? $bucket : $this->bucket;        $this->client = new \Qiniu\Storage\BucketManager($this->auth);        $result = $this->client->delete($bucket, $file);        return $result;    }    # 下载文件    public function download($bucket, $file, $local = false)    {        $this->client = new \Qiniu\Storage\UploadManager();        if ($local) {            $options = array(                OssClient::OSS_FILE_DOWNLOAD => $local            );        } else {            $options = array();        }                $content = $this->client->getObject($bucket, $file, $options);        return $content;    }    # 上传文件    public function upload($file, $source_file, $options = array(), $base64 = false)    {        if ($base64) {            $method = 'put';            $this->client = new \Qiniu\Storage\UploadManager();            list($ret, $err) = $this->client->$method($this->token, $file, $source_file, $options);        } elseif (strstr($source_file, 'http')) {            $method = 'fetch';            $this->client = new \Qiniu\Storage\BucketManager($this->auth);            $items = $this->client->$method($source_file, $this->bucket, $file);            if (isset($items[0])) {                $ret = $items[0];            }        } else {            $method = 'putFile';            $this->client = new \Qiniu\Storage\UploadManager();            list($ret, $err) = $this->client->$method($this->token, $file, $source_file, $options);        }        return $ret;    }	# 视频转码    public function convert($key, $file, $config, $upload)    {        $accessKey = $config['appkey'];        $secretKey = $config['appsecret'];        $host = $yun['host'];        $auth = new \Qiniu\Auth($accessKey, $secretKey);        $bucket = $upload['bucket'];        # 转码        //转码是使用的队列名称。 https://portal.qiniu.com/mps/pipeline        $pipeline = $upload['pipeline'];        if ($pipeline) {            $array = explode(',', $pipeline);            $index = array_rand($array);            if (isset($array[$index])) {                $pipeline = $array[$index];            }        } else {            $pipeline = 'convert';        }        $ext = 'mp4';        if (strstr($file, '.mp3') || strstr($file, '.wma') || strstr($file, '.wav')) {            $ext = 'mp3';        }                $force = false;        //转码完成后通知到你的业务服务器。        $notifyUrl = 'http://375dec79.ngrok.com/notify.php';        $notifyUrl = false;        $config = new \Qiniu\Config();        //$config->useHTTPS=true;        $pfop = new \Qiniu\Processing\PersistentFop($auth, $config);        //要进行转码的转码操作。 http://developer.qiniu.com/docs/v6/api/reference/fop/av/avthumb.html        //$fops = "avthumb/m3u8/s/640x360/vb/1.4m|saveas/" . \Qiniu\base64_urlSafeEncode($bucket . ":qiniu_640x360.mp4");        $fops = "avthumb/mp4/vb/1.4m|saveas/" . \Qiniu\base64_urlSafeEncode($bucket . ':' . $file . '_c.' . $ext);        list($id, $err) = $pfop->execute($bucket, $file, $fops, $pipeline, $notifyUrl, $force);        /*        echo "\n====> pfop avthumb result: \n";        if ($err != null) {            var_dump($err);        } else {            echo "PersistentFop Id: $id\n";        }        die;        //查询转码的进度和状态        list($ret, $err) = $pfop->status($id);        echo "\n====> pfop avthumb status: \n";        if ($err != null) {            var_dump($err);        } else {            var_dump($ret);        }*/    }    # 视频截图    public function cover($key, $video, $num = 1, $local = 2)    {        $file = $video . '?vframe/jpg/offset/' . $num;        if ($local == 1) {            $data = Dever::load('upload/save')->copy($file, 7, false, false, 'jpg');            return $data['url'] . '?time=' . time();        } else {            return $file;        }                //vframe/jpg/offset/7/w/480/h/360    }}
 |