add: S3Adapter & OssAdapter

pull/95/head
HFO4 7 years ago
parent 195d747c5d
commit 044ece0b80

@ -5,8 +5,6 @@ use think\Model;
use think\Db; use think\Db;
use think\Validate; use think\Validate;
use \app\index\model\Option; use \app\index\model\Option;
use Upyun\Upyun;
use Upyun\Config;
class FileManage extends Model{ class FileManage extends Model{
@ -54,6 +52,12 @@ class FileManage extends Model{
case 'oss': case 'oss':
$this->adapter = new \app\index\model\OssAdapter($this->fileData,$this->policyData,$this->userData); $this->adapter = new \app\index\model\OssAdapter($this->fileData,$this->policyData,$this->userData);
break; break;
case 'upyun':
$this->adapter = new \app\index\model\UpyunAdapter($this->fileData,$this->policyData,$this->userData);
break;
case 's3':
$this->adapter = new \app\index\model\S3Adapter($this->fileData,$this->policyData,$this->userData);
break;
default: default:
# code... # code...
break; break;
@ -116,24 +120,6 @@ class FileManage extends Model{
} }
} }
/**
* 获取又拍云策略文本文件内容
*
* @return string 文件内容
*/
public function getUpyunFileContent(){
return file_get_contents($this->upyunPreview()[1]);
}
/**
* 获取S3策略文本文件内容
*
* @return string 文件内容
*/
public function getS3FileContent(){
return file_get_contents($this->s3Preview()[1]);
}
/** /**
* 获取远程策略文本文件内容 * 获取远程策略文本文件内容
* *
@ -187,33 +173,6 @@ class FileManage extends Model{
echo ('{ "result": { "success": true} }'); echo ('{ "result": { "success": true} }');
} }
/**
* 保存Upyun文件内容
*
* @param string $content 文件内容
* @return void
*/
public function saveUpyunContent($content){
$bucketConfig = new Config($this->policyData["bucketname"], $this->policyData["op_name"], $this->policyData["op_pwd"]);
$client = new Upyun($bucketConfig);
if(empty($content)){
$content = " ";
}
$res=$client->write($this->fileData["pre_name"],$content);
}
/**
* 保存S3文件内容
*
* @param string $content 文件内容
* @return void
*/
public function saveS3Content($content){
$s3 = new \S3\S3($this->policyData["ak"], $this->policyData["sk"],false,$this->policyData["op_pwd"]);
$s3->setSignatureVersion('v4');
$s3->putObjectString($content, $this->policyData["bucketname"], $this->fileData["pre_name"]);
}
/** /**
* 保存远程文件内容 * 保存远程文件内容
* *
@ -604,12 +563,14 @@ class FileManage extends Model{
LocalAdapter::DeleteFile($value,$uniquePolicy["localPolicyData"][$key][0]); LocalAdapter::DeleteFile($value,$uniquePolicy["localPolicyData"][$key][0]);
self::deleteFileRecord(array_column($value, 'id'),array_sum(array_column($value, 'size')),$value[0]["upload_user"]); self::deleteFileRecord(array_column($value, 'id'),array_sum(array_column($value, 'size')),$value[0]["upload_user"]);
}else if(in_array($key,$uniquePolicy["ossList"])){ }else if(in_array($key,$uniquePolicy["ossList"])){
LocalAdapter::DeleteFile($value,$uniquePolicy["ossPolicyData"][$key][0]); OssAdapter::DeleteFile($value,$uniquePolicy["ossPolicyData"][$key][0]);
self::deleteFileRecord(array_column($value, 'id'),array_sum(array_column($value, 'size')),$value[0]["upload_user"]); self::deleteFileRecord(array_column($value, 'id'),array_sum(array_column($value, 'size')),$value[0]["upload_user"]);
}else if(in_array($key,$uniquePolicy["upyunList"])){ }else if(in_array($key,$uniquePolicy["upyunList"])){
self::upyunDelete($value,$uniquePolicy["upyunPolicyData"][$key][0]); UpyunAdapter::DeleteFile($value,$uniquePolicy["upyunPolicyData"][$key][0]);
self::deleteFileRecord(array_column($value, 'id'),array_sum(array_column($value, 'size')),$value[0]["upload_user"]);
}else if(in_array($key,$uniquePolicy["s3List"])){ }else if(in_array($key,$uniquePolicy["s3List"])){
self::s3Delete($value,$uniquePolicy["s3PolicyData"][$key][0]); 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"])){ }else if(in_array($key,$uniquePolicy["remoteList"])){
self::remoteDelete($value,$uniquePolicy["remotePolicyData"][$key][0]); self::remoteDelete($value,$uniquePolicy["remotePolicyData"][$key][0]);
} }
@ -683,27 +644,6 @@ class FileManage extends Model{
} }
/**
* 删除某一策略下的指定upyun文件
*
* @param array $fileList 待删除文件的数据库记录
* @param array $policyData 待删除文件的上传策略信息
* @return void
*/
static function upyunDelete($fileList,$policyData){
foreach (array_column($fileList, 'pre_name') as $key => $value) {
self::deleteUpyunFile($value,$policyData);
}
self::deleteFileRecord(array_column($fileList, 'id'),array_sum(array_column($fileList, 'size')),$fileList[0]["upload_user"]);
}
static function s3Delete($fileList,$policyData){
foreach (array_column($fileList, 'pre_name') as $key => $value) {
self::deleteS3File($value,$policyData);
}
self::deleteFileRecord(array_column($fileList, 'id'),array_sum(array_column($fileList, 'size')),$fileList[0]["upload_user"]);
}
static function remoteDelete($fileList,$policyData){ static function remoteDelete($fileList,$policyData){
$remoteObj = new Remote($policyData); $remoteObj = new Remote($policyData);
$remoteObj->remove(array_column($fileList, 'pre_name')); $remoteObj->remove(array_column($fileList, 'pre_name'));
@ -724,56 +664,11 @@ class FileManage extends Model{
])->setDec('used_storage', $size); ])->setDec('used_storage', $size);
} }
private function getUpyunThumb(){
$picInfo = explode(",",$this->fileData["pic_info"]);
$thumbSize = self::getThumbSize($picInfo[0],$picInfo[1]);
$baseUrl =$this->policyData["url"].$this->fileData["pre_name"]."!/fwfh/90x39";
return [1,$this->upyunPreview($baseUrl,$this->fileData["pre_name"]."!/fwfh/90x39")[1]];
}
public function s3Preview(){
$timeOut = Option::getValue("timeout");
return [1,\S3\S3::aws_s3_link($this->policyData["ak"], $this->policyData["sk"],$this->policyData["bucketname"],"/".$this->fileData["pre_name"],3600,$this->policyData["op_name"])];
}
public function remotePreview(){ public function remotePreview(){
$remote = new Remote($this->policyData); $remote = new Remote($this->policyData);
return [1,$remote->preview($this->fileData["pre_name"])]; return [1,$remote->preview($this->fileData["pre_name"])];
} }
public function upyunPreview($base=null,$name=null){
if(!$this->policyData['bucket_private']){
$fileUrl = $this->policyData["url"].$this->fileData["pre_name"]."?auth=0";
if(!empty($base)){
$fileUrl = $base;
}
return[true,$fileUrl];
}else{
$baseUrl = $this->policyData["url"].$this->fileData["pre_name"];
if(!empty($base)){
$baseUrl = $base;
}
$etime = time() + Option::getValue("timeout");
$key = $this->policyData["sk"];
$path = "/".$this->fileData["pre_name"];
if(!empty($name)){
$path = "/".$name;
}
$sign = substr(md5($key.'&'.$etime.'&'.$path), 12, 8).$etime;
$signedUrl = $baseUrl."?_upt=".$sign;
return[true,$signedUrl];
}
}
public function upyunDownload(){
return [true,$this->upyunPreview()[1]."&_upd=".urlencode($this->fileData["orign_name"])];
}
public function s3Download(){
$timeOut = Option::getValue("timeout");
return [1,\S3\S3::aws_s3_link($this->policyData["ak"], $this->policyData["sk"],$this->policyData["bucketname"],"/".$this->fileData["pre_name"],3600,$this->policyData["op_name"],array(),false)];
}
private function remoteDownload(){ private function remoteDownload(){
$remote = new Remote($this->policyData); $remote = new Remote($this->policyData);
return [1,$remote->download($this->fileData["pre_name"],$this->fileData["orign_name"])]; return [1,$remote->download($this->fileData["pre_name"],$this->fileData["orign_name"])];
@ -952,10 +847,10 @@ class FileManage extends Model{
return OssAdapter::deleteOssFile($fname,$policy); return OssAdapter::deleteOssFile($fname,$policy);
break; break;
case 'upyun': case 'upyun':
return self::deleteUpyunFile($fname,$policy); return UpyunAdapter::deleteUpyunFile($fname,$policy);
break; break;
case 's3': case 's3':
return self::deleteS3File($fname,$policy); return S3Adapter::deleteS3File($fname,$policy);
break; break;
default: default:
# code... # code...
@ -963,18 +858,6 @@ class FileManage extends Model{
} }
} }
static function deleteUpyunFile($fname,$policy){
$bucketConfig = new Config($policy["bucketname"], $policy["op_name"], $policy["op_pwd"]);
$client = new Upyun($bucketConfig);
$res=$client->delete($fname,true);
}
static function deleteS3File($fname,$policy){
$s3 = new \S3\S3($policy["ak"], $policy["sk"],false,$policy["op_pwd"]);
$s3->setSignatureVersion('v4');
return $s3->deleteObject($policy["bucketname"],$fname);
}
static function uniqueArray($data = array()){ static function uniqueArray($data = array()){
$tempList = []; $tempList = [];
$qiniuList = []; $qiniuList = [];

@ -117,7 +117,7 @@ class OssAdapter extends Model{
* @param array $policyData 待删除文件的上传策略信息 * @param array $policyData 待删除文件的上传策略信息
* @return void * @return void
*/ */
static function ossDelete($fileList,$policyData){ static function DeleteFile($fileList,$policyData){
$accessKeyId = $policyData["ak"]; $accessKeyId = $policyData["ak"];
$accessKeySecret = $policyData["sk"]; $accessKeySecret = $policyData["sk"];
$endpoint = "http".ltrim(ltrim($policyData["server"],"https"),"http"); $endpoint = "http".ltrim(ltrim($policyData["server"],"https"),"http");
@ -131,7 +131,6 @@ class OssAdapter extends Model{
} catch(OssException $e) { } catch(OssException $e) {
return false; return false;
} }
self::deleteFileRecord(array_column($fileList, 'id'),array_sum(array_column($fileList, 'size')),$fileList[0]["upload_user"]);
} }
/** /**

@ -40,7 +40,7 @@ class QiniuAdapter extends Model{
* @return void * @return void
*/ */
public function Preview($thumb=null){ public function Preview($thumb=null){
if($thumb===true){ if($thumb===true || $thumb===false){
$thumb =null; $thumb =null;
} }
if(!$this->policyModel['bucket_private']){ if(!$this->policyModel['bucket_private']){

@ -0,0 +1,109 @@
<?php
namespace app\index\model;
use think\Model;
use think\Db;
use Upyun\Upyun;
use Upyun\Config;
use \app\index\model\Option;
/**
* AWS S3策略文件管理适配器
*/
class S3Adapter extends Model{
private $fileModel;
private $policyModel;
private $userModel;
public function __construct($file,$policy,$user){
$this->fileModel = $file;
$this->policyModel = $policy;
$this->userModel = $user;
}
/**
* 获取又拍云策略文本文件内容
*
* @return string 文件内容
*/
public function getFileContent(){
return file_get_contents($this->Preview()[1]);
}
/**
* 签名S3预览URL
*
* @return void
*/
public function Preview($base=null,$name=null){
if($base===true || $base ===false){
$base = null;
}
$timeOut = Option::getValue("timeout");
return [1,\S3\S3::aws_s3_link($this->policyModel["ak"], $this->policyModel["sk"],$this->policyModel["bucketname"],"/".$this->fileModel["pre_name"],3600,$this->policyModel["op_name"])];
}
/**
* 保存文件内容
*
* @param string $content 文件内容
* @return void
*/
public function saveContent($content){
$s3 = new \S3\S3($this->policyModel["ak"], $this->policyModel["sk"],false,$this->policyModel["op_pwd"]);
$s3->setSignatureVersion('v4');
$s3->putObjectString($content, $this->policyModel["bucketname"], $this->fileModel["pre_name"]);
}
/**
* 删除某一策略下的指定文件
*
* @param array $fileList 待删除文件的数据库记录
* @param array $policyData 待删除文件的上传策略信息
* @return void
*/
static function DeleteFile($fileList,$policyData){
foreach (array_column($fileList, 'pre_name') as $key => $value) {
self::deleteS3File($value,$policyData);
}
}
/**
* 生成文件下载URL
*
* @return array
*/
public function Download(){
$timeOut = Option::getValue("timeout");
return [1,\S3\S3::aws_s3_link($this->policyModel["ak"], $this->policyModel["sk"],$this->policyModel["bucketname"],"/".$this->fileModel["pre_name"],3600,$this->policyModel["op_name"],array(),false)];
}
/**
* 删除临时文件
*
* @param string $fname 文件名
* @param array $policy 上传策略信息
* @return boolean
*/
static function deleteS3File($fname,$policy){
$s3 = new \S3\S3($policy["ak"], $policy["sk"],false,$policy["op_pwd"]);
$s3->setSignatureVersion('v4');
return $s3->deleteObject($policy["bucketname"],$fname);
}
/**
* 签名临时URL用于Office预览
*
* @return array
*/
public function signTmpUrl(){
return $this->Preview();
}
}
?>

@ -0,0 +1,162 @@
<?php
namespace app\index\model;
use think\Model;
use think\Db;
use Upyun\Upyun;
use Upyun\Config;
use \app\index\model\Option;
/**
* 又拍云策略文件管理适配器
*/
class UpyunAdapter extends Model{
private $fileModel;
private $policyModel;
private $userModel;
public function __construct($file,$policy,$user){
$this->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($base=null,$name=null){
if($base===true || $base===false){
$base =null;
}
if(!$this->policyModel['bucket_private']){
$fileUrl = $this->policyModel["url"].$this->fileModel["pre_name"]."?auth=0";
if(!empty($base)){
$fileUrl = $base;
}
return[true,$fileUrl];
}else{
$baseUrl = $this->policyModel["url"].$this->fileModel["pre_name"];
if(!empty($base)){
$baseUrl = $base;
}
$etime = time() + Option::getValue("timeout");
$key = $this->policyModel["sk"];
$path = "/".$this->fileModel["pre_name"];
if(!empty($name)){
$path = "/".$name;
}
$sign = substr(md5($key.'&'.$etime.'&'.$path), 12, 8).$etime;
$signedUrl = $baseUrl."?_upt=".$sign;
return[true,$signedUrl];
}
}
/**
* 保存文件内容
*
* @param string $content 文件内容
* @return void
*/
public function saveContent($content){
$bucketConfig = new Config($this->policyModel["bucketname"], $this->policyModel["op_name"], $this->policyModel["op_pwd"]);
$client = new Upyun($bucketConfig);
if(empty($content)){
$content = " ";
}
$res=$client->write($this->fileModel["pre_name"],$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]);
$baseUrl =$this->policyModel["url"].$this->fileModel["pre_name"]."!/fwfh/90x39";
return [1,$this->Preview($baseUrl,$this->fileModel["pre_name"]."!/fwfh/90x39")[1]];
}
/**
* 删除某一策略下的指定upyun文件
*
* @param array $fileList 待删除文件的数据库记录
* @param array $policyData 待删除文件的上传策略信息
* @return void
*/
static function DeleteFile($fileList,$policyData){
foreach (array_column($fileList, 'pre_name') as $key => $value) {
self::deleteUpyunFile($value,$policyData);
}
}
/**
* 生成文件下载URL
*
* @return array
*/
public function Download(){
return [true,$this->Preview()[1]."&_upd=".urlencode($this->fileModel["orign_name"])];
}
/**
* 删除临时文件
*
* @param string $fname 文件名
* @param array $policy 上传策略信息
* @return boolean
*/
static function deleteUpyunFile($fname,$policy){
$bucketConfig = new Config($policy["bucketname"], $policy["op_name"], $policy["op_pwd"]);
$client = new Upyun($bucketConfig);
$res=$client->delete($fname,true);
}
/**
* 签名临时URL用于Office预览
*
* @return array
*/
public function signTmpUrl(){
return $this->Preview();
}
}
?>
Loading…
Cancel
Save