Skip to content

Commit

Permalink
修复歌曲时文件名过长导致歌曲无法下载的问题(lyswhut#1877
Browse files Browse the repository at this point in the history
  • Loading branch information
lyswhut committed May 3, 2024
1 parent ee6a6f4 commit b680a89
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions publish/changeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- 修复Mac下即使开启了托盘, `cmd+w` 仍会中断播放的问题(#1844
- 修复播放详情页的歌词无法使用触碰拖动的问题(#1865
- 修复与优化繁体中文、英语翻译显示(#1845
- 修复歌曲时文件名过长导致歌曲无法下载的问题(#1877

### 变更

Expand Down
2 changes: 1 addition & 1 deletion src/common/utils/download/Downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ class Task extends EventEmitter {
if (err) {
console.log(err)
this.__handleError(err)
void this.stop()
return
}
if (this.closeWaiting && !this.dataWriteQueueLength) this.ws?.close()
})
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/store/download/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ const downloadLyric = (downloadInfo: LX.Download.ListItem) => {
tlrc: appSetting['download.isDownloadTLrc'] && lrcs.tlyric ? lrcs.tlyric : null,
rlrc: appSetting['download.isDownloadRLrc'] && lrcs.rlyric ? lrcs.rlyric : null,
}
void window.lx.worker.download.saveLrc(lrcData, downloadInfo.metadata.filePath.replace(/(mp3|flac|ape|wav)$/, 'lrc'),
void window.lx.worker.download.saveLrc(lrcData,
downloadInfo.metadata.filePath.substring(0, downloadInfo.metadata.filePath.lastIndexOf('.')) + '.lrc',
appSetting['download.lrcFormat'])
}
})
Expand Down
7 changes: 4 additions & 3 deletions src/renderer/worker/download/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 17 additions & 3 deletions src/renderer/worker/download/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit b680a89

Please sign in to comment.