<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/31 0031
 * Time: 下午 3:23
 */
namespace app\lumaapi\controller;

use app\utils\Watermark;
use Qiniu\Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;
use think\Db;
use think\Validate;

define('BASE_PATH', getcwd());

class FileUpload extends Common
{

    //上传图片
    public function upload(){
        $file = request()->file('file');
        //   exit(json_encode(['err'=>'演示站无权操作！']));
        // 要上传图片的本地路径
        $filePath = $file->getRealPath();
        if(!$filePath){
            $this->error("非法操作");
        }
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀
        $geshi=['jpg','png','gif','jpeg','JPEG','JPG','GIF','PNG'];
        if(!in_array($ext,$geshi)){
            $this->error("图片格式不对，请换张图片吧");
        }
        $key ="photo/".substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(100, 999999) . '.jpg' ;
        require_once APP_PATH . '/../vendor/qiniu/autoload.php';
        $db=db::name('member_config')->where(['type'=>'sdkQiniu'])->cache(_cache('db'))->select();
        $accessKey = $db['0']['text'];
        $secretKey = $db['1']['text'];
        $auth = new Auth($accessKey, $secretKey);
        $bucket = $db['2']['text'];
        $domain = $db['3']['text'];
        $token = $auth->uploadToken($bucket);
        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
        if ($err !== null) {
            $this->error("上传失败");
        }else{
            $this->success($key);
        }
    }
    public function uploadify(){
        require_once APP_PATH . '/../vendor/qiniu/autoload.php';
        $db=db::name('member_config')->where(['type'=>'sdkQiniu'])->select();
        $accessKey = $db['0']['text'];
        $secretKey = $db['1']['text'];
        $bucket = $db['2']['text'];
        $pipeline = $db['4']['text'];
        $fops = $db['5']['text'];
        $auth = new Auth($accessKey, $secretKey);
        //$file= $_FILES['video'];
        //$filePath = $file['tmp_name'];
        //$key ="video/".substr(md5(uniqid(microtime(true),true)) , 0, 5). date('YmdHis') . rand(100, 999999) . '.mp4';
        $file = request()->file('video');
        if (empty($file)){
            $this->error("文件不存在");
        }
        $key ="video/".substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(100, 999999) . '.mp4';
        $token = $auth->uploadToken($bucket);
        $savekey = \Qiniu\base64_urlSafeEncode($bucket);
        $fops = $fops.'|saveas/'.$savekey;
        $wenurl=config()['appconfig']['url'];
        $policy = array(
            'persistentOps' => $fops,
            'persistentPipeline' => $pipeline,
            'persistentNotifyUrl' => $wenurl.'/index.php/api/index/notify',
        );
        $uptoken = $auth->uploadToken($bucket, null, 3600, $policy);
        //上传文件的本地路径
        $filePath = $file->getRealPath();
        $uploadMgr = new UploadManager();
        list($ret, $err) = $uploadMgr->putFile($uptoken, $key, $filePath);
        //$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
        // $txt =  $ret['key'];
        // fwrite($myfile, $txt);
        // $rets="1231";
        /*if ($err !== null) {
            $this->error("上传失败");
        }else{

        }*/
        $this->success($key);
    }
}