作者 lyh

gx

@@ -66,68 +66,19 @@ class FileController @@ -66,68 +66,19 @@ class FileController
66 header("HTTP/1.1 304 Not Modified"); 66 header("HTTP/1.1 304 Not Modified");
67 exit; 67 exit;
68 } 68 }
69 - $file_model = new File();  
70 - $info = $file_model->read(['hash' => $hash]); 69 + $fileModel = new File();
  70 + $info = $fileModel->read(['hash' => $hash]);
71 if ($info === false) { 71 if ($info === false) {
72 $this->response('指定文件不存在!', Code::USER_ERROR); 72 $this->response('指定文件不存在!', Code::USER_ERROR);
73 } 73 }
74 - //获取cos链接  
75 - if($info['is_cos'] == 1){  
76 - $cos = new CosService();  
77 - return $cos->getImageUrl($info['path']);  
78 - }  
79 $path = $this->config['root'].'/'.$info['path']; 74 $path = $this->config['root'].'/'.$info['path'];
80 if (!is_file($path)) { 75 if (!is_file($path)) {
81 $this->response('指定文件已被系统删除!', Code::USER_ERROR); 76 $this->response('指定文件已被系统删除!', Code::USER_ERROR);
82 } 77 }
83 - $size = $info['size'];  
84 // 设置Content-Type头部 78 // 设置Content-Type头部
85 - if($info['type'] == 'mp4'){  
86 - $header['Content-Type'] = 'video/' . $info['type'];  
87 - }  
88 - // 设置Accept-Ranges头部  
89 - $header['Accept-Ranges'] = 'bytes';  
90 - // 检查是否有范围请求  
91 - if (isset($_SERVER['HTTP_RANGE'])) {  
92 - $range = $_SERVER['HTTP_RANGE'];  
93 - $ranges = explode('-', substr($range, 6));  
94 - $start = intval($ranges[0]);  
95 - $end = $size - 1;  
96 - if (!empty($ranges[1])) {  
97 - $end = intval($ranges[1]);  
98 - }  
99 - $length = $end - $start + 1;  
100 - // 设置部分响应头部  
101 - $header['Content-Length'] = $length;  
102 - $header['Content-Range'] = 'bytes ' . $start . '-' . $end . '/' . $size;  
103 - // 发送206 Partial Content状态码  
104 - header('HTTP/1.1 206 Partial Content');  
105 - header('Status: 206 Partial Content');  
106 - header('Accept-Ranges: bytes');  
107 - header('Content-Range: bytes ' . $start . '-' . $end . '/' . $size);  
108 - // 读取部分内容并发送响应  
109 - $file = fopen($path, 'rb');  
110 - fseek($file, $start);  
111 - $buffer = 1024 * 8; // 设置缓冲区大小  
112 - while (!feof($file) && ($p = ftell($file)) <= $end) {  
113 - if ($p + $buffer > $end) {  
114 - // 最后一块缓冲区  
115 - $buffer = $end - $p + 1;  
116 - }  
117 - echo fread($file, $buffer);  
118 - flush(); // 将输出刷新到浏览器  
119 - }  
120 - fclose($file);  
121 - exit;  
122 - }  
123 - // 无范围请求,发送完整文件  
124 - $header['Content-Length'] = $size;  
125 - $content = file_get_contents($path);  
126 - // 发送完整响应  
127 - foreach ($header as $name => $value) {  
128 - header("$name: $value");  
129 - }  
130 - echo $content; 79 + header("Content-Type: video/mp4");
  80 + // 发送完整文件
  81 + readfile($path);
131 exit; 82 exit;
132 } 83 }
133 84