Commit c5e32fc4 by 杨昕

将获取截屏数据修改为异步

parent 857ea5b6
Pipeline #18750 passed with stages
in 1 minute 16 seconds
......@@ -326,7 +326,6 @@ class MediaController extends Controller
*/
public function getMediaListByMediaIds(Request $request)
{
$data = handler_drive(function () use ($request) {
SrsHookValidate::srsHookCallback($request, [
'media_ids' => 'required',
......@@ -336,30 +335,22 @@ class MediaController extends Controller
return success($data);
}
/**
* @param Request $request
* @param $id
* @return \Illuminate\Http\JsonResponse
* @return mixed
* @throws FormException
* @throws \App\Exceptions\ControllerException
* @throws \App\Exceptions\DatabaseException
*/
public function getScreeShotInfo(Request $request)
{
public function screenShotCallback(Request $request){
$data = handler_drive(function () use ($request) {
SrsHookValidate::srsHookCallback($request, [
'media_id' => 'required',
'secret_key' => '',
'media_url' => '',
'media_url' => 'required',
]);
return array(
"media_id" => $request->media_id,
"secret_key" => $request->secret_key,
"media_url" => $request->media_url,
);
//return MediaModel::getScreeShotInfo($request);
return MediaModel::UpdateScreenShotInfo($request);
});
return success($data);
}
......
......@@ -27,7 +27,6 @@ class MediaModel extends Eloquent
public static $max_page_size = 500;
public function media_category()
{
return $this->belongsTo('MediaCategoryModel');
......@@ -39,72 +38,73 @@ class MediaModel extends Eloquent
* @return mixed
* @throws DatabaseException
*/
public static function mediaList($request){
public static function mediaList($request)
{
try{
$medias = MediaModel::query()->raw(function ($collection) use($request){
try {
$medias = MediaModel::query()->raw(function ($collection) use ($request) {
$limit = (isset($request->page_size) ? (int)($request->page_size) : Constant::PAGE_NUMBER) ;
$limit = (isset($request->page_size) ? (int)($request->page_size) : Constant::PAGE_NUMBER);
if ($limit >self::$max_page_size){
if ($limit > self::$max_page_size) {
$limit = self::$max_page_size;
}
$page = $request->page??1;
$page = $request->page ?? 1;
$aggregate = [];
/**
* 默认降序
*/
$sort = -1;
if ($request->sort == 'asc'){
if ($request->sort == 'asc') {
$sort = 1;
}
$match = [
'status' => "1",
'status' => "1",
// "media_url" => ["\$ne" => 'null']
];
if (isset($request->media_id) && !empty($request->media_id)){
if (isset($request->media_id) && !empty($request->media_id)) {
$match['_id'] = $request->media_id;
}
if (isset($request->category_id) && !empty($request->category_id)){
if (isset($request->category_id) && !empty($request->category_id)) {
$match['media_type_id'] = $request->category_id;
}
// $aggregate[]['$match']['_id'] = 'mda-864371b9a82d1e21';
if (isset($request->media_name) && !empty($request->media_name)){
$match['media_name'] = ['$regex' => addslashes($request->media_name) ];
if (isset($request->media_name) && !empty($request->media_name)) {
$match['media_name'] = ['$regex' => addslashes($request->media_name)];
}
if (isset($request->username) && !empty($request->username)){
$match['username'] = ['$regex' => addslashes($request->username) ];
if (isset($request->username) && !empty($request->username)) {
$match['username'] = ['$regex' => addslashes($request->username)];
}
if (isset($request->description) && !empty($request->description)){
$match['description'] = ['$regex' => $request->description ];
if (isset($request->description) && !empty($request->description)) {
$match['description'] = ['$regex' => $request->description];
}
if (isset($request->start_time)&& !empty($request->start_time)){
$start_time = new UTCDateTime(strtotime($request->start_time) * 1000);
$match['start_time'] = ['$gt' => $start_time ];
if (isset($request->start_time) && !empty($request->start_time)) {
$start_time = new UTCDateTime(strtotime($request->start_time) * 1000);
$match['start_time'] = ['$gt' => $start_time];
}
if (isset($request->end_time)&& !empty($request->end_time)){
$end_time = new UTCDateTime(strtotime($request->end_time) * 1000);;
$match['end_time'] = ['$lt' => $end_time ];
if (isset($request->end_time) && !empty($request->end_time)) {
$end_time = new UTCDateTime(strtotime($request->end_time) * 1000);;
$match['end_time'] = ['$lt' => $end_time];
}
$aggregate[]['$match'] = $match;
$aggregate[]['$sort'] = ['start_time' => $sort];
$aggregate[]['$sort'] = ['start_time' => $sort];
$count = MediaModel::where($match)->count();
$skip = ((empty($request->page) ? 1 : $request->page)-1) * $limit;
$skip = ((empty($request->page) ? 1 : $request->page) - 1) * $limit;
$aggregate[]['$skip'] = $skip;
$aggregate[]['$limit'] = $limit;
......@@ -116,10 +116,10 @@ class MediaModel extends Eloquent
/**
* 将mongoDate 转化为普通时间
*/
foreach ($tmp_arr as $media){
foreach ($tmp_arr as $media) {
$tz = new \DateTimeZone("Asia/Shanghai");
if (is_object($media['start_time'])){
if (is_object($media['start_time'])) {
$start_time = $media['start_time']->toDateTime();
$start_time->setTimezone($tz);
......@@ -128,19 +128,19 @@ class MediaModel extends Eloquent
$media['start_time'] = $start_time;
}
if (is_object($media['end_time'])){
if (is_object($media['end_time'])) {
$end_time = $media['end_time']->toDateTime();
$end_time->setTimezone($tz);
$media['end_time'] = $end_time->format('Y-m-d H:i:s');
}
if (is_object($media['create_time'])){
if (is_object($media['create_time'])) {
$create_time = $media['create_time']->toDateTime();
$create_time->setTimezone($tz);
$media['create_time'] = $create_time->format('Y-m-d H:i:s');
}
}
return new LengthAwarePaginator($tmp_arr, $count, $limit, $page,[
return new LengthAwarePaginator($tmp_arr, $count, $limit, $page, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => 'page',
]);
......@@ -148,7 +148,7 @@ class MediaModel extends Eloquent
});
}catch (\Exception $exception){
} catch (\Exception $exception) {
throw new DatabaseException($exception->getMessage());
}
......@@ -162,9 +162,10 @@ class MediaModel extends Eloquent
* @return mixed
* @throws DatabaseException
*/
public static function hookRestoreMedia($request,$category = "media"){
public static function hookRestoreMedia($request, $category = "media")
{
try{
try {
/**
* 判断媒资ID是否存在
......@@ -172,32 +173,32 @@ class MediaModel extends Eloquent
$media_id = $request->media_id;
$media_type = $request->media_type;
$mediaCatg = MediaCategoryModel::where('_id',$media_type)->first();
if (empty($mediaCatg)){
$mediaCatg = MediaCategoryModel::where('_id', $media_type)->first();
if (empty($mediaCatg)) {
throw new \Exception("媒资类别不存在");
}
//replace 不为空的时候代表做视频替换操作,1.需要清空CDN缓存,2.将原来的视频保存至回收站,3.更新视频信息
if ($request->replace){
if ($request->replace) {
$recycleId = RecycleModel::addRecycle($media_id);
$call_back = $mediaCatg['call_back'];
#清空CND缓存
$url = getRbac($request,$call_back,$mediaCatg['media_category_id'] ,$type='clear_cache');
http_request_code($url,null,'POST',['mediaId'=>$media_id]);
$url = getRbac($request, $call_back, $mediaCatg['media_category_id'], $type = 'clear_cache');
http_request_code($url, null, 'POST', ['mediaId' => $media_id]);
#更新视频信息
self::updatehookMedia($request,$media_id);
self::updatehookMedia($request, $media_id);
if (!$recycleId){
if (!$recycleId) {
throw new \Exception('回收站保存失败');
}
return $media_id;
}else{
} else {
$media = MediaModel::where("_id",$media_id)->first();
$media = MediaModel::where("_id", $media_id)->first();
if (!empty($media)){
if (!empty($media)) {
throw new \Exception("媒资编号已被使用");
}
}
......@@ -206,77 +207,77 @@ class MediaModel extends Eloquent
/**
* 媒资名称是由课程编号与课程名称组成
*/
$media_info = explode("&",$request->media_name);
$media_info = explode("&", $request->media_name);
$media_url = $request->media_url;
$media_url = str_replace("http:","https:",$media_url);
$media_url = str_replace("http:", "https:", $media_url);
/**
* 构造开始时间,结束时间
*/
$start_time = new UTCDateTime(strtotime($request->start_time) * 1000);
$end_time = new UTCDateTime(strtotime($request->end_time) * 1000);
$start_time = new UTCDateTime(strtotime($request->start_time) * 1000);
$end_time = new UTCDateTime(strtotime($request->end_time) * 1000);
$create_time = new UTCDateTime(time() * 1000);;
/**
* 处理文件名称,确认是否包含"&"符合
*/
if (count($media_info)>=2){
if (count($media_info) >= 2) {
$data = [
'_id' => $media_id,
'media_name' => $request->media_name,
'_id' => $media_id,
'media_name' => $request->media_name,
'media_type_id' => $mediaCatg['_id'],
'media_type' => $mediaCatg['name'],
'book_num' => $media_info[0],
'book_name' => $media_info[1],
'media_url' => $media_url,
'access_key' => $request->access_key,
'secret_key' => $request->secret_key,
'create_time' => $create_time,
'description' => $request->description,
'username' => $request->token_username,
'user_id' => $request->token_user_id,
'mis_uid' => $request->mis_uid,//员工工号
'start_time' => $start_time,
'end_time' => $end_time,
'duration' => $request->duration??"0",
'status' => $request->status??"0",
'media_type' => $mediaCatg['name'],
'book_num' => $media_info[0],
'book_name' => $media_info[1],
'media_url' => $media_url,
'access_key' => $request->access_key,
'secret_key' => $request->secret_key,
'create_time' => $create_time,
'description' => $request->description,
'username' => $request->token_username,
'user_id' => $request->token_user_id,
'mis_uid' => $request->mis_uid,//员工工号
'start_time' => $start_time,
'end_time' => $end_time,
'duration' => $request->duration ?? "0",
'status' => $request->status ?? "0",
];
}else{
} else {
$data = [
'_id' => $media_id,
'media_name' => $request->media_name,
'_id' => $media_id,
'media_name' => $request->media_name,
'media_type_id' => $mediaCatg['_id'],
'media_type' => $mediaCatg['name'],
'book_num' => '',
'book_name' => $request->media_name,
'media_url' => $media_url,
'access_key' => $request->access_key,
'secret_key' => $request->secret_key,
'create_time' => $create_time,
'description' => $request->description,
'username' => $request->token_username,
'user_id' => $request->token_user_id,
'mis_uid' => $request->mis_uid,//员工工号
'start_time' => $start_time,
'end_time' => $end_time,
'duration' => $request->duration??"0",
'status' => $request->status??"0",
'media_type' => $mediaCatg['name'],
'book_num' => '',
'book_name' => $request->media_name,
'media_url' => $media_url,
'access_key' => $request->access_key,
'secret_key' => $request->secret_key,
'create_time' => $create_time,
'description' => $request->description,
'username' => $request->token_username,
'user_id' => $request->token_user_id,
'mis_uid' => $request->mis_uid,//员工工号
'start_time' => $start_time,
'end_time' => $end_time,
'duration' => $request->duration ?? "0",
'status' => $request->status ?? "0",
];
}
$id = MediaModel::insertGetId($data);
//处理封面图片以及分辨率
//self::getScreeShotInfo($media_id);
self::getScreeShotInfo($media_id);
LogModel::addlog(["添加媒资信息"=> $data,['媒资ID为:=> '.$id]]);
}catch (\Exception $exception){
LogModel::addlog(["添加媒资信息" => $data, ['媒资ID为:=> ' . $id]]);
} catch (\Exception $exception) {
throw new DatabaseException($exception->getMessage());
}
return $id;
......@@ -289,12 +290,13 @@ class MediaModel extends Eloquent
* @return mixed
* @throws DatabaseException
*/
public static function updatehookMedia($request,$id){
public static function updatehookMedia($request, $id)
{
try{
try {
$media = MediaModel::find($id);
if (empty($media)){
if (empty($media)) {
throw new \Exception("媒资不存在");
}
......@@ -302,72 +304,71 @@ class MediaModel extends Eloquent
/**
* 媒资名称是由课程编号与课程名称组成
*/
$media_info = explode("&",$request->media_name);
$media_info = explode("&", $request->media_name);
$mediaCatg = MediaCategoryModel::where('_id',$request->media_type)->first();
if (empty($mediaCatg)){
$mediaCatg = MediaCategoryModel::where('_id', $request->media_type)->first();
if (empty($mediaCatg)) {
throw new \Exception("媒资类别不存在");
}
$start_time = new UTCDateTime(strtotime($request->start_time) * 1000);
$end_time = new UTCDateTime(strtotime($request->end_time) * 1000);
$start_time = new UTCDateTime(strtotime($request->start_time) * 1000);
$end_time = new UTCDateTime(strtotime($request->end_time) * 1000);
/**
* 处理文件名称,确认是否包含"&"符合
*/
if (count($media_info)>=2){
if (count($media_info) >= 2) {
$data = [
// '_id' => $id,
'media_name' => $request->media_name,
'media_type' => $mediaCatg['name'],
'media_name' => $request->media_name,
'media_type' => $mediaCatg['name'],
'media_type_id' => $mediaCatg['_id'],
'book_num' => $media_info[0],
'book_name' => $media_info[1],
'media_url' => $request->media_url?$request->media_url:$media['media_url'],
'book_num' => $media_info[0],
'book_name' => $media_info[1],
'media_url' => $request->media_url ? $request->media_url : $media['media_url'],
// 'access_key' => $request->access_key,
'secret_key' => $request->secret_key?$request->secret_key:$media['secret_key'],
'secret_key' => $request->secret_key ? $request->secret_key : $media['secret_key'],
// 'create_time' => $media['create_time'],
'description' => $request->description,
'start_time' => $request->start_time?$start_time:$media['start_time'],
'end_time' => $request->end_time?$end_time:$media['start_time'],
'description' => $request->description,
'start_time' => $request->start_time ? $start_time : $media['start_time'],
'end_time' => $request->end_time ? $end_time : $media['start_time'],
//'duration' => $request->duration??"0",
];
}else{
} else {
$data = [
// '_id' => $id,
'media_name' => $request->media_name,
'media_type' => $mediaCatg['name'],
'media_name' => $request->media_name,
'media_type' => $mediaCatg['name'],
'media_type_id' => $mediaCatg['_id'],
'book_num' => '',
'book_name' => $request->media_name,
'media_url' => $request->media_url?$request->media_url:$media['media_url'],
'book_num' => '',
'book_name' => $request->media_name,
'media_url' => $request->media_url ? $request->media_url : $media['media_url'],
// 'access_key' => $request->access_key,
'secret_key' => $request->secret_key?$request->secret_key:$media['secret_key'],
'secret_key' => $request->secret_key ? $request->secret_key : $media['secret_key'],
// 'create_time' => date('Y-m-d H:i:s',time()),
'description' => $request->description,
'start_time' => $request->start_time?$start_time:$media['start_time'],
'end_time' => $request->end_time?$end_time:$media['start_time'],
// 'duration' => $request->duration??"0",
'description' => $request->description,
'start_time' => $request->start_time ? $start_time : $media['start_time'],
'end_time' => $request->end_time ? $end_time : $media['start_time'],
// 'duration' => $request->duration??"0",
];
}
$media_duration=$request->duration??"0";
if($media_duration!=0)
{
$data['duration']=$request->duration;
$media_duration = $request->duration ?? "0";
if ($media_duration != 0) {
$data['duration'] = $request->duration;
}
$flag = MediaModel::where("_id",$id)->update($data);
if (!$flag){
$flag = MediaModel::where("_id", $id)->update($data);
if (!$flag) {
throw new \Exception("更新失败");
}
//处理封面图片以及分辨率
//self::getScreeShotInfo($id);
LogModel::addlog(["更新媒资信息=>".$id,$data]);
self::getScreeShotInfo($id);
LogModel::addlog(["更新媒资信息=>" . $id, $data]);
}catch (\Exception $exception){
} catch (\Exception $exception) {
throw new DatabaseException($exception->getMessage());
}
return $flag;
......@@ -380,16 +381,17 @@ class MediaModel extends Eloquent
* @return mixed
* @throws DatabaseException
*/
public static function getMediaDetail($id){
public static function getMediaDetail($id)
{
try{
try {
$media = MediaModel::find($id);
if (empty($media)){
if (empty($media)) {
throw new \Exception('媒资不存在');
}
}catch (\Exception $exception){
} catch (\Exception $exception) {
throw new DatabaseException($exception->getMessage());
}
......@@ -403,43 +405,44 @@ class MediaModel extends Eloquent
* @return mixed
* @throws DatabaseException
*/
public static function getMediaList($request){
public static function getMediaList($request)
{
try{
try {
/**
*[{"bookNum":["hb15011","2"],"mediaCatg":"一题一码"}]
*/
$params = json_decode($request->params,true);
$params = json_decode($request->params, true);
if (empty($params)){
if (empty($params)) {
throw new \Exception("参数格式有误");
}
$tmp = [];
foreach ($params as $param){
foreach ($params as $param) {
if (!isset($param['mediaCatg']) || !isset($param['bookNum'])){
if (!isset($param['mediaCatg']) || !isset($param['bookNum'])) {
throw new \Exception("格式有误");
}
$media_name= $param['mediaCatg'];
$media_name = $param['mediaCatg'];
$media_ids = $param['bookNum'];
$media_catg = MediaCategoryModel::where('name',trim($media_name))->first();
$media_catg = MediaCategoryModel::where('name', trim($media_name))->first();
if (empty($media_catg)){
if (empty($media_catg)) {
throw new \Exception("媒资类别不存在");
}
$tmp[] = MediaModel::select("_id","book_num","book_name","secret_key","media_url")->where("media_type_id",$media_catg['_id'])->whereIn('book_num',$media_ids)->get();
$tmp[] = MediaModel::select("_id", "book_num", "book_name", "secret_key", "media_url")->where("media_type_id", $media_catg['_id'])->whereIn('book_num', $media_ids)->get();
}
}catch (\Exception $exception){
} catch (\Exception $exception) {
throw new DatabaseException($exception->getMessage());
}
$res = [];
foreach ($tmp as $item){
foreach ($tmp as $item) {
$res = $item->groupBy("book_num");
}
......@@ -452,11 +455,12 @@ class MediaModel extends Eloquent
* @return int
* @throws \Exception
*/
public static function deleteMediaById($request,$id){
public static function deleteMediaById($request, $id)
{
try{
$media = MediaModel::where('_id',$id)->first();
if (empty($media)){
try {
$media = MediaModel::where('_id', $id)->first();
if (empty($media)) {
throw new \Exception('媒资不存在!');
}
......@@ -470,20 +474,20 @@ class MediaModel extends Eloquent
$recycleId = RecycleModel::addRecycle($id);
if (!$recycleId){
if (!$recycleId) {
$session->abortTransaction();
throw new \Exception('回收站保存失败');
}
$flag = MediaModel::destroy($id);
if (!$flag){
LogModel::addlog(["删除媒资失败"=> $id]);
if (!$flag) {
LogModel::addlog(["删除媒资失败" => $id]);
$session->abortTransaction();
throw new \Exception('删除失败');
}
$session->commitTransaction();
}catch (\Exception $exception){
} catch (\Exception $exception) {
throw new \Exception($exception->getMessage());
......@@ -497,26 +501,27 @@ class MediaModel extends Eloquent
* @return mixed
* @throws DatabaseException
*/
public static function playUrl($media_id){
public static function playUrl($media_id)
{
try{
try {
$media = MediaModel::select('media_url','secret_key','duration','media_name','status','height','width','screen_shot')->find($media_id);
$media = MediaModel::select('media_url', 'secret_key', 'duration', 'media_name', 'status', 'height', 'width', 'screen_shot')->find($media_id);
if (empty($media)){
if (empty($media)) {
throw new \Exception("媒资不存在");
}
if (!isset($media['duration'])){
if (!isset($media['duration'])) {
$media['duration'] = "0";
}
if (!isset($media['status'])){
if (!isset($media['status'])) {
$media['status'] = "1";
}
}catch (\Exception $exception){
} catch (\Exception $exception) {
throw new DatabaseException($exception->getMessage());
}
......@@ -529,60 +534,61 @@ class MediaModel extends Eloquent
* @return \Illuminate\Database\Query\Expression
* @throws DatabaseException
*/
public static function handleMediaUrl($request){
public static function handleMediaUrl($request)
{
try{
$medias = MediaModel::query()->raw(function ($collection) use($request){
try {
$medias = MediaModel::query()->raw(function ($collection) use ($request) {
$limit = (isset($request->page_size) ? (int)($request->page_size) : Constant::PAGE_NUMBER) ;
$limit = (isset($request->page_size) ? (int)($request->page_size) : Constant::PAGE_NUMBER);
$page = $request->page??1;
$page = $request->page ?? 1;
/**
* 默认降序
*/
$sort = -1;
if ($request->sort == 'asc'){
if ($request->sort == 'asc') {
$sort = 1;
}
if (isset($request->media_id) && !empty($request->media_id)){
if (isset($request->media_id) && !empty($request->media_id)) {
$aggregate[]['$match']['_id'] = $request->media_id;
}
$aggregate[]['$match']['media_url'] = ['$regex' => 'qcoenfz67.bkt.clouddn.com' ];
$aggregate[]['$match']['media_url'] = ['$regex' => 'qcoenfz67.bkt.clouddn.com'];
if (isset($request->username) && !empty($request->username)){
if (isset($request->username) && !empty($request->username)) {
$aggregate[]['$match']['username'] = $request->username;
}
$aggregate[]['$sort'] = ['start_time' => $sort];
$aggregate[]['$sort'] = ['start_time' => $sort];
$aggregate1=$aggregate;
$aggregate1 = $aggregate;
$aggregate1[]['$group'] = [
'_id' => array(),//更具性别进行分组
'count'=>array('$sum'=>1)
'count' => array('$sum' => 1)
];
$count = $collection->aggregate($aggregate1)->toArray();
if (isset($count[0])){
if (isset($count[0])) {
$count = $count[0]['count'];
}else{
} else {
$count = 0;
}
//
//$count = sizeof($collection->aggregate($aggregate)->toArray());
$skip = ((empty($request->page) ? 1 : $request->page)-1) * $limit;
$skip = ((empty($request->page) ? 1 : $request->page) - 1) * $limit;
$aggregate[]['$skip'] = $skip;
$aggregate[]['$limit'] = $limit;
return new LengthAwarePaginator($collection->aggregate($aggregate)->toArray(), $count, $limit, $page,[
return new LengthAwarePaginator($collection->aggregate($aggregate)->toArray(), $count, $limit, $page, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => 'page',
]);
......@@ -591,17 +597,16 @@ class MediaModel extends Eloquent
});
if (!empty($medias)) {
if (!empty($medias)){
foreach ($medias as $media){
foreach ($medias as $media) {
$data = [
'media_url' => "https://vod-mam.eoffcn.com/".$media->_id.'/index.m3u8',
'media_url' => "https://vod-mam.eoffcn.com/" . $media->_id . '/index.m3u8',
];
$flag = MediaModel::where("_id",$media->_id)->update($data);
if ($flag==0){
$flag = MediaModel::where("_id", $media->_id)->update($data);
if ($flag == 0) {
throw new \Exception("更新失败");
}
}
......@@ -609,8 +614,7 @@ class MediaModel extends Eloquent
}
}catch (\Exception $exception){
} catch (\Exception $exception) {
throw new DatabaseException($exception->getMessage());
}
return $medias;
......@@ -624,56 +628,57 @@ class MediaModel extends Eloquent
* @return mixed
* @throws DatabaseException
*/
public static function updateMediaStatus($request,$media_id){
try{
public static function updateMediaStatus($request, $media_id)
{
try {
$media = MediaModel::where('_id',$media_id)->first();
if (empty($media)){
$media = MediaModel::where('_id', $media_id)->first();
if (empty($media)) {
$mediaCatg = MediaCategoryModel::where('_id',$request->media_type)->first();
if (empty($mediaCatg)){
$mediaCatg = MediaCategoryModel::where('_id', $request->media_type)->first();
if (empty($mediaCatg)) {
throw new \Exception("媒资类别不存在");
}
$create_time = new UTCDateTime(time() * 1000);;
$data = [
'_id' => $media_id,
'media_name' => $request->media_name??"",
'media_type' => $mediaCatg['name'],
'_id' => $media_id,
'media_name' => $request->media_name ?? "",
'media_type' => $mediaCatg['name'],
'media_type_id' => $mediaCatg['_id'],
'media_url' => $request->media_url??"",
'access_key' => $request->access_key??"",
'secret_key' => $request->secret_key??"",
'create_time' => $create_time,
'description' => $request->description??"",
'start_time' => $create_time,
'end_time' => $create_time,
'duration' => $request->duration??"0",
"username" => "offcn",
"mis_uid" => "admin"
'media_url' => $request->media_url ?? "",
'access_key' => $request->access_key ?? "",
'secret_key' => $request->secret_key ?? "",
'create_time' => $create_time,
'description' => $request->description ?? "",
'start_time' => $create_time,
'end_time' => $create_time,
'duration' => $request->duration ?? "0",
"username" => "offcn",
"mis_uid" => "admin"
];
$id = MediaModel::insertGetId($data);
LogModel::addlog(["添加媒资信息"=> $data,['媒资ID为:=> '.$id]]);
LogModel::addlog(["添加媒资信息" => $data, ['媒资ID为:=> ' . $id]]);
return $id;
}
if ($request->status){
if ($request->status) {
$data = [
'media_url' => $request->media_url??"",
'media_name' => $request->media_name??"",
'duration' => $request->duration??"0",
'access_key' => $request->access_key??"",
'secret_key' => $request->secret_key??"",
'status' => $request->status??''
'media_url' => $request->media_url ?? "",
'media_name' => $request->media_name ?? "",
'duration' => $request->duration ?? "0",
'access_key' => $request->access_key ?? "",
'secret_key' => $request->secret_key ?? "",
'status' => $request->status ?? ''
];
LogModel::addlog(["原media信息为"=>json_encode($media),"更新为"=>json_encode($data)]);
}else{
LogModel::addlog(["原media信息为" => json_encode($media), "更新为" => json_encode($data)]);
} else {
$data['status'] = "0";
}
......@@ -687,15 +692,15 @@ class MediaModel extends Eloquent
// }
// }
$flag = MediaModel::where("_id",$media_id)->update($data);
if (!$flag){
$flag = MediaModel::where("_id", $media_id)->update($data);
if (!$flag) {
LogModel::addlog("Status更新失败");
//throw new \Exception("Status更新失败");
}
LogModel::addlog(["更新媒资信息Status=>".$media_id,$data]);
LogModel::addlog(["更新媒资信息Status=>" . $media_id, $data]);
}catch (\Exception $exception){
} catch (\Exception $exception) {
throw new DatabaseException($exception->getMessage());
}
return $media_id;
......@@ -708,24 +713,25 @@ class MediaModel extends Eloquent
* @return \Illuminate\Database\Query\Expression
* @throws DatabaseException
*/
public static function updateDatetime($request){
public static function updateDatetime($request)
{
try{
$medias = MediaModel::query()->raw(function ($collection) use($request){
try {
$medias = MediaModel::query()->raw(function ($collection) use ($request) {
$limit = (isset($request->page_size) ? (int)($request->page_size) : Constant::PAGE_NUMBER) ;
$limit = (isset($request->page_size) ? (int)($request->page_size) : Constant::PAGE_NUMBER);
$page = $request->page??1;
$page = $request->page ?? 1;
/**
* 默认降序
*/
$sort = -1;
if ($request->sort == 'asc'){
if ($request->sort == 'asc') {
$sort = 1;
}
if (isset($request->media_id) && !empty($request->media_id)){
if (isset($request->media_id) && !empty($request->media_id)) {
$aggregate[]['$match']['_id'] = $request->media_id;
}
......@@ -737,32 +743,32 @@ class MediaModel extends Eloquent
// $aggregate[]['$match']['media_name'] = "test3.mp3";
$aggregate[]['$sort'] = ['start_time' => $sort];
$aggregate[]['$sort'] = ['start_time' => $sort];
$aggregate1=$aggregate;
$aggregate1 = $aggregate;
$aggregate1[]['$group'] = [
'_id' => array(),//更具性别进行分组
'count'=>array('$sum'=>1)
'count' => array('$sum' => 1)
];
$count = $collection->aggregate($aggregate1)->toArray();
if (isset($count[0])){
if (isset($count[0])) {
$count = $count[0]['count'];
}else{
} else {
$count = 0;
}
//$count = sizeof($collection->aggregate($aggregate)->toArray());
$skip = ((empty($request->page) ? 1 : $request->page)-1) * $limit;
$skip = ((empty($request->page) ? 1 : $request->page) - 1) * $limit;
$aggregate[]['$skip'] = $skip;
$aggregate[]['$limit'] = $limit;
return new LengthAwarePaginator($collection->aggregate($aggregate)->toArray(), $count, $limit, $page,[
return new LengthAwarePaginator($collection->aggregate($aggregate)->toArray(), $count, $limit, $page, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => 'page',
]);
......@@ -770,19 +776,19 @@ class MediaModel extends Eloquent
});
if (!empty($medias)){
if (!empty($medias)) {
foreach ($medias as $media){
foreach ($medias as $media) {
/**
* 构造开始时间,结束时间
*/
$tmp_medias = MediaModel::where("media_name",$media['media_name'])->count();
$tmp_medias = MediaModel::where("media_name", $media['media_name'])->count();
if ($tmp_medias>1){
if ($tmp_medias > 1) {
MediaModel::deleteMediaById($request,$media['_id']);
MediaModel::deleteMediaById($request, $media['_id']);
var_dump($tmp_medias);
}
......@@ -792,7 +798,7 @@ class MediaModel extends Eloquent
}
}catch (\Exception $exception){
} catch (\Exception $exception) {
throw new DatabaseException($exception->getMessage());
}
return $medias;
......@@ -803,20 +809,21 @@ class MediaModel extends Eloquent
* @return string
* @throws DatabaseException
*/
public static function getMediaId(){
public static function getMediaId()
{
try{
try {
$i = 0;
do {
$media_id = "mda-".generateRandomNum(16);
$media_id = "mda-" . generateRandomNum(16);
$media = MediaModel::find($media_id);
$i ++;
} while ($i < 3 && !empty($media) );
$i++;
} while ($i < 3 && !empty($media));
if ($i == 3){
if ($i == 3) {
throw new \Exception('获取媒资编号失败');
}
}catch (\Exception $exception){
} catch (\Exception $exception) {
throw new DatabaseException($exception->getMessage());
}
return $media_id;
......@@ -828,37 +835,35 @@ class MediaModel extends Eloquent
* @return mixed
* @throws DatabaseException
*/
public static function setMediaNum($request){
try{
public static function setMediaNum($request)
{
try {
$media_id = $request->media_id;
$data = [
'media_num' => md5(md5($media_id)),
'media_num' => md5(md5($media_id)),
];
$flag = MediaModel::where("_id",$media_id)->update($data);
}catch (\Exception $exception){
$flag = MediaModel::where("_id", $media_id)->update($data);
} catch (\Exception $exception) {
throw new DatabaseException($exception->getMessage());
}
return $flag;
}
public static function multiplySetMediaNum($request){
public static function multiplySetMediaNum($request)
{
try{
try {
$media_ids = $request->media_ids;
//$flag = MediaModel::whereIn("_id",$media_ids)->get($data,['multiple'=>true]);
$medias = MediaModel::whereIn("_id",$media_ids)->limit(self::$max_page_size)->get();
$medias = MediaModel::whereIn("_id", $media_ids)->limit(self::$max_page_size)->get();
$multiplied = collect($medias)->map(function ($item, $key) {
return $item * 2;
});
......@@ -871,10 +876,10 @@ class MediaModel extends Eloquent
$media_id = $request->media_id;
$data = [
'media_num' => md5(md5($media_id)),
'media_num' => md5(md5($media_id)),
];
$flag = MediaModel::whereIn("_id",$media_id)->update($data,['multiple'=>true]);
}catch (\Exception $exception){
$flag = MediaModel::whereIn("_id", $media_id)->update($data, ['multiple' => true]);
} catch (\Exception $exception) {
throw new DatabaseException($exception->getMessage());
}
return $flag;
......@@ -882,21 +887,20 @@ class MediaModel extends Eloquent
}
/**
* 根据媒资编号获取媒资随机码
* @param $request
* @return mixed
* @throws DatabaseException
*/
public static function getMediaInfoByNum($request){
try{
$media = MediaModel::select('_id','secret_key','duration','media_name','status')->where("media_num",$request->media_num)->first();
if (empty($media)){
public static function getMediaInfoByNum($request)
{
try {
$media = MediaModel::select('_id', 'secret_key', 'duration', 'media_name', 'status')->where("media_num", $request->media_num)->first();
if (empty($media)) {
throw new \Exception("媒资信息不存在");
}
}catch (\Exception $exception){
} catch (\Exception $exception) {
throw new DatabaseException($exception->getMessage());
}
return $media;
......@@ -907,32 +911,30 @@ class MediaModel extends Eloquent
* @return mixed
* @throws DatabaseException
*/
public static function clearCdnCache($request,$media_id){
public static function clearCdnCache($request, $media_id)
{
try{
try {
/**
* 判断媒资ID是否存在
*/
$media = MediaModel::where("media_num",$request->media_num)->first();
if (empty($media)){
$media = MediaModel::where("media_num", $request->media_num)->first();
if (empty($media)) {
throw new \Exception("媒资信息不存在");
}
$media_type = $media['media_type_id'];
$mediaCatg = MediaCategoryModel::where('_id',$media_type)->first();
if (empty($mediaCatg)){
$mediaCatg = MediaCategoryModel::where('_id', $media_type)->first();
if (empty($mediaCatg)) {
throw new \Exception("媒资类别不存在");
}
$call_back = $mediaCatg['call_back'];
$url = getRbac($request,$call_back,$mediaCatg['media_category_id'] ,$type='clear_cache');
http_request_code($url,null,'POST',['mediaId'=>$media_id]);
$url = getRbac($request, $call_back, $mediaCatg['media_category_id'], $type = 'clear_cache');
http_request_code($url, null, 'POST', ['mediaId' => $media_id]);
}catch (\Exception $exception){
} catch (\Exception $exception) {
throw new DatabaseException($exception->getMessage());
}
return $media;
......@@ -946,37 +948,38 @@ class MediaModel extends Eloquent
* @return mixed
* @throws DatabaseException
*/
public static function getMediaForeverM3u8($request){
public static function getMediaForeverM3u8($request)
{
try{
try {
$media_id = $request->media_id;
/**
* 判断媒资ID是否存在
*/
$media = MediaModel::where("_id",$media_id)->first();
if (empty($media)){
$media = MediaModel::where("_id", $media_id)->first();
if (empty($media)) {
throw new \Exception("媒资信息不存在");
}
$media_type = $media['media_type_id'];
$mediaCatg = MediaCategoryModel::where('_id',$media_type)->first();
if (empty($mediaCatg)){
$mediaCatg = MediaCategoryModel::where('_id', $media_type)->first();
if (empty($mediaCatg)) {
throw new \Exception("媒资类别不存在");
}
$url = getRbac($request,'video/info',$media_id ,$type='play_m3u8');
$url = getRbac($request, 'video/info', $media_id, $type = 'play_m3u8');
$res = http_request_code($url,null,'GET');
$res = http_request_code($url, null, 'GET');
$result = json_decode($res,true);
$result = json_decode($res, true);
if ($result['code'] != 0){
if ($result['code'] != 0) {
LogModel::addlog($result);
throw new \Exception("获取数据失败");
}
}catch (\Exception $exception){
} catch (\Exception $exception) {
throw new DatabaseException($exception->getMessage());
}
......@@ -989,31 +992,32 @@ class MediaModel extends Eloquent
* @return array
* @throws DatabaseException
*/
public static function batchGetMediaForverM3u8($request,array $orginal_data){
public static function batchGetMediaForverM3u8($request, array $orginal_data)
{
try{
try {
$ids = implode(',',array_keys($orginal_data));
$ids = implode(',', array_keys($orginal_data));
#生成批量处理地址
$url = getRbac($request,'video/infos','' ,$type='mul_play_m3u8');
$url = getRbac($request, 'video/infos', '', $type = 'mul_play_m3u8');
$res = http_request_code($url,null,'POST',array('mediaIds'=>$ids));
$result = json_decode($res,true);
$res = http_request_code($url, null, 'POST', array('mediaIds' => $ids));
$result = json_decode($res, true);
if (empty($result)){
if (empty($result)) {
return [];
}
if ($result['code'] != 0){
if ($result['code'] != 0) {
LogModel::addlog($result);
throw new \Exception("获取数据失败");
}
return self::mediaExportFormat($result['data'],$orginal_data);
return self::mediaExportFormat($result['data'], $orginal_data);
}catch (\Exception $exception){
} catch (\Exception $exception) {
throw new DatabaseException($exception->getMessage());
}
}
......@@ -1024,35 +1028,36 @@ class MediaModel extends Eloquent
* @param $media_ids
* @return mixed
*/
private static function mediaExportFormat(array $data,$orginal_data){
private static function mediaExportFormat(array $data, $orginal_data)
{
foreach ($orginal_data as $key => &$item){
foreach ($orginal_data as $key => &$item) {
if (empty($data[$key])){
$item['media_id'] = $key;
$item['media_name'] = '未查到相关数据';
if (empty($data[$key])) {
$item['media_id'] = $key;
$item['media_name'] = '未查到相关数据';
$item['forvery_url'] = '未查到相关数据';
$item['is_full_url'] = "未查到相关数据";
}else{
} else {
$media_id = $item['media_id'];
$media = MediaModel::select('media_name')->where('_id',$media_id)->first();
$media = MediaModel::select('media_name')->where('_id', $media_id)->first();
$media_num = md5(md5($media_id));
$item['media_name'] = $media['media_name'];
$tmp = [
'media_num' => $media_num,
'media_num' => $media_num,
];
MediaModel::where("_id",$media_id)->update($tmp);
MediaModel::where("_id", $media_id)->update($tmp);
$url = getenv('preview_domain')?getenv('preview_domain'):"https://xue.t.eoffcn.com/preview/temp/";
$url = getenv('preview_domain') ? getenv('preview_domain') : "https://xue.t.eoffcn.com/preview/temp/";
$item['forvery_url'] = $data[$key]['url'];
$item['is_full_url'] = $url."player".'/'.$media_num;
$item['is_full_url'] = $url . "player" . '/' . $media_num;
}
......@@ -1067,12 +1072,13 @@ class MediaModel extends Eloquent
* @return mixed
* @throws DatabaseException
*/
public static function getMediaListByMediaIds($request){
public static function getMediaListByMediaIds($request)
{
try{
try {
$media_ids = $request->media_ids;
$medias = MediaModel::select("_id","media_name","duration","secret_key","media_url")->where("status","1")->where("duration",">","0")->where("media_url","!=","")->whereIn('_id',$media_ids)->limit(100)->get();
}catch (\Exception $exception){
$medias = MediaModel::select("_id", "media_name", "duration", "secret_key", "media_url")->where("status", "1")->where("duration", ">", "0")->where("media_url", "!=", "")->whereIn('_id', $media_ids)->limit(100)->get();
} catch (\Exception $exception) {
throw new DatabaseException($exception->getMessage());
}
......@@ -1085,45 +1091,59 @@ class MediaModel extends Eloquent
* @return mixed
* @throws DatabaseException
*/
public static function getScreeShotInfo($request){
try{
$media = MediaModel::select("_id","media_name","duration","secret_key","media_url")->where('_id',$media_id)->first();
}catch (\Exception $exception){
public static function GetScreeShotInfo($media_id)
{
LogModel::addlog(["向第三方发送获取 screenshot 数据请求",$media_id]);
try {
$media = MediaModel::select("_id", "media_name", "duration", "secret_key", "media_url")->where('_id', $media_id)->first();
} catch (\Exception $exception) {
throw new DatabaseException($exception->getMessage());
}
//获取媒资截图数据
//http://test-playback.offcncloud.com/api/v1/vodRecordInfo
$playbackUrl = getenv('playback')?getenv('playback'):'http://test-playback.offcncloud.com';
$playbackUrl = getenv('playback') ? getenv('playback') : 'http://test-playback.offcncloud.com';
$params = array(
"url" => $media["media_url"],
"key" => $media["secret_key"]
"key" => $media["secret_key"],
"media_id" => $media_id,
);
$data = json_encode($params);
$header = array(
"Content-Type: application/json",
"Content-Length: ".strlen($data)
"Content-Length: " . strlen($data)
);
$url = $playbackUrl."/api/v1/vodRecordInfo";
LogModel::addlog(["第三方获取媒资 screenshot 请求路径"=>$url,"请求参数"=>$data,"请求头"=>$header]);
$res = http_request_code($url,$header,'POST',$data,15);
$result = json_decode($res,true);
LogModel::addlog(["第三方获取媒资 screenshot 数据"=>$res,"请求路径"=>$url,"请求参数"=>$data,"请求头"=>$header]);
$item = array();
if (isset($result["data"])){
$item["screen_shot"]["height"] = $result["data"]["height"];
$item["screen_shot"]["width"] = $result["data"]["width"];
$item["screen_shot"]["url"] = $result["data"]["screenshot"];
$flag = MediaModel::where("_id",$media_id)->update($item);
if (!$flag){
LogModel::addlog(["更新 screenshot 数据失败 第三方screenshot数据"=>$res,"请求路径"=>$url,"请求参数"=>$data,"请求头"=>$header]);
throw new DatabaseException("更新媒资 screenshot 数据失败");
}
LogModel::addlog(["媒资 screenshot 数据成功 最终screenshot结果"=>$result,"请求参数"=>$params]);
$url = $playbackUrl . "/api/v1/vodRecordInfo";
LogModel::addlog(["第三方获取媒资 screenshot 请求路径" => $url, "请求参数" => $data, "请求头" => $header]);
$res = http_request_code($url, $header, 'POST', $data);
LogModel::addlog(["第三方获取媒资 screenshot 数据" => $res, "请求路径" => $url, "请求参数" => $data, "请求头" => $header]);
}
/**
* @param $request
* @return array
* @throws DatabaseException
*/
public static function UpdateScreenShotInfo($request)
{
LogModel::addlog(["接收到更新 screenshot 数据回调 " => $request->all()]);
$media_id = $request->media_id;
try {
MediaModel::select("_id", "media_name", "duration", "secret_key", "media_url")->where('_id', $media_id)->first();
} catch (\Exception $exception) {
throw new DatabaseException($exception->getMessage());
}
$item["screen_shot"]["height"] = $request->height;
$item["screen_shot"]["width"] = $request->width;
$item["screen_shot"]["url"] = $request->screenshot;
$flag = MediaModel::where("_id", $media_id)->update($item);
if (!$flag) {
throw new DatabaseException("更新媒资 screenshot 数据失败");
}
return $item;
LogModel::addlog(["更新媒资 screenshot 数据成功 " => $item]);
}
}
......@@ -53,6 +53,5 @@ Route::group(['namespace' => 'Api', 'prefix' => 'user','middleware' => ['api']],
Route::post("getMediaListByMediaIds","MediaController@getMediaListByMediaIds");
//Route::get("media_url_change","MediaController@handleMediaUrl");
Route::post("screenShot","MediaController@getScreeShotInfo");
Route::post("screenShotCallback","MediaController@updateScreeShotInfo");
Route::post("screenShotCallback","MediaController@screenShotCallback");
});
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment