提交 6593cb7d 编写于 作者: H HFO4

add: LocalAdapter

上级 7a34a5ff
...@@ -4,7 +4,7 @@ namespace app\index\model; ...@@ -4,7 +4,7 @@ namespace app\index\model;
use think\Model; use think\Model;
use think\Db; use think\Db;
use \app\index\model\Option; use \app\index\model\Option;
use \app\index\model\FileManage; use \app\index\model\LocalAdapter;
class Avatar extends Model{ class Avatar extends Model{
...@@ -90,7 +90,7 @@ class Avatar extends Model{ ...@@ -90,7 +90,7 @@ class Avatar extends Model{
$filePath = ROOT_PATH . 'public/avatars/' . $this->fileName.$siezSuffix; $filePath = ROOT_PATH . 'public/avatars/' . $this->fileName.$siezSuffix;
if(file_exists($filePath)){ if(file_exists($filePath)){
ob_end_clean(); ob_end_clean();
header('Content-Type: '.FileManage::getMimetype($filePath)); header('Content-Type: '.LocalAdapter::getMimetype($filePath));
$fileObj = fopen($filePath,"r"); $fileObj = fopen($filePath,"r");
while(!feof($fileObj)){ while(!feof($fileObj)){
echo fread($fileObj,2097152); echo fread($fileObj,2097152);
...@@ -131,7 +131,7 @@ class Avatar extends Model{ ...@@ -131,7 +131,7 @@ class Avatar extends Model{
} }
ob_end_clean(); ob_end_clean();
$filePath = ROOT_PATH . 'static/img/default.png' .$siezSuffix; $filePath = ROOT_PATH . 'static/img/default.png' .$siezSuffix;
header('Content-Type: '.FileManage::getMimetype($filePath)); header('Content-Type: '.LocalAdapter::getMimetype($filePath));
$fileObj = fopen($filePath,"r"); $fileObj = fopen($filePath,"r");
while(!feof($fileObj)){ while(!feof($fileObj)){
echo fread($fileObj,2097152); echo fread($fileObj,2097152);
......
...@@ -497,30 +497,31 @@ class FileManage extends Model{ ...@@ -497,30 +497,31 @@ class FileManage extends Model{
* @return array 重定向信息 * @return array 重定向信息
*/ */
public function getThumb(){ public function getThumb(){
switch ($this->policyData["policy_type"]) { return $this->adapter->getThumb();
case 'qiniu': // switch ($this->policyData["policy_type"]) {
$Redirect = $this->getQiniuThumb(); // case 'qiniu':
return $Redirect; // $Redirect = $this->getQiniuThumb();
case 'local': // return $Redirect;
$Redirect = $this->getLocalThumb(); // case 'local':
return $Redirect; // $Redirect = $this->getLocalThumb();
break; // return $Redirect;
case 'oss': // break;
$Redirect = $this->getOssThumb(); // case 'oss':
return $Redirect; // $Redirect = $this->getOssThumb();
break; // return $Redirect;
case 'upyun': // break;
$Redirect = $this->getUpyunThumb(); // case 'upyun':
return $Redirect; // $Redirect = $this->getUpyunThumb();
break; // return $Redirect;
case 'remote': // break;
$remote = new Remote($this->policyData); // case 'remote':
return [1,$remote->thumb($this->fileData["pre_name"],explode(",",$this->fileData["pic_info"]))]; // $remote = new Remote($this->policyData);
break; // return [1,$remote->thumb($this->fileData["pre_name"],explode(",",$this->fileData["pic_info"]))];
default: // break;
# code... // default:
break; // # code...
} // break;
// }
} }
/** /**
...@@ -530,29 +531,30 @@ class FileManage extends Model{ ...@@ -530,29 +531,30 @@ class FileManage extends Model{
* @return array 文件下载URL * @return array 文件下载URL
*/ */
public function Download($isAdmin=false){ public function Download($isAdmin=false){
switch ($this->policyData["policy_type"]) { return $this->adapter->Download($isAdmin);
case 'qiniu': // switch ($this->policyData["policy_type"]) {
return $DownloadHandler = $this->qiniuDownload(); // case 'qiniu':
break; // return $DownloadHandler = $this->qiniuDownload();
case 'local': // break;
return $DownloadHandler = $this->localDownload($isAdmin); // case 'local':
break; // return $DownloadHandler = $this->localDownload($isAdmin);
case 'oss': // break;
return $DownloadHandler = $this->ossDownload(); // case 'oss':
break; // return $DownloadHandler = $this->ossDownload();
case 'upyun': // break;
return $DownloadHandler = $this->upyunDownload(); // case 'upyun':
break; // return $DownloadHandler = $this->upyunDownload();
case 's3': // break;
return $DownloadHandler = $this->s3Download(); // case 's3':
break; // return $DownloadHandler = $this->s3Download();
case 'remote': // break;
return $DownloadHandler = $this->remoteDownload(); // case 'remote':
break; // return $DownloadHandler = $this->remoteDownload();
default: // break;
# code... // default:
break; // # code...
} // break;
// }
} }
/** /**
...@@ -659,7 +661,8 @@ class FileManage extends Model{ ...@@ -659,7 +661,8 @@ class FileManage extends Model{
if(in_array($key,$uniquePolicy["qiniuList"])){ if(in_array($key,$uniquePolicy["qiniuList"])){
self::qiniuDelete($value,$uniquePolicy["qiniuPolicyData"][$key][0]); self::qiniuDelete($value,$uniquePolicy["qiniuPolicyData"][$key][0]);
}else if(in_array($key,$uniquePolicy["localList"])){ }else if(in_array($key,$uniquePolicy["localList"])){
self::localDelete($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"]);
}else if(in_array($key,$uniquePolicy["ossList"])){ }else if(in_array($key,$uniquePolicy["ossList"])){
self::ossDelete($value,$uniquePolicy["ossPolicyData"][$key][0]); self::ossDelete($value,$uniquePolicy["ossPolicyData"][$key][0]);
}else if(in_array($key,$uniquePolicy["upyunList"])){ }else if(in_array($key,$uniquePolicy["upyunList"])){
...@@ -739,24 +742,6 @@ class FileManage extends Model{ ...@@ -739,24 +742,6 @@ class FileManage extends Model{
} }
/**
* 删除某一策略下的指定本地文件
*
* @param array $fileList 待删除文件的数据库记录
* @param array $policyData 待删除文件的上传策略信息
* @return void
*/
static function localDelete($fileList,$policyData){
$fileListTemp = array_column($fileList, 'pre_name');
foreach ($fileListTemp as $key => $value) {
@unlink(ROOT_PATH . 'public/uploads/'.$value);
if(file_exists(ROOT_PATH . 'public/thumb/'.$value."_thumb")){
@unlink(ROOT_PATH . 'public/thumb/'.$value."_thumb");
}
}
self::deleteFileRecord(array_column($fileList, 'id'),array_sum(array_column($fileList, 'size')),$fileList[0]["upload_user"]);
}
/** /**
* 删除某一策略下的指定七牛文件 * 删除某一策略下的指定七牛文件
* *
...@@ -839,46 +824,6 @@ class FileManage extends Model{ ...@@ -839,46 +824,6 @@ class FileManage extends Model{
])->setDec('used_storage', $size); ])->setDec('used_storage', $size);
} }
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];
}
static function outputThumb($path){
ob_end_clean();
if(!input("get.cache")=="no"){
header("Cache-Control: max-age=10800");
}
header('Content-Type: '.self::getMimetype($path));
$fileObj = fopen($path,"r");
echo fread($fileObj,filesize($path));
fclose($file);
}
public function getLocalThumb(){
$picInfo = explode(",",$this->fileData["pic_info"]);
$picInfo = self::getThumbSize($picInfo[0],$picInfo[1]);
if(file_exists(ROOT_PATH . "public/thumb/".$this->fileData["pre_name"]."_thumb")){
self::outputThumb(ROOT_PATH . "public/thumb/".$this->fileData["pre_name"]."_thumb");
return [0,0];
}
$thumbImg = new Thumb(ROOT_PATH . "public/uploads/".$this->fileData["pre_name"]);
$thumbImg->thumb($picInfo[1], $picInfo[0]);
if(!is_dir(dirname(ROOT_PATH . "public/thumb/".$this->fileData["pre_name"]))){
mkdir(dirname(ROOT_PATH . "public/thumb/".$this->fileData["pre_name"]),0777,true);
}
$thumbImg->out(ROOT_PATH . "public/thumb/".$this->fileData["pre_name"]."_thumb");
self::outputThumb(ROOT_PATH . "public/thumb/".$this->fileData["pre_name"]."_thumb");
return [0,0];
}
public function getOssThumb(){ public function getOssThumb(){
if(!$this->policyData['bucket_private']){ if(!$this->policyData['bucket_private']){
$fileUrl = $this->policyData["url"].$this->fileData["pre_name"]."?x-oss-process=image/resize,m_lfit,h_39,w_90"; $fileUrl = $this->policyData["url"].$this->fileData["pre_name"]."?x-oss-process=image/resize,m_lfit,h_39,w_90";
...@@ -1030,28 +975,6 @@ class FileManage extends Model{ ...@@ -1030,28 +975,6 @@ class FileManage extends Model{
} }
} }
public function localDownload($isAdmin=false){
$speedLimit = Db::name('groups')->where('id',$this->userData["user_group"])->find();
$rangeTransfer = $speedLimit["range_transfer"];
$speedLimit = $speedLimit["speed"];
$sendFileOptions = Option::getValues(["download"]);
if($sendFileOptions["sendfile"] == "1"){
$this->sendFile($speedLimit,$rangeTransfer,true,$sendFileOptions["header"]);
}else{
if($isAdmin){
$speedLimit = "";
}
if($speedLimit == "0"){
exit();
}else if(empty($speedLimit)){
$this->outputWithoutLimit(true,$rangeTransfer);
exit();
}else if((int)$speedLimit > 0){
$this->outputWithLimit($speedLimit,true);
}
}
}
/** /**
* [List description] * [List description]
* @param [type] $path [description] * @param [type] $path [description]
...@@ -1363,33 +1286,34 @@ class FileManage extends Model{ ...@@ -1363,33 +1286,34 @@ class FileManage extends Model{
} }
public function signTmpUrl(){ public function signTmpUrl(){
switch ($this->policyData["policy_type"]) { return $this->adapter->signTmpUrl()[1];
case 'qiniu': // switch ($this->policyData["policy_type"]) {
return $this->qiniuPreview()[1]; // case 'qiniu':
break; // return $this->qiniuPreview()[1];
case 'oss': // break;
return $this->ossPreview()[1]; // case 'oss':
break; // return $this->ossPreview()[1];
case 'upyun': // break;
return $this->upyunPreview()[1]; // case 'upyun':
break; // return $this->upyunPreview()[1];
case 's3': // break;
return $this->s3Preview()[1]; // case 's3':
break; // return $this->s3Preview()[1];
case 'local': // break;
$options = Option::getValues(["oss","basic"]); // case 'local':
$timeOut = $options["timeout"]; // $options = Option::getValues(["oss","basic"]);
$delayTime = time()+$timeOut; // $timeOut = $options["timeout"];
$key=$this->fileData["id"].":".$delayTime.":".md5($this->userData["user_pass"].$this->fileData["id"].$delayTime.config("salt")); // $delayTime = time()+$timeOut;
return $options['siteURL']."Callback/TmpPreview/key/".$key; // $key=$this->fileData["id"].":".$delayTime.":".md5($this->userData["user_pass"].$this->fileData["id"].$delayTime.config("salt"));
break; // return $options['siteURL']."Callback/TmpPreview/key/".$key;
case 'remote': // break;
return $this->remotePreview()[1]; // case 'remote':
break; // return $this->remotePreview()[1];
default: // break;
# code... // default:
break; // # code...
} // break;
// }
} }
} }
......
...@@ -2,9 +2,13 @@ ...@@ -2,9 +2,13 @@
namespace app\index\model; namespace app\index\model;
use think\Model; use think\Model;
use think\Db;
use \app\index\model\Option; use \app\index\model\Option;
/**
* 本地策略文件管理适配器
*/
class LocalAdapter extends Model{ class LocalAdapter extends Model{
private $fileModel; private $fileModel;
...@@ -265,7 +269,118 @@ class LocalAdapter extends Model{ ...@@ -265,7 +269,118 @@ class LocalAdapter extends Model{
return $range; return $range;
} }
return null; return null;
} }
/**
* 生成/返回 文件缩略图
*
* @return array 重定向信息
*/
public function getThumb(){
$picInfo = explode(",",$this->fileModel["pic_info"]);
$picInfo = self::getThumbSize($picInfo[0],$picInfo[1]);
if(file_exists(ROOT_PATH . "public/thumb/".$this->fileModel["pre_name"]."_thumb")){
self::outputThumb(ROOT_PATH . "public/thumb/".$this->fileModel["pre_name"]."_thumb");
return [0,0];
}
$thumbImg = new Thumb(ROOT_PATH . "public/uploads/".$this->fileModel["pre_name"]);
$thumbImg->thumb($picInfo[1], $picInfo[0]);
if(!is_dir(dirname(ROOT_PATH . "public/thumb/".$this->fileModel["pre_name"]))){
mkdir(dirname(ROOT_PATH . "public/thumb/".$this->fileModel["pre_name"]),0777,true);
}
$thumbImg->out(ROOT_PATH . "public/thumb/".$this->fileModel["pre_name"]."_thumb");
self::outputThumb(ROOT_PATH . "public/thumb/".$this->fileModel["pre_name"]."_thumb");
return [0,0];
}
/**
* 计算缩略图大小
*
* @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];
}
/**
* 输出缩略图
*
* @param string $path 缩略图文件路径
* @return void
*/
static function outputThumb($path){
ob_end_clean();
if(!input("get.cache")=="no"){
header("Cache-Control: max-age=10800");
}
header('Content-Type: '.self::getMimetype($path));
$fileObj = fopen($path,"r");
echo fread($fileObj,filesize($path));
fclose($file);
}
/**
* 处理下载请求
*
* @param boolean $isAdmin 是否为管理员请求
* @return void
*/
public function Download($isAdmin=false){
$speedLimit = Db::name('groups')->where('id',$this->userModel["user_group"])->find();
$rangeTransfer = $speedLimit["range_transfer"];
$speedLimit = $speedLimit["speed"];
$sendFileOptions = Option::getValues(["download"]);
if($sendFileOptions["sendfile"] == "1"){
$this->sendFile($speedLimit,$rangeTransfer,true,$sendFileOptions["header"]);
}else{
if($isAdmin){
$speedLimit = "";
}
if($speedLimit == "0"){
exit();
}else if(empty($speedLimit)){
$this->outputWithoutLimit(true,$rangeTransfer);
exit();
}else if((int)$speedLimit > 0){
$this->outputWithLimit($speedLimit,true);
}
}
}
/**
* 删除指定本地文件
*
* @param array $fileList 待删除文件的数据库记录
* @param array $policyData 待删除文件的上传策略信息
* @return void
*/
static function DeleteFile($fileList,$policyData){
$fileListTemp = array_column($fileList, 'pre_name');
foreach ($fileListTemp as $key => $value) {
@unlink(ROOT_PATH . 'public/uploads/'.$value);
if(file_exists(ROOT_PATH . 'public/thumb/'.$value."_thumb")){
@unlink(ROOT_PATH . 'public/thumb/'.$value."_thumb");
}
}
}
public function signTmpUrl(){
$options = Option::getValues(["oss","basic"]);
$timeOut = $options["timeout"];
$delayTime = time()+$timeOut;
$key=$this->fileModel["id"].":".$delayTime.":".md5($this->userModel["user_pass"].$this->fileModel["id"].$delayTime.config("salt"));
return [1,$options['siteURL']."Callback/TmpPreview/key/".$key];
}
} }
......
*
!.gitignore
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册