|
@ -10,7 +10,6 @@ use think\facade\Config; |
|
|
|
|
|
|
|
|
class Upload extends AdminBaseController |
|
|
class Upload extends AdminBaseController |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
// 上传图片 |
|
|
// 上传图片 |
|
|
public function upload() |
|
|
public function upload() |
|
|
{ |
|
|
{ |
|
@ -56,11 +55,9 @@ class Upload extends AdminBaseController |
|
|
} |
|
|
} |
|
|
// 生成唯一的文件名 |
|
|
// 生成唯一的文件名 |
|
|
$fileName = uniqid() . '.' . $file->getOriginalExtension(); |
|
|
$fileName = uniqid() . '.' . $file->getOriginalExtension(); |
|
|
|
|
|
// 初始化s3客户端 |
|
|
// 初始化S3客户端 |
|
|
|
|
|
$s3Config = Config::get('common.aws_s3'); |
|
|
$s3Config = Config::get('common.aws_s3'); |
|
|
|
|
|
$s3Client = new S3Client([ |
|
|
$s3 = new S3Client([ |
|
|
|
|
|
'version' => 'latest', |
|
|
'version' => 'latest', |
|
|
'region' => $s3Config['aws_region'], |
|
|
'region' => $s3Config['aws_region'], |
|
|
'credentials' => [ |
|
|
'credentials' => [ |
|
@ -69,7 +66,7 @@ class Upload extends AdminBaseController |
|
|
], |
|
|
], |
|
|
]); |
|
|
]); |
|
|
// 上传文件到S3 |
|
|
// 上传文件到S3 |
|
|
$result = $s3->putObject([ |
|
|
$result = $s3Client->putObject([ |
|
|
'Bucket' => $s3Config['aws_bucket'], |
|
|
'Bucket' => $s3Config['aws_bucket'], |
|
|
'Key' => 'bourse-video-node/' . $fileName, // s3中的存储路径 |
|
|
'Key' => 'bourse-video-node/' . $fileName, // s3中的存储路径 |
|
|
'Body' => fopen($file->getRealPath(), 'r'), |
|
|
'Body' => fopen($file->getRealPath(), 'r'), |
|
@ -98,4 +95,153 @@ class Upload extends AdminBaseController |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 初始化分片上传 |
|
|
|
|
|
public function initiateUpload(){ |
|
|
|
|
|
try { |
|
|
|
|
|
$param = $this->request->param(); |
|
|
|
|
|
if (empty($param['file_type'])) { |
|
|
|
|
|
return json(['code' => '100500', 'message' => '缺少参数 file_type', 'data' => []]); |
|
|
|
|
|
} |
|
|
|
|
|
// 允许的视频类型 |
|
|
|
|
|
$allowed_types = [ |
|
|
|
|
|
'video/mp4', |
|
|
|
|
|
'video/quicktime', |
|
|
|
|
|
'video/x-msvideo', |
|
|
|
|
|
'video/x-ms-wmv', |
|
|
|
|
|
'video/x-matroska', |
|
|
|
|
|
]; |
|
|
|
|
|
if (!in_array($param['file_type'], $allowed_types)) { |
|
|
|
|
|
return json(['code' => '100500', 'message' => '上传的文件类型不在允许范围内', 'data' => []]); |
|
|
|
|
|
} |
|
|
|
|
|
// 生成唯一的文件名 |
|
|
|
|
|
$fileName = uniqid('video_', true); |
|
|
|
|
|
// 初始化s3客户端 |
|
|
|
|
|
$s3Config = Config::get('common.aws_s3'); |
|
|
|
|
|
$s3Client = new S3Client([ |
|
|
|
|
|
'version' => 'latest', |
|
|
|
|
|
'region' => $s3Config['aws_region'], |
|
|
|
|
|
'credentials' => [ |
|
|
|
|
|
'key' => $s3Config['aws_key'], |
|
|
|
|
|
'secret' => $s3Config['aws_secret'], |
|
|
|
|
|
], |
|
|
|
|
|
]); |
|
|
|
|
|
// 初始化Multipart Upload |
|
|
|
|
|
$result = $s3Client->createMultipartUpload([ |
|
|
|
|
|
'Bucket' => $s3Config['aws_bucket'], |
|
|
|
|
|
'Key' => 'bourse-video-node/' . $fileName, |
|
|
|
|
|
]); |
|
|
|
|
|
return json([ |
|
|
|
|
|
'code' => 0, |
|
|
|
|
|
'data' => [ |
|
|
|
|
|
'uploadId' => $result['UploadId'], |
|
|
|
|
|
'key' => $result['Key'] |
|
|
|
|
|
] |
|
|
|
|
|
]); |
|
|
|
|
|
} catch (\Exception $exception) { |
|
|
|
|
|
return json(['code' => '100500', 'message' => '初始化上传失败', 'data' => [$exception->getMessage()]]); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 上传分片 |
|
|
|
|
|
public function uploadPart(){ |
|
|
|
|
|
try { |
|
|
|
|
|
$param = $this->request->param(); |
|
|
|
|
|
if (empty($param['uploadId']) || empty($param['key']) || empty($param['partNumber'])) { |
|
|
|
|
|
return json(['code' => 400, 'message' => '缺少参数']); |
|
|
|
|
|
} |
|
|
|
|
|
$file = $this->request->file('file'); |
|
|
|
|
|
if (!$file) { |
|
|
|
|
|
return json(['code' => 400, 'message' => 'No file uploaded']); |
|
|
|
|
|
} |
|
|
|
|
|
// 初始化s3客户端 |
|
|
|
|
|
$s3Config = Config::get('common.aws_s3'); |
|
|
|
|
|
$s3Client = new S3Client([ |
|
|
|
|
|
'version' => 'latest', |
|
|
|
|
|
'region' => $s3Config['aws_region'], |
|
|
|
|
|
'credentials' => [ |
|
|
|
|
|
'key' => $s3Config['aws_key'], |
|
|
|
|
|
'secret' => $s3Config['aws_secret'], |
|
|
|
|
|
], |
|
|
|
|
|
]); |
|
|
|
|
|
$result = $s3Client->uploadPart([ |
|
|
|
|
|
'Bucket' => $s3Config['aws_bucket'], |
|
|
|
|
|
'Key' => $param['key'], |
|
|
|
|
|
'PartNumber' => $param['partNumber'], |
|
|
|
|
|
'UploadId' => $param['uploadId'], |
|
|
|
|
|
'Body' => fopen($file->getRealPath(), 'r'), |
|
|
|
|
|
]); |
|
|
|
|
|
|
|
|
|
|
|
return json([ |
|
|
|
|
|
'code' => 200, |
|
|
|
|
|
'data' => [ |
|
|
|
|
|
'ETag' => $result['ETag'], |
|
|
|
|
|
'PartNumber' => $param['partNumber'] |
|
|
|
|
|
] |
|
|
|
|
|
]); |
|
|
|
|
|
} catch (\Exception $exception) { |
|
|
|
|
|
return json(['code' => '100500', 'message' => '上传失败', 'data' => [$exception->getMessage()]]); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 完成上传 |
|
|
|
|
|
public function completeUpload(){ |
|
|
|
|
|
try { |
|
|
|
|
|
$param = $this->request->param(); |
|
|
|
|
|
if (empty($param['uploadId']) || empty($param['key']) || empty($param['parts'])) { |
|
|
|
|
|
return json(['code' => 400, 'message' => '缺少参数']); |
|
|
|
|
|
} |
|
|
|
|
|
// 初始化s3客户端 |
|
|
|
|
|
$s3Config = Config::get('common.aws_s3'); |
|
|
|
|
|
$s3Client = new S3Client([ |
|
|
|
|
|
'version' => 'latest', |
|
|
|
|
|
'region' => $s3Config['aws_region'], |
|
|
|
|
|
'credentials' => [ |
|
|
|
|
|
'key' => $s3Config['aws_key'], |
|
|
|
|
|
'secret' => $s3Config['aws_secret'], |
|
|
|
|
|
], |
|
|
|
|
|
]); |
|
|
|
|
|
$s3Client->completeMultipartUpload([ |
|
|
|
|
|
'Bucket' => $s3Config['aws_bucket'], |
|
|
|
|
|
'Key' => $param['key'], |
|
|
|
|
|
'UploadId' => $param['uploadId'], |
|
|
|
|
|
'MultipartUpload' => [ |
|
|
|
|
|
'Parts' => $param['parts'] |
|
|
|
|
|
], |
|
|
|
|
|
]); |
|
|
|
|
|
|
|
|
|
|
|
return json(['code' => 200, 'message' => '上传成功']); |
|
|
|
|
|
} catch (\Exception $exception) { |
|
|
|
|
|
return json(['code' => '100500', 'message' => '完成上传失败', 'data' => [$exception->getMessage()]]); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 取消上传 |
|
|
|
|
|
public function abortUpload(){ |
|
|
|
|
|
try { |
|
|
|
|
|
$param = $this->request->param(); |
|
|
|
|
|
if (empty($param['uploadId']) || empty($param['key'])) { |
|
|
|
|
|
return json(['code' => 400, 'message' => '缺少参数']); |
|
|
|
|
|
} |
|
|
|
|
|
// 初始化s3客户端 |
|
|
|
|
|
$s3Config = Config::get('common.aws_s3'); |
|
|
|
|
|
$s3Client = new S3Client([ |
|
|
|
|
|
'version' => 'latest', |
|
|
|
|
|
'region' => $s3Config['aws_region'], |
|
|
|
|
|
'credentials' => [ |
|
|
|
|
|
'key' => $s3Config['aws_key'], |
|
|
|
|
|
'secret' => $s3Config['aws_secret'], |
|
|
|
|
|
], |
|
|
|
|
|
]); |
|
|
|
|
|
$s3Client->abortMultipartUpload([ |
|
|
|
|
|
'Bucket' => $s3Config['aws_bucket'], |
|
|
|
|
|
'Key' => $param['key'], |
|
|
|
|
|
'UploadId' => $param['uploadId'], |
|
|
|
|
|
]); |
|
|
|
|
|
|
|
|
|
|
|
return json(['code' => 200, 'message' => '上传已取消']); |
|
|
|
|
|
} catch (\Exception $exception) { |
|
|
|
|
|
return json(['code' => '100500', 'message' => '取消上传失败', 'data' => [$exception->getMessage()]]); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |