diff --git a/application/config.php b/application/config.php index a7d2ce8a..30b42d7b 100644 --- a/application/config.php +++ b/application/config.php @@ -18,7 +18,7 @@ return [ // 应用命名空间 'app_namespace' => 'app', // 应用调试模式 - 'app_debug' => false, + 'app_debug' => true, // 应用Trace 'app_trace' => false, // 应用模式状态 diff --git a/application/index/controller/RemoteDownload.php b/application/index/controller/RemoteDownload.php new file mode 100644 index 00000000..04201747 --- /dev/null +++ b/application/index/controller/RemoteDownload.php @@ -0,0 +1,72 @@ +userObj = new User(cookie('user_id'),cookie('login_key')); + if(!$this->userObj->loginStatus){ + echo "Bad request"; + exit(); + } + } + + private function checkPerimission($permissionId){ + $permissionData = $this->userObj->groupData["aria2"]; + if(explode(",",$permissionData)[$permissionId] != "1"){ + return false; + } + return true; + } + + private function insertRecord($aria2,$url){ + Db::name("download")->insert([ + "pid" => $aria2->pid, + "path_id" => $aria2->pathId, + "owner" => $this->userObj->uid, + "save_dir" => 1, + "status" => "ready", + "msg" => "", + "info"=>"", + "source" =>$url, + ]); + } + + public function addUrl(){ + $policyData = Db::name("policy")->where("id",$this->userObj->groupData["policy_name"])->find(); + if(!$this->checkPerimission(0) || $policyData["policy_type"] != "local"){ + return json(['error'=>1,'message'=>'您当前的无用户无法执行此操作']); + } + $aria2Options = Option::getValues(["aria2"]); + $aria2 = new Aria2($aria2Options); + $downloadStart = $aria2->addUrl(input("post.url")); + if($aria2->reqStatus){ + $this->insertRecord($aria2,input("post.url")); + }else{ + return json(['error'=>1,'message'=>$aria2->reqMsg]); + } + } + + public function FlushStatus(){ + $aria2Options = Option::getValues(["aria2"]); + $aria2 = new Aria2($aria2Options); + if(!input("?post.id")){ + return json(['error'=>1,'message'=>"信息不完整"]); + } + $policyData = Db::name("policy")->where("id",$this->userObj->groupData["policy_name"])->find(); + if(!$aria2->flushStatus(input("post.id"),$this->userObj->uid,$policyData)){ + return json(['error'=>1,'message'=>$aria2->reqMsg]); + } + } + +} \ No newline at end of file diff --git a/application/index/model/Aria2.php b/application/index/model/Aria2.php new file mode 100644 index 00000000..ebaf3429 --- /dev/null +++ b/application/index/model/Aria2.php @@ -0,0 +1,153 @@ +authToken = $options["aria2_token"]; + $this->apiUrl = rtrim($options["aria2_rpcurl"],"/")."/"; + $this->saveOptions = json_decode($options["aria2_options"],true); + $this->savePath = rtrim(rtrim($options["aria2_tmppath"],"/"),"\\").DS; + } + + public function addUrl($url){ + $this->pathId = uniqid(); + $reqFileds = [ + "params" => ["token:".$this->authToken, + [$url],["dir" => $this->savePath.$this->pathId], + ], + "jsonrpc" => "2.0", + "id" => $this->pathId, + "method" => "aria2.addUri" + ]; + $reqFileds["params"][2] = array_merge($reqFileds["params"][2],$this->saveOptions); + $reqFileds = json_encode($reqFileds,JSON_OBJECT_AS_ARRAY); + $respondData = $this->sendReq($reqFileds); + if(isset($respondData["result"])){ + $this->reqStatus = 1; + $this->pid = $respondData["result"]; + }else{ + $this->reqStatus = 0; + $this->reqMsg = $respondData["error"]["message"]; + } + } + + public function flushStatus($id,$uid,$policy){ + $this->uid = $uid; + $this->policy = $policy; + $downloadInfo = Db::name("download")->where("id",$id)->find(); + if(empty($downloadInfo)){ + $this->reqStatus = 0; + $this->reqMsg = "未找到下载记录"; + return false; + } + if(in_array($downloadInfo["status"], ["error","complete"])){ + $this->reqStatus = 1; + return true; + } + if($uid != $downloadInfo["owner"]){ + $this->reqStatus = 0; + $this->reqMsg = "无权操作"; + return false; + } + $reqFileds = [ + "params" => ["token:".$this->authToken,$downloadInfo["pid"]], + "jsonrpc" => "2.0", + "id" => uniqid(), + "method" => "aria2.tellStatus" + ]; + $reqFileds = json_encode($reqFileds,JSON_OBJECT_AS_ARRAY); + $respondData = $this->sendReq($reqFileds); + if(isset($respondData["result"])){ + if($this->storageCheck($respondData["result"],$downloadInfo)){ + Db::name("download")->where("id",$id) + ->update([ + "status" => $respondData["result"]["status"], + "last_update" => date("Y-m-d h:i:s"), + "info" => json_encode([ + "completedLength" => $respondData["result"]["completedLength"], + "totalLength" => $respondData["result"]["totalLength"], + "dir" => $respondData["result"]["dir"], + "downloadSpeed" => $respondData["result"]["downloadSpeed"], + "errorMessage" => $respondData["result"]["errorMessage"], + ]), + ]); + switch ($respondData["result"]["status"]) { + case 'complete': + $this->setComplete($respondData["result"],$downloadInfo); + break; + + default: + # code... + break; + } + }else{ + $this->reqStatus = 0; + $this->reqMsg = "空间容量不足"; + //取消离线下载 + return false; + } + }else{ + $this->reqStatus = 0; + $this->reqMsg = $respondData["error"]["message"]; + } + return true; + } + + private function setComplete($quenInfo,$sqlData){ + FileManage::storageCheckOut($this->uid,(int)$quenInfo["totalLength"]); + if($this->policy["policy_type"] != "local"){ + return false; + } + $suffixTmp = explode('.', $quenInfo["dir"]); + $fileSuffix = array_pop($suffixTmp); + $allowedSuffix = explode(',', UploadHandler::getAllowedExt(json_decode($this->policy["filetype"],true))); + $sufficCheck = !in_array($fileSuffix,$allowedSuffix); + if(empty(UploadHandler::getAllowedExt(json_decode($this->policy["filetype"],true)))){ + $sufficCheck = false; + } + var_dump($sufficCheck); + } + + private function storageCheck($quenInfo,$sqlData){ + if(!FileManage::sotrageCheck($this->uid,(int)$quenInfo["totalLength"])){ + return false; + } + if(!FileManage::sotrageCheck($this->uid,(int)$quenInfo["completedLength"])){ + return false; + } + return true; + } + + private function sendReq($data){ + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, $this->apiUrl."jsonrpc"); + curl_setopt($curl, CURLOPT_POST, 1); + curl_setopt($curl, CURLOPT_POSTFIELDS, $data); + curl_setopt($curl, CURLOPT_TIMEOUT, 15); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + $tmpInfo = curl_exec($curl); + if (curl_errno($curl)) { + $this->reqStatus = 0; + $this->reqMsg = "请求失败,".curl_error($curl); + } + curl_close($curl); + return json_decode($tmpInfo,true); + } + +} +?> \ No newline at end of file diff --git a/public/downloads/.gitignore b/public/downloads/.gitignore new file mode 100644 index 00000000..c96a04f0 --- /dev/null +++ b/public/downloads/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/static/js/qiniu.js b/static/js/qiniu.js index c6d01ae7..2800307f 100644 --- a/static/js/qiniu.js +++ b/static/js/qiniu.js @@ -1501,6 +1501,8 @@ function QiniuJsSDK() { local_path = '/path/'+that.URLSafeBase64Encode(pathTmp); } if(uploadConfig.saveType == "remote"){ + pathTmp = file.path; + local_path = that.URLSafeBase64Encode(pathTmp); var url = qiniuUploadUrl + 'mkfile.php?size=' + file.size +"&key="+ key+"&fname="+ fname +"&path="+local_path; }else{ var url = qiniuUploadUrl + '/mkfile/' + file.size + key + fname + x_vars_url+local_path;