From 2b646fb6e308ceef7e74b06b5fd6bf69dfb2283a Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Sat, 8 Sep 2018 13:52:26 +0800 Subject: [PATCH] add: RemoteAdapter --- application/index/model/FileManage.php | 194 +--------------------- application/index/model/RemoteAdapter.php | 97 +++++++++++ extend/Krizalys/Onedrive/Client.php | 2 - 3 files changed, 102 insertions(+), 191 deletions(-) create mode 100644 application/index/model/RemoteAdapter.php diff --git a/application/index/model/FileManage.php b/application/index/model/FileManage.php index e335cd9c..f970d323 100644 --- a/application/index/model/FileManage.php +++ b/application/index/model/FileManage.php @@ -58,6 +58,9 @@ class FileManage extends Model{ case 's3': $this->adapter = new \app\index\model\S3Adapter($this->fileData,$this->policyData,$this->userData); break; + case 'remote': + $this->adapter = new \app\index\model\RemoteAdapter($this->fileData,$this->policyData,$this->userData); + break; default: # code... break; @@ -88,30 +91,6 @@ class FileManage extends Model{ die('{ "result": { "success": false, "error": "您当前用户组最大可编辑'.$sizeLimit.'字节的文件"} }'); }else{ $fileContent = $this->adapter->getFileContent(); - // switch ($this->policyData["policy_type"]) { - // case 'local': - // $filePath = ROOT_PATH . 'public/uploads/' . $this->fileData["pre_name"]; - // $fileContent = $this->getLocalFileContent($filePath); - // break; - // case 'qiniu': - // $fileContent = $this->getQiniuFileContent(); - // break; - // case 'oss': - // $fileContent = $this->getOssFileContent(); - // break; - // case 'upyun': - // $fileContent = $this->getUpyunFileContent(); - // break; - // case 's3': - // $fileContent = $this->getS3FileContent(); - // break; - // case 'remote': - // $fileContent = $this->getRemoteFileContent(); - // break; - // default: - // # code... - // break; - // } $result["result"] = $fileContent; if(empty(json_encode($result))){ $result["result"] = iconv('gb2312','utf-8',$fileContent); @@ -120,15 +99,6 @@ class FileManage extends Model{ } } - /** - * 获取远程策略文本文件内容 - * - * @return string 文件内容 - */ - public function getRemoteFileContent(){ - return file_get_contents($this->remotePreview()[1]); - } - /** * 保存可编辑文件 * @@ -142,48 +112,12 @@ class FileManage extends Model{ die('{ "result": { "success": false, "error": "空间容量不足" } }'); } $this->adapter->saveContent($content); - // switch ($this->policyData["policy_type"]) { - // case 'local': - // $filePath = ROOT_PATH . 'public/uploads/' . $this->fileData["pre_name"]; - // file_put_contents($filePath, ""); - // file_put_contents($filePath, $content); - // break; - // case 'qiniu': - // $this->saveQiniuContent($content); - // break; - // case 'oss': - // $this->saveOssContent($content); - // break; - // case 'upyun': - // $this->saveUpyunContent($content); - // break; - // case 's3': - // $this->saveS3Content($content); - // break; - // case 'remote': - // $this->saveRemoteContent($content); - // break; - // default: - // # code... - // break; - // } FileManage::storageGiveBack($this->userID,$originSize); FileManage::storageCheckOut($this->userID,$contentSize); Db::name('files')->where('id', $this->fileData["id"])->update(['size' => $contentSize]); echo ('{ "result": { "success": true} }'); } - /** - * 保存远程文件内容 - * - * @param string $content 文件内容 - * @return void - */ - public function saveRemoteContent($content){ - $remote = new Remote($this->policyData); - $remote->updateContent($this->fileData["pre_name"],$content); - } - /** * 文件名合法性初步检查 * @@ -358,35 +292,6 @@ class FileManage extends Model{ */ public function PreviewHandler($isAdmin=false){ return $this->adapter->Preview($isAdmin); - // switch ($this->policyData["policy_type"]) { - // case 'qiniu': - // $Redirect = $this->qiniuPreview(); - // return $Redirect; - // break; - // case 'local': - // $Redirect = $this->localPreview($isAdmin); - // return $Redirect; - // break; - // case 'oss': - // $Redirect = $this->ossPreview(); - // return $Redirect; - // break; - // case 'upyun': - // $Redirect = $this->upyunPreview(); - // return $Redirect; - // break; - // case 's3': - // $Redirect = $this->s3Preview(); - // return $Redirect; - // break; - // case 'remote': - // $Redirect = $this->remotePreview(); - // return $Redirect; - // break; - // default: - // # code... - // break; - // } } /** @@ -396,30 +301,6 @@ class FileManage extends Model{ */ public function getThumb(){ return $this->adapter->getThumb(); - // switch ($this->policyData["policy_type"]) { - // case 'qiniu': - // $Redirect = $this->getQiniuThumb(); - // return $Redirect; - // case 'local': - // $Redirect = $this->getLocalThumb(); - // return $Redirect; - // break; - // case 'oss': - // $Redirect = $this->getOssThumb(); - // return $Redirect; - // break; - // case 'upyun': - // $Redirect = $this->getUpyunThumb(); - // return $Redirect; - // break; - // case 'remote': - // $remote = new Remote($this->policyData); - // return [1,$remote->thumb($this->fileData["pre_name"],explode(",",$this->fileData["pic_info"]))]; - // break; - // default: - // # code... - // break; - // } } /** @@ -430,29 +311,6 @@ class FileManage extends Model{ */ public function Download($isAdmin=false){ return $this->adapter->Download($isAdmin); - // switch ($this->policyData["policy_type"]) { - // case 'qiniu': - // return $DownloadHandler = $this->qiniuDownload(); - // break; - // case 'local': - // return $DownloadHandler = $this->localDownload($isAdmin); - // break; - // case 'oss': - // return $DownloadHandler = $this->ossDownload(); - // break; - // case 'upyun': - // return $DownloadHandler = $this->upyunDownload(); - // break; - // case 's3': - // return $DownloadHandler = $this->s3Download(); - // break; - // case 'remote': - // return $DownloadHandler = $this->remoteDownload(); - // break; - // default: - // # code... - // break; - // } } /** @@ -572,7 +430,8 @@ class FileManage extends Model{ S3Adapter::DeleteFile($value,$uniquePolicy["s3PolicyData"][$key][0]); self::deleteFileRecord(array_column($value, 'id'),array_sum(array_column($value, 'size')),$value[0]["upload_user"]); }else if(in_array($key,$uniquePolicy["remoteList"])){ - self::remoteDelete($value,$uniquePolicy["remotePolicyData"][$key][0]); + RemoteAdapter::DeleteFile($value,$uniquePolicy["remotePolicyData"][$key][0]); + self::deleteFileRecord(array_column($value, 'id'),array_sum(array_column($value, 'size')),$value[0]["upload_user"]); } } return ["result"=>["success"=>true,"error"=>null]]; @@ -644,12 +503,6 @@ class FileManage extends Model{ } - static function remoteDelete($fileList,$policyData){ - $remoteObj = new Remote($policyData); - $remoteObj->remove(array_column($fileList, 'pre_name')); - self::deleteFileRecord(array_column($fileList, 'id'),array_sum(array_column($fileList, 'size')),$fileList[0]["upload_user"]); - } - static function deleteFileRecord($id,$size,$uid){ Db::name('files')->where([ 'id' => ["in",$id], @@ -664,16 +517,6 @@ class FileManage extends Model{ ])->setDec('used_storage', $size); } - public function remotePreview(){ - $remote = new Remote($this->policyData); - return [1,$remote->preview($this->fileData["pre_name"])]; - } - - private function remoteDownload(){ - $remote = new Remote($this->policyData); - return [1,$remote->download($this->fileData["pre_name"],$this->fileData["orign_name"])]; - } - /** * [List description] * @param [type] $path [description] @@ -945,33 +788,6 @@ class FileManage extends Model{ public function signTmpUrl(){ return $this->adapter->signTmpUrl()[1]; - // switch ($this->policyData["policy_type"]) { - // case 'qiniu': - // return $this->qiniuPreview()[1]; - // break; - // case 'oss': - // return $this->ossPreview()[1]; - // break; - // case 'upyun': - // return $this->upyunPreview()[1]; - // break; - // case 's3': - // return $this->s3Preview()[1]; - // break; - // case 'local': - // $options = Option::getValues(["oss","basic"]); - // $timeOut = $options["timeout"]; - // $delayTime = time()+$timeOut; - // $key=$this->fileData["id"].":".$delayTime.":".md5($this->userData["user_pass"].$this->fileData["id"].$delayTime.config("salt")); - // return $options['siteURL']."Callback/TmpPreview/key/".$key; - // break; - // case 'remote': - // return $this->remotePreview()[1]; - // break; - // default: - // # code... - // break; - // } } } diff --git a/application/index/model/RemoteAdapter.php b/application/index/model/RemoteAdapter.php new file mode 100644 index 00000000..a2d9a334 --- /dev/null +++ b/application/index/model/RemoteAdapter.php @@ -0,0 +1,97 @@ +fileModel = $file; + $this->policyModel = $policy; + $this->userModel = $user; + } + + /** + * 获取文本文件内容 + * + * @return string 文件内容 + */ + public function getFileContent(){ + return file_get_contents($this->Preview()[1]); + } + + /** + * 签名文件预览URL + * + * @return void + */ + public function Preview(){ + $remote = new Remote($this->policyModel); + return [1,$remote->preview($this->fileModel["pre_name"])]; + } + + /** + * 保存文件内容 + * + * @param string $content 文件内容 + * @return bool + */ + public function saveContent($content){ + $remote = new Remote($this->policyModel); + $remote->updateContent($this->fileModel["pre_name"],$content); + } + + /** + * 获取缩略图地址 + * + * @return string 缩略图地址 + */ + public function getThumb(){ + $remote = new Remote($this->policyModel); + return [1,$remote->thumb($this->fileModel["pre_name"],explode(",",$this->fileModel["pic_info"]))]; + } + +/** + * 删除某一策略下的指定文件 + * + * @param array $fileList 待删除文件的数据库记录 + * @param array $policyData 待删除文件的上传策略信息 + * @return void + */ + static function deleteFile($fileList,$policyData){ + $remoteObj = new Remote($policyData); + $remoteObj->remove(array_column($fileList, 'pre_name')); + } + + /** + * 生成文件下载URL + * + * @return array + */ + public function Download(){ + $remote = new Remote($this->policyModel); + return [1,$remote->download($this->fileModel["pre_name"],$this->fileModel["orign_name"])]; + } + + /** + * 签名临时URL用于Office预览 + * + * @return array + */ + public function signTmpUrl(){ + return $this->Preview(); + } + +} + +?> \ No newline at end of file diff --git a/extend/Krizalys/Onedrive/Client.php b/extend/Krizalys/Onedrive/Client.php index 40aae94e..c9c6cf73 100644 --- a/extend/Krizalys/Onedrive/Client.php +++ b/extend/Krizalys/Onedrive/Client.php @@ -1,7 +1,5 @@