作者 lyh

gx

@@ -62,21 +62,18 @@ class FileManageController extends BaseController @@ -62,21 +62,18 @@ class FileManageController extends BaseController
62 * @method :post 62 * @method :post
63 * @time :2023/12/28 17:18 63 * @time :2023/12/28 17:18
64 */ 64 */
65 - public function downLoad(){ 65 + public function downLoad()
  66 + {
66 // 检查参数 67 // 检查参数
67 if (!isset($this->param['path']) || empty($this->param['path'])) { 68 if (!isset($this->param['path']) || empty($this->param['path'])) {
68 $this->response('参数错误', Code::SYSTEM_ERROR); 69 $this->response('参数错误', Code::SYSTEM_ERROR);
69 } 70 }
70 -  
71 $username = basename($this->param['path']); 71 $username = basename($this->param['path']);
72 $parsed_url = parse_url($this->param['path']); 72 $parsed_url = parse_url($this->param['path']);
73 -  
74 // 构造文件 URL 73 // 构造文件 URL
75 - if (isset($parsed_url['scheme'])) {  
76 - $fileUrl = $this->param['path'];  
77 - } else {  
78 - $fileUrl = 'https://file.globalso.com' . $this->param['path'];  
79 - } 74 + $fileUrl = isset($parsed_url['scheme'])
  75 + ? $this->param['path']
  76 + : 'https://file.globalso.com' . $this->param['path'];
80 // 初始化 curl 77 // 初始化 curl
81 $ch = curl_init(); 78 $ch = curl_init();
82 curl_setopt($ch, CURLOPT_URL, $fileUrl); 79 curl_setopt($ch, CURLOPT_URL, $fileUrl);
@@ -86,14 +83,17 @@ class FileManageController extends BaseController @@ -86,14 +83,17 @@ class FileManageController extends BaseController
86 // 执行 curl 请求 83 // 执行 curl 请求
87 $fileContent = curl_exec($ch); 84 $fileContent = curl_exec($ch);
88 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 85 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  86 + $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
  87 + $curlError = curl_error($ch);
89 curl_close($ch); 88 curl_close($ch);
90 - // 检查 HTTP 响应码  
91 - if ($httpCode != 200 || $fileContent === false) {  
92 - $this->response('文件下载失败', Code::SYSTEM_ERROR); 89 + // 检查 curl 错误或 HTTP 状态码
  90 + if ($fileContent === false || $httpCode != 200) {
  91 + $errorMsg = $fileContent === false ? "Curl Error: $curlError" : "HTTP Error: $httpCode";
  92 + $this->response("文件下载失败 - $errorMsg", Code::SYSTEM_ERROR);
93 } 93 }
94 // 设置响应头 94 // 设置响应头
95 header('Content-Description: File Transfer'); 95 header('Content-Description: File Transfer');
96 - header('Content-Type: application/octet-stream'); 96 + header('Content-Type: ' . ($contentType ?: 'application/octet-stream'));
97 header('Content-Disposition: attachment; filename="' . $username . '"'); 97 header('Content-Disposition: attachment; filename="' . $username . '"');
98 header('Content-Length: ' . strlen($fileContent)); 98 header('Content-Length: ' . strlen($fileContent));
99 // 输出文件内容 99 // 输出文件内容
@@ -101,6 +101,7 @@ class FileManageController extends BaseController @@ -101,6 +101,7 @@ class FileManageController extends BaseController
101 exit; 101 exit;
102 } 102 }
103 103
  104 +
104 public function upload(Request $request, FileManage $fileManage){ 105 public function upload(Request $request, FileManage $fileManage){
105 $request->validate([ 106 $request->validate([
106 'file'=>['required'], 107 'file'=>['required'],