gengx
This commit is contained in:
@@ -118,12 +118,46 @@ class Upload extends BaseApi
|
||||
}
|
||||
|
||||
$mime = $upload->mime_type ?: (@mime_content_type($path) ?: 'application/octet-stream');
|
||||
|
||||
return response(file_get_contents($path), 200, [
|
||||
'Content-Type' => $mime,
|
||||
'Content-Length' => (string) filesize($path),
|
||||
$size = (int) filesize($path);
|
||||
$headers = [
|
||||
'Content-Type' => $mime,
|
||||
'Content-Length' => (string) $size,
|
||||
'Accept-Ranges' => 'bytes',
|
||||
'Cache-Control' => 'public, max-age=604800',
|
||||
]);
|
||||
];
|
||||
|
||||
$range = trim((string) $this->request->header('range', ''));
|
||||
if ($range !== '' && preg_match('/^bytes=(\d*)-(\d*)$/', $range, $matches)) {
|
||||
$start = $matches[1] === '' ? 0 : (int) $matches[1];
|
||||
$end = $matches[2] === '' ? $size - 1 : (int) $matches[2];
|
||||
if ($matches[1] === '' && $matches[2] !== '') {
|
||||
$length = min($size, (int) $matches[2]);
|
||||
$start = $size - $length;
|
||||
$end = $size - 1;
|
||||
}
|
||||
if ($start < 0 || $start >= $size || $end < $start) {
|
||||
return response('', 416, [
|
||||
'Content-Range' => 'bytes */' . $size,
|
||||
'Accept-Ranges' => 'bytes',
|
||||
]);
|
||||
}
|
||||
$end = min($end, $size - 1);
|
||||
$length = $end - $start + 1;
|
||||
$handle = fopen($path, 'rb');
|
||||
if ($handle === false || fseek($handle, $start) !== 0) {
|
||||
if (is_resource($handle)) {
|
||||
fclose($handle);
|
||||
}
|
||||
throw new HttpResponseException(response('文件读取失败', 500));
|
||||
}
|
||||
$content = (string) fread($handle, $length);
|
||||
fclose($handle);
|
||||
$headers['Content-Length'] = (string) strlen($content);
|
||||
$headers['Content-Range'] = "bytes {$start}-{$end}/{$size}";
|
||||
return response($content, 206, $headers);
|
||||
}
|
||||
|
||||
return response(file_get_contents($path), 200, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user