rabin 6 months ago
parent
commit
3366e2b24e
7 changed files with 148 additions and 38 deletions
  1. 7 0
      api/Manage.php
  2. 1 1
      api/Save.php
  3. 71 13
      lib/Save.php
  4. 11 1
      lib/Tool/Oss.php
  5. 43 22
      lib/Tool/Qiniu.php
  6. 8 1
      table/manage/rule.php
  7. 7 0
      table/rule.php

+ 7 - 0
api/Manage.php

@@ -32,4 +32,11 @@ class Manage extends Auth
         $file = $file[0];
         return Dever::db('file', 'upload')->update(array('rule_id' => $id, 'name' => $file), array('status' => 2));
     }
+
+    # 添加文件
+    public function addFile($id, $url, $file, $source, $name, $size)
+    {
+        $data = Dever::load('save', 'upload')->init($id)->addFile($url, $source, $name, $file, $size);
+        return $data;
+    }
 }

+ 1 - 1
api/Save.php

@@ -17,7 +17,7 @@ class Save
         }
         Dever::config('setting', array('output' => array
         (
-            'status' => array('errno' => array('1' => '0', '2' => '1')),
+            'status' => array('errno', array('1' => '0', '2' => '1')),
             'msg' => 'message',
         )));
         return $this->act($id, $file);

+ 71 - 13
lib/Save.php

@@ -15,7 +15,7 @@ class Save
         8 => 'exe,msi',
     );
     private $type = 1;
-    private function init($id)
+    public function init($id)
     {
         $this->config = Dever::db('rule', 'upload')->find($id);
         if (!$this->config) {
@@ -25,6 +25,37 @@ class Save
         if (!$this->config['save']) {
             Dever::error('存储位置错误');
         }
+        return $this;
+    }
+
+    public function get($id)
+    {
+        $this->init($id);
+        $result = array();
+        $result['id'] = $id;
+        $result['chunkSize'] = $this->config['chunk'];
+        $result['path'] = $id;
+        $result['size'] = $this->config['size'];
+        $result['accept'] = '';
+        $result['mine'] = array();
+        $mine = $this->getExtByMine(false, true);
+        $type = explode(',', $this->config['type']);
+        foreach ($type as $v) {
+            if (isset($this->ext[$v])) {
+                $result['accept'] .= '.' . str_replace(',', ',.', $this->ext[$v]);
+                $temp = explode(',', $this->ext[$v]);
+                foreach ($temp as $v2) {
+                    if (isset($mine[$v2])) {
+                        $result['mine'][] = $mine[$v2];
+                    }
+                }
+            }
+        }
+        $result['type'] = $this->config['save']['type'];
+        if ($result['type'] > 1) {
+            $result += Dever::load('tool', 'upload')->get($this->config['save'])->getInfo();
+        }
+        return $result;
     }
 
     public function act($id, $source, $default_ext = '', $uid = false)
@@ -45,6 +76,7 @@ class Save
             }
             $total = Dever::input('chunks');
             if ($total > 1) {
+                $chunk['size'] = $this->config['chunk'];
                 $chunk['uid'] = Dever::input('uid');
                 $chunk['timestamp'] = Dever::input('timestamp');
                 $chunk['cur'] = Dever::input('chunk');
@@ -121,25 +153,45 @@ class Save
         $dest = $this->config['id'] . '/' . $this->getDest($name, $ext, $uid);
         # type 1是文件复制 2是base64 3是远程文件复制
         $url = Dever::load('tool', 'upload')->get($this->config['save'])->upload($type, $source, $dest, $chunk, $this);
+        $data = $this->up($source_name, $name, $dest, $this->config['size'], $this->config['width'] ?? 0, $this->config['height'] ?? 0);
+        $data['url'] = $url;
+        $data['type'] = $this->type;
+        if (isset($content)) {
+            @unlink($source);
+        }
+        return $data;
+    }
+
+    public function addFile($url, $source_name, $name, $dest, $size)
+    {
+        if ($this->config['save']['type'] > 1) {
+            $url = Dever::load('tool', 'upload')->get($this->config['save'])->getUrl(3, $url, $this);
+            if (isset($this->config['size']) && $this->config['size']) {
+                $size = $this->config['size'];
+            }
+        }
+        
+        $data = $this->up($source_name, $name, $dest, $size, $this->config['width'] ?? 0, $this->config['height'] ?? 0);
+        $data['url'] = $url;
+        return $data;
+    }
+
+    private function up($source_name, $name, $dest, $size, $width = 0, $height = 0)
+    {
         $file['rule_id'] = $this->config['id'];
         $file['name'] = $name;
         $data = $file;
         $data['source_name'] = $source_name;
         $data['file'] = $dest;
         $data['save_id'] = $this->config['save_id'];
-        $data['size'] = $this->config['size'];
-        if (isset($this->config['width'])) {
-            $data['width'] = $this->config['width'];
+        $data['size'] = $size;
+        if ($width) {
+            $data['width'] = $width;
         }
-        if (isset($this->config['height'])) {
-            $data['height'] = $this->config['height'];
+        if ($height) {
+            $data['height'] = $height;
         }
         $data['id'] = Dever::db('file', 'upload')->up($file, $data);
-        $data['url'] = $url;
-        $data['type'] = $this->type;
-        if (isset($content)) {
-            @unlink($source);
-        }
         return $data;
     }
 
@@ -258,9 +310,8 @@ class Save
         return $dest;
     }
 
-    protected function getExtByMine($mine)
+    protected function getExtByMine($mine, $flip = false, $result = false)
     {
-        $mine = trim($mine);
         $config = array
         (
             'application/envoy' => 'evy',
@@ -415,6 +466,13 @@ class Save
             'image/webp' => 'webp',
         );
 
+        if ($flip) {
+            $config = array_flip($config);
+        }
+        if (!$mine) {
+            return $config;
+        }
+        $mine = trim($mine);
         if (isset($config[$mine])) {
             return $config[$mine];
         } else {

+ 11 - 1
lib/Tool/Oss.php

@@ -12,10 +12,20 @@ class Oss
         $this->auth = new \Qiniu\Auth($config['appkey'], $config['appsecret']);
         $this->token = $this->auth->uploadToken($config['bucket'], null, 3600);
         $this->bucket = $config['bucket'];
-
         $this->client = new OssClient($accessKey, $secretKey, $endpoint, false, $token);
     }
 
+    # 获取基本信息
+    public function getInfo()
+    {
+        return array
+        (
+            'token' => $this->token,
+            'bucket' => $this->bucket,
+            'host' => $this->host,
+        );
+    }
+
     # 查看文件
     public function view($file)
     {

+ 43 - 22
lib/Tool/Qiniu.php

@@ -15,6 +15,17 @@ class Qiniu
         $this->host = $config['host'];
     }
 
+    # 获取基本信息
+    public function getInfo()
+    {
+        return array
+        (
+            'token' => $this->token,
+            'bucket' => $this->bucket,
+            'host' => $this->host,
+        );
+    }
+
     # 查看文件
     public function view($file)
     {
@@ -28,7 +39,12 @@ class Qiniu
         $options = array();
         if ($type == 1) {
             $client = new \Qiniu\Storage\UploadManager();
-            list($result, $err) = $client->putFile($this->token, $dest, $source, $options);
+            if ($chunk) {
+                $log = Dever::file('upload/log');
+                list($result, $err) = $client->putFile($this->token, $dest, $source, $options, 'application/octet-stream', false, $log, 'v2', $chunk['size'] * 1024 * 1024);
+            } else {
+                list($result, $err) = $client->putFile($this->token, $dest, $source, $options);
+            }
         } elseif ($type == 2) {
             $client = new \Qiniu\Storage\UploadManager();
             list($result, $err) = $client->put($this->token, $dest, $source, $options);
@@ -44,34 +60,39 @@ class Qiniu
         if (isset($result['hash']) && isset($result['key'])) {
             $file = $result['key'];
             $url = $this->host . $file;
-            if ($type == 3) {
-                $image = Dever::curl($url . '?imageInfo')->result();
-                if ($image && !strstr($image, 'unsupported format')) {
-                    $image = Dever::json_decode($image);
-                    if (isset($image['width'])) {
-                        $state = $upload->check($image['format'], $image['size'], array($image['width'], $image['height']));
-                        if (is_string($state)) {
-                            $this->delete($file);
-                            Dever::error($state);
-                        }
-                    } else {
+            return $this->getUrl($type, $url, $upload);
+        }
+        Dever::error('上传失败');
+    }
+
+    public function getUrl($type, $url, $upload)
+    {
+        if ($type == 3) {
+            $image = Dever::curl($url . '?imageInfo')->result();
+            if ($image && !strstr($image, 'unsupported format')) {
+                $image = Dever::json_decode($image);
+                if (isset($image['width'])) {
+                    $state = $upload->check($image['format'], $image['size'], array($image['width'], $image['height']));
+                    if (is_string($state)) {
                         $this->delete($file);
-                        Dever::error('上传失败');
+                        Dever::error($state);
                     }
+                } else {
+                    $this->delete($file);
+                    Dever::error('上传失败');
                 }
             }
-            $after = $upload->after();
-            if ($after) {
-                foreach ($after as $k => $v) {
-                    if (isset($v['table'])) {
-                        $method = 'handle_' . $v['table'];
-                        $url = $this->$method($url, $v['param']);
-                    }
+        }
+        $after = $upload->after();
+        if ($after) {
+            foreach ($after as $k => $v) {
+                if (isset($v['table'])) {
+                    $method = 'handle_' . $v['table'];
+                    $url = $this->$method($url, $v['param']);
                 }
             }
-            return $url;
         }
-        Dever::error('上传失败');
+        return $url;
     }
 
     public function handle($id, $file, $type = 'thumb')

+ 8 - 1
table/manage/rule.php

@@ -31,7 +31,14 @@ return array
                 'type' => 'checkbox',
                 'control' => true,
             ),
-            'size',
+            'chunk' => array
+            (
+                'desc' => '单位:M',
+            ),
+            'size' => array
+            (
+                'desc' => '单位:M',
+            ),
             'limit' => 'radio',
             'min_width',
             'min_height',

+ 7 - 0
table/rule.php

@@ -37,6 +37,13 @@ return array
             'control' => true,
         ),
 
+        'chunk'      => array
+        (
+            'name'      => '分片大小',
+            'type'      => 'varchar(11)',
+            'default'   => '5',
+        ),
+
         'size'      => array
         (
             'name'      => '限制大小',