<?php
namespace App\Services;

use Illuminate\Support\Facades\Log;
use Carbon\Carbon;
use Exception;
use Illuminate\Support\Facades\Storage;
use App\Lib\Ccwl\Oss\CcwlOssManager;

class UploadServer
{
    private $time;

    public function __construct()
    {
        $this->time = Carbon::now()->format('Y-m-d H:i:s');
    }

    /**
     * 上传文件
     * @return array
     * @date 2021/06/01
     * @author live
     */
    public function getUpload($postData,$file)
    {
        $array = ['code'=>200,'msg'=>''];
        $size = sprintf('%.2f',$postData['imgFile']->getClientSize() / (1024 * 1024));
        if($size > 2){
            $array['code'] = 10057;
            $array['msg'] = '上传文件最大不得超过2M';
            return $array;
        }
        $suffix = $file->getClientOriginalExtension();
        if(!in_array($suffix,['jpg','jpeg','png'])){
            $array['code'] = 10058;
            $array['msg'] = '上传文件只支持jpg,jpeg,png三种格式';
            return $array;
        }
        $originName = $file->getClientOriginalName();
        self::getCheckDir( storage_path('app'),'card');
        $fileUrl = self::getUploadFile($file,'card','aldzn-img',$originName);
        if($fileUrl['error'] != ''){
            $array['code'] = 10059;
            $array['msg'] = $fileUrl['error'];
            return $array;
        }
        $array['data'] = $fileUrl;
        return $array;
    }

    /**
     * 建立目录
     * @return array
     * @date 2021/06/01
     * @author live
     */
    public static function getCheckDir( $dir ,$folder, $listNum = 0 , $level = 1 ){
        if($listNum < 1){
            $listNum++;
            if(file_exists($dir.'/'.$folder)){
                self::getCheckDir($dir.'/'.$folder,'',$listNum);
            }else{
                mkdir($dir.'/'.$folder,0775,true);
                chmod($dir.'/'.$folder,0775);
                self::getCheckDir($dir.'/'.$folder,'',$listNum);
            }
        }else{
            $listNum++;
            $firstDir = date('Ym');
            $secondDir = date('d');
            if($level == 1){
                $level++;
                if(file_exists($dir.'/'.$firstDir)){
                    self::getCheckDir($dir.'/'.$firstDir,'',$listNum,$level);
                }else{
                    mkdir($dir.'/'.$firstDir,0775,true);
                    chmod($dir.'/'.$firstDir,0775);
                    self::getCheckDir($dir.'/'.$firstDir,'',$listNum,$level);
                }
            }else{
                if(!file_exists($dir.'/'.$secondDir)){
                    mkdir($dir.'/'.$secondDir,0775,true);
                    chmod($dir.'/'.$secondDir,0775);
                }
            }
        }
    }

    /**
     * 上传文件本地
     * @return array
     * @date 2021/06/01
     * @author live
     */
    public static function getUploadFile($file,$folder,$bucket,$originName){

        $imgUrl = array();
        $imgUrl['error'] = '';
        $fileName = date('YmdHis').rand(10000,99999).'-'.$originName;
        $path = $folder.'/'.date('Ym').'/'.date('d');
        $realPath = $file->getRealPath();
        if(Storage::disk('upload')->put($path.'/'.$fileName, file_get_contents($realPath))){
            $imgUrl['url'] = $folder.'/'.date('Ym').'/'.date('d').'/'.$fileName;
            $imgUrl['yunUrl'] = self::getUploadAliyun($bucket, $folder.'/'.date('Ym').'/'.date('d').'/'.$fileName,storage_path('app').'/'.$imgUrl['url']);
        }else{
            $imgUrl['error'] = '文件上传失败!';
        }
        return $imgUrl;
    }

    /**
     * 上传文件阿里云oss
     * @return array
     * @date 2021/06/01
     * @author live
     */
    public static function getUploadAliyun($bucket,$object,$fileUrl){
        require_once app_path().'/Lib/autoload.php';
        $aliyunConfig = [
            'oss_access_id' => config('sign.OSS_ACCESS_ID'),
            'oss_access_key' => config('sign.OSS_ACCESS_KEY'),
            'oss_endpoint' => config('sign.OSS_ENDPOINT'),
            'oss_bucket' => $bucket,
            'oss_domain' => config('sign.OSS_DOMAIN'),
        ];
        $manager = new CcwlOssManager(CcwlOssManager::DRIVER_ALIYUN);
        $client = $manager->init($aliyunConfig);

        $result = $client->uploadFile($object, $fileUrl);
        if($result == false){
            exit($client->getError());
        }

        unset($result,$manager,$client);
        return  config('sign.OSS_CDN').$object;
    }

}