id = sysconf('storage_oss_keyid'); $this->key = sysconf('storage_oss_secret'); $this->host = "https://" . sysconf('storage_oss_domain'); } /** * @Apidoc\Title("上传文件接口(上传到本地(导入模板需要))") * @Apidoc\Desc("上传文件Desc") * @Apidoc\Tag("上传文件Tag") * @Apidoc\Method("POST") * @Apidoc\Author("上传文件Author") * @Apidoc\ParamType("formdata") * @Apidoc\Param("file",type="file", require=true,desc="附件") * @Apidoc\Returned("url", type="string", desc="上传后的文件路径") * @Apidoc\Returned("fullurl", type="string", desc="文件地址") * @Apidoc\Returned("name", type="string", desc="文件名称") * @Apidoc\Returned("size", type="string", desc="文件大小") */ public function upload() { $file = $this->request->file('file'); if (!$file) { $this->error('文件上传异常,文件可能过大或未上传!'); } if (!$file->checkExt(strtolower(sysconf('storage_local_exts')))) { $this->error('文件上传类型受限,请在后台配置!'); } if ($file->checkExt('php,sh')) { $this->error('可执行文件禁止上传到本地服务器!'); } $fileinfo = $file->getInfo(); $this->safe = boolval(input('safe')); $this->uptype = $this->getUploadType(); $this->extend = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION); $subPath = date('Ymd'); $savePath = $subPath; $filename = date('YmdHis') . uniqid(); $suffix = '.' . $this->extend; $realpath = $savePath . '/' . $filename . $suffix; try { $info = File::instance($this->uptype)->save($realpath, file_get_contents($file->getRealPath()), $this->safe); } catch (Exception $e) { $this->error($e->getMessage()); } if (is_array($info) && isset($info['url'])) { $url = $this->safe ? $realpath : $info['url']; $data = [ 'url' => '/' . $info['key'], 'fullurl' => $info['url'], 'name' => $fileinfo['name'], 'size' => $fileinfo['size'], ]; $this->success('上传成功', $data); } else { $this->error('文件处理失败,请稍候再试!'); } } private function getUploadType() { $this->uptype = input('uptype'); $this->uptype = 'local'; if (!in_array($this->uptype, ['local', 'oss', 'qiniu'])) { $this->uptype = sysconf('storage_type'); } return $this->uptype; } /** * @Apidoc\Title("获取OSS配置(用于上传文件到OSS使用)") * @Apidoc\Desc("获取配置") * @Apidoc\Param("type", type="string",require=true, desc="文件要保存的目录名称(需前端创建好)") * @Apidoc\Returned("OSSAccessKeyId", type="string", desc="keyId") * @Apidoc\Returned("policy", type="string", desc="规则") * @Apidoc\Returned("Signature", type="string", desc="签名") * @Apidoc\Returned("host", type="string", desc="oss地址") * @Apidoc\Returned("expire", type="string", desc="有效截止时间") * @Apidoc\Returned("key", type="string", desc="路径(前端生成的目录名称+/)") */ public function getSignedUrl() { $type = $this->request->get('type', 'image'); $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"; } }