合并分支 'master-server' 到 'master'
gx复制产品 查看合并请求 !1010
正在显示
1 个修改的文件
包含
32 行增加
和
17 行删除
| @@ -68,43 +68,58 @@ class FileManageController extends BaseController | @@ -68,43 +68,58 @@ class FileManageController extends BaseController | ||
| 68 | if (!isset($this->param['path']) || empty($this->param['path'])) { | 68 | if (!isset($this->param['path']) || empty($this->param['path'])) { |
| 69 | $this->response('参数错误', Code::SYSTEM_ERROR); | 69 | $this->response('参数错误', Code::SYSTEM_ERROR); |
| 70 | } | 70 | } |
| 71 | + // 获取文件名 | ||
| 71 | $username = basename($this->param['path']); | 72 | $username = basename($this->param['path']); |
| 72 | $parsed_url = parse_url($this->param['path']); | 73 | $parsed_url = parse_url($this->param['path']); |
| 73 | // 构造文件 URL | 74 | // 构造文件 URL |
| 74 | $fileUrl = isset($parsed_url['scheme']) | 75 | $fileUrl = isset($parsed_url['scheme']) |
| 75 | ? $this->param['path'] | 76 | ? $this->param['path'] |
| 76 | : 'https://file.globalso.com' . $this->param['path']; | 77 | : 'https://file.globalso.com' . $this->param['path']; |
| 77 | - // 初始化 curl | 78 | + // 初始化 cURL |
| 78 | $ch = curl_init(); | 79 | $ch = curl_init(); |
| 79 | curl_setopt($ch, CURLOPT_URL, $fileUrl); | 80 | curl_setopt($ch, CURLOPT_URL, $fileUrl); |
| 80 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
| 81 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | 81 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
| 82 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); | ||
| 82 | curl_setopt($ch, CURLOPT_HEADER, false); | 83 | curl_setopt($ch, CURLOPT_HEADER, false); |
| 83 | - // 跳过 SSL 验证 | ||
| 84 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | 84 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
| 85 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); | 85 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
| 86 | - // 执行 curl 请求 | ||
| 87 | - $fileContent = curl_exec($ch); | ||
| 88 | - $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | ||
| 89 | - $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); | ||
| 90 | - $curlError = curl_error($ch); | ||
| 91 | - curl_close($ch); | ||
| 92 | - // 检查 curl 错误或 HTTP 状态码 | ||
| 93 | - if ($fileContent === false || $httpCode != 200) { | ||
| 94 | - $errorMsg = $fileContent === false ? "Curl Error: $curlError" : "HTTP Error: $httpCode"; | ||
| 95 | - $this->response("文件下载失败 - $errorMsg", Code::SYSTEM_ERROR); | 86 | + curl_setopt($ch, CURLOPT_BUFFERSIZE, 8192); // 设置缓冲区大小 |
| 87 | + curl_setopt($ch, CURLOPT_TIMEOUT, 60); // 设置超时时间 | ||
| 88 | + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // 连接超时时间 | ||
| 89 | + | ||
| 90 | + // 设置为分块输出 | ||
| 91 | + curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $data) { | ||
| 92 | + echo $data; | ||
| 93 | + flush(); // 强制刷新缓冲区 | ||
| 94 | + return strlen($data); | ||
| 95 | + }); | ||
| 96 | + // 检查文件头信息 | ||
| 97 | + $headers = get_headers($fileUrl, 1); | ||
| 98 | + if ($headers === false || !isset($headers['Content-Length'])) { | ||
| 99 | + $this->response('无法获取文件信息', Code::SYSTEM_ERROR); | ||
| 96 | } | 100 | } |
| 97 | // 设置响应头 | 101 | // 设置响应头 |
| 98 | header('Content-Description: File Transfer'); | 102 | header('Content-Description: File Transfer'); |
| 99 | - header('Content-Type: ' . ($contentType ?: 'application/octet-stream')); | 103 | + header('Content-Type: ' . ($headers['Content-Type'] ?? 'application/octet-stream')); |
| 100 | header('Content-Disposition: attachment; filename="' . $username . '"'); | 104 | header('Content-Disposition: attachment; filename="' . $username . '"'); |
| 101 | - header('Content-Length: ' . strlen($fileContent)); | ||
| 102 | - // 输出文件内容 | ||
| 103 | - echo $fileContent; | 105 | + header('Content-Length: ' . $headers['Content-Length']); |
| 106 | + header('Cache-Control: must-revalidate'); | ||
| 107 | + header('Pragma: public'); | ||
| 108 | + // 执行 cURL | ||
| 109 | + ob_end_clean(); // 清空之前的输出缓冲区 | ||
| 110 | + curl_exec($ch); | ||
| 111 | + | ||
| 112 | + // 检查 cURL 错误 | ||
| 113 | + if (curl_errno($ch)) { | ||
| 114 | + curl_close($ch); | ||
| 115 | + $this->response('文件下载失败: ' . curl_error($ch), Code::SYSTEM_ERROR); | ||
| 116 | + } | ||
| 117 | + curl_close($ch); | ||
| 104 | exit; | 118 | exit; |
| 105 | } | 119 | } |
| 106 | 120 | ||
| 107 | 121 | ||
| 122 | + | ||
| 108 | public function upload(Request $request, FileManage $fileManage){ | 123 | public function upload(Request $request, FileManage $fileManage){ |
| 109 | $request->validate([ | 124 | $request->validate([ |
| 110 | 'file'=>['required'], | 125 | 'file'=>['required'], |
-
请 注册 或 登录 后发表评论