dever 3 years ago
parent
commit
a7e1bd89d3
6 changed files with 325 additions and 2 deletions
  1. 24 2
      database/yun.php
  2. 12 0
      lib/Sts.php
  3. 59 0
      lib/View/Oss.php
  4. 91 0
      lib/View/Qiniu.php
  5. 6 0
      src/Save.php
  6. 133 0
      src/Yun.php

+ 24 - 2
database/yun.php

@@ -44,13 +44,14 @@ return array
         'type'     => array
         (
             'type'      => 'int-1',
-            'name'      => '存储类别',
+            'name'      => '存储类别-oss建议申请sts保证安全性,参考https://help.aliyun.com/document_detail/100624.html',
             'default'   => '2',
             'desc'      => '存储类别',
             'match'     => 'is_numeric',
             'option'    => $type,
             'update'    => 'radio',
-            'list'        => true,
+            'list'      => true,
+            'control'   => 'type',
         ),
 
         'host'      => array
@@ -88,6 +89,27 @@ return array
             'search'    => 'fulltext',
             'list'      => true,
         ),
+
+        'region_id'      => array
+        (
+            'type'      => 'varchar-200',
+            'name'      => '区域id-oss直接填RegionId即可,如beijing,七牛可填写z1',
+            'default'   => '',
+            'desc'      => 'appsecret',
+            'match'     => 'is_string',
+            'update'    => 'text',
+        ),
+
+        'role_arn'      => array
+        (
+            'type'      => 'varchar-200',
+            'name'      => '权限策略ARN-oss需要填写,如未填写,系统默认使用非sts形式上传至oss,不能保证安全性',
+            'default'   => '',
+            'desc'      => 'appsecret',
+            'match'     => 'option',
+            'update'    => 'text',
+            'show'      => 'type=1',
+        ),
         
         'state'     => array
         (

+ 12 - 0
lib/Sts.php

@@ -0,0 +1,12 @@
+<?php
+namespace Upload\Lib;
+
+use Dever;
+
+class Sts
+{
+	public function import_api()
+	{
+		
+	}
+}

+ 59 - 0
lib/View/Oss.php

@@ -0,0 +1,59 @@
+<?php
+namespace Upload\Lib\View;
+
+use Dever;
+Dever::apply('sdk/oss', 'upload');
+Dever::apply('vendor/autoload', 'alibaba');
+use OSS\OssClient;
+use OSS\Core\OssException;
+use AlibabaCloud\Client\AlibabaCloud;
+use AlibabaCloud\Client\Exception\ClientException;
+use AlibabaCloud\Client\Exception\ServerException;
+class Oss
+{
+	public function token($config, $upload)
+    {
+    	//构建一个阿里云客户端,用于发起请求。
+		//构建阿里云客户端时需要设置AccessKey ID和AccessKey Secret。
+		AlibabaCloud::accessKeyClient($config['appkey'], $config['appsecret'])->regionId('cn-beijing')->asDefaultClient();
+		//设置参数,发起请求。
+		try {
+		    $result = AlibabaCloud::rpc()
+              ->product('Sts')
+              ->scheme('https') // https | http
+              ->version('2015-04-01')
+              ->action('AssumeRole')
+              ->method('POST')
+              ->host('sts.aliyuncs.com')
+              ->options([
+                            'query' => [
+                              'RegionId' => "cn-beijing",
+                              'RoleArn' => "acs:ram::1118875946432366:role/api",
+                              'RoleSessionName' => "upload",
+                            ],
+                        ])
+              ->request();
+		    print_r($result->toArray());die;
+		} catch (ClientException $e) {
+		    echo $e->getErrorMessage() . PHP_EOL;die;
+		} catch (ServerException $e) {
+		    echo $e->getErrorMessage() . PHP_EOL;die;
+		}
+        return array('oss', '', '');
+    }
+
+    public function callback()
+	{
+		$body = file_get_contents('php://input');
+		Dever::log($body, 'oss_callback');
+		$body = json_decode($body, true);
+
+		return $body;
+	}
+
+	# 视频转码
+    public function convert($key, $file, $config, $upload)
+    {
+        
+    }
+}

+ 91 - 0
lib/View/Qiniu.php

@@ -0,0 +1,91 @@
+<?php
+namespace Upload\Lib\View;
+
+use Dever;
+Dever::apply('sdk/qiniu', 'upload');
+
+class Qiniu
+{
+	public function token($config, $upload)
+    {
+        $auth = new \Qiniu\Auth($config['appkey'], $config['appsecret']);
+
+        # 暂时没用到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 = $auth->uploadToken($upload['bucket'], null, 3600);
+        $domain = 'http://up-z1.qiniup.com/';
+
+        return array('qiniu', $token, $domain);
+    }
+
+    public function callback()
+	{
+		$body = file_get_contents('php://input');
+		Dever::log($body, 'qiniu_callback');
+		$body = json_decode($body, true);
+
+		return $body;
+	}
+
+	# 视频转码
+    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);
+        }*/
+    }
+}

+ 6 - 0
src/Save.php

@@ -39,6 +39,9 @@ class Save
 		$image['file'] 	= $files ? $files : Dever::input('file', $file);
 		$image['key'] 	= Dever::input('key', $key);
 		$image['search'] = Dever::input('search', $search);
+		if ($image['search'] == 'undefined') {
+			$image['search'] = '';
+		}
 		$search = Dever::input('search_key', $search_key);
 		if ($search && $search > 0) {
 			$image['search'] .= '_' . $search;
@@ -66,6 +69,9 @@ class Save
 
 	public function copy($file = false, $key = 1, $state = false, $search = '')
 	{
+		if ($search == 'undefined') {
+			$search = '';
+		}
 		if (Dever::input('file') && $state == false) {
 			$this->upload();
 			Dever::outDiy($this->output);

+ 133 - 0
src/Yun.php

@@ -0,0 +1,133 @@
+<?php
+/*
+|--------------------------------------------------------------------------
+| 处理云端图片的一些常用方法
+|--------------------------------------------------------------------------
+*/
+namespace Upload\Src;
+
+use Dever;
+
+class Yun
+{
+	private function getMethod($type)
+	{
+		if ($type == 1) {
+        	$method = 'oss';
+        } else {
+        	$method = 'qiniu';
+        }
+		return Dever::load('upload/lib/view/' . $method);
+	}
+
+	public function token()
+    {
+        $return = Dever::input('return', 2);
+        $key = Dever::input('key');
+        $upload = Dever::db('upload/upload')->one($key);
+        if (!$upload) {
+            return;
+        }
+        
+        if (!$upload['yun']) {
+            return;
+        }
+        $config = Dever::db('upload/yun')->one($upload['yun']);
+        if (!$config) {
+            return;
+        }
+        
+        list($type, $token, $domain) = $this->getMethod($config['type'])->token($config, $upload);
+        $result = array('type' => $type, 'uptoken' => $token, 'domain' => $domain, 'host' => $config['host']);
+        if ($return == 1) {
+            return $result;
+        }
+        echo json_encode($result);die;
+    }
+
+	public function callback()
+	{
+		$config = Dever::input('config');
+		if (!$config) {
+			return 'error';
+		}
+		$config = Dever::db('upload/yun')->one($config);
+		if (!$config) {
+			return 'error';
+		}
+		$body = $this->getMethod($config['type'])->callback();
+		$file = $body['filename'];
+		$key = $body['key'];
+		$size = $body['filesize'];
+		$width = $body['width'];
+		$height = $body['height'];
+
+        $upload = Dever::db('upload/upload')->one($key);
+        if ($upload) {
+            $this->initFile($key, $file, $file, $width, $height, $size);
+        }
+		return 'ok';
+	}
+
+    private function initFile($key, $file, $source, $width = false, $height = false, $size = false, $search = '')
+    {
+        $temp = explode('/', $file);
+        $name = $temp[count($temp) - 1];
+        $temp = explode('.', $name);
+        $name = $temp[0];
+        $ext = $temp[count($temp) - 1];
+
+        $info = Dever::load('upload/file-name', array('where_name' => $name, 'where_upload' => $key));
+
+        $data['name'] = $name;
+        $data['file'] = $file;
+        $data['key'] = md5($file);
+        $data['ext'] = '.' . $ext;
+
+        if ($width) {
+            $data['width'] = $width;
+        }
+
+        if ($height) {
+            $data['height'] = $height;
+        }
+
+        if ($size) {
+            $data['size'] = $size;
+        }
+        $data['upload'] = $key;
+        if ($search) {
+            $data['search'] = $search;
+        }
+        
+        if ($info) {
+            $data['where_id'] = $info['id'];
+            Dever::db('upload/file')->update($data);
+        } else {
+            $data['source_name'] = $source;
+            Dever::db('upload/file')->insert($data);
+        }
+    }
+
+    # 上传文件之后同步到本地数据库
+    public function addFile()
+    {
+        $file = Dever::input('file');
+        $key = Dever::input('key');
+        $source = Dever::input('source');
+        $search = Dever::input('search');
+
+        $upload = Dever::db('upload/upload')->one($key);
+
+        if ($upload) {
+
+            if ($upload['yun'] > 0 && $upload['vod_convert'] == 2) {
+                $config = Dever::db('upload/yun')->one($upload['yun']);
+                if ($config) {
+                    $this->getMethod($method)->convert($key, $file, $config, $upload);
+                }
+            }
+            $this->initFile($key, $file, $source, false, false, false, $search);
+        }
+    }
+}