From e989aa47d9365c6697fa250c3b15c9b5248a3f02 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Wed, 5 Sep 2018 19:19:24 +0800 Subject: [PATCH] add: Single Onedrive reUpload handle function --- application/index/command/Task.php | 31 +++++++++- application/index/model/AdminHandler.php | 6 +- application/index/model/Task.php | 72 +++++++++++++++++++++++ application/index/model/UploadHandler.php | 5 +- 4 files changed, 111 insertions(+), 3 deletions(-) diff --git a/application/index/command/Task.php b/application/index/command/Task.php index 35726e88..fd3d0346 100644 --- a/application/index/command/Task.php +++ b/application/index/command/Task.php @@ -1,20 +1,49 @@ setName('run')->setDescription('Start processing tasks for Cloudreve'); } + protected function Init(Output $output){ + $output->writeln("Cloudreve tasks processor started."); + } + + protected function setComplete($taskId,Output $output){ + $output->writeln("Cloudreve tasks processor started."); + } + protected function execute(Input $input, Output $output) { - $output->writeln("TestCommand:"); + self::Init($output); + while (1){ + $newTaskInfo = Db::name("task")->where("status","todo")->find(); + if(empty($newTaskInfo)){ + sleep(self::SLEEP_TIME); + continue; + } + Db::name("task")->where("id",$newTaskInfo["id"])->update(["status"=>"processing"]); + $output->writeln("[New task] Name:".$newTaskInfo["task_name"]." Type:".$newTaskInfo["type"]); + $task = new \app\index\model\Task(); + $task->taskModel = $newTaskInfo; + $task->input = $input; + $task->output = $output; + $task->Do(); + if($task->status=="error"){ + $output->writeln("[Error] ".$task->errorMsg); + } + } } } ?> \ No newline at end of file diff --git a/application/index/model/AdminHandler.php b/application/index/model/AdminHandler.php index cfc48b28..3143d59a 100644 --- a/application/index/model/AdminHandler.php +++ b/application/index/model/AdminHandler.php @@ -734,6 +734,9 @@ class AdminHandler extends Model{ } public function oneDriveCalllback($code){ + if(input("?get.error")){ + throw new \think\Exception(input("get.error_description")); + } $policyId = \think\Session::get('onedrive.pid'); $policyData = Db::name("policy")->where("id",$policyId)->find(); $onedrive = new Client([ @@ -758,6 +761,7 @@ class AdminHandler extends Model{ $policyData = Db::name("policy")->where("id",$policyId)->find(); $onedrive = new Client([ + 'stream_back_end' => \Krizalys\Onedrive\StreamBackEnd::TEMP, 'client_id' => $policyData["bucketname"], // Restore the previous state while instantiating this client to proceed in @@ -769,7 +773,7 @@ class AdminHandler extends Model{ "sk" => json_encode($onedrive->getState()), ]); $file = fopen("C:/Users/i/Downloads/Video/test.mp4","r"); - $onedrive->createFile(urlencode("Git提交代码简教程.mp4"),"/me/drive/root:/sdfdsf",$file); + $onedrive->createFile(urlencode("Git提交代码简教程.txt"),"/me/drive/root:/sdfdsf",$file); } diff --git a/application/index/model/Task.php b/application/index/model/Task.php index b0801816..52b8668a 100644 --- a/application/index/model/Task.php +++ b/application/index/model/Task.php @@ -4,6 +4,12 @@ namespace app\index\model; use think\Model; use think\Db; use \app\index\model\Option; +use \app\index\model\FileManage; + +use think\console\Input; +use think\console\Output; + +use \Krizalys\Onedrive\Client; class Task extends Model{ @@ -11,6 +17,13 @@ class Task extends Model{ public $taskName; public $taskType; public $taskContent; + public $input; + public $output; + public $userId; + + public $status = "success"; + public $errorMsg; + public function __construct($id=null){ if($id!==null){ @@ -24,8 +37,67 @@ class Task extends Model{ "attr" => $this->taskContent, "type" => $this->taskType, "status" => "todo", + "uid" => $this->userId, ]); } + public function Do(){ + switch ($this->taskModel["type"]){ + case "uploadSingleToOnedrive": + $this->uploadSingleToOnedrive(); + break; + default: + $this->output->writeln("Unknown task type"); + break; + } + } + + private function uploadSingleToOnedrive(){ + $this->taskContent = json_decode($this->taskModel["attr"],true); + $policyData = Db::name("policy")->where("id",$this->taskContent["policyId"])->find(); + $onedrive = new Client([ + 'stream_back_end' => \Krizalys\Onedrive\StreamBackEnd::TEMP, + 'client_id' => $policyData["bucketname"], + + // Restore the previous state while instantiating this client to proceed in + // obtaining an access token. + 'state' => json_decode($policyData["sk"]), + ]); + + $filePath = ROOT_PATH . 'public/uploads/'.$this->taskContent["savePath"] . "/" . $this->taskContent["objname"]; + if($file = @fopen($filePath,"r")){ + try{ + $onedrive->createFile(urlencode($this->taskContent["objname"]),"/me/drive/root:/".$this->taskContent["savePath"],$file); + }catch(\Exception $e){ + $this->status="error"; + $this->errorMsg = $e->getMessage(); + return; + } + + $jsonData = array( + "path" => $this->taskContent["path"], + "fname" => $this->taskContent["fname"], + "objname" => $this->taskContent["savePath"]."/".$this->taskContent["objname"], + "fsize" => $this->taskContent["fsize"], + ); + + $addAction = FileManage::addFile($jsonData,$policyData,$this->taskModel["uid"],$this->taskContent["picInfo"]); + if(!$addAction[0]){ + // $tmpFileName = $Uploadinfo->getSaveName(); + // unset($Uploadinfo); + // $this->setError($addAction[1],true,$tmpFileName,$savePath); + } + + //TO-DO删除本地文件 + + fclose($file); + }else{ + $this->status = "error"; + $this->errorMsg = "Failed to open file [".$filePath."]"; + } + + } + } + ?> \ No newline at end of file diff --git a/application/index/model/UploadHandler.php b/application/index/model/UploadHandler.php index f83214fd..bd75fa65 100644 --- a/application/index/model/UploadHandler.php +++ b/application/index/model/UploadHandler.php @@ -193,15 +193,18 @@ class UploadHandler extends Model{ $task->taskContent = json_encode([ "path" => $info["path"], "fname" => $info["name"], - "objname" => $generatePath."/".$Uploadinfo->getSaveName(), + "objname" => $Uploadinfo->getSaveName(), + "savePath" => $generatePath, "fsize" => $Uploadinfo->getSize(), "picInfo" => $picInfo, "policyId" => $this->policyContent['id'] ]); + $task->userId = $this->userId; $task->saveTask(); echo json_encode(array("key" => $info["name"])); + FileManage::storageCheckOut($this->userId,$jsonData["fsize"],$Uploadinfo->getInfo('size')); return; }