dever 2 years ago
parent
commit
5b8c70f653
4 changed files with 40 additions and 18 deletions
  1. 16 5
      lib/Store/Core.php
  2. 4 0
      lib/Store/Oss.php
  3. 19 13
      lib/View/Qiniu.php
  4. 1 0
      sdk/qiniu.php

+ 16 - 5
lib/Store/Core.php

@@ -1,6 +1,6 @@
 <?php
 namespace Upload\Lib\Store;
-
+error_reporting(7);
 use Dever;
 use Dever\String\Helper as Helper;
 use Dever\Loader\Config;
@@ -164,7 +164,7 @@ class Core
 	private function check_size()
 	{
 		if (!$this->limit && !$this->base64 && ($this->ext == '.jpg' || $this->ext == '.gif' || $this->ext == '.png') && is_file($this->data['file']['tmp_name'])) {
-			$this->limit = getimagesize($this->data['file']['tmp_name']);
+			$this->limit = @getimagesize($this->data['file']['tmp_name']);
 		}
 
 		# 默认30M
@@ -225,7 +225,7 @@ class Core
 			}
 
 			$this->data['file'] = array();
-			if (((isset($this->data['cropper']) && !$this->yun) || !isset($this->data['cropper'])) && strstr($name, 'http')) {
+			if (((isset($this->data['cropper']) && !$this->yun) || !isset($this->data['cropper'])) && strstr($name, 'http') && !$this->yun) {
 				$this->base64 = false;
 				$this->root();
 				//header('Content-type: text/json; charset=utf-8');
@@ -262,9 +262,17 @@ class Core
 					if (!isset($file)) {
 						Dever::alert('错误的文件来源');
 					}
-					file_put_contents($this->data['file']['tmp_name'], $file);
+					$state = file_put_contents($this->data['file']['tmp_name'], $file);
+					if (!$state) {
+						$this->delete();
+						return $this->output;
+					}
 				}
 				$this->data['file']['size'] = filesize($this->data['file']['tmp_name']);
+				if ($this->data['file']['size'] <= 0) {
+					$this->delete();
+					return $this->output;
+				}
 			} elseif (isset($file)) {
 				$this->data['file']['name'] = $name;
 				$this->data['file']['tmp_name'] = $file;
@@ -505,7 +513,10 @@ class Core
 	 */
 	protected function getExt($filename)
 	{
-		if (isset($this->data['file']['type'])) {
+		if (strstr($filename, 'http')) {
+			$temp = explode('.', $filename);
+			$ext = $temp[count($temp)-1];
+		} elseif (isset($this->data['file']['type'])) {
 			$ext = $this->getExtByMine($this->data['file']['type']);
 		} elseif (function_exists('finfo_open')) {
 			$finfo = finfo_open(FILEINFO_MIME); // 返回 mime 类型

+ 4 - 0
lib/Store/Oss.php

@@ -14,6 +14,10 @@ class Oss extends Core implements Config
     {
         list($yun, $file) = $this->yun();
 
+        if (!$this->base64 && !is_file($this->data['file']['tmp_name'])) {
+            return;
+        }
+
         $options = array();
 
         $ret = Dever::load('upload/lib/view/oss')->connect($yun, $this->config, $file)->upload($this->config['bucket'], $file, $this->data['file']['tmp_name'], $options, $this->base64);

+ 19 - 13
lib/View/Qiniu.php

@@ -8,13 +8,15 @@ 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) {
-            $auth = new \Qiniu\Auth($config['appkey'], $config['appsecret']);
 
             # 暂时没用到callback
             $policy = array(
@@ -22,7 +24,7 @@ class Qiniu
                 'callbackBody' => 'filename=$(fname)&key=$(key)&filesize=$(fsize)&width=$(imageInfo.width)&height=$(imageInfo.height)'
             );
 
-            $token = $auth->uploadToken($upload['bucket'], $file, 3600);
+            $token = $this->auth->uploadToken($upload['bucket'], $file, 3600);
             $up['token'] = $token;
             $up['token_endtime'] = $cur + 3600 - 60;
             $up['where_id'] = $config['id'];
@@ -50,17 +52,14 @@ class Qiniu
     {
         list($type, $token, $domain, $bucket) = $this->token($config, $upload, $file);
         $this->token = $token;
-        // 构建 UploadManager 对象
-        $this->client = new \Qiniu\Storage\UploadManager();
+        $this->bucket = $bucket;
         return $this;
     }
 
     # 下载文件
     public function download($bucket, $file, $local = false)
     {
-        if (!$this->client) {
-            return false;
-        }
+        $this->client = new \Qiniu\Storage\UploadManager();
         if ($local) {
             $options = array(
                 OssClient::OSS_FILE_DOWNLOAD => $local
@@ -77,16 +76,23 @@ class Qiniu
     # 上传文件
     public function upload($file, $source_file, $options = array(), $base64 = false)
     {
-        if (!$this->client) {
-            return array();
-        }
-        $method = 'putFile';
         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);
         }
 
-        list($ret, $err) = $this->client->$method($this->token, $file, $source_file, $options);
-
         return $ret;
     }
 

+ 1 - 0
sdk/qiniu.php

@@ -17,6 +17,7 @@ Dever::apply('sdk/Qiniu/Storage/ResumeUploader', 'upload');
 Dever::apply('sdk/Qiniu/Storage/FormUploader', 'upload');
 
 
+Dever::apply('sdk/Qiniu/Storage/BucketManager', 'upload');
 Dever::apply('sdk/Qiniu/Storage/UploadManager', 'upload');
 
 Dever::apply('sdk/Qiniu/Processing/Operation', 'upload');