From 1b87365b8a4e2108f51f57ad4b019949aee32ad7 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Sat, 7 Apr 2018 15:16:57 +0800 Subject: [PATCH] =?UTF-8?q?aria2=20=E7=A7=8D=E5=AD=90=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E5=8F=8A=E5=A4=9A=E6=96=87=E4=BB=B6=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../index/controller/RemoteDownload.php | 18 +++ application/index/model/Aria2.php | 107 +++++++++++++----- 2 files changed, 99 insertions(+), 26 deletions(-) diff --git a/application/index/controller/RemoteDownload.php b/application/index/controller/RemoteDownload.php index 04201747..1259d512 100644 --- a/application/index/controller/RemoteDownload.php +++ b/application/index/controller/RemoteDownload.php @@ -39,6 +39,8 @@ class RemoteDownload extends Controller{ "msg" => "", "info"=>"", "source" =>$url, + "file_index" => 0, + "is_single" => 1, ]); } @@ -57,6 +59,22 @@ class RemoteDownload extends Controller{ } } + public function AddTorrent(){ + $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); + $torrentObj = new \app\index\model\FileManage(input("post.path"),$this->userObj->uid); + $downloadStart = $aria2->addTorrent($torrentObj->signTmpUrl()); + if($aria2->reqStatus){ + $this->insertRecord($aria2,input("post.path")); + }else{ + return json(['error'=>1,'message'=>$aria2->reqMsg]); + } + } + public function FlushStatus(){ $aria2Options = Option::getValues(["aria2"]); $aria2 = new Aria2($aria2Options); diff --git a/application/index/model/Aria2.php b/application/index/model/Aria2.php index ab6617cf..b6c44481 100644 --- a/application/index/model/Aria2.php +++ b/application/index/model/Aria2.php @@ -46,6 +46,28 @@ class Aria2 extends Model{ } } + public function addTorrent($torrentUrl){ + $this->pathId = uniqid(); + $reqFileds = [ + "params" => ["token:".$this->authToken, + [$torrentUrl],["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; @@ -74,14 +96,25 @@ class Aria2 extends Model{ $respondData = $this->sendReq($reqFileds); if(isset($respondData["result"])){ if($this->storageCheck($respondData["result"],$downloadInfo)){ + if($downloadInfo["is_single"] && count($respondData["result"]["files"]) >1){ + $this->updateToMuiltpe($respondData["result"],$downloadInfo); + return false; + } + if(isset($respondData["result"]["followedBy"])){ + Db::name("download")->where("id",$id) + ->update([ + "pid" => $respondData["result"]["followedBy"][0], + ]); + return false; + } 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"]["files"][0]["path"], + "completedLength" => $respondData["result"]["files"][$downloadInfo["file_index"]]["completedLength"], + "totalLength" => $respondData["result"]["files"][$downloadInfo["file_index"]]["length"], + "dir" => $respondData["result"]["files"][$downloadInfo["file_index"]]["path"], "downloadSpeed" => $respondData["result"]["downloadSpeed"], "errorMessage" => isset($respondData["result"]["errorMessage"]) ? $respondData["result"]["errorMessage"] : "", ]), @@ -105,10 +138,30 @@ class Aria2 extends Model{ }else{ $this->reqStatus = 0; $this->reqMsg = $respondData["error"]["message"]; + $this->setError($respondData,$downloadInfo,$respondData["error"]["message"],"error",false); + return false; } return true; } + private function updateToMuiltpe($quenInfo,$sqlData){ + foreach ($quenInfo["files"] as $key => $value) { + Db::name("download")->insert([ + "pid" => $sqlData["pid"], + "path_id" => $sqlData["path_id"], + "owner" => $sqlData["owner"], + "save_dir" => 1, + "status" => "ready", + "msg" => "", + "info"=>"", + "source" =>$sqlData["source"], + "file_index" => $key, + "is_single" => 0, + ]); + } + Db::name("download")->where("id",$sqlData["id"])->delete(); + } + private function setComplete($quenInfo,$sqlData){ if($this->policy["policy_type"] != "local"){ $this->setError($quenInfo,$sqlData,"您当前的上传策略无法使用离线下载"); @@ -128,20 +181,20 @@ class Aria2 extends Model{ return false; } if($this->policy['autoname']){ - $fileName = $uploadHandller->getObjName($this->policy['namerule'],"local",basename($quenInfo["files"][0]["path"])); + $fileName = $uploadHandller->getObjName($this->policy['namerule'],"local",basename($quenInfo["files"][$sqlData["file_index"]]["path"])); }else{ - $fileName = basename($quenInfo["files"][0]["path"]); + $fileName = basename($quenInfo["files"][$sqlData["file_index"]]["path"]); } $generatePath = $uploadHandller->getDirName($this->policy['dirrule']); $savePath = ROOT_PATH . 'public/uploads/'.$generatePath.DS.$fileName; is_dir(dirname($savePath))? :mkdir(dirname($savePath),0777,true); - rename($quenInfo["files"][0]["path"],$savePath); - @unlink(dirname($quenInfo["files"][0]["path"])); + rename($quenInfo["files"][$sqlData["file_index"]]["path"],$savePath); + @unlink(dirname($quenInfo["files"][$sqlData["file_index"]]["path"])); $jsonData = array( "path" => "", - "fname" => basename($quenInfo["files"][0]["path"]), + "fname" => basename($quenInfo["files"][$sqlData["file_index"]]["path"]), "objname" => $generatePath.DS.$fileName, - "fsize" => $quenInfo["totalLength"], + "fsize" => $quenInfo["files"][$sqlData["file_index"]]["length"], ); @list($width, $height, $type, $attr) = getimagesize($savePath); $picInfo = empty($width)?" ":$width.",".$height; @@ -151,15 +204,17 @@ class Aria2 extends Model{ $this->setError($quenInfo,$sqlData,$addAction[1]); return false; } - FileManage::storageCheckOut($this->uid,(int)$quenInfo["totalLength"]); + FileManage::storageCheckOut($this->uid,(int)$quenInfo["files"][$sqlData["file_index"]]["length"]); } - private function setError($quenInfo,$sqlData,$msg,$status="error"){ + private function setError($quenInfo,$sqlData,$msg,$status="error",$delete=true){ $this->Remove($sqlData["pid"],$sqlData); $this->removeDownloadResult($sqlData["pid"],$sqlData); - if(file_exists($quenInfo["files"][0]["path"])){ - @unlink($quenInfo["files"][0]["path"]); - @unlink(dirname($quenInfo["files"][0]["path"])); + if($delete){ + if(file_exists($quenInfo["files"][$sqlData["file_index"]]["path"])){ + @unlink($quenInfo["files"][$sqlData["file_index"]]["path"]); + @unlink(dirname($quenInfo["files"][$sqlData["file_index"]]["path"])); + } } Db::name("download")->where("id",$sqlData["id"])->update([ "msg" => $msg, @@ -209,18 +264,18 @@ class Aria2 extends Model{ 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); + 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); } }