From 2fc6e5a7932cbe1733abbe16c18e90c33921312a Mon Sep 17 00:00:00 2001 From: JULM-COMPUTER Date: Fri, 18 Apr 2025 10:58:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=B8=8B=E8=BD=BD=E5=9C=B0?= =?UTF-8?q?=E5=9D=80=E6=96=87=E4=BB=B6=E5=90=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/command/Zm.php | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/app/command/Zm.php b/app/command/Zm.php index 6358161..cc1f2b0 100644 --- a/app/command/Zm.php +++ b/app/command/Zm.php @@ -92,25 +92,25 @@ class Zm extends Command $class_id = $class_details['data']['classSection']['id']; if(!empty($class_details['data']['classSection']['video'])){ - $file_name = urldecode(basename($class_details['data']['classSection']['video'])); + $file_name = $this->getFileNameFromUrl($class_details['data']['classSection']['video']); $downloadurl = $class_path . '/' . $file_name . '-下载地址.txt'; $this->savetxt($downloadurl,$class_details['data']['classSection']['video']); $this->download($class_details['data']['classSection']['video'],$class_path,$file_name); } if(!empty($class_details['data']['classSection']['courseware'])){ - $file_name = urldecode(basename($class_details['data']['classSection']['courseware'])); + $file_name = $this->getFileNameFromUrl($class_details['data']['classSection']['courseware']); $downloadurl = $class_path . '/' . $file_name . '-下载地址.txt'; $this->savetxt($downloadurl,$class_details['data']['classSection']['courseware']); $this->download($class_details['data']['classSection']['courseware'],$class_path,$file_name); } if(!empty($class_details['data']['classSection']['construction_draw'])){ - $file_name = urldecode(basename($class_details['data']['classSection']['construction_draw'])); + $file_name = $this->getFileNameFromUrl($class_details['data']['classSection']['construction_draw']); $downloadurl = $class_path . '/' . $file_name . '-下载地址.txt'; $this->savetxt($downloadurl,$class_details['data']['classSection']['construction_draw']); $this->download($class_details['data']['classSection']['construction_draw'],$class_path,$file_name); } if(!empty($class_details['data']['classSection']['construction_draw_3d'])){ - $file_name = urldecode(basename($class_details['data']['classSection']['construction_draw_3d'])); + $file_name = $this->getFileNameFromUrl($class_details['data']['classSection']['construction_draw_3d']); $downloadurl = $class_path . '/' . $file_name . '-下载地址.txt'; $this->savetxt($downloadurl,urldecode($class_details['data']['classSection']['construction_draw_3d'])); $this->download($class_details['data']['classSection']['construction_draw_3d'],$class_path,$file_name); @@ -366,4 +366,29 @@ class Zm extends Command fclose($myfile); // dump('已保存文本内容:'.$file); } + /** + * 从URL中提取并解码文件名 + * @param string $url 包含文件名的URL + * @param bool $throwExceptions 是否抛出异常 (默认true) + * @return string|null 成功返回文件名,失败返回null(仅当$throwExceptions=false时) + * @throws \InvalidArgumentException + */ + function getFileNameFromUrl(string $url, bool $throwExceptions = true): ?string + { + // 解析URL结构 + $parsed = parse_url($url); + + // 错误检测逻辑 + if ($parsed === false || !isset($parsed['path'])) { + $errorMsg = "无效的URL格式: " . json_encode($url); + if ($throwExceptions) { + throw new \InvalidArgumentException($errorMsg); + } + trigger_error($errorMsg, E_USER_WARNING); + return null; + } + + // 提取并解码文件名 + return urldecode(basename($parsed['path'])); + } } \ No newline at end of file