修复歌曲时文件名过长导致歌曲无法下载的问题(#1877)

This commit is contained in:
lyswhut
2024-05-03 18:49:48 +08:00
parent ee6a6f4b94
commit b680a89033
5 changed files with 25 additions and 8 deletions
+4 -3
View File
@@ -114,7 +114,7 @@ const createTask = async(downloadInfo: LX.Download.ListItem, savePath: string, s
console.log('on complate')
},
onError(err: any) {
console.log(err)
console.error(err)
if (err.code == 'EPERM') {
sendAction(downloadInfo.id, {
action: 'error',
@@ -157,8 +157,9 @@ const createTask = async(downloadInfo: LX.Download.ListItem, savePath: string, s
sendAction(downloadInfo.id, { action: 'refreshUrl' })
} else {
console.log('Download failed, Attempting Retry')
void dls.get(downloadInfo.id)?.start()
console.log('正在重试')
setTimeout(() => {
void dls.get(downloadInfo.id)?.start()
}, 1000)
}
},
onFail(response) {
+17 -3
View File
@@ -65,7 +65,21 @@ export const getMusicType = (musicInfo: LX.Music.MusicInfoOnline, type: LX.Quali
// const checkExistList = (list: LX.Download.ListItem[], musicInfo: LX.Music.MusicInfo, type: LX.Quality, ext: string): boolean => {
// return list.some(s => s.id === musicInfo.id && (s.metadata.type === type || s.metadata.ext === ext))
// }
const MAX_NAME_LENGTH = 80
const MAX_FILE_NAME_LENGTH = 150
const clipNameLength = (name: string) => {
if (name.length <= MAX_NAME_LENGTH || !name.includes('、')) return name
const names = name.split('、')
let newName = names.shift()!
for (const name of names) {
if (newName.length + name.length > MAX_NAME_LENGTH) break
newName = newName + '、' + name
}
return newName
}
const clipFileNameLength = (name: string) => {
return name.length > MAX_FILE_NAME_LENGTH ? name.substring(0, MAX_FILE_NAME_LENGTH) : name
}
export const createDownloadInfo = (musicInfo: LX.Music.MusicInfoOnline, type: LX.Quality, fileName: string, savePath: string, qualityList: LX.QualityList) => {
type = getMusicType(musicInfo, type, qualityList)
let ext = getExt(type)
@@ -87,9 +101,9 @@ export const createDownloadInfo = (musicInfo: LX.Music.MusicInfoOnline, type: LX
quality: type,
ext,
filePath: '',
fileName: filterFileName(`${fileName
fileName: filterFileName(`${clipFileNameLength(fileName
.replace('歌名', musicInfo.name)
.replace('歌手', musicInfo.singer)}.${ext}`),
.replace('歌手', clipNameLength(musicInfo.singer)))}.${ext}`),
},
}
downloadInfo.metadata.filePath = joinPath(savePath, downloadInfo.metadata.fileName)