修复(服务器):增强文件名清理的健壮性

实施更严格的文件名清理措施,包括Unicode标准化处理(NFC)、控制字符移除以及200字符的长度限制,以防止歌曲下载过程中出现文件系统错误。
This commit is contained in:
史悦
2026-01-09 10:13:53 +08:00
parent 33ab93aa33
commit 4ea05279bd

View File

@@ -127,9 +127,20 @@ async function tryDownloadSongs(data) {
function processSong(song) {
return new Promise((resolve) => {
// Basic sanitization
const safeName = (song.name || 'unknown').replace(/[\\/:*?"<>|]/g, '_');
const safeArtist = (song.artist || 'unknown').replace(/[\\/:*?"<>|]/g, '_');
// Sanitize filename with proper encoding handling
const sanitizeFilename = (str) => {
if (!str) return 'unknown';
// Normalize Unicode (NFC) to ensure consistent encoding
const normalized = str.normalize('NFC');
// Replace Windows illegal characters and control characters
return normalized
.replace(/[\\/:*?"<>|\x00-\x1f\x7f]/g, '_')
.trim()
.substring(0, 200); // Limit length to avoid path too long errors
};
const safeName = sanitizeFilename(song.name);
const safeArtist = sanitizeFilename(song.artist);
const source = song.platform || song.source;
// Filename: Artist - Name [source_id]
const baseName = `${safeArtist} - ${safeName} [${source}_${song.id}]`;