|
...
|
...
|
@@ -66,68 +66,19 @@ class FileController |
|
|
|
header("HTTP/1.1 304 Not Modified");
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
$file_model = new File();
|
|
|
|
$info = $file_model->read(['hash' => $hash]);
|
|
|
|
$fileModel = new File();
|
|
|
|
$info = $fileModel->read(['hash' => $hash]);
|
|
|
|
if ($info === false) {
|
|
|
|
$this->response('指定文件不存在!', Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
//获取cos链接
|
|
|
|
if($info['is_cos'] == 1){
|
|
|
|
$cos = new CosService();
|
|
|
|
return $cos->getImageUrl($info['path']);
|
|
|
|
}
|
|
|
|
$path = $this->config['root'].'/'.$info['path'];
|
|
|
|
if (!is_file($path)) {
|
|
|
|
$this->response('指定文件已被系统删除!', Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
$size = $info['size'];
|
|
|
|
// 设置Content-Type头部
|
|
|
|
if($info['type'] == 'mp4'){
|
|
|
|
$header['Content-Type'] = 'video/' . $info['type'];
|
|
|
|
}
|
|
|
|
// 设置Accept-Ranges头部
|
|
|
|
$header['Accept-Ranges'] = 'bytes';
|
|
|
|
// 检查是否有范围请求
|
|
|
|
if (isset($_SERVER['HTTP_RANGE'])) {
|
|
|
|
$range = $_SERVER['HTTP_RANGE'];
|
|
|
|
$ranges = explode('-', substr($range, 6));
|
|
|
|
$start = intval($ranges[0]);
|
|
|
|
$end = $size - 1;
|
|
|
|
if (!empty($ranges[1])) {
|
|
|
|
$end = intval($ranges[1]);
|
|
|
|
}
|
|
|
|
$length = $end - $start + 1;
|
|
|
|
// 设置部分响应头部
|
|
|
|
$header['Content-Length'] = $length;
|
|
|
|
$header['Content-Range'] = 'bytes ' . $start . '-' . $end . '/' . $size;
|
|
|
|
// 发送206 Partial Content状态码
|
|
|
|
header('HTTP/1.1 206 Partial Content');
|
|
|
|
header('Status: 206 Partial Content');
|
|
|
|
header('Accept-Ranges: bytes');
|
|
|
|
header('Content-Range: bytes ' . $start . '-' . $end . '/' . $size);
|
|
|
|
// 读取部分内容并发送响应
|
|
|
|
$file = fopen($path, 'rb');
|
|
|
|
fseek($file, $start);
|
|
|
|
$buffer = 1024 * 8; // 设置缓冲区大小
|
|
|
|
while (!feof($file) && ($p = ftell($file)) <= $end) {
|
|
|
|
if ($p + $buffer > $end) {
|
|
|
|
// 最后一块缓冲区
|
|
|
|
$buffer = $end - $p + 1;
|
|
|
|
}
|
|
|
|
echo fread($file, $buffer);
|
|
|
|
flush(); // 将输出刷新到浏览器
|
|
|
|
}
|
|
|
|
fclose($file);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
// 无范围请求,发送完整文件
|
|
|
|
$header['Content-Length'] = $size;
|
|
|
|
$content = file_get_contents($path);
|
|
|
|
// 发送完整响应
|
|
|
|
foreach ($header as $name => $value) {
|
|
|
|
header("$name: $value");
|
|
|
|
}
|
|
|
|
echo $content;
|
|
|
|
header("Content-Type: video/mp4");
|
|
|
|
// 发送完整文件
|
|
|
|
readfile($path);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
...
|
...
|
|