add: Single Onedrive reUpload handle function

This commit is contained in:
HFO4 2018-09-05 19:19:24 +08:00
parent 159ee86b60
commit e989aa47d9
4 changed files with 111 additions and 3 deletions

View file

@ -1,20 +1,49 @@
<?php
namespace app\index\command;
use think\Db;
use think\console\Command;
use think\console\Input;
use think\console\Output;
class Task extends Command
{
const SLEEP_TIME = 1;
protected function configure()
{
$this->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);
}
}
}
}
?>

View file

@ -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);
}

View file

@ -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."]";
}
}
}
?>

View file

@ -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;
}