diff --git a/api/application/config/routes.php b/api/application/config/routes.php index a27daeb8..c39f11f8 100755 --- a/api/application/config/routes.php +++ b/api/application/config/routes.php @@ -389,5 +389,10 @@ $route['getFlyhiLenderList'] = 'Flyhi_Controller/lenderMasterForFlyhiPD'; $route['saveFlyhiPDImage'] = 'Flyhi_Controller/saveFlyhiPDImage'; $route['getFlyhiPDImgDataCusLink']='Flyhi_Controller/getFlyhiPDImgDataCusLink'; $route['saveFlyhiPDsection'] = 'Flyhi_Controller/saveFlyhiPDsection'; +$route['saveCusFlyhiVideo'] = 'Flyhi_Controller/saveCusFlyhiVideo'; +$route['getFlyhiVideoURL'] = 'Flyhi_Controller/getFlyhiVideoURL'; +$route['deleteFlyhiVideo'] = 'Flyhi_Controller/deleteFlyhiVideo'; +$route['uploadFlyhiVideoFile'] = 'Flyhi_Controller/uploadFlyhiVideoFile'; +$route['updateTwlioFlyhiVideoStatus'] = 'Flyhi_Controller/updateTwlioFlyhiVideoStatus'; diff --git a/api/application/controllers/Flyhi_Controller.php b/api/application/controllers/Flyhi_Controller.php index 3f16c3ca..ceb52c54 100644 --- a/api/application/controllers/Flyhi_Controller.php +++ b/api/application/controllers/Flyhi_Controller.php @@ -11,12 +11,15 @@ class Flyhi_Controller extends REST_Controller { parent::__construct(); $this->load->model('Flyhi_Model','Flyhi_Model'); $this->load->library('AWS_S3'); + $this->load->library('AWS_COGNITO'); + $this->load->library('AWS_SNS'); $this->load->library('pdfgenerator'); $this->load->helper('satellite_image'); $this->load->helper('camunda'); $this->external_path = $this->config->item('external_data_path'); - - + $this->load->helper('MYUTIL'); + $this->load->helper('faceapi'); + $this->load->library('phpmailer_library'); } public function getMasterForFlyhiPD_get(){ $FHFSL_lender = (ENVIRONMENT == 'production' ? 19 : 49); @@ -1362,5 +1365,438 @@ class Flyhi_Controller extends REST_Controller { } } + public function saveCusFlyhiVideo_post() + { + + $records = json_decode($this->post('records'),true); + $id = null; + //log_message('ERROR','hi'); + // print_r($records); + // print_r($_FILES); + // die(); + + + if(!empty($_FILES['video_file'])) + { + $pd_master_details = $this->Flyhi_Model->selectCustomRecords(array('*'),array('sales_pd_id' => $records['pd_id']),SALES_PD_DATA); + $bucketname = LENDER_BUCKET_NAME_PREFIX.$pd_master_details[0]['lender_id']; + $key = 'pd'.$records['pd_id'].'/video/'.$records['filename']; + $file_name_wo_extension = explode('.', ($records['filename']))[0]; + $file_type = explode('/', ($_FILES['video_file']['type']))[1]; + $folder_to_check = XLPATH.'/sales_pd/'.$records['pd_id'].'/videos'; + //echo $folder_to_check;//die(); + if(!file_exists($folder_to_check)) { mkdir(XLPATH.'/sales_pd/'.$records['pd_id'],0777); mkdir($folder_to_check,0777); } + //die(); + $config = array('upload_path' => $folder_to_check,'overwrite' => TRUE,'allowed_types' => '*','file_name' => $file_name_wo_extension.'.'.$file_type); + $this->load->library('upload', $config); + if($this->upload->do_upload('video_file')) + { + $upload_data = $this->upload->data(); + //$this->load->view('upload_success',$data); + //print_r($upload_data); + + $command_to_execute = FFMPEG_PATH.' -i '.$folder_to_check.'/'.$file_name_wo_extension.'.'.$file_type.' '.$folder_to_check.'/'.$file_name_wo_extension.'.mp4'.' 2>&1' ; + //print_r($command_to_execute);die(); + exec($command_to_execute,$o,$v); + // print_r($o); + // echo $command_to_execute;die(); + if($o) + { + $sourcefile = $folder_to_check.'/'.$file_name_wo_extension.'.mp4'; + + $bucket_data = array('bucket_name'=>$bucketname,'key'=>$key,'sourcefile'=>$sourcefile); + $s3result= $this->aws_s3->uploadFileToS3Bucket($bucket_data); + // print_r($s3result); + if(is_object($s3result) && $s3result['ObjectURL'] != '' && $s3result['@metadata']['statusCode'] == 200) + { + log_message('ERROR','###PDVIDEO stored in S3'.$records['pd_id']); + $record_data = array('fk_pd_id' => $records['pd_id'],'video_part_name'=>$records['filename'],'video_key' => $records['video_key'],'flyhi_pd_flag'=>$records['flyhi_pd_flag']); + $id = $this->Flyhi_Model->saveRecords($record_data,CUS_VIDEO); + unlink($folder_to_check.'/'.$file_name_wo_extension.'.webm'); + unlink($folder_to_check.'/'.$file_name_wo_extension.'.mp4'); + + } + + // if it is last part of a video call, then call video merge functionality + if(isset($records['is_call_ended']) && $records['is_call_ended'] == 1) + { + $final_video_name = $records['pd_id'].'-'.$records['video_key']; + sleep(10); + $this->mergeVideo($records['pd_id'],$pd_master_details,$final_video_name,$records['video_key']); + } + + + if($id != null || $id != "") + { + + + $data['dataStatus'] = true; + $data['status'] = REST_Controller::HTTP_OK; + $data['msg'] = 'Video Saved Successfully'; + $this->response($data,REST_Controller::HTTP_OK); + } + else + { + $data['dataStatus'] = false; + $data['status'] = REST_Controller::HTTP_NOT_FOUND; + $data['msg'] = 'Something Wrong. Try Later!'; + $this->response($data,REST_Controller::HTTP_OK); + } + } + else + { + $data['dataStatus'] = false; + $data['status'] = REST_Controller::HTTP_NOT_FOUND; + $data['msg'] = 'Something Wrong. Try Later!'; + $this->response($data,REST_Controller::HTTP_OK); + } + + } + else + { + $data['dataStatus'] = false; + $data['status'] = REST_Controller::HTTP_NOT_FOUND; + $data['msg'] = 'Something Wrong. Try Later!'; + $this->response($data,REST_Controller::HTTP_OK); + } + + + } + else + { + $data['dataStatus'] = false; + $data['status'] = REST_Controller::HTTP_NOT_FOUND; + $data['msg'] = 'No Image found to upload'; + $this->response($data,REST_Controller::HTTP_OK); + } + } + + + //merge all splitted videos + public function getFlyhiVideoURL_post() + { + $pd_id = $this->post('pd_id'); + $random_string = $this->post('random_string'); + $this->load->helper('video_grabber_helper'); + $pd_master_details = $this->Flyhi_Model->selectCustomRecords(array('*'),array('sales_pd_id' => $pd_id,'rand_string'=>$random_string),SALES_PD_DATA); + + $result_data = (new video_grabber_helper)->grabAllVideo($pd_master_details[0]['sales_pd_id'],$pd_master_details[0]['rand_string'],$pd_master_details); + + $data['dataStatus'] = true; + $data['status'] = REST_Controller::HTTP_OK; + $data['records'] = $result_data; + $this->response($data,REST_Controller::HTTP_OK); + + + } + + // To Inactive Video Recorded. + public function deleteFlyhiVideo_get(){ + $id = $this->get('id'); + // $id = (int) $id; + // if(!empty($id)){ + // // $multipleWhere['fk_pd_id'] = $multipleWhere['pd_id']; + // // unset($multipleWhere['pd_id']); + $this->load->helper('video_grabber_helper'); + $result_data = (new video_grabber_helper)->deleteVideo($id); + // }else{ + // $result_data = "ID Not Available. Unable to Delete"; + // } + $data['dataStatus'] = true; + $data['status'] = REST_Controller::HTTP_OK; + $data['msg'] = $result_data ; + $this->response($data,REST_Controller::HTTP_OK); + } + + function updateTwlioFlyhiVideoStatus_post() + { + $pd_id = $this->post('pd_id'); + $random_string = $this->post('random_string'); + $room_id = $this->post('room_id'); + + $result_data = $this->getTwiloVideoStatus($pd_id,$random_string,$room_id); + + $data['dataStatus'] = true; + $data['status'] = REST_Controller::HTTP_OK; + $data['records'] = $result_data; + $this->response($data,REST_Controller::HTTP_OK); + + + } + + function getTwiloVideoStatus($pd_id,$random_string,$room_id = '') + { + $this->load->library('twlio'); + $result_data = 0; + // $query = "SELECT * FROM ".VIDEO_ROOM_INFO." where fk_pd_id = $pd_id and twilo_status = 'enqueued' and composition_id is not null;"; + $pd_master_details = $this->Flyhi_Model->selectCustomRecords(array('*'),array('sales_pd_id' => $pd_id,'rand_string'=>$random_string),SALES_PD_DATA); + $query = "SELECT * FROM ".VIDEO_ROOM_INFO." where fk_pd_id = $pd_id and isactive = 1 and is_uploaded = 0";// and twilo_status = 'enqueued' and composition_id is not null;"; + if($room_id) + { + $query .=" and room_id = '$room_id'"; + } + $video_info = $this->db->query($query); + if($video_info->num_rows()) + { + $video_info = $video_info->result_array(); + //print_r($video_info);die(); + foreach ($video_info as $key => $value) + { + + if($value['composition_id'] && $value['vps'] != 'google') + { + $res = $this->twlio->getCompositionStatus($value['composition_id']); + //echo $res['status']; + if(count($res) && $res['status'] == 'completed') + { + + $is_copied = $this->copyMediaFromTwiloToS3($pd_master_details,$value);// copy media from twlio s3 to our s3 + $fields_to_update = array('twilo_status' => 'completed'); + if($is_copied) { $fields_to_update['s3_copied'] = 1; } + $id = $this->Flyhi_Model->updateRecords($fields_to_update,VIDEO_ROOM_INFO,array('video_room_info_id' => $value['video_room_info_id'])); + + if($id){ if($room_id) {$result_data = $is_copied;}else{$result_data++ ;}} + + } + } + else + { + $room_status = $this->twlio->getRoomDataByRoomID($value['room_id']); + if($room_status == 'completed') + { + $composition_result_data = $this->twlio->startComposistion($value['room_id']); + // $composition_result_data = $this->twlio->startComposistion('RM23b8d502434e46e696d1fbd5d64c59c8'); + // print_r($composition_result_data); + $composition_result_data_json = json_encode($composition_result_data); + // print_r($composition_result_data_json); + $records['res_json'] = $composition_result_data_json; + $records['twilo_status'] = $composition_result_data['status']; + $records['composition_id'] = $composition_result_data['compositionId']; + $records['is_call_ended'] = 1; + $records['flyhi_pd_flag'] = 1; + $where_condition_array = array('fk_pd_id' => $pd_id); + if($value['room_id']){ $where_condition_array['room_id'] = $value['room_id']; } + $id = $this->Flyhi_Model->updateRecords($records,VIDEO_ROOM_INFO,$where_condition_array); + } + } + + } + + } + return $result_data; + } + + function copyMediaFromTwiloToS3($pd_master_details,$value)// copy media from twlio s3 to our s3 + { + EXTERNAL_DIR_CHECK::checkExternalDirectory($pd_master_details[0]['sales_pd_id']); + //get media uri from twlio + $singed_uri = $this->twlio->compositionVideoFile($value['composition_id']); + //echo $singed_uri; + $internal_server_tmp_location = XLPATH.'/sales_pd/'.$pd_master_details[0]['sales_pd_id'].'/videos'; + $internal_server_tmp_location_filename = ($internal_server_tmp_location.'/'.$value['composition_id'].'.mp4'); + //echo $internal_server_tmp_location; + if(!file_exists($internal_server_tmp_location)){ mkdir($internal_server_tmp_location); }; + $is_writed = file_put_contents($internal_server_tmp_location_filename,file_get_contents($singed_uri)); + //upload it to aws s3 + $bucket_name = LENDER_BUCKET_NAME_PREFIX.$pd_master_details[0]['lender_id']; + $final_key = 'pd'.$pd_master_details[0]['sales_pd_id'].'/video/'.$value['composition_id'].'.mp4'; + + $bucket_data = array('bucket_name'=>$bucket_name,'key'=>$final_key,'sourcefile'=>($internal_server_tmp_location_filename)); + $s3result= $this->aws_s3->uploadFileToS3Bucket($bucket_data); + if(is_object($s3result) && $s3result['ObjectURL'] != '' && $s3result['@metadata']['statusCode'] == 200) + { + log_message('ERROR','###TWLIO TO S3 STARTED'.$value['composition_id']); + + unlink($internal_server_tmp_location_filename); + return $temp_uri = $this->aws_s3->getSingleObjectInaBucketAsSignedURI($bucket_name,$final_key,'+7 days'); + //unlink($folder_to_check.'/'.$final_video_name.'.mp4'); + } + return null; + + + } + + function mergeVideo($pd_id,$pd_master_details,$final_video_name,$video_key) + { + // $pd_id = $this->post('pd_id'); + // $random_string = $this->post('random_string'); + //$final_video_name = $pd_id.'-'.$random_string; + + //echo $final_video_name;die(); + //Get PD master DEtails + //$pd_master_details = $this->PD_Model->getPDMasterDetails($pd_id,$random_string); + //$is_video_merged = $this->PD_Model->selectCustomRecords(array('*'),array('fk_pd_id' => $pd_id,'video_part_name' => $final_video_name),CUS_VIDEO); + //prit_r($pd_master_details);die(); + // if(count($pd_master_details) && isset($pd_master_details[0]['pd_id'])) + // { + + $bucket_name = LENDER_BUCKET_NAME_PREFIX.$pd_master_details[0]['lender_id']; + $final_key = 'pd'.$pd_master_details[0]['pd_id'].'/video/'.$final_video_name.'.mp4'; + + //Get all video info + $qu = "SELECT *,convert(substr(substring_index(video_part_name,'-',1),6),UNSIGNED INTEGER) as sno FROM t_cus_video + where fk_pd_id = $pd_id and video_key = '$video_key' and is_active = 1 and flyhi_pd_flag = 1 order by sno"; + + $video_info = $this->db->query($qu)->result_array(); + $folder_to_check = XLPATH.'/sales_pd/'.$pd_id.'/videos'; + $file_list = $folder_to_check.'/filelist.txt'; + + //Get All video and temp store it in xlpath + $file_pointer = fopen($file_list, 'w+'); + $all_input_files_names_with_path = ''; + foreach ($video_info as $video_info_key => $video_info_value) + { + $key = 'pd'.$pd_id.'/video/'.$video_info_value['video_part_name']; + $temp_uri = $this->aws_s3->getSingleObjectInaBucketAsSignedURI($bucket_name,$key,'+10 minutes'); + $file_to_create = $folder_to_check.'/'.$video_info_value['video_part_name']; + $output = file_put_contents($file_to_create,file_get_contents($temp_uri)); + if($output) + { + + $file_name_wo_extension = explode('.', ($video_info_value['video_part_name']))[0]; + + //fwrite($file_pointer,"file "."'".$file_name_wo_extension.".mp4"."'\n"); + fwrite($file_pointer,"file "."'".$file_name_wo_extension."'\n"); + } + } + fclose($file_pointer); + // echo '***************************************';//die(); + + $merge_cmd = FFMPEG_PATH.' -f concat -safe 0 -i '.$folder_to_check.'/'.'filelist.txt -c copy '.$folder_to_check.'/'.$final_video_name.'.mp4 2>&1'; + //echo '#########################################'; + //echo $merge_cmd;//die(); + exec($merge_cmd,$ou,$v); + //print_r($ou);//die(); + if($ou && file_exists($folder_to_check.'/'.$final_video_name.'.mp4')) + { + //echo 'upload to s3'; + $bucket_data = array('bucket_name'=>$bucket_name,'key'=>$final_key,'sourcefile'=>($folder_to_check.'/'.$final_video_name.'.mp4')); + $s3result= $this->aws_s3->uploadFileToS3Bucket($bucket_data); + if(is_object($s3result) && $s3result['ObjectURL'] != '' && $s3result['@metadata']['statusCode'] == 200) + { + log_message('ERROR','###PDVIDEO merged video stored in S3'.$pd_id); + $record_data = array('is_video_merged' => 1); + //$id = $this->PD_Model->updateRecords($record_data,PDTRIGGER,array('pd_id' => $pd_id)); + $id = 1; + unlink($folder_to_check.'/'.$final_video_name.'.mp4'); + + } + + foreach($video_info as $video_info_key => $video_info_value) + { + $file_name_wo_extension = explode('.', ($video_info_value['video_part_name']))[0]; + //unlink($folder_to_check.'/'.$file_name_wo_extension.'.ts'); + unlink($folder_to_check.'/'.$file_name_wo_extension); + } + + + // $singed_uri = $this->aws_s3->getSingleObjectInaBucketAsSignedURI($bucket_name,$final_key,'+3 hrs'); + // $data['dataStatus'] = true; + // $data['status'] = REST_Controller::HTTP_OK; + // $data['url'] = $singed_uri; + // $this->response($data,REST_Controller::HTTP_OK); + + + } + else + { + log_message('ERROR','###PDVIDEO merged video not stored in S3'.$pd_id); + } + + + + + + + } + + public function uploadFlyhiVideoFile_post() + { + $records = json_decode($this->post('records'),true); + // $pd_master_details = $this->PD_Model->getPDMasterDetails($records['fk_pd_id'],$records['random_string']); + $pd_master_details = $this->Flyhi_Model->selectCustomRecords(array('*'),array('sales_pd_id' => $records['fk_pd_id'],'rand_string'=>$records['random_string']),SALES_PD_DATA); + $bucket_name = LENDER_BUCKET_NAME_PREFIX.$pd_master_details[0]['lender_id']; + // echo XLPATH; + // print_r($records);die(); + // print_r($pd_master_details);die(); + $is_bucket_upload_sucess = false; + $id = null; + //die(); + if(!empty($_FILES['video_file'])) + { + $folder_to_check = XLPATH.'/sales_pd/'.$records['fk_pd_id'].'/videos'; + + if(!file_exists($folder_to_check)) + { + mkdir($folder_to_check,0777); + } + // die(); + // $modified_fname = $records['rand_key'].'~'.str_replace(' ','_',($_FILES['pd_excel_data']['name'])); + $modified_fname = str_replace(' ','_',($_FILES['video_file']['name'])); + // echo file_exists($folder_to_check);die(); + + $config = array('upload_path' => $folder_to_check,'overwrite' => TRUE,'allowed_types' => '*','file_name' => $modified_fname); + + $this->load->library('upload', $config); + $this->upload->initialize($config); + + + try { + + + if($this->upload->do_upload('video_file')) + { + log_message('ERROR','video uplaod done :' . $folder_to_check); + $sourcefile = $folder_to_check.'/'.$modified_fname; + // echo $sourcefile; + $key = 'pd'.$records['fk_pd_id'].'/video/'.$modified_fname; + + $bucket_data = array('bucket_name'=>$bucket_name,'key'=>$key,'sourcefile'=>$sourcefile); + // print_r($bucket_data);die(); + $s3result= $this->aws_s3->uploadFileToS3Bucket($bucket_data); + //print_r($s3result);die(); + if(is_object($s3result) && $s3result['ObjectURL'] != '' && $s3result['@metadata']['statusCode'] == 200) + { + $is_bucket_upload_sucess = 1; + $id = $this->PD_Model->saveRecords(array('fk_pd_id' =>$records['fk_pd_id'],'room_id' => $modified_fname,'composition_id' => $modified_fname,'twilo_status' => 'completed','is_call_ended' => 1,'s3_copied' => 1,'is_uploaded' => 1,'flyhi_pd_flag'=>1),VIDEO_ROOM_INFO); + unlink($sourcefile); + } + } + else + { + log_message('ERROR','video uplaod failed :' . $folder_to_check); + } + + } catch (Exception $e) { + log_message('ERROR','video uplaod failed exception:' . $folder_to_check.' - '.$e->getMessage()); + } + // die(); + + if($is_bucket_upload_sucess && $id) + { + $data['dataStatus'] = true; + $data['status'] = REST_Controller::HTTP_OK; + $data['msg'] = 'File Uploaded Successfully'; + // $data['excel_file_id'] = $id; + // $data['records'] = $result_data; + $this->response($data,REST_Controller::HTTP_OK); + } + else + { + $data['dataStatus'] = false; + $data['status'] = REST_Controller::HTTP_NOT_FOUND; + $data['msg'] = 'Something Wrong. Try Later!'; + $this->response($data,REST_Controller::HTTP_OK); + } + } + else + { + $data['dataStatus'] = false; + $data['status'] = REST_Controller::HTTP_NOT_FOUND; + $data['msg'] = 'No Data File Found!'; + $this->response($data,REST_Controller::HTTP_OK); + } + } + }// Class Ends