<?php
declare (strict_types=1);
namespace app\system\controller;
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | 
// +----------------------------------------------------------------------
// | Date: 2020-08-31 15:05
// +----------------------------------------------------------------------
use app\Request;
use app\system\controller\AuthController;
use library\lib\upMp4;
use library\utils\Qiniu;
use app\model\api\Sys as SysModel;
use think\Image;

class Upload extends AuthController{


    /**
     * 七牛上传
     * @param Request $request
     */
    public function index(Request $request){
        $file = $request->file('file');
        if (empty($file)) {
            return app('json')->fail("未上传文件");
        }
        $rootTmp    = config('filesystem.disks.local.root') . '/' . \think\facade\Filesystem::putFile('tmp', $file);
        $image_size = @getimagesize($rootTmp);
        if ($image_size[0] > 1000) {
            $imgS = Image::open($rootTmp);
            $imgS->thumb(1000, $image_size[1]);
            $imgS->save($rootTmp);
        } else {
            if ($image_size[1] > 1000) {
                $imgS = Image::open($rootTmp);
                $imgS->thumb($image_size[0], 1000);
                $imgS->save($rootTmp);
            }
        }
        $qiniu   = new  Qiniu;
        $img_url = $qiniu->updateFile('sysimg', $rootTmp, $rootTmp);
        if (empty($img_url['url'])) {
            return app('json')->fail($qiniu->getError());
        }
        @unlink($rootTmp);
        return app('json')->success(['img' => $img_url['url']]);
    }
    /**
     * 本地上传
     * @param Request $request
     * @return type
     */
    public function index2(Request $request){
        $uploadConfig = config('filesystem');
        $sysData = (new SysModel())->where("id",1)->find();
        $file = $request->file('file');
        $code = $request->post("code","image");
        if(empty($file)) {
            return app('json')->fail("未上传文件");
        }
        $size = $file->getSize();
        if(!empty($size) && $size>$uploadConfig["image"]["size"]*1024*1024){
            return app('json')->fail("图片不能超过{$uploadConfig["image"]["size"]}M");
        }
        if(!in_array($code, $uploadConfig['image']['path'])){
            return app('json')->fail("上传路径错误");
        }
        $ext = $file->extension();
        if(!in_array($ext, $uploadConfig['image']['ext'])){
            return app('json')->fail("图片类型错误");
        }
        $upPath = \think\facade\Filesystem::disk('resource')->putFile( $code, $file);
        if(!$upPath){
            return app('json')->fail("图片上传失败01");
        }
        $savePath = $sysData['system_url'].'/resource/' . $upPath;
        $imgUrl = str_replace("\\", "/", $savePath);
        return app('json')->success(['img'=>$imgUrl]);
    }
    
    /**
     * 证书文件上传
     * @param Request $request
     */
    public function uploadCertFile(Request $request){
        $uploadConfig = config('filesystem');
        $file = $request->file('file');
        $type = $request->post("type","");
        if(empty($file)) {
            return app('json')->fail("未上传文件");
        }
        $upPath = \think\facade\Filesystem::disk('resource')->putFileAs( "cert", $file,"weixin". DIRECTORY_SEPARATOR.date('Ymd').DIRECTORY_SEPARATOR.$file->getOriginalName());
        if(!$upPath){
            return app('json')->fail("上传失败01");
        }
        $savePath = '/resource/' . $upPath;
        return app('json')->success(['cert'=>$savePath,"type"=>$type,"name"=>$file->getOriginalName()]);
    }
    
    
    
    
    /**
     * 上传文件
     * @param Request $request
     */
    public function uploadFile(Request $request){
        $fileType = $request->post('type','');
        $file =$request->file('file');
        if(empty($file)) {
            return app('json')->fail("未上传文件");
        }
        //上传文件夹选择
        if(!in_array($fileType,["animated"])){
            return app('json')->fail("请选择上传文件夹");
        }
        $rootTmp = config('filesystem.disks.local.root') .'/' . \think\facade\Filesystem::putFile( 'tmp', $file);
        $qiniu = new  Qiniu;
        $file_url = $qiniu->pathFileUpload($fileType, $rootTmp, $rootTmp);
        if(empty($file_url['url'])){
            return app('json')->fail( $qiniu->getError());
        }
        return app('json')->success(['url'=>$file_url['url']]);
    }


    /**
     * 上传视频
     * @param Request $request
     */
    public function mp4(Request $request) {
        $file = $request->file('Filedata');
        if(empty($file)) {
            return app('json')->fail('上传失败');
        }
        $name = $file->getOriginalName();
        $expAr= explode('.',$name);
        $exp = end($expAr);
        $dEXp = ['mp4'];
        if(!in_array(strtolower($exp),$dEXp)) {
            return app('json')->fail('上传格式不支持.'.$exp);
        }
        $mp4up = new upMp4([
            'file' => 'Filedata',
            'checkTmpRoot' => config('filesystem.disks.local.root')  . "/mp4/",
            'mp4Root' =>  config('filesystem.disks.local.root')  . "/mp4/"
        ]);
        $mp4up->setParam($request->param());
        $b = $mp4up->render('jpg');
        if ($b) {
            $return =  $mp4up->getResult();
            return app('json')->success($return);
        }
        return app('json')->fail( $mp4up->getError());

    }

}