From df9a1370c30793e826c21f047dceaac624b09e2b Mon Sep 17 00:00:00 2001 From: alger Date: Fri, 14 Mar 2025 21:19:23 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20fix:=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=90=8D=E6=B8=85=E7=90=86=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E4=BB=A5=E5=A4=84=E7=90=86=E9=9D=9E=E6=B3=95=E5=AD=97=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 sanitizeFilename 函数,清理文件名中的非法字符 - 在下载音乐功能中应用清理后的文件名,确保文件名有效性 fixed: #78 --- src/main/modules/fileManager.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/modules/fileManager.ts b/src/main/modules/fileManager.ts index be46448..3fa061b 100644 --- a/src/main/modules/fileManager.ts +++ b/src/main/modules/fileManager.ts @@ -257,6 +257,17 @@ async function processDownloadQueue(event: Electron.IpcMainEvent) { } } +/** + * 清理文件名中的非法字符 + */ +function sanitizeFilename(filename: string): string { + // 替换 Windows 和 Unix 系统中的非法字符 + return filename + .replace(/[<>:"/\\|?*]/g, '_') // 替换特殊字符为下划线 + .replace(/\s+/g, ' ') // 将多个空格替换为单个空格 + .trim(); // 移除首尾空格 +} + /** * 下载音乐功能 */ @@ -276,9 +287,12 @@ async function downloadMusic( const store = new Store(); const downloadPath = (store.get('set.downloadPath') as string) || app.getPath('downloads'); + // 清理文件名中的非法字符 + const sanitizedFilename = sanitizeFilename(filename); + // 从URL中获取文件扩展名,如果没有则使用传入的type或默认mp3 const urlExt = type ? `.${type}` : '.mp3'; - const filePath = path.join(downloadPath, `${filename}${urlExt}`); + const filePath = path.join(downloadPath, `${sanitizedFilename}${urlExt}`); // 检查文件是否已存在,如果存在则添加序号 finalFilePath = filePath;