dever 4 years ago
parent
commit
b21d3a6fc5
7 changed files with 129 additions and 68 deletions
  1. 3 3
      database/file.php
  2. 13 0
      lib/File.php
  3. 18 1
      lib/Store/Core.php
  4. 3 34
      lib/Store/Oss.php
  5. 5 30
      lib/Store/Qiniu.php
  6. 58 0
      lib/View/Oss.php
  7. 29 0
      lib/View/Qiniu.php

+ 3 - 3
database/file.php

@@ -245,13 +245,13 @@ return array
 	'index' => array
 	(
 		# 索引名 => 索引id
-		3 => array
+		4 => array
 		(
 			'search' => '`key`,cate,search',
-			'name' => 'name',
+			'name' => 'name,upload',
 		),
 
-		'version' => 3,
+		'version' => 4,
 	),
 
 	# 更新表结构

+ 13 - 0
lib/File.php

@@ -0,0 +1,13 @@
+<?php
+namespace Upload\Lib;
+
+use Dever;
+
+class File
+{
+	# 获取文件内容
+	public function content($file)
+	{
+		
+	}
+}

+ 18 - 1
lib/Store/Core.php

@@ -421,7 +421,7 @@ class Core
 		# 覆盖原文件 检查文件是否存在
 		$this->path = '';
 		if ($this->config['cover'] == 1) {
-			$info = Dever::db('upload/file')->one(array('name' => $this->name));
+			$info = Dever::db('upload/file')->one(array('name' => $this->name, 'upload' => $this->data['key']));
 			if ($info && $info['file'] && strstr($info['file'], $this->name)) {
 				# 如果存在,直接使用旧文件
 				$temp = explode($this->name, $info['file']);
@@ -458,6 +458,23 @@ class Core
 		return $this;
 	}
 
+	protected function yun()
+	{
+		$yun = $this->config['yun'];
+        $this->host = $yun['host'];
+        $this->data['host']  = $this->host;
+
+        $this->getName();
+
+        $file = $this->file;
+        if ($this->config['cover'] == 3) {
+            $this->name($this->output['file'] . '_' . microtime() . '_' . rand(0,1000))->file();
+            $file = $this->file;
+        }
+
+        return array($yun, $file);
+	}
+
 	/**
 	 * getExt
 	 * 

+ 3 - 34
lib/Store/Oss.php

@@ -2,52 +2,21 @@
 namespace Upload\Lib\Store;
 
 use Dever;
-Dever::apply('sdk/oss', 'upload');
-use OSS\OssClient;
-use OSS\Core\OssException;
 class Oss extends Core implements Config
 {
     public $yun = true;
     /**
-     * 创建文件和目录
+     * 上传文件
      * 
      * @return mixed
      */
     public function save()
     {
-        $config = $this->config['yun'];
-        $accessKey = $config['appkey'];
-        $secretKey = $config['appsecret'];
-        $endpoint = "http://oss-cn-".$config['region_id'].".aliyuncs.com";
-        $bucket = $this->config['bucket'];
-        $this->host = $config['host'];
-        $this->data['host']  = $this->host;
-        list($type, $token, $domain, $bucket) = Dever::load('upload/lib/view/oss')->token($config, $this->config);
-        $data = explode('||', $token);
-        if ($data[0]) {
-            $token = $data[0];
-            $accessKey = $data[1];
-            $secretKey = $data[2];
-        } else {
-            $token = false;
-        }
-        $ossClient = new OssClient($accessKey, $secretKey, $endpoint, false, $token);
-
-        $this->getName();
-
-        $file = $this->file;
-        if ($this->config['cover'] == 3) {
-            $this->name($this->output['file'] . '_' . microtime() . '_' . rand(0,1000))->file();
-            $file = $this->file;
-        }
+        list($yun, $file) = $this->yun();
 
         $options = array();
 
-        if ($this->base64) {
-            $ret = $ossClient->putObject($bucket, $file, $this->data['file']['tmp_name'], $options);
-        } else {
-            $ret = $ossClient->uploadFile($bucket, $file, $this->data['file']['tmp_name']);
-        }
+        $ret = Dever::load('upload/lib/view/oss')->connect($yun, $this->config)->upload($this->config['bucket'], $file, $this->data['file']['tmp_name'], $options, $this->base64);
 
         if (isset($ret['info']['url'])) {
 

+ 5 - 30
lib/Store/Qiniu.php

@@ -8,42 +8,17 @@ class Qiniu extends Core implements Config
 {
     public $yun = true;
     /**
-     * 创建文件和目录
+     * 上传文件
      * 
      * @return mixed
      */
     public function save()
     {
-        $config = $this->config['yun'];
-        $accessKey = $config['appkey'];
-        $secretKey = $config['appsecret'];
-        $this->host = $config['host'];
-        $auth = new \Qiniu\Auth($accessKey, $secretKey);
-        $bucket = $this->config['bucket'];
-
-        $policy = array(
-            'callbackUrl' => 'http://your.domain.com/upload_verify_callback.php',
-            'callbackBody' => 'filename=$(fname)&filesize=$(fsize)'
-        );
-
-        // 生成上传Token
-        $token = $auth->uploadToken($bucket, null, 3600);
-        // 构建 UploadManager 对象
-        $upload = new \Qiniu\Storage\UploadManager();
-
-        $this->getName();
-
-        $file = $this->file;
-        if ($this->config['cover'] == 3) {
-            $this->name($this->output['file'] . '_' . microtime() . '_' . rand(0,1000))->file();
-            $file = $this->file;
-        }
+        list($yun, $file) = $this->yun();
 
-        if ($this->base64) {
-            list($ret, $err) = $upload->put($token, $file, $this->data['file']['tmp_name'], $param = null);
-        } else {
-            list($ret, $err) = $upload->putFile($token, $file, $this->data['file']['tmp_name'], $param = null);
-        }
+        $options = array();
+
+        $ret = Dever::load('upload/lib/view/qiniu')->connect($yun, $this->config)->upload($file, $this->data['file']['tmp_name'], $options, $this->base64);
 
         if (isset($ret['hash']) && isset($ret['key'])) {
 

+ 58 - 0
lib/View/Oss.php

@@ -11,6 +11,8 @@ use AlibabaCloud\Client\Exception\ClientException;
 use AlibabaCloud\Client\Exception\ServerException;
 class Oss
 {
+    private $client;
+
     public function token($config, $upload)
     {
         $region = 'cn-' . $config['region_id'];
@@ -66,6 +68,62 @@ class Oss
         return array('oss', $token, $region, $upload['bucket']);
     }
 
+    # 连接
+    public function connect($config, $upload)
+    {
+        $accessKey = $config['appkey'];
+        $secretKey = $config['appsecret'];
+        $endpoint = "http://oss-cn-".$config['region_id'].".aliyuncs.com";
+
+        list($type, $token, $domain, $bucket) = $this->token($config, $upload);
+        $data = explode('||', $token);
+        if ($data[0]) {
+            $token = $data[0];
+            $accessKey = $data[1];
+            $secretKey = $data[2];
+        } else {
+            $token = false;
+        }
+
+        $this->client = new OssClient($accessKey, $secretKey, $endpoint, false, $token);
+        return $this;
+    }
+
+    # 上传文件
+    public function upload($bucket, $file, $source_file, $options = array(), $base64 = false)
+    {
+        if (!$this->client) {
+            return array();
+        }
+        $method = 'uploadFile';
+        if ($base64) {
+            $method = 'putObject';
+        }
+
+        $ret = $this->client->$method($bucket, $file, $source_file, $options);
+
+        return $ret;
+    }
+
+    # 下载文件
+    public function download($bucket, $file, $local = false)
+    {
+        if (!$this->client) {
+            return false;
+        }
+        if ($local) {
+            $options = array(
+                OssClient::OSS_FILE_DOWNLOAD => $local
+            );
+        } else {
+            $options = array();
+        }
+        
+        $content = $this->client->getObject($bucket, $file, $options);
+
+        return $content;
+    }
+
     public function callback()
   	{
   		$body = file_get_contents('php://input');

+ 29 - 0
lib/View/Qiniu.php

@@ -6,6 +6,9 @@ Dever::apply('sdk/qiniu', 'upload');
 
 class Qiniu
 {
+    private $client;
+    private $token;
+
 	public function token($config, $upload)
     {
         $cur = time();
@@ -41,6 +44,32 @@ class Qiniu
 		return $body;
 	}
 
+    # 连接
+    public function connect($config, $upload)
+    {
+        list($type, $token, $domain, $bucket) = $this->token($config, $upload);
+        $this->token = $token;
+        // 构建 UploadManager 对象
+        $this->client = new \Qiniu\Storage\UploadManager();
+        return $this;
+    }
+
+    # 上传文件
+    public function upload($file, $source_file, $options = array(), $base64 = false)
+    {
+        if (!$this->client) {
+            return array();
+        }
+        $method = 'putFile';
+        if ($base64) {
+            $method = 'put';
+        }
+
+        list($ret, $err) = $this->client->$method($this->token, $file, $source_file, $options);
+
+        return $ret;
+    }
+
 	# 视频转码
     public function convert($key, $file, $config, $upload)
     {