add: Onedrive adapter

pull/95/head
HFO4 7 years ago
parent 3462e6665b
commit 62eca52fe7

@ -16,7 +16,7 @@ class Installer{
/ /___| | (_) | |_| | (_| | | | __/\ V / __/
\____/|_|\___/ \__,_|\__,_|_| \___| \_/ \___|
Ver $version
Ver $version
================================================
";
$ioContext->write($welcomMsg);
@ -98,11 +98,11 @@ class Installer{
return self::getSqlInformation($event);
}
return [
"hostname" => $hostname,
"database" => $database,
"username" => $username,
"password" => $password,
"hostport" => $hostport,
"hostname" => $hostname,
"database" => $database,
"username" => $username,
"password" => $password,
"hostport" => $hostport,
];
}

@ -61,6 +61,9 @@ class FileManage extends Model{
case 'remote':
$this->adapter = new \app\index\model\RemoteAdapter($this->fileData,$this->policyData,$this->userData);
break;
case 'onedrive':
$this->adapter = new \app\index\model\OnedriveAdapter($this->fileData,$this->policyData,$this->userData);
break;
default:
# code...
break;
@ -90,6 +93,11 @@ class FileManage extends Model{
if($this->fileData["size"]>$sizeLimit){
die('{ "result": { "success": false, "error": "您当前用户组最大可编辑'.$sizeLimit.'字节的文件"} }');
}else{
try{
$fileContent = $this->adapter->getFileContent();
}catch(\Exception $e){
die('{ "result": { "success": false, "error": "'.$e->getMessage().'"} }');
}
$fileContent = $this->adapter->getFileContent();
$result["result"] = $fileContent;
if(empty(json_encode($result))){
@ -432,6 +440,9 @@ class FileManage extends Model{
}else if(in_array($key,$uniquePolicy["remoteList"])){
RemoteAdapter::DeleteFile($value,$uniquePolicy["remotePolicyData"][$key][0]);
self::deleteFileRecord(array_column($value, 'id'),array_sum(array_column($value, 'size')),$value[0]["upload_user"]);
}else if(in_array($key,$uniquePolicy["onedriveList"])){
OnedriveAdapter::DeleteFile($value,$uniquePolicy["onedrivePolicyData"][$key][0]);
self::deleteFileRecord(array_column($value, 'id'),array_sum(array_column($value, 'size')),$value[0]["upload_user"]);
}
}
return ["result"=>["success"=>true,"error"=>null]];
@ -715,6 +726,8 @@ class FileManage extends Model{
$s3PolicyData = [];
$remoteList = [];
$remotePolicyData = [];
$onedriveList = [];
$onedrivePolicyData = [];
foreach ($data as $key => $value) {
if(!in_array($value['policy_id'],$tempList)){
array_push($tempList,$value['policy_id']);
@ -762,6 +775,13 @@ class FileManage extends Model{
}
array_push($remotePolicyData[$value['policy_id']],$policyTempData);
break;
case 'onedrive':
array_push($onedriveList,$value['policy_id']);
if(empty($onedrivePolicyData[$value['policy_id']])){
$onedrivePolicyData[$value['policy_id']] = [];
}
array_push($onedrivePolicyData[$value['policy_id']],$policyTempData);
break;
default:
# code...
break;
@ -782,6 +802,8 @@ class FileManage extends Model{
's3PolicyData' => $s3PolicyData,
'remoteList' => $remoteList,
'remotePolicyData' => $remotePolicyData,
'onedriveList' => $onedriveList,
'onedrivePolicyData' => $onedrivePolicyData,
);
return $returenValue;
}

@ -0,0 +1,140 @@
<?php
namespace app\index\model;
use think\Model;
use think\Db;
use \Krizalys\Onedrive\Client;
use \app\index\model\Option;
/**
* Onedrive策略文件管理适配器
*/
class OnedriveAdapter extends Model{
private $fileModel;
private $policyModel;
private $userModel;
private $clinet;
public function __construct($file,$policy,$user){
$this->fileModel = $file;
$this->policyModel = $policy;
$this->userModel = $user;
$this->clinet = new Client([
'stream_back_end' => \Krizalys\Onedrive\StreamBackEnd::TEMP,
'client_id' => $this->policyModel["bucketname"],
// Restore the previous state while instantiating this client to proceed in
// obtaining an access token.
'state' => json_decode($this->policyModel["sk"]),
]);
}
/**
* 获取文本文件内容
*
* @return string 文件内容
*/
public function getFileContent(){
$file = new \Krizalys\Onedrive\File($this->clinet,"/me/drive/root:/".$this->fileModel["pre_name"].":");
return $file->fetchContent();
}
/**
* 签名预览URL
*
* @return void
*/
public function Preview($base=null,$name=null){
$preview = json_decode(json_encode($this->clinet->apiGet("/me/drive/root:/".rawurlencode($this->fileModel["pre_name"]).":")),true);
return [1,$preview["@microsoft.graph.downloadUrl"]];
}
/**
* 保存文件内容
*
* @param string $content 文件内容
* @return void
*/
public function saveContent($content){
$this->clinet->createFile(rawurldecode($this->fileModel["pre_name"]),"/me/drive/root:/",$content);
}
/**
* 计算缩略图大小
*
* @param int $width 原始宽
* @param int $height 原始高
* @return array
*/
static function getThumbSize($width,$height){
$rate = $width/$height;
$maxWidth = 90;
$maxHeight = 39;
$changeWidth = 39*$rate;
$changeHeight = 90/$rate;
if($changeWidth>=$maxWidth){
return [(int)$changeHeight,90];
}
return [39,(int)$changeWidth];
}
/**
* 获取缩略图地址
*
* @return string 缩略图地址
*/
public function getThumb(){
$picInfo = explode(",",$this->fileModel["pic_info"]);
$thumbSize = self::getThumbSize($picInfo[0],$picInfo[1]);
$thumb = json_decode(json_encode($this->clinet->apiGet("/me/drive/root:/".rawurlencode($this->fileModel["pre_name"]).":/thumbnails")),true);
return [1,$thumb["value"][0]["small"]["url"]];
}
/**
* 删除某一策略下的指定upyun文件
*
* @param array $fileList 待删除文件的数据库记录
* @param array $policyData 待删除文件的上传策略信息
* @return void
*/
static function DeleteFile($fileList,$policyData){
$clinet = 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"]),
]);
foreach (array_column($fileList, 'pre_name') as $key => $value) {
$clinet->deleteObject("/me/drive/root:/".rawurlencode($value).":");
}
}
/**
* 生成文件下载URL
*
* @return array
*/
public function Download(){
$preview = json_decode(json_encode($this->clinet->apiGet("/me/drive/root:/".rawurlencode($this->fileModel["pre_name"]).":")),true);
return [1,$preview["@microsoft.graph.downloadUrl"]];
}
/**
* 签名临时URL用于Office预览
*
* @return array
*/
public function signTmpUrl(){
return $this->Preview();
}
}
?>

@ -113,7 +113,7 @@ class UploadHandler extends Model{
$path = "";
}
$task = new Task();
$task->taskName = "Upload Big File " . $fname . "to Onedrive";
$task->taskName = "Upload Big File " . $fname . " to Onedrive";
$task->taskType = "uploadChunksToOnedrive";
$task->taskContent = json_encode([
"path" => $path,
@ -243,7 +243,7 @@ class UploadHandler extends Model{
//处理Onedrive等非直传
if($this->policyContent['policy_type'] == "onedrive"){
$task = new Task();
$task->taskName = "Upload" . $info["name"] . "to Onedrive";
$task->taskName = "Upload" . $info["name"] . " to Onedrive";
$task->taskType = "uploadSingleToOnedrive";
$task->taskContent = json_encode([
"path" => $info["path"],

@ -515,12 +515,10 @@ class Client
{
$url =
self::API_URL
. $path
. '?access_token=' . urlencode(
$this->_state->token->data->access_token
);
. $path;
$curl = self::_createCurl($path, $options);
curl_setopt($curl, CURLOPT_HTTPHEADER, [ 'Authorization: Bearer '
. $this->_state->token->data->access_token,]);
curl_setopt($curl, CURLOPT_URL, $url);
return $this->_processResult($curl);
}
@ -538,7 +536,6 @@ class Client
$url = self::API_URL . $path;
$data = (object) $data;
$curl = self::_createCurl($path);
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_POST => true,

File diff suppressed because one or more lines are too long

@ -127,10 +127,12 @@ function(e, r) {
},
e.openImagePreview = function() {
var r = e.singleSelection();
if(r.model.pic==""){
}else{
t =e.apiMiddleware.listPic(r);
loadPreview(t);
}
@ -566,6 +568,7 @@ function(e) {
perms: new n(r && r.rights),
content: r && r.content || "",
fileId: r && r.id || '',
pic: r && r.pic || '',
recursive: !1,
fullPath: function() {
var e = this.path.filter(Boolean);

Loading…
Cancel
Save