From 61910ccf3537d2003e6496d280c71d751d9c3d5a Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Wed, 5 Sep 2018 15:55:51 +0800 Subject: [PATCH] add: Onedrive --- application/command.php | 4 +- application/index/command/Task.php | 20 ++++++++ application/index/controller/Admin.php | 15 ++++++ application/index/model/AdminHandler.php | 63 ++++++++++++++++++++++++ composer.json | 3 +- task | 17 +++++++ 6 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 application/index/command/Task.php create mode 100644 task diff --git a/application/command.php b/application/command.php index 826bb2b2..a6e5e096 100644 --- a/application/command.php +++ b/application/command.php @@ -9,4 +9,6 @@ // | Author: yunwuxin <448901948@qq.com> // +---------------------------------------------------------------------- -return []; +return [ + 'app\index\command\Task', +]; diff --git a/application/index/command/Task.php b/application/index/command/Task.php new file mode 100644 index 00000000..35726e88 --- /dev/null +++ b/application/index/command/Task.php @@ -0,0 +1,20 @@ +setName('run')->setDescription('Start processing tasks for Cloudreve'); + } + + protected function execute(Input $input, Output $output) + { + $output->writeln("TestCommand:"); + } +} +?> \ No newline at end of file diff --git a/application/index/controller/Admin.php b/application/index/controller/Admin.php index 0a2c52f5..e5f16bb4 100644 --- a/application/index/controller/Admin.php +++ b/application/index/controller/Admin.php @@ -513,5 +513,20 @@ class Admin extends Controller{ return json(['error'=>1,'message'=>"取消失败"]); } } + + public function UpdateOnedriveToken(){ + $policyId = input("get.id"); + $this->adminObj->updateOnedriveToken($policyId); + + } + + public function OneDriveCalllback(){ + $code = input("get.code"); + $this->adminObj->oneDriveCalllback($code); + } + + public function uploadTest(){ + $this->adminObj->oneDriveTest(); + } } diff --git a/application/index/model/AdminHandler.php b/application/index/model/AdminHandler.php index c8e4bcf1..cfc48b28 100644 --- a/application/index/model/AdminHandler.php +++ b/application/index/model/AdminHandler.php @@ -5,6 +5,7 @@ use think\Model; use think\Db; use \app\index\model\Mail; use \app\index\model\FileManage; +use \Krizalys\Onedrive\Client; class AdminHandler extends Model{ @@ -709,6 +710,68 @@ class AdminHandler extends Model{ } return ["error"=>0,"msg"=>"设置已保存"]; } + + public function updateOnedriveToken($policyId){ + $policyData = Db::name("policy")->where("id",$policyId)->find(); + + if(empty($policyData)){ + throw new \think\Exception("Policy not found"); + } + $onedrive = new Client([ + 'client_id' => $policyData["bucketname"], + ]); + $url = $onedrive->getLogInUrl([ + 'offline_access', + 'files.readwrite.all', + ], Option::getValue("siteURL")."Admin/oneDriveCalllback"); + echo "继续绑定账号"; + + Db::name("policy")->where("id",$policyId)->update([ + "sk" => json_encode($onedrive->getState()), + ]); + \think\Session::set('onedrive.pid',$policyId); + + } + + public function oneDriveCalllback($code){ + $policyId = \think\Session::get('onedrive.pid'); + $policyData = Db::name("policy")->where("id",$policyId)->find(); + $onedrive = new Client([ + 'client_id' => $policyData["bucketname"], + + // Restore the previous state while instantiating this client to proceed in + // obtaining an access token. + 'state' => json_decode($policyData["sk"]), + ]); + + // Obtain the token using the code received by the OneDrive API. + $onedrive->obtainAccessToken($policyData["ak"], $_GET['code']); + + // Persist the OneDrive client' state for next API requests. + Db::name("policy")->where("id",$policyId)->update([ + "sk" => json_encode($onedrive->getState()), + ]); + } + + public function oneDriveTest(){ + $policyId = \think\Session::get('onedrive.pid'); + $policyData = Db::name("policy")->where("id",$policyId)->find(); + + $onedrive = new Client([ + 'client_id' => $policyData["bucketname"], + + // Restore the previous state while instantiating this client to proceed in + // obtaining an access token. + 'state' => json_decode($policyData["sk"]), + ]); + $onedrive->renewAccessToken($policyData["ak"]); + Db::name("policy")->where("id",$policyId)->update([ + "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); + } + } ?> \ No newline at end of file diff --git a/composer.json b/composer.json index 73b481b2..2f65e37a 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,8 @@ "topthink/think-captcha":"1.*", "aliyuncs/oss-sdk-php": "~2.0", "sabre/dav":"~3.2.0", - "upyun/sdk": "^3.3" + "upyun/sdk": "^3.3", + "krizalys/onedrive-php-sdk": "^1.2" }, "autoload": { "psr-0": { diff --git a/task b/task new file mode 100644 index 00000000..4e2688f3 --- /dev/null +++ b/task @@ -0,0 +1,17 @@ +#!/usr/bin/env php + +// +---------------------------------------------------------------------- + +// 定义项目路径 +define('APP_PATH', __DIR__ . '/application/'); + +// 加载框架引导文件 +require __DIR__.'/thinkphp/console.php';