dever 3 years ago
parent
commit
3e52ed4dc1
5 changed files with 64 additions and 25 deletions
  1. 25 17
      lib/Store/Core.php
  2. 9 5
      lib/Store/Oss.php
  3. 11 3
      lib/Store/Qiniu.php
  4. 9 0
      lib/View/Oss.php
  5. 10 0
      lib/View/Qiniu.php

+ 25 - 17
lib/Store/Core.php

@@ -163,35 +163,41 @@ 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']);
+		if (!$this->limit && !$this->base64 && ($this->ext == '.jpg' || $this->ext == '.gif' || $this->ext == '.png') && !strstr($this->data['file']['tmp_name'], 'http')) {
+			$this->limit = getimagesize($this->data['file']['tmp_name']);
 		}
 
+		if (isset($this->data['file']['size'])) {
+			$this->size = $this->data['file']['size'];
+			$this->limit();
+		}
+	}
+
+	protected function limit()
+	{
 		# 默认30M
 		$size = $this->config['size'] > 0 ? 1024*1024*$this->config['size'] : 30*1024*1024;
 
-		if (isset($this->data['file']['size'])) {
-			$this->size = $this->data['file']['size'];
-		
-			if (($size < $this->data['file']['size']) && $size > 0) {
-				$this->output['status'] = -1;
-				$this->output['message'] = '文件不能超过'.(($size/1024)/1024).'MB';
-			} elseif ($this->limit && isset($this->config['setwh']) && $this->config['setwh'] == 2 && $this->limit[0] < $this->limit[1]) {
+		if (($size < $this->size) && $size > 0) {
+			$this->output['status'] = -1;
+			$this->output['message'] = '文件不能超过'.$this->config['size'].'MB';
+		} elseif ($this->limit) {
+			if (isset($this->config['setwh']) && $this->config['setwh'] == 2 && $this->limit[0] < $this->limit[1]) {
 				$this->output['status'] = -1;
 				$this->output['message'] = '图片高度不能超过图片宽度';
-			} elseif ($this->limit && isset($this->config['setwh']) &&  $this->config['setwh'] == 3 && $this->limit[0] > $this->limit[1]) {
+			} elseif (isset($this->config['setwh']) &&  $this->config['setwh'] == 3 && $this->limit[0] > $this->limit[1]) {
 				$this->output['status'] = -1;
 				$this->output['message'] = '图片宽度不能超过图片高度';
-			} elseif ($this->limit && $this->config['width'] > 0 && $this->config['width'] < $this->limit[0]) {
+			} elseif ($this->config['width'] > 0 && $this->config['width'] > $this->limit[0]) {
 				$this->output['status'] = -1;
-				$this->output['message'] = '图片宽度不能超过' . $this->config['width'] . 'px';
-			} elseif ($this->limit && $this->config['height'] > 0 && $this->config['height'] < $this->limit[1]) {
+				$this->output['message'] = '图片宽度不能小于' . $this->config['width'] . 'px';
+			} elseif ($this->config['height'] > 0 && $this->config['height'] > $this->limit[1]) {
 				$this->output['status'] = -1;
-				$this->output['message'] = '图片高度不能超过' . $this->config['height'] . 'px';
+				$this->output['message'] = '图片高度不能小于' . $this->config['height'] . 'px';
 			}
-
-			$this->size = number_format($this->size/1024, 2);
 		}
+
+		$this->size = number_format($this->size/1024, 2);
 	}
 	
 	/**
@@ -260,7 +266,9 @@ class Core
 						$this->ext = '.jpg';
 					}
 					if (!isset($file)) {
-						Dever::alert('错误的文件来源');
+						$this->output['status'] = -1;
+						$this->output['message'] = '错误的文件来源';
+						return $this->output;
 					}
 					$state = file_put_contents($this->data['file']['tmp_name'], $file);
 					if (!$state) {

+ 9 - 5
lib/Store/Oss.php

@@ -14,13 +14,11 @@ 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);
+        $method = Dever::load('upload/lib/view/oss')->connect($yun, $this->config, $file);
+
+        $ret = $method->upload($this->config['bucket'], $file, $this->data['file']['tmp_name'], $options, $this->base64);
 
         if (isset($ret['info']['url'])) {
 
@@ -35,6 +33,12 @@ class Oss extends Core implements Config
                         $this->limit = array($data['ImageWidth']['value'], $data['ImageHeight']['value']);
                         $this->size = $data['FileSize']['value'];
                         $this->ext = '.' . $data['Format']['value'];
+
+                        $this->limit();
+                        if ($this->output['status'] == -1) {
+                            $method->delete($this->bucket, $this->file);
+                            return $this->output;
+                        }
                     }
                 }
             }

+ 11 - 3
lib/Store/Qiniu.php

@@ -18,13 +18,13 @@ class Qiniu extends Core implements Config
 
         $options = array();
 
-        $ret = Dever::load('upload/lib/view/qiniu')->connect($yun, $this->config, $file)->upload($file, $this->data['file']['tmp_name'], $options, $this->base64);
+        $method = Dever::load('upload/lib/view/qiniu')->connect($yun, $this->config, $file);
+
+        $ret = $method->upload($file, $this->data['file']['tmp_name'], $options, $this->base64);
 
         if (isset($ret['hash']) && isset($ret['key'])) {
 
             $this->file = $ret['key'];
-            $file = Dever::load('upload/file-name', array('where_name' => $this->name, 'where_upload' => $this->data['key'], 'rand' => time()));
-            
             if (!$this->limit) {
                 $data = Dever::curl($this->output['url'] . '?imageInfo');
                 if ($data) {
@@ -33,9 +33,17 @@ class Qiniu extends Core implements Config
                         $this->limit = array($data['width'], $data['height']);
                         $this->size = $data['size'];
                         $this->ext = '.' . $data['format'];
+
+                        $this->limit();
+                        if ($this->output['status'] == -1) {
+                            $method->delete($this->file);
+                            return $this->output;
+                        }
                     }
                 }
             }
+
+            $file = Dever::load('upload/file-name', array('where_name' => $this->name, 'where_upload' => $this->data['key'], 'clear' => true));
             
             if (isset($file) && $file) {
                 $this->update($file['id']);

+ 9 - 0
lib/View/Oss.php

@@ -105,6 +105,15 @@ class Oss
         return $ret;
     }
 
+    # 删除文件
+    public function delete($bucket, $file)
+    {
+        $this->client = new \Qiniu\Storage\BucketManager($this->auth);
+        $result = $this->client->delete($bucket, $file);
+
+        return $result;
+    }
+
     # 下载文件
     public function download($bucket, $file, $local = false)
     {

+ 10 - 0
lib/View/Qiniu.php

@@ -56,6 +56,16 @@ class Qiniu
         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)
     {