<?php
namespace app\admin\controller;

use Think\Loader;
use think\Model;
use think\Db;
use think\File;
use think\Request;
use org\Http;
use org\Upload;
use think\Log;
use think\Cache;
use think\Controller;
class Up extends Controller
{
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021 http://mifenlei.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 梦雨 <50361804@qq.com>
// +----------------------------------------------------------------------
    public function up($filename = 'file')
    {
        $rs=checkHex($_FILES['file']['tmp_name']);
        if ($rs==5) {
            return array('code' => 0, 'msg' => "当前文件有问题不能上传");
        } 
        if (set('qiniu') == 1) {
            $file = request()->file($filename);
            $md5 = $file->hash('md5');
            $n = Db::name('file')->where('md5', $md5)->find();
            if (empty($n)) {
                $driverConfig = array('secrectKey' => set('secrectKey'), 'accessKey' => set('accessKey'), 'domain' => set('qiniudomain'), 'bucket' => set('qiniubucket'));
                $setting['exts'] = explode(',', 'png,jpg,jpeg,gif,bmp,flv,swf,mkv,avi,rm,rmvb,mpeg,mpg,ogg,ogv,mov,wmv,mp4,webm,mp3,wav,mid,rar,zip,tar,gz,7z,bz2,cab,iso,doc,docx,xls,xlsx,ppt,pptx,pdf,txt,md,xml');
                $setting['maxSize'] = 50 * 1024 * 1024;
                $File = $_FILES['file'];
                $Upload = new Upload($setting, 'Qiniu', $driverConfig);
                $info = $Upload->uploadOne($File);
                if ($info) {
                    $data['sha1'] = $info['sha1'];
                    $data['md5'] = $info['md5'];
                    $data['create_time'] = time();
                    $data['uid'] = 1;
                    $data['download'] = 0;
                    $data['size'] = $info['size'];
                    $data['name'] = $info['name'];
                    $data['ext'] = $info['ext'];
                    $data['savepath'] = $info['url'];
                    $data['savename'] = $info['savename'];
                    $data['mime'] = $info['type'];
                    if ($nS == "image") {
                        $data['img'] = 1;
                    } else {
                        $data['img'] = 0;
                    }
                    Db::name('file')->insert($data);
                    $res = Db::name('file')->getLastInsID();
                    if ($res > 0) {
                        return array('code' => 1, 'msg' => '上传成功', 'hasscore' => 0, 'ext' => $data['ext'], 'cc' => $cc, 'thumb' => $thumb, 'size' => size($data['size']), 'id' => $res, 'headpath' => $data['savepath'], 'path' => $data['savepath'], 'md5' => $data['md5'], 'savename' => $data['savename'], 'filename' => $data['name'], 'info' => $info);
                    } else {
                        return array('code' => 0, 'msg' => '上传失败');
                    }
                } else {
                    return array('code' => 0, 'msg' => $Upload->getError());
                }
            } else {
                $cc = $n['cc'];
                $path = $n['savepath'];
                $realpath = WEB_URL . $path;
                return array('code' => 1, 'msg' => '上传成功', 'hasscore' => 1, 'ext' => $n['ext'], 'id' => $n['id'], 'size' => size($n['size']), 'path' => $path, 'headpath' => $realpath, 'md5' => $n['md5'], 'savename' => $n['savename'], 'filename' => $n['name'], 'info' => $n);
            }
        } else {
            $file = request()->file($filename);
            $md5 = $file->hash('md5');
            $n = Db::name('file')->where('md5', $md5)->find();
            if (empty($n)) {
                $info = $file->validate(['size' => 50 * 1024 * 1024, 'ext' => 'png,jpg,jpeg,gif,bmp,flv,swf,mkv,avi,rm,rmvb,mpeg,mpg,ogg,ogv,mov,wmv,mp4,webm,mp3,wav,mid,rar,zip,tar,gz,7z,bz2,cab,iso,doc,docx,xls,xlsx,ppt,pptx,pdf,txt,md,xml'])->move(ROOT_PATH . DS . 'uploads');
                if ($info) {
                    $path = DS . 'uploads' . DS . $info->getSaveName();
                    $path = str_replace("\\", "/", $path);
                    $realpath = WEB_URL . $path;
                    $data['sha1'] = $info->sha1();
                    $data['md5'] = $info->md5();
                    $data['create_time'] = time();
                    $data['uid'] = 1;
                    $data['download'] = 0;
                    $data['size'] = $info->getSize();
                    $fileinfo = $info->getInfo();
                    $data['name'] = $fileinfo['name'];
                    $data['ext'] = $info->getExtension();
                    if (set('ym') == 1) {
                        $data['savepath'] = '' . http() . '' . ps(0) . '' . $path . '';
                    } else {
                        $data['savepath'] = $path;
                    }
                    $data['savename'] = $info->getFilename();
                    $data['mime'] = $fileinfo['type'];
                    $nS = preg_replace("/\\/(.*)/", "", $data['mime']);
                    if ($nS == "image") {
                        $data['img'] = 1;
                    } else {
                        $data['img'] = 0;
                    }
                    Db::name('file')->insert($data);
                    $res = Db::name('file')->getLastInsID();
                    if ($res > 0) {

                            return array('code' => 1, 'msg' => '上传成功', 'hasscore' => 0, 'ext' => $data['ext'], 'size' => size($data['size']), 'id' => $res, 'path' => $data['savepath'], 'headpath' => $realpath, 'md5' => $data['md5'], 'savename' => $data['savename'], 'filename' => $data['name'], 'info' => $info->getInfo());
                        
                    } else {
                        return array('code' => 0, 'msg' => '上传失败');
                    }
                } else {
                    return array('code' => 0, 'msg' => $file->getError());
                }
            } else {
                $path = $n['savepath'];
                $realpath = WEB_URL . $path;
                return array('code' => 1, 'msg' => '上传成功..', 'hasscore' => 1, 'ext' => $n['ext'], 'id' => $n['id'], 'size' => size($n['size']), 'path' => $path, 'headpath' => $realpath, 'md5' => $n['md5'], 'savename' => $n['savename'], 'filename' => $n['name'], 'info' => $n);
            }
        }
    }
    /*
    图片上传编辑器
    @七牛本地
    */
    public function Pic()
    {
        if ($rs==5) {
            return array('code' => 0, 'msg' => "当前文件有问题不能上传");
        } 
        if (set('qiniu') == 1) {
            $file = request()->file('FileName');
            $md5 = $file->hash('md5');
            $n = Db::name('file')->where('md5', $md5)->find();
            if (empty($n)) {
                $driverConfig = ['secrectKey' => set('secrectKey'), 'accessKey' => set('accessKey'), 'domain' => set('qiniudomain'), 'bucket' => set('qiniubucket')];
                $setting['exts'] = explode(',', 'png,jpg,jpeg,gif,bmp');
                $setting['maxSize'] = 50 * 1024 * 1024;
                $File = $_FILES['file'];
                $Upload = new Upload($setting, 'Qiniu', $driverConfig);
                $info = $Upload->uploadOne($_FILES['FileName']);
                if ($info) {
                    $data['sha1'] = $info['sha1'];
                    $data['md5'] = $info['md5'];
                    $data['create_time'] = time();
                    $data['uid'] = 1;
                    $data['download'] = 0;
                    $data['size'] = $info['size'];
                    $data['name'] = $info['name'];
                    $data['ext'] = $info['ext'];
                    $data['savepath'] = $info['url'];
                    $data['savename'] = $info['savename'];
                    $data['mime'] = $info['type'];
                    if ($nS == "image") {
                        $data['img'] = 1;
                    } else {
                        $data['img'] = 0;
                    }
                    Db::name('file')->insert($data);
                    $res = Db::name('file')->getLastInsID();
                    if ($res > 0) {
                        echo str_replace("\\", "/", $data['savepath']);
                    }
                } else {
                    return array('code' => 0, 'msg' => $Upload->getError());
                }
            } else {
                $path = $n['savepath'];

                    echo str_replace("\\", "/", $path);
           
            }
        } else {
            //普通上传
            $file = request()->file('FileName');
            $md5 = $file->hash('md5');
            $n = Db::name('file')->where('md5', $md5)->find();
            if (empty($n)) {
                $info = $file->validate(['size' => 10 * 1024 * 1024, 'ext' => 'png,jpg,jpeg,gif,bmp,flv'])->move(ROOT_PATH . DS . 'uploads');
                if ($info) {
                    $path = DS . 'uploads' . DS . $info->getSaveName();
                    $realpath = WEB_URL . $path;
                    $data['sha1'] = $info->sha1();
                    $data['md5'] = $info->md5();
                    $data['create_time'] = time();
                    $data['uid'] = 1;
                    $data['download'] = 0;
                    $data['size'] = $info->getSize();
                    $fileinfo = $info->getInfo();
                    $data['name'] = $fileinfo['name'];
                    $data['ext'] = $info->getExtension();
                    if (set('ym') == 1) {
                        $data['savepath'] = '' . http() . '' . ps(0) . '' . $path . '';
                    } else {
                        $data['savepath'] = $path;
                    }
                    $data['savename'] = $info->getFilename();
                    $data['mime'] = $fileinfo['type'];
                    $nS = preg_replace("/\\/(.*)/", "", $data['mime']);
                    if ($nS == "image") {
                        $data['img'] = 1;
                    } else {
                        $data['img'] = 0;
                    }
                    Db::name('file')->insert($data);
                    $res = Db::name('file')->getLastInsID();
                    if ($res > 0) {
                        echo str_replace("\\", "/", $data['savepath']);
                    }
                }
            } else {
                $path = $n['savepath'];
                echo str_replace("\\", "/", $path);
            }
        }
    }
}