104 lines
2.9 KiB
TypeScript
104 lines
2.9 KiB
TypeScript
import { DOWNLOAD_STATUS, QUALITYS } from '@common/constants'
|
|
import { filterFileName } from '@common/utils/common'
|
|
import { joinPath } from '@common/utils/nodejs'
|
|
import fs from 'fs'
|
|
|
|
/**
|
|
* 保存歌词文件
|
|
* @param {*} filePath
|
|
* @param {*} lrc
|
|
* @param {*} format
|
|
*/
|
|
export const saveLrc = async(filePath: string, lrc: string, format: LX.LyricFormat) => {
|
|
const iconv = await import('iconv-lite')
|
|
switch (format) {
|
|
case 'gbk':
|
|
fs.writeFile(filePath, iconv.encode(lrc, 'gbk', { addBOM: true }), err => {
|
|
if (err) console.log(err)
|
|
})
|
|
break
|
|
case 'utf8':
|
|
default:
|
|
fs.writeFile(filePath, lrc, 'utf8', err => {
|
|
if (err) console.log(err)
|
|
})
|
|
break
|
|
}
|
|
}
|
|
|
|
export const getExt = (type: string): LX.Download.FileExt => {
|
|
switch (type) {
|
|
case 'ape':
|
|
return 'ape'
|
|
case 'flac':
|
|
case 'flac24bit':
|
|
return 'flac'
|
|
case 'wav':
|
|
return 'wav'
|
|
case '128k':
|
|
case '192k':
|
|
case '320k':
|
|
default:
|
|
return 'mp3'
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取音乐音质
|
|
* @param musicInfo
|
|
* @param type
|
|
* @param qualityList
|
|
*/
|
|
export const getMusicType = (musicInfo: LX.Music.MusicInfoOnline, type: LX.Quality, qualityList: LX.QualityList): LX.Quality => {
|
|
let list = qualityList[musicInfo.source]
|
|
if (!list) return '128k'
|
|
if (!list.includes(type)) type = list[list.length - 1]
|
|
const rangeType = QUALITYS.slice(QUALITYS.indexOf(type))
|
|
for (const type of rangeType) {
|
|
if (musicInfo.meta._qualitys[type]) return type
|
|
}
|
|
return '128k'
|
|
}
|
|
|
|
// 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))
|
|
// }
|
|
|
|
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)
|
|
const key = `${musicInfo.id}_${type}_${ext}`
|
|
// if (checkExistList(list, musicInfo, type, ext)) return null
|
|
const downloadInfo: LX.Download.ListItem = {
|
|
id: key,
|
|
isComplate: false,
|
|
status: DOWNLOAD_STATUS.WAITING,
|
|
statusText: '待下载',
|
|
downloaded: 0,
|
|
total: 0,
|
|
progress: 0,
|
|
speed: '',
|
|
metadata: {
|
|
musicInfo,
|
|
url: null,
|
|
quality: type,
|
|
ext,
|
|
filePath: '',
|
|
fileName: filterFileName(`${fileName
|
|
.replace('歌名', musicInfo.name)
|
|
.replace('歌手', musicInfo.singer)}.${ext}`),
|
|
},
|
|
}
|
|
downloadInfo.metadata.filePath = joinPath(savePath, downloadInfo.metadata.fileName)
|
|
// commit('addTask', downloadInfo)
|
|
|
|
// 删除同路径下的同名文件
|
|
// TODO
|
|
// deleteFile(downloadInfo.metadata.filePath)
|
|
// .catch(err => {
|
|
// if (err.code !== 'ENOENT') return commit('setStatusText', { downloadInfo, text: '文件删除失败' })
|
|
// })
|
|
|
|
return downloadInfo
|
|
}
|