dever 6 lat temu
rodzic
commit
da1169e0dd
5 zmienionych plików z 349 dodań i 1 usunięć
  1. 10 0
      database/file.php
  2. 39 0
      database/upload.php
  3. 1 1
      src/Save.php
  4. 52 0
      src/Store/Handle.php
  5. 247 0
      src/Store/Qiniu.php

+ 10 - 0
database/file.php

@@ -159,6 +159,16 @@ return array
 			'match' 	=> 'is_string',
 			//'list'		=> true,
 		),
+
+		'yunfile'		=> array
+		(
+			'type' 		=> 'varchar-150',
+			'name' 		=> '云端访问路径',
+			'default' 	=> '',
+			'desc' 		=> '云端访问路径',
+			'match' 	=> 'is_string',
+			//'list'		=> true,
+		),
 		
 		'width'		=> array
 		(

+ 39 - 0
database/upload.php

@@ -17,6 +17,19 @@ $cover = array
 	3 => '不覆盖,直接生成新文件',
 );
 
+$yun = array
+(
+	1 => '七牛云',
+);
+
+$save_type = array
+(
+	1 => '仅本地存储',
+	2 => '本地存储为主,云端存储为辅',
+	3 => '云端存储为主,本地存储为辅',
+	4 => '仅云端存储',
+);
+
 return array
 (
 	# 表名
@@ -116,6 +129,32 @@ return array
 			'update'	=> 'radio',
 			'list'		=> true,
 		),
+
+		'save_type'		=> array
+		(
+			'type' 		=> 'int-1',
+			'name' 		=> '存储位置',
+			'default' 	=> '1',
+			'desc' 		=> '存储位置',
+			'match' 	=> 'is_numeric',
+			'option' 	=> $save_type,
+			'update'	=> 'radio',
+			//'list'		=> true,
+			'control'	=> 'save_type',
+		),
+
+		'yun'		=> array
+		(
+			'type' 		=> 'int-1',
+			'name' 		=> '选择云端',
+			'default' 	=> '1',
+			'desc' 		=> '选择云端',
+			'match' 	=> 'is_numeric',
+			'option' 	=> $yun,
+			'update'	=> 'select',
+			//'list'		=> true,
+			'show'	=> 'save_type=2,3,4',
+		),
 		
 		'state'		=> array
 		(

+ 1 - 1
src/Save.php

@@ -7,7 +7,7 @@
 namespace Upload\Src;
 
 use Dever;
-use Upload\Src\Store\Local as Handle;
+use Upload\Src\Store\Handle;
 
 class Save
 {

+ 52 - 0
src/Store/Handle.php

@@ -0,0 +1,52 @@
+<?php
+namespace Upload\Src\Store;
+
+use Dever;
+
+class Handle
+{
+    /**
+     * __construct
+     * 
+     * @return mixed
+     */
+    public function __construct($data = array())
+    {
+        $config = Dever::db('upload/upload')->one($data['key']);
+
+        $this->save_type = $config['save_type'];
+
+        $this->yun = $config['yun'];
+        if ($this->save_type >= 2) {
+            if ($this->yun == 1) {
+                $yun = 'Qiniu';
+            }
+            
+            $this->yun = new $yun($data);
+        }
+
+        if ($this->save_type <= 3) {
+            $this->local = new Local($data);
+        }
+    }
+
+    public function copy()
+    {
+        $yun = $local = array();
+
+        if ($this->save_type <= 3) {
+            $local = $this->local->copy();
+        }
+
+        if ($this->save_type >= 2) {
+            $yun = $this->yun->copy();
+        }
+        
+
+        if ($this->save_type <= 2) {
+            return $local;
+        } elseif ($this->save_type > 2) {
+            return $yun;
+        }
+    }
+}

+ 247 - 0
src/Store/Qiniu.php

@@ -0,0 +1,247 @@
+<?php
+namespace Upload\Src\Store;
+
+use Dever;
+
+class Qiniu extends Core implements Config
+{
+    /**
+     * 创建文件和目录
+     * 
+     * @return mixed
+     */
+    public function save()
+    {
+        $this->root();
+        
+        $this->path()->name($this->data['file']['name'])->ext()->file();
+        
+        # 不覆盖
+        if (is_file($this->output['file']) && $this->config['cover'] == 2) {
+            $this->output['status'] = -1;
+            $this->output['message'] = '文件已经存在';
+        } else {
+            # 不覆盖 生成新文件
+            if (is_file($this->output['file']) && $this->config['cover'] == 3) {
+                // . microtime() . rand(0,1000)
+                $this->name($this->output['file'] . '_' . microtime() . '_' . rand(0,1000))->file();
+            } else {
+                $file = Dever::load('upload/file-name', array('where_name' => $this->name));
+            }
+
+            if (!$this->limit) {
+                //$this->limit = getimagesize($this->output['file']);
+            }
+
+            if (!$this->size) {
+                $this->size = filesize($this->output['file']);
+            }
+            
+            if (isset($file) && $file) {
+                $this->update($file['id']);
+                
+            } else {
+                $this->insert();
+            }
+            
+            if (!copy($this->data['file']['tmp_name'], $this->output['file'])) {
+                $this->output['status'] = -1;
+                $this->output['message'] = '文件已经存在';
+            } else {
+                unlink($this->data['file']['tmp_name']);
+            }
+
+            @chmod($this->output['file'], 0755);
+            @system('chmod -R 777 ' . $this->output['file']);
+
+            return $this->output['url'];
+        }
+    }
+
+    private function path()
+    {
+        $date = explode('-', date("Y-m-d"));
+
+        $this->path = $this->config['id'] . '/' . $date[0] . '/' . $date[1] . '/' . $date[2] . '/';
+
+        return $this;
+    }
+
+    private function name($name)
+    {
+        $this->name = md5($name);
+
+        return $this;
+    }
+
+    private function ext()
+    {
+        $this->ext  = $this->ext ? $this->ext : '.' . pathinfo($this->data['file']['name'], PATHINFO_EXTENSION);
+
+        return $this;
+    }
+
+    private function file()
+    {
+        $this->file = $this->path . $this->name . $this->ext;
+
+        $this->output['file'] = is_file($this->base . $this->file) ? $this->base . $this->file : Dever::path($this->base, $this->file);
+        $this->output['url'] = Dever::config('host')->uploadRes . $this->file;
+
+        return $this->output['file'];
+    }
+
+    private function img()
+    {
+        $this->img = isset($this->img) ? $this->img : new Img();
+        $this->img->setType('im');
+
+        return $this->img;
+    }
+    
+    /**
+     * water
+     * 
+     * @return mixed
+     */
+    public function handle_w($id)
+    {
+        $config = Dever::load('upload/pic_water-one', $id);
+        if ($config) {
+            $this->name .= '_w' . $id;
+            
+            $water = array('water'=> $config['water'], 'position'=> ($config['water_position'] ? $config['water_position'] : 1));
+            
+            $this->img()->mark($this->output['file'], $water, true, $this->file());
+        }
+    }
+
+    /**
+     * compress
+     * 
+     * @return mixed
+     */
+    public function handle_p($num, $source = false, $dest = false)
+    {
+        if (!$source) {
+            $source = $this->output['file'];
+        }
+
+        if (!$dest) {
+            if (strpos($this->name, '_p') !== false) {
+                $temp = explode('_p', $this->name);
+                $this->name = $temp[0];
+            }
+
+            $file = $this->output['file'];
+            $url = $this->output['url'];
+
+            $this->name .= '_p' . $num;
+            $dest = $this->file();
+
+            $this->output['file'] = $file;
+            $this->output['url'] = $url;
+        }
+
+        $this->img()->setType('im');
+        $this->img()->compress($source, $num, $dest);
+    }
+    
+    /**
+     * webp
+     * 
+     * @return mixed
+     */
+    public function handle_wp($num, $source = false, $dest = false)
+    {
+        if (!$source) {
+            $source = $this->output['file'];
+        }
+
+
+        if (!$dest) {
+            if (strpos($this->name, '_wp') !== false) {
+                $temp = explode('_wp', $this->name);
+                $this->name = $temp[0];
+            }
+
+            $file = $this->output['file'];
+            $url = $this->output['url'];
+
+            $this->ext = '.webp';
+            $this->name .= '_wp' . $num;
+            $dest = $this->file();
+
+            $this->output['file'] = $file;
+            $this->output['url'] = $url;
+        }
+
+        $this->img()->setType('im');
+        $this->img()->webp($source, $num, $dest);
+    }
+
+    /**
+     * thumb
+     * 
+     * @return mixed
+     */
+    public function handle_t($id, $source = false, $dest = false)
+    {
+        $config = Dever::load('upload/pic_thumb-one', $id);
+        if ($config) {
+
+            if (!$source) {
+                $source = $this->output['file'];
+            }
+
+            if (!$dest) {
+                if (strpos($this->name, '_t') !== false) {
+                    $temp = explode('_t', $this->name);
+                    $this->name = $temp[0];
+                }
+                $this->name .= '_t' . $id;
+                $dest = $this->file();
+            }       
+            
+            $size = $config['width'] . '_' . $config['height'] . '_2';
+
+            $this->img()->thumb($source, $size, true, $dest);
+
+            # 同时进行压缩
+            if (isset($config['compress']) && $config['compress'] > 0) {
+                $this->img()->setType('im');
+                $this->img()->compress($dest, $config['compress'], $dest);
+            }
+        }
+    }
+    
+    /**
+     * crop
+     * 
+     * @return mixed
+     */
+    public function handle_c($id, $source = false, $dest = false)
+    {
+        $config = Dever::load('upload/pic_crop-one', $id);
+        
+        if ($config) {
+
+            if (!$source) {
+                $source = $this->output['file'];
+            }
+
+            if (!$dest) {
+                if (strpos($this->name, '_c') !== false) {
+                    $temp = explode('_c', $this->name);
+                    $this->name = $temp[0];
+                }
+                $this->name .= '_c' . $id;
+                $dest = $this->file();
+            }
+            
+            $size = $config['width'] . '_' . $config['height'] . '_2';
+
+            $this->img()->crop($source, $size, false, true, $dest);
+        }
+    }
+}