作者 lyh

gx

... ... @@ -85,38 +85,38 @@ class FileController
// 设置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;
}
// 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);
... ... @@ -177,6 +177,7 @@ class FileController
//同步数据到cos
if($this->upload_location == 1){
$cosService = new CosService();
$is_cos = 1;//上传到cos
$cosService->uploadFile($files,$this->path,$fileName);
}else{
$res = $files->move($url,$fileName);
... ... @@ -184,7 +185,7 @@ class FileController
return $this->response($files->getError(), Code::USER_ERROR);
}
}
$this->saveMysql($fileModel,$size,$file_type,$fileName,$hash);
$this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$is_cos);
return $this->response('资源',Code::SUCCESS,['file'=>$hash]);
}
... ... @@ -195,7 +196,7 @@ class FileController
* @method :post
* @time :2023/7/19 16:38
*/
public function saveMysql(&$fileModel,$size,$image_type,$fileName,$hash){
public function saveMysql(&$fileModel,$size,$image_type,$fileName,$hash,$is_cos = 0){
$data = [
'path' => $this->path.'/'.$fileName,
'created_at' => date('Y-m-d H:i:s',time()),
... ... @@ -203,6 +204,7 @@ class FileController
'hash' => $hash,
'type'=>$image_type,
'refer'=>$this->param['refer'] ?? 1,
'is_cos'=>$is_cos
];
$rs = $fileModel->add($data);
if ($rs === false) {
... ...