|
|
|
@ -9,6 +9,7 @@
|
|
|
|
|
namespace Admin\Controller;
|
|
|
|
|
use OSS\OssClient;
|
|
|
|
|
use Think\ShardUpload;
|
|
|
|
|
use Base\Service\OssService;
|
|
|
|
|
/**
|
|
|
|
|
* 文件控制器
|
|
|
|
|
* 主要用于下载模型的文件上传和下载
|
|
|
|
@ -79,7 +80,7 @@ class FileController extends AdminController {
|
|
|
|
|
if(($path = $uploader->upload('file', $_POST)) !== false){
|
|
|
|
|
if(I('get.type') == "1"){
|
|
|
|
|
$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";
|
|
|
|
|
$path = json_encode($file_info);
|
|
|
|
|
}
|
|
|
|
@ -170,93 +171,11 @@ class FileController extends AdminController {
|
|
|
|
|
/**
|
|
|
|
|
*上传到OSS
|
|
|
|
|
*/
|
|
|
|
|
public function upload_oss($oss_path,$file_name,$file_path)
|
|
|
|
|
public function uploadOss($ossPath, $fileName,$filePath)
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* 根据Config配置,得到一个OssClient实例
|
|
|
|
|
*/
|
|
|
|
|
try {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
$ossFilePath = $ossPath . $fileName;
|
|
|
|
|
$ossService = new OssService();
|
|
|
|
|
$ossService->upload($filePath, $ossFilePath);
|
|
|
|
|
return $ossService->getUrl($ossFilePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|