Files

228 lines
7.7 KiB
PHP

<?php
namespace addons\alivod\controller;
use think\addons\Controller;
use AlibabaCloud\SDK\Vod\V20170321\Vod;
use AlibabaCloud\SDK\Vod\V20170321\Models\SubmitTranscodeJobsRequest;
use AlibabaCloud\SDK\Vod\V20170321\Models\DecryptKMSDataKeyRequest;
use AlibabaCloud\SDK\Vod\V20170321\Models\GetVideoPlayAuthRequest;
use AlibabaCloud\SDK\Vod\V20170321\Models\GetPlayInfoRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use think\Config;
use app\common\library\Upload;
use app\common\model\Attachment;
use ShaoZeMing\AliVod\Services\ReadService;
use ShaoZeMing\AliVod\Services\UploadService;
/**
* 阿里VOD
*/
class Index extends Controller
{
public function _initialize()
{
//跨域检测
check_cors_request();
parent::_initialize();
Config::set('default_return_type', 'json');
}
public function index()
{
Config::set('default_return_type', 'html');
$this->error("当前插件暂无前台页面");
}
public function getConfig(){
if(defined('TUZHI_SYSTEM_TYPE') && TUZHI_SYSTEM_TYPE == 'single'){
if(!\app\admin\library\Auth::instance()->isLogin()){
$this->error("请先登录");
}
}else{
global $_W;
if(!($_W['isfounder'] || $_W['isadmin'])){
$this->error("请先登录");
}
}
$config = \app\common\model\config\System::getConfig('alivod');
$this->success("获取成功","",$config);
}
/**
* 获取上传地址
* @return void
*/
public function getUploadUrl(){
if(!\app\admin\library\Auth::instance()->isLogin()){
return $this->error("请先登录");
}
$config = \app\common\model\config\System::getConfig('alivod');
$title = input('title');
$filename = input('filename');
//$config['template_group_id']
$data = (new \ShaoZeMing\AliVod\Services\UploadService())->createUploadVideo($title,$filename,'',$config['storage_location']);
$data = json_decode(json_encode($data),true);
return $this->success("","获取成功",$data);
}
/**
* 刷新上传地址
* @return void
*/
public function refreshUploadUrl(){
if(!\app\admin\library\Auth::instance()->isLogin()){
$this->error("请先登录");
}
$videoId = input('video_id');
$data = (new \ShaoZeMing\AliVod\Services\UploadService())->refreshUploadVideo($videoId);
$data = json_decode(json_encode($data),true);
return $this->success("","获取成功",$data);
}
/**
* 回调
*/
public function notify()
{
if(!\app\admin\library\Auth::instance()->isLogin()){
$this->error("请先登录");
}
$this->request->filter('trim,strip_tags,htmlspecialchars,xss_clean');
$size = $this->request->post('size/d');
$name = $this->request->post('name', '');
$url = $this->request->post('url', '');
$group = $this->request->post('group', '');
$md5 = $this->request->post('md5', '');
$type = $this->request->post('type', '');
$width = $this->request->post('width/d');
$height = $this->request->post('height/d');
$category = $this->request->post('category', '');
$category = array_key_exists($category, config('site.attachmentcategory') ?? []) ? $category : '';
$suffix = strtolower(pathinfo($name, PATHINFO_EXTENSION));
$suffix = $suffix && preg_match("/^[a-zA-Z0-9]+$/", $suffix) ? $suffix : 'file';
// $attachment = Attachment::where('url', $url)->where('storage', 'alivod')->find();
// if (!$attachment) {
//前端获取上传地址接口中已经提交的转码的请求,这里无需重复转码
$config = \app\common\model\config\System::getConfig('alivod');
\addons\alivod\library\Alivod::submitTranscodeJobs($url,$config['template_group_id']);
$params = array(
'uniacid'=>UNIACID,
'category' => $category,
'admin_id' => (int)session('admin.id'),
'user_id' => (int)cookie('uid'),
'filesize' => $size,
'filename' => mb_substr(htmlspecialchars(strip_tags($name)), 0, 100),
'imagewidth' => $width,
'attachment_group'=>$group,
'imageheight' => $height,
'imagetype' => $suffix,
'imageframes' => 0,
'filetype'=> (new \app\common\library\Upload())->getFileType($type),
'mimetype' => $type,
'url' => $url,
'uploadtime' => time(),
'storage' => 'alivod',
'sha1' => $md5
);
Attachment::create($params, true);
// }
$this->success();
return;
}
/**
* 视频解码
* @return void
*/
public function decode(){
$cipherText = input('CipherText');
$alivodLibrary = new \addons\alivod\library\Alivod();
$data = $alivodLibrary->decryptKMSDataKey($cipherText);
echo base64_decode($data->Plaintext);
}
/**
* 服务器中转上传文件
* 上传分片
* 合并分片
* @param bool $isApi
*/
public function upload($isApi = false)
{
$config = \app\common\model\config\System::getConfig('alivod');
//检测删除文件或附件
$checkDeleteFile = function ($attachment, $upload, $force = false) use ($config) {
//如果设定为不备份则删除文件和记录 或 强制删除
if ((isset($config['backup']) && !$config['backup']) || $force) {
if ($attachment && !empty($attachment['id'])) {
$attachment->delete();
}
if ($upload) {
//文件绝对路径
$filePath = $upload->getFile()->getRealPath() ?: $upload->getFile()->getPathname();
@unlink($filePath);
}
}
};
$attachment = null;
//默认普通上传文件
$file = $this->request->file('file');
try {
$upload = new Upload($file);
$attachment = $upload->upload();
} catch (UploadException $e) {
$this->error($e->getMessage());
}
//文件绝对路径
$filePath = $upload->getFile()->getRealPath() ?: $upload->getFile()->getPathname();
try {
//要把他上传到点播中
$fileName = $upload->getFile()->getFilename();
$uploader = new \AliyunVodUploader($config['access_key_id'], $config['access_key_secret'], $config['region_id']);
$uploadAttachedRequest = new \UploadVideoRequest($filePath, $fileName);
$uploadAttachedRequest->setStorageLocation($config['storage_location']);
$videoId = $uploader->uploadLocalVideo($uploadAttachedRequest);
$attachment->url = $videoId;
// \addons\alivod\library\Alivod::submitTranscodeJobs($videoId,$config['template_group_id']);
//成功不做任何操作
} catch (\Exception $e) {
$checkDeleteFile($attachment, $upload, true);
\think\Log::write($e->getMessage());
$this->error("上传失败");
}
$checkDeleteFile($attachment, $upload);
// 记录云存储记录
$data = $attachment->toArray();
unset($data['id']);
$data['storage'] = 'alivod';
Attachment::create($data, true);
$this->success("上传成功", '', ['url' => $attachment->url, 'full_url' => "<aliovd>$attachment->url</aliovd>>"]);
}
}