Upapi.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. class Upapi extends CI_Controller {
  3. function __construct(){
  4. parent::__construct();
  5. $this->load->helper('string');
  6. $this->load->database();
  7. if(Up_Api == 0) getjson('上传接口关闭');
  8. }
  9. public function index(){
  10. $cid = (int)$this->input->get('cid');
  11. $t = time();
  12. $key = md5($t.$cid.'yun');
  13. $data['uploadsave'] = site_url('upapi/save/'.$cid).'?key='.$key.'&t='.$t;
  14. $data['vlist'] = $this->db->query("select * from ".CS_SqlPrefix."class order by id asc")->result();
  15. $data['cid'] = $cid;
  16. $this->load->view('upapi.html',$data);
  17. }
  18. public function save($cid=0){
  19. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  20. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  21. header("Cache-Control: no-store, no-cache, must-revalidate");
  22. header("Cache-Control: post-check=0, pre-check=0", false);
  23. header("Pragma: no-cache");
  24. set_time_limit(0);
  25. $cid = (int)$cid;
  26. $key = $this->input->get('key');
  27. $t = $this->input->get('t');
  28. if(empty($key) || empty($t)) getjson('请求参数错误');
  29. if(md5($t.$cid.'yun') != $key) getjson('非法请求');
  30. if(substr(Video_Path,0,2) == './'){
  31. $Video_Path = FCPATH.substr(Video_Path,2);
  32. }else{
  33. $Video_Path = Video_Path;
  34. }
  35. $targetDir = $Video_Path.'/temp/'.date('Ymd').'/';
  36. $uploadDir = $Video_Path.'/'.date('Y').'/'.date('m').'/'.date('d').'/';
  37. $uploadStr = date('Y').'/'.date('m').'/'.date('d').'/';
  38. //判断MD5秒传
  39. $filemd5 = $this->input->post('md5',true);
  40. $filename = $this->input->post('filename',true);
  41. if(!empty($filemd5) && !empty($filename)){
  42. if(!preg_match("/[0-9A-Za-z]/",$filemd5)){
  43. getjson('MD5不合法');
  44. }elseif(Video_SrcOn == 0){
  45. //判断MD5是否存在
  46. $row = $this->db->query("select * from ".CS_SqlPrefix."video where md5='".$filemd5."'")->row();
  47. if($row){
  48. $file_ext = strtolower(trim(substr(strrchr($row->path, '.'), 1)));
  49. $filename = safe_replace(str_replace('.'.$file_ext,'',$filename));
  50. //判断标题是否存在
  51. $res = $this->db->query("select id from ".CS_SqlPrefix."video where name='".$filename."'")->row();
  52. if($res) getjson("yes");
  53. //复制文件
  54. $newname = date('YmdHis').time().'.'.$file_ext;
  55. if(copy($row->path, $uploadDir.$newname)){
  56. //video
  57. $data['cid'] = $cid;
  58. $data['name'] = $filename;
  59. $data['path'] = $uploadDir.$newname;
  60. $data['md5'] = $filemd5;
  61. $data['duration'] = $row->duration;
  62. $data['size'] = $row->size;
  63. $data['addtime'] = time();
  64. $this->db->insert("video",$data);
  65. $insert_id = $this->db->insert_id();
  66. //M3U8保存目录
  67. $m3u8_dir = substr(Mu_Path,0,2) == './' ? FCPATH.substr(Mu_Path,2) : Mu_Path;
  68. //路径
  69. $time = time();
  70. $m3u8path = m3u8_dir($insert_id,$time);
  71. $jpgpath = m3u8_dir($insert_id,$time,'jpg');
  72. $gifpath = m3u8_dir($insert_id,$time,'gif');
  73. $this->db->update('video',array('m3u8'=>$m3u8path,'pic'=>$jpgpath),array('id'=>$insert_id));
  74. //queue
  75. $queue['xu'] = 1;
  76. $queue['vid'] = $insert_id;
  77. $queue['path'] = $data['path'];
  78. $queue['jpg'] = $m3u8_dir.$jpgpath;
  79. $queue['gif'] = $m3u8_dir.$gifpath;
  80. $queue['m3u8'] = $m3u8_dir.$m3u8path;
  81. $queue['duration'] = $row->duration;
  82. $this->db->insert('queue',$queue);
  83. //多码率
  84. if(Mu_Type == 1){
  85. $ext = end(explode('/', $m3u8path));
  86. $queue['xu'] = 2;
  87. $queue['m3u8'] = $m3u8_dir.str_replace($ext, Mu_Kbps2.'/'.$ext, $m3u8path);
  88. $this->db->insert('queue',$queue);
  89. }
  90. //返回数据
  91. $ok['name'] = $filename;
  92. $ok['m3u8'] = m3u8_link($m3u8path);
  93. $ok['pic'] = m3u8_link($jpgpath,'jpg');
  94. $ok['purl'] = 'http://'.Web_Url.links('play/'.$insert_id);
  95. $ok['size'] = formatsize($row->size);
  96. $ok['sec'] = formattime($row->duration,1);
  97. $ok['msg'] = 'ok';
  98. getjson($ok,0,1);
  99. }
  100. }
  101. }
  102. getjson('参数错误');
  103. }
  104. //定义允许上传的文件扩展名
  105. $ext_arr = array_filter(explode('|', Up_Ext));
  106. //防止外部跨站提交
  107. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  108. getjson('提交方式不正确');
  109. }
  110. //上传出错
  111. if (!empty($_REQUEST[ 'debug' ]) ) {
  112. $random = rand(0, intval($_REQUEST[ 'debug' ]) );
  113. if ( $random === 0 ) {
  114. header("HTTP/1.0 500 Internal Server Error"); exit;
  115. }
  116. }
  117. //创建目录
  118. if (!file_exists($targetDir)) {
  119. creatdir($targetDir);
  120. }
  121. //创建目录
  122. if (!file_exists($uploadDir)) {
  123. creatdir($uploadDir);
  124. }
  125. //原文件名
  126. if(!isset($_FILES['video']['name'])){
  127. getjson('No FILES');
  128. }
  129. $file_name = $_FILES['video']['name'];
  130. //文件后缀
  131. $file_ext = strtolower(trim(substr(strrchr($file_name, '.'), 1)));
  132. $newname = iconv("UTF-8","GBK//IGNORE",$file_name);
  133. $video_name = safe_replace(str_replace('.'.$file_ext, '', $file_name));
  134. //判断是否存在
  135. $res = $this->db->query("select id from ".CS_SqlPrefix."video where name='".$video_name."'")->row();
  136. if($res){
  137. getjson("视频已经存在");
  138. }
  139. //判断文件后缀
  140. if(in_array($file_ext, $ext_arr) == false) {
  141. getjson("不支持的格式");
  142. }
  143. //根据文件名和会员ID生成一个唯一MD5
  144. $fileName = md5($file_name).'.'.$file_ext;
  145. $oldName = $fileName;
  146. $filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName;
  147. //分片ID
  148. $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
  149. //分片总数量
  150. $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 1;
  151. //打开临时文件
  152. if (!$out = @fopen("{$filePath}_{$chunk}.parttmp", "wb")) {
  153. getjson('Failed to open output stream.');
  154. }
  155. if (!empty($_FILES)) {
  156. if ($_FILES["video"]["error"] || !is_uploaded_file($_FILES["video"]["tmp_name"])) {
  157. getjson('Failed to move uploaded file.');
  158. }
  159. //读取二进制输入流并将其附加到临时文件
  160. if (!$in = @fopen($_FILES["video"]["tmp_name"], "rb")) {
  161. getjson('Failed to open input stream.');
  162. }
  163. } else {
  164. if (!$in = @fopen("php://input", "rb")) {
  165. getjson('Failed to open input stream.');
  166. }
  167. }
  168. while ($buff = fread($in, 4096)) {
  169. fwrite($out, $buff);
  170. }
  171. @fclose($out);
  172. @fclose($in);
  173. rename("{$filePath}_{$chunk}.parttmp", "{$filePath}_{$chunk}.part");
  174. $index = 0;
  175. $done = true;
  176. for( $index = 0; $index < $chunks; $index++ ) {
  177. if (!file_exists("{$filePath}_{$index}.part") ) {
  178. $done = false;
  179. break;
  180. }
  181. }
  182. if($done){
  183. $pathInfo = pathinfo($fileName);
  184. $hashStr = substr(md5($pathInfo['basename']),8,5);
  185. $hashName = date('YmdHis').$hashStr.'.'.$pathInfo['extension'];
  186. $uploadPath = $uploadDir.$hashName;
  187. $uploadStr .= $hashName;
  188. if (!$out = @fopen($uploadPath, "wb")) {
  189. getjson('Failed to open output stream.');
  190. }
  191. if ( flock($out, LOCK_EX) ) {
  192. for( $index = 0; $index < $chunks; $index++ ) {
  193. if (!$in = @fopen("{$filePath}_{$index}.part", "rb")) {
  194. break;
  195. }
  196. while ($buff = fread($in, 4096)) {
  197. fwrite($out, $buff);
  198. }
  199. @fclose($in);
  200. @unlink("{$filePath}_{$index}.part");
  201. }
  202. flock($out, LOCK_UN);
  203. }
  204. @fclose($out);
  205. //video
  206. $data['cid'] = $cid;
  207. $data['name'] = $video_name;
  208. $data['path'] = $uploadPath;
  209. $data['md5'] = md5_file($uploadPath);
  210. $data['addtime'] = time();
  211. $this->load->library('xyz');
  212. $format = $this->xyz->format($uploadPath);
  213. //if(empty($format['size']) || empty($format['duration'])){
  214. // unlink($uploadPath);
  215. // getjson('ffmpeg error');
  216. //}
  217. $data['duration'] = $format['duration'];
  218. $data['size'] = $format['size'];
  219. $this->db->insert("video",$data);
  220. $insert_id = $this->db->insert_id();
  221. //M3U8保存目录
  222. $m3u8_dir = substr(Mu_Path,0,2) == './' ? FCPATH.substr(Mu_Path,2) : Mu_Path;
  223. //路径
  224. $time = time();
  225. $m3u8path = m3u8_dir($insert_id,$time);
  226. $jpgpath = m3u8_dir($insert_id,$time,'jpg');
  227. $gifpath = m3u8_dir($insert_id,$time,'gif');
  228. $this->db->update('video',array('m3u8'=>$m3u8path,'pic'=>$jpgpath),array('id'=>$insert_id));
  229. //queue
  230. $queue2['xu'] = 1;
  231. $queue['vid'] = $insert_id;
  232. $queue['path'] = $uploadPath;
  233. $queue['jpg'] = $m3u8_dir.$jpgpath;
  234. $queue['gif'] = $m3u8_dir.$gifpath;
  235. $queue['m3u8'] = $m3u8_dir.$m3u8path;
  236. $queue['duration'] = $format['duration'];
  237. $this->db->insert('queue',$queue);
  238. //多码率
  239. if(Mu_Type == 1){
  240. $ext = end(explode('/', $m3u8path));
  241. $queue['xu'] = 2;
  242. $queue['m3u8'] = $m3u8_dir.str_replace($ext, Mu_Kbps2.'/'.$ext, $m3u8path);
  243. $this->db->insert('queue',$queue);
  244. }
  245. //返回数据
  246. $ok['name'] = $video_name;
  247. $ok['m3u8'] = m3u8_link($m3u8path);
  248. $ok['pic'] = m3u8_link($jpgpath,'jpg');
  249. $ok['purl'] = 'http://'.Web_Url.links('play/'.$insert_id);
  250. $ok['size'] = formatsize($format['size']);
  251. $ok['sec'] = formattime($format['duration'],1);
  252. $ok['msg'] = 'ok';
  253. getjson($ok,0,1);
  254. }
  255. }
  256. }