文件保存改成cdn

master
ELF 5 years ago
parent 95dddd9126
commit f472f5e9d3

@ -14,6 +14,7 @@ use Admin\Controller\BosController;
use Admin\Event\QiNiuEvent; use Admin\Event\QiNiuEvent;
use Think\Model; use Think\Model;
use Think\Upload; use Think\Upload;
use Base\Service\OssService;
/** /**
* 图片模型 * 图片模型
@ -54,8 +55,8 @@ class PictureModel extends Model{
if (!isset($value['id'])) { if (!isset($value['id'])) {
$value['path'] = substr($setting['rootPath'], 1) . $value['savepath'] . $value['savename']; //在模板里的url路径 $value['path'] = substr($setting['rootPath'], 1) . $value['savepath'] . $value['savename']; //在模板里的url路径
if ($flag) { if ($flag) {
$info[$key]['water'] = $value['water'] = substr($setting['waterPath'], 1) . $value['savename']; //在模板里的water路径 $info[$key]['water'] = $value['water'] = substr($setting['waterPath'], 1) . $value['savename']; //在模板里的water路径
} }
if ($this->create($value) && ($id = $this->add())) { if ($this->create($value) && ($id = $this->add())) {
$value['id'] = $id; $value['id'] = $id;
} else { } else {
@ -63,53 +64,21 @@ class PictureModel extends Model{
unset($info[$key]); unset($info[$key]);
} }
}else { }else {
if ($flag) { if ($flag) {
$savename = substr(strrchr($value['path'], '/'), 1); $savename = substr(strrchr($value['path'], '/'), 1);
$info[$key]['water'] = $data['water'] = substr($setting['waterPath'], 1) . $savename; //在模板里的water路径 $info[$key]['water'] = $data['water'] = substr($setting['waterPath'], 1) . $savename; //在模板里的water路径
$this->where(['id'=>$value['id']])->save($data); $this->where(['id'=>$value['id']])->save($data);
}
}
if (get_tool_status("oss_storage") == 1) {
$path = explode("/", $value['path']);
$to = "http://" . C("oss_storage.bucket") . "." . C("oss_storage.domain") . "/icon/" . $path[4];
$to = str_replace('-internal', '', $to);
// $to="http://down.vlcms.com/icon/".$path[4];
$updata['savename'] = $path[4];
$updata['path'] = "." . $value['path'];
$oss = new OssController();
$oss->upload_game_pak_oss($updata);
$data_url['oss_url'] = $to;
$coo = $this->where(array('id' => $value['id']))->save($data_url);
} elseif (get_tool_status("qiniu_storage") == 1) {//七牛上传
$path = explode("/", $value['path']);
$newName = $path[4];
$QiNiu = new QiNiuEvent();
$value['url'] = $data_url['url'] = "http://" . $QiNiu->upQiNiuFile($newName, "." . $value['path']);
$coo = $this->where(array('id' => $value['id']))->save($data_url);
}elseif(get_tool_status("cos_storage") == 1){
$path = explode("/", $value['path']);
$newName = $path[4];
$cos=new Coscontroller();
$cos_res=$cos->cosupload("." . $value['path'],"/Icon/".$newName);
if(strlen($cos_res)>10){
$data_url['url'] = $cos_res;
$coo = $this->where(array('id' => $value['id']))->save($data_url);
}else{
return false;
} }
}elseif(get_tool_status("bos_storage") == 1){ //上传到百度云
$path = explode("/", $value['path']);
$to = "http://" . C("bos_storage.bucket") . "." . C("bos_storage.domain") . "/icon/" . $path[4];
$to = str_replace('-internal', '', $to);
$updata['savename'] = $path[4];
$updata['path'] = "." . $value['path'];
$oss = new BosController();
$oss->upload_bos($updata);
$data_url['bos_url'] = $to;
$coo = $this->where(array('id' => $value['id']))->save($data_url);
} }
if (C('OSS_STATUS')) {
$path = explode('/', $value['path']);
$saveFileName = 'icon/' . $path[4];
$localFilePath = '.' . $value['path'];
$ossService = new OssService();
$ossService->upload($localFilePath, $saveFileName);
$dataUrl['oss_url'] = $ossService->getUrl($saveFileName);
$coo = $this->where(['id' => $value['id']])->save($dataUrl);
}
} }
return $info; //文件上传成功 return $info; //文件上传成功
} else { } else {

@ -155,34 +155,15 @@ function get_img_url($cover_id){
return ""; return "";
} }
$picture = M('Picture')->where(array('status'=>1))->getById($cover_id); $picture = M('Picture')->where(array('status'=>1))->getById($cover_id);
if (get_tool_status("oss_storage") == 1) { if (C('OSS_STATUS')) {
if(!empty($picture['oss_url'])){ if(!empty($picture['oss_url'])){
return $picture['oss_url']; return $picture['oss_url'];
}else{ }else{
return 'http://' . $_SERVER['HTTP_HOST'] . __ROOT__.$picture['path']; return 'http://' . $_SERVER['HTTP_HOST'] . __ROOT__.$picture['path'];
} }
}elseif(get_tool_status("qiniu_storage") == 1){ } else {
if(!empty($picture['url'])){
return $picture['url'];
}else{
return 'http://' . $_SERVER['HTTP_HOST'] . __ROOT__.$picture['path'];
}
}elseif(get_tool_status("cos_storage") == 1){
if(!empty($picture['url'])){
return $picture['url'];
}else{
return 'http://' . $_SERVER['HTTP_HOST'] . __ROOT__.$picture['path'];
}
}elseif(get_tool_status("bos_storage") == 1){
if(!empty($picture['bos_url'])){
return $picture['bos_url'];
}else{
return 'http://' . $_SERVER['HTTP_HOST'] . __ROOT__.$picture['path'];
}
}else{
return 'http://' . $_SERVER['HTTP_HOST'] .__ROOT__.$picture['path']; return 'http://' . $_SERVER['HTTP_HOST'] .__ROOT__.$picture['path'];
} }
} }

@ -328,7 +328,7 @@ class GameSourceService {
public function uploadPackage($localFilePath, $distFilePath, $isDeleteLocal = false) public function uploadPackage($localFilePath, $distFilePath, $isDeleteLocal = false)
{ {
$isChunk = C('PACKAGE_CHUNK_ENABLED') ? true : false; $isChunk = C('PACKAGE_CHUNK_ENABLED') ? true : false;
if (get_tool_status('oss_storage') == 1) { if (C('OSS_STATUS')) {
if ($isChunk) { if ($isChunk) {
return $this->uploadPackageChunk($localFilePath, $distFilePath, $isDeleteLocal); return $this->uploadPackageChunk($localFilePath, $distFilePath, $isDeleteLocal);
} else { } else {

@ -12,23 +12,23 @@ class OssService {
private $accessKeyId = ''; private $accessKeyId = '';
private $accessKeySecret = ''; private $accessKeySecret = '';
private $domain = ''; private $endpoint = '';
private $isCName = false; private $isCName = false;
private $bucket = ''; private $bucket = '';
private $bdDomain = ''; private $domain = '';
private $client; private $client;
private $errorMessage = ''; private $errorMessage = '';
public function __construct() public function __construct()
{ {
Vendor('OSS.autoload'); Vendor('OSS.autoload');
$this->accessKeyId = C('oss_storage.accesskeyid'); $this->accessKeyId = C('OSS_ACCESS_KEY_ID');
$this->accessKeySecret = C('oss_storage.accesskeysecr'); $this->accessKeySecret = C('OSS_ACCESS_KEY_SECRET');
$this->domain = C('oss_storage.domain'); $this->endpoint = C('OSS_ENDPOINT');
$this->isCName = C('oss_storage.is_cname'); $this->domain = C('OSS_DOMAIN');
$this->bdDomain = C('oss_storage.bd_domain'); $this->isCName = C('OSS_IS_CNAME');
$this->bucket = C('oss_storage.bucket'); $this->bucket = C('OSS_BUCKET');
$this->client = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->domain, $this->isCName); $this->client = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint, $this->isCName);
} }
public function upload($localFilePath, $saveFileName) public function upload($localFilePath, $saveFileName)
@ -106,18 +106,13 @@ class OssService {
private function getUrl($saveFileName) private function getUrl($saveFileName)
{ {
$url = ''; $url = '';
if ($this->isCName) { /* if ($this->isCName) {
$url = 'http://' . $this->domain . '/' . $saveFileName; $url = 'http://' . $this->endpoint . '/' . $saveFileName;
} else { } else {
$url = 'https://' . $this->bucket . '.' . $this->domain . '/' . $saveFileName; $url = 'https://' . $this->bucket . '.' . $this->endpoint . '/' . $saveFileName;
$url = str_replace('-internal', '', $url); $url = str_replace('-internal', '', $url);
} } */
/** $url = $this->domain . '/' . $saveFileName;
* @todo unknown
*/
if (!empty($this->bdDomain) && strlen($this->bdDomain) > 5) {
$url = $this->bdDomain . $saveFileName;
}
return $url; return $url;
} }
} }

@ -2345,14 +2345,10 @@ function get_child_ids($id){
} }
//获取图片连接 //获取图片连接
function icon_url($value){ function icon_url($value){
if (get_tool_status("oss_storage") == 1){ if (C('OSS_STATUS')){
$url=get_cover($value, 'path'); $url = get_cover($value, 'path');
}elseif(get_tool_status("qiniu_storage") == 1){ } else {
$url=get_cover($value, 'path'); $url = 'http://' . $_SERVER['HTTP_HOST'] . get_cover($value, 'path');
}elseif(get_tool_status("cos_storage") == 1){
$url=get_cover($value, 'path');
}else{
$url='http://' . $_SERVER['HTTP_HOST'] . get_cover($value, 'path');
} }
return $url; return $url;
} }

@ -1016,30 +1016,12 @@ function get_cover($cover_id, $field = null, $root = 1, $flag = true)
return ""; return "";
} }
$picture = M('Picture')->where(array('status' => 1))->getById($cover_id); $picture = M('Picture')->where(array('status' => 1))->getById($cover_id);
if (get_tool_status("oss_storage") == 1) { if (C('OSS_STATUS')) {
if (!empty($picture['oss_url'])) { if (!empty($picture['oss_url'])) {
return str_replace('http:', 'https:', $picture['oss_url']); return str_replace('http:', 'https:', $picture['oss_url']);
} else { } else {
return 'https://' . $_SERVER['HTTP_HOST'] . __ROOT__ . $picture['path']; return 'https://' . $_SERVER['HTTP_HOST'] . __ROOT__ . $picture['path'];
} }
} elseif (get_tool_status("qiniu_storage") == 1) {
if (!empty($picture['url'])) {
return $picture['url'];
} else {
return '//' . $_SERVER['HTTP_HOST'] . __ROOT__ . $picture['path'];
}
} elseif (get_tool_status("cos_storage") == 1) {
if (!empty($picture['url'])) {
return $picture['url'];
} else {
return '//' . $_SERVER['HTTP_HOST'] . __ROOT__ . $picture['path'];
}
} elseif (get_tool_status("bos_storage") == 1) {
if (!empty($picture['bos_url'])) {
return $picture['bos_url'];
} else {
return '//' . $_SERVER['HTTP_HOST'] . __ROOT__ . $picture['path'];
}
} else { } else {
if ($field == 'path') { if ($field == 'path') {
if (!empty($picture['url']) && get_tool_status('qiniu_storage') == 1) { if (!empty($picture['url']) && get_tool_status('qiniu_storage') == 1) {

@ -10,6 +10,7 @@
namespace Home\Model; namespace Home\Model;
use Think\Model; use Think\Model;
use Think\Upload; use Think\Upload;
use Base\Service\OssService;
/** /**
* 图片模型 * 图片模型
@ -55,29 +56,14 @@ class PictureModel extends Model{
unset($info[$key]); unset($info[$key]);
} }
} }
if (get_tool_status("oss_storage") == 1) { if (C('OSS_STATUS')) {
$path = explode("/", $value['path']); $path = explode('/', $value['path']);
$to = "http://" . C("oss_storage.bucket") . "." . C("oss_storage.domain") . "/icon/" . $path[4]; $saveFileName = 'icon/' . $path[4];
$to = str_replace('-internal', '', $to); $localFilePath = '.' . $value['path'];
$updata['savename'] = $path[4]; $ossService = new OssService();
$updata['path'] = "." . $value['path']; $ossService->upload($localFilePath, $saveFileName);
$oss = A('Oss'); $dataUrl['oss_url'] = $ossService->getUrl($saveFileName);
$oss->upload_game_pak_oss($updata); $coo = $this->where(['id' => $value['id']])->save($dataUrl);
$data_url['oss_url'] = $to;
$coo = $this->where(array('id' => $value['id']))->save($data_url);
} elseif (get_tool_status("qiniu_storage") == 1) {//七牛上传
$path = explode("/", $value['path']);
$newName = $path[4];
$QiNiu = A('QiNiu','Event');
$value['url'] = $data_url['url'] = "http://".$QiNiu->upQiNiuFile($newName, ".".$value['path']);
$coo = $this->where(array('id' => $value['id']))->save($data_url);
} elseif(get_tool_status("bos_storage") == 1){ //上传到百度云
$path = explode("/", $value['path']);
$updata['savename'] = $path[4];
$updata['path'] = "." . $value['path'];
$bos =A('Bos');
$bos ->upload_bos($updata);
$coo = $this->where(array('id' => $value['id']))->save($data_url);
} }
} }
return $info; //文件上传成功 return $info; //文件上传成功

@ -548,34 +548,15 @@ function get_img_url($cover_id){
return ""; return "";
} }
$picture = M('Picture')->where(array('status'=>1))->getById($cover_id); $picture = M('Picture')->where(array('status'=>1))->getById($cover_id);
if (get_tool_status("oss_storage") == 1) { if (C('OSS_STATUS')) {
if(!empty($picture['oss_url'])){ if(!empty($picture['oss_url'])){
return $picture['oss_url']; return $picture['oss_url'];
}else{ }else{
return 'http://' . $_SERVER['HTTP_HOST'] . __ROOT__.$picture['path']; return 'http://' . $_SERVER['HTTP_HOST'] . __ROOT__.$picture['path'];
} }
}elseif(get_tool_status("qiniu_storage") == 1){ }else{
if(!empty($picture['url'])){
return $picture['url'];
}else{
return 'http://' . $_SERVER['HTTP_HOST'] . __ROOT__.$picture['path'];
}
}elseif(get_tool_status("cos_storage") == 1){
if(!empty($picture['url'])){
return $picture['url'];
}else{
return 'http://' . $_SERVER['HTTP_HOST'] . __ROOT__.$picture['path'];
}
}elseif(get_tool_status("bos_storage") == 1){
if(!empty($picture['bos_url'])){
return $picture['bos_url'];
}else{
return 'http://' . $_SERVER['HTTP_HOST'] . __ROOT__.$picture['path'];
}
}else{
return 'http://' . $_SERVER['HTTP_HOST'] .__ROOT__.$picture['path']; return 'http://' . $_SERVER['HTTP_HOST'] .__ROOT__.$picture['path'];
} }
} }
/** /**

@ -9,6 +9,7 @@
namespace Admin\Controller; namespace Admin\Controller;
use OSS\OssClient; use OSS\OssClient;
use Think\ShardUpload; use Think\ShardUpload;
use Base\Service\OssService;
/** /**
* 文件控制器 * 文件控制器
* 主要用于下载模型的文件上传和下载 * 主要用于下载模型的文件上传和下载
@ -78,8 +79,8 @@ class FileController extends AdminController {
} }
if(($path = $uploader->upload('file', $_POST)) !== false){ if(($path = $uploader->upload('file', $_POST)) !== false){
if(I('get.type') == "1"){ if(I('get.type') == "1"){
$file_info = json_decode($path,true); $file_info = json_decode($path, true);
$this->upload_oss("Material/",$file_info['name'],$file_info['path'].'/'.$file_info['name']); $url = $this->uploadOss("Material/",$file_info['name'],$file_info['path'].'/'.$file_info['name']);
$file_info['path'] = "http://".C('CND_ADDRESS')."/Material"; $file_info['path'] = "http://".C('CND_ADDRESS')."/Material";
$path = json_encode($file_info); $path = json_encode($file_info);
} }
@ -170,93 +171,11 @@ class FileController extends AdminController {
/** /**
*上传到OSS *上传到OSS
*/ */
public function upload_oss($oss_path,$file_name,$file_path) public function uploadOss($ossPath, $fileName,$filePath)
{ {
/** $ossFilePath = $ossPath . $fileName;
* 根据Config配置得到一个OssClient实例 $ossService = new OssService();
*/ $ossService->upload($filePath, $ossFilePath);
try { return $ossService->getUrl($ossFilePath);
Vendor('OSS.autoload');
$ossClient = new \OSS\OssClient(C("oss_storage.accesskeyid"), C("oss_storage.accesskeysecr"), C("oss_storage.domain"));
} catch (OssException $e) {
$this->error($e->getMessage());
}
$oss_file_path = $oss_path . $file_name;
$avatar = $file_path;
try {
$this->multiuploadFile($ossClient, C("oss_storage.bucket"), $oss_file_path, $avatar);
return true;
} catch (OssException $e) {
/* 返回JSON数据 */
$this->error($e->getMessage());
}
}
public function multiuploadFile($ossClient, $bucket, $url, $file)
{
//$file = __FILE__;
$options = array();
try {
#初始化分片上传文件
$uploadId = $ossClient->initiateMultipartUpload($bucket, $url);
//$ossClient->multiuploadFile($bucket, $url, $file, $options);
} catch (OssException $e) {
printf(__FUNCTION__ . ": initiateMultipartUpload FAILED\n");
printf($e->getMessage() . "\n");
return;
}
/*
* step 2. 上传分片
*/
$partSize = 5 * 1000 * 1024;
$uploadFile = $file;
$uploadFileSize = filesize($uploadFile);
$pieces = $ossClient->generateMultiuploadParts($uploadFileSize, $partSize);
$responseUploadPart = array();
$uploadPosition = 0;
$isCheckMd5 = true;
foreach ($pieces as $i => $piece) {
$fromPos = $uploadPosition + (integer)$piece[$ossClient::OSS_SEEK_TO];
$toPos = (integer)$piece[$ossClient::OSS_LENGTH] + $fromPos - 1;
$upOptions = array(
$ossClient::OSS_FILE_UPLOAD => $uploadFile,
$ossClient::OSS_PART_NUM => ($i + 1),
$ossClient::OSS_SEEK_TO => $fromPos,
$ossClient::OSS_LENGTH => $toPos - $fromPos + 1,
$ossClient::OSS_CHECK_MD5 => $isCheckMd5,
);
if ($isCheckMd5) {
$contentMd5 = \OSS\Core\OssUtil::getMd5SumForFile($uploadFile, $fromPos, $toPos);
$upOptions[$ossClient::OSS_CONTENT_MD5] = $contentMd5;
}
//2. 将每一分片上传到OSS
try {
$responseUploadPart[] = $ossClient->uploadPart($bucket, $url, $uploadId, $upOptions);
} catch (OssException $e) {
printf(__FUNCTION__ . ": initiateMultipartUpload, uploadPart - part#{$i} FAILED\n");
printf($e->getMessage() . "\n");
return;
}
//printf(__FUNCTION__ . ": initiateMultipartUpload, uploadPart - part#{$i} OK\n");
}
$uploadParts = array();
foreach ($responseUploadPart as $i => $eTag) {
$uploadParts[] = array(
'PartNumber' => ($i + 1),
'ETag' => $eTag,
);
}
/**
* step 3. 完成上传
*/
try {
$a = $ossClient->completeMultipartUpload($bucket, $url, $uploadId, $uploadParts);
} catch (OssException $e) {
printf(__FUNCTION__ . ": completeMultipartUpload FAILED\n");
printf($e->getMessage() . "\n");
return;
}
} }
} }

Loading…
Cancel
Save