id = config('app.ali_set.Key'); $this->key = config('app.ali_set.secret'); $this->host = config('app.ali_set.host'); } public function upload() { if (!($file = $this->getUploadFile()) || empty($file)) { $this->error('文件上传异常,文件可能过大或未上传!'); } if (!$file->checkExt(strtolower(sysconf('storage_local_exts')))) { $this->error('文件上传类型受限,请在后台配置!'); } if ($file->checkExt('php,sh')) { $this->error('可执行文件禁止上传到本地服务器!'); } $this->safe = boolval(input('safe')); $this->uptype = $this->getUploadType(); $this->extend = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION); $name = File::name($file->getPathname(), $this->extend, '', 'md5_file'); $info = File::instance($this->uptype)->save($name, file_get_contents($file->getRealPath()), $this->safe); if (is_array($info) && isset($info['url'])) { $url = $this->safe ? $name : $info['url']; $this->success('上传成功',$url); } else { $this->error('文件处理失败,请稍候再试!'); } } /** * 获取本地文件对象 * @return \think\File */ private function getUploadFile() { try { return $this->request->file('file'); } catch (\Exception $e) { $this->error(lang($e->getMessage())); } } /** * 获取文件上传方式 * @return string * @throws \think\Exception * @throws \think\exception\PDOException */ private function getUploadType() { $this->uptype = input('uptype'); if (!in_array($this->uptype, ['local', 'oss', 'qiniu'])) { $this->uptype = sysconf('storage_type'); } return $this->uptype; } /** * @title 获取配置 * @desc 获取配置 * @author QGF * @url /api/Upload/getSignedUrl * @method GET * @tag 配置信息 * @param name:type type:string require:1 default:-- desc: */ public function getSignedUrl(){ $type=$this->request->get('type'); $data=$this->getPolicy($type.'/'); $this->success('操作成功',$data); } public function getPolicy($path,$maxSize=5000000){ $now = time(); $expire = 600; //设置该policy超时时间是10s. 即这个policy过了这个有效时间,将不能访问 $end = $now + $expire; $expiration = $this->_gmt_iso8601($end); $policy = [ 'expiration'=>$expiration, 'conditions'=>[ ['content-length-range', 1, $maxSize], ['starts-with', '$key', $path], ] ]; $policy = json_encode($policy); $policy = base64_encode($policy); $signature = base64_encode(hash_hmac('sha1', $policy, $this->key, true)); $response = array(); $response['OSSAccessKeyId'] = $this->id; $response['policy'] = $policy; $response['Signature'] = $signature; $response['host'] = $this->host; $response['expire'] = $end; $response['success_action_status'] = 200; //这个参数是设置用户上传指定的前缀 $response['key'] = $path; return $response; } protected function _gmt_iso8601($time) { $dtStr = date("c", $time); $mydatetime = new \DateTime($dtStr); $expiration = $mydatetime->format(\DateTime::ISO8601); $pos = strpos($expiration, '+'); $expiration = substr($expiration, 0, $pos); return $expiration."Z"; } public function getSysDirection() { $data = [ 'film_notice'=>htmlspecialchars_decode(sysconf('film_notice')), 'film_direction'=>htmlspecialchars_decode(sysconf('film_direction')) ]; $this->success('获取成功',$data); } }