Files
Melo/src/renderer/core/music/download.ts
T
2022-10-29 13:19:15 +08:00

73 lines
2.4 KiB
TypeScript

import { appSetting } from '@renderer/store/setting'
import { getDownloadFilePath } from '@renderer/utils/music'
import {
getMusicUrl as getOnlineMusicUrl,
getPicUrl as getOnlinePicUrl,
getLyricInfo as getOnlineLyricInfo,
} from './online'
import { buildLyricInfo, getCachedLyricInfo } from './utils'
export const getMusicUrl = async({ musicInfo, isRefresh, onToggleSource = () => {} }: {
musicInfo: LX.Download.ListItem
isRefresh: boolean
onToggleSource?: (musicInfo?: LX.Music.MusicInfoOnline) => void
}): Promise<string> => {
if (!isRefresh) {
const path = await getDownloadFilePath(musicInfo, appSetting['download.savePath'])
if (path) return path
}
return await getOnlineMusicUrl({ musicInfo: musicInfo.metadata.musicInfo, isRefresh, onToggleSource })
}
export const getPicUrl = async({ musicInfo, isRefresh, listId, onToggleSource = () => {} }: {
musicInfo: LX.Download.ListItem
isRefresh: boolean
listId?: string | null
onToggleSource?: (musicInfo?: LX.Music.MusicInfoOnline) => void
}): Promise<string> => {
if (!isRefresh) {
const path = await getDownloadFilePath(musicInfo, appSetting['download.savePath'])
if (path) {
const pic = await window.lx.worker.main.getMusicFilePic(path)
if (pic) return pic
}
const onlineMusicInfo = musicInfo.metadata.musicInfo
if (onlineMusicInfo.meta.picUrl) return onlineMusicInfo.meta.picUrl
}
return await getOnlinePicUrl({ musicInfo: musicInfo.metadata.musicInfo, isRefresh, onToggleSource }).then((url) => {
// TODO: when listId required save url (update downloadInfo)
return url
})
}
export const getLyricInfo = async({ musicInfo, isRefresh, onToggleSource = () => {} }: {
musicInfo: LX.Download.ListItem
isRefresh: boolean
onToggleSource?: (musicInfo?: LX.Music.MusicInfoOnline) => void
}): Promise<LX.Player.LyricInfo> => {
if (!isRefresh) {
const lyricInfo = await getCachedLyricInfo(musicInfo.metadata.musicInfo)
if (lyricInfo) return await buildLyricInfo(lyricInfo)
}
return getOnlineLyricInfo({
musicInfo: musicInfo.metadata.musicInfo,
isRefresh,
onToggleSource,
}).catch(async() => {
// 尝试读取文件内歌词
const path = await getDownloadFilePath(musicInfo, appSetting['download.savePath'])
if (path) {
const rawlrcInfo = await window.lx.worker.main.getMusicFileLyric(path)
if (rawlrcInfo) return buildLyricInfo(rawlrcInfo)
}
throw new Error('failed')
})
}