v2.0.0
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import { getDB } from '../../db'
|
||||
import {
|
||||
createQueryStatement,
|
||||
createInsertStatement,
|
||||
createDeleteStatement,
|
||||
createUpdateStatement,
|
||||
createUpdatePositionStatement,
|
||||
createClearStatement,
|
||||
} from './statements'
|
||||
|
||||
/**
|
||||
* 查询下载歌曲列表
|
||||
*/
|
||||
export const queryDownloadList = (): LX.DBService.DownloadMusicInfo[] => {
|
||||
const queryStatement = createQueryStatement()
|
||||
return queryStatement.all()
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量插入下载歌曲并刷新顺序
|
||||
* @param mInfos 列表
|
||||
*/
|
||||
export const inertDownloadList = (mInfos: LX.DBService.DownloadMusicInfo[], listPositions: Array<{ id: string, position: number }>) => {
|
||||
const db = getDB()
|
||||
const insertStatement = createInsertStatement()
|
||||
const updatePositionStatement = createUpdatePositionStatement()
|
||||
db.transaction((mInfos: LX.DBService.DownloadMusicInfo[]) => {
|
||||
for (const info of mInfos) insertStatement.run(info)
|
||||
for (const info of listPositions) updatePositionStatement.run(info)
|
||||
})(mInfos)
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除下载歌曲
|
||||
* @param ids 列表
|
||||
*/
|
||||
export const deleteDownloadList = (ids: string[]) => {
|
||||
const db = getDB()
|
||||
const deleteStatement = createDeleteStatement()
|
||||
db.transaction((ids: string[]) => {
|
||||
for (const id of ids) deleteStatement.run(id)
|
||||
})(ids)
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量更新下载歌曲
|
||||
* @param urlInfo 列表
|
||||
*/
|
||||
export const updateDownloadList = (urlInfo: LX.DBService.DownloadMusicInfo[]) => {
|
||||
const db = getDB()
|
||||
const updateStatement = createUpdateStatement()
|
||||
db.transaction((urlInfo: LX.DBService.DownloadMusicInfo[]) => {
|
||||
for (const info of urlInfo) updateStatement.run(info)
|
||||
})(urlInfo)
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空下载歌曲列表
|
||||
*/
|
||||
export const clearDownloadList = () => {
|
||||
const clearStatement = createClearStatement()
|
||||
clearStatement.run()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
import {
|
||||
queryDownloadList,
|
||||
inertDownloadList,
|
||||
updateDownloadList,
|
||||
deleteDownloadList,
|
||||
clearDownloadList,
|
||||
} from './dbHelper'
|
||||
|
||||
let list: LX.Download.ListItem[]
|
||||
|
||||
const toDBDownloadInfo = (musicInfos: LX.Download.ListItem[], offset: number = 0): LX.DBService.DownloadMusicInfo[] => {
|
||||
return musicInfos.map((info, index) => {
|
||||
return {
|
||||
id: info.id,
|
||||
isComplate: info.isComplate ? 1 : 0,
|
||||
status: info.status,
|
||||
statusText: info.statusText,
|
||||
progress_downloaded: info.downloaded,
|
||||
progress_total: info.total,
|
||||
url: info.metadata.url,
|
||||
quality: info.metadata.quality,
|
||||
ext: info.metadata.ext,
|
||||
fileName: info.metadata.fileName,
|
||||
filePath: info.metadata.filePath,
|
||||
musicInfo: JSON.stringify(info.metadata.musicInfo),
|
||||
position: offset + index,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const initDownloadList = () => {
|
||||
list = queryDownloadList().map(item => {
|
||||
const musicInfo = JSON.parse(item.musicInfo) as LX.Music.MusicInfoOnline
|
||||
return {
|
||||
id: item.id,
|
||||
isComplate: item.isComplate == 1,
|
||||
status: item.status,
|
||||
statusText: item.statusText,
|
||||
downloaded: item.progress_downloaded,
|
||||
total: item.progress_total,
|
||||
progress: item.progress_total ? parseInt((item.progress_downloaded / item.progress_total).toFixed(2)) * 100 : 0,
|
||||
speed: '',
|
||||
metadata: {
|
||||
musicInfo,
|
||||
url: item.url,
|
||||
quality: item.quality,
|
||||
ext: item.ext,
|
||||
fileName: item.fileName,
|
||||
filePath: item.filePath,
|
||||
},
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下载列表
|
||||
* @returns 下载列表
|
||||
*/
|
||||
export const getDownloadList = (): LX.Download.ListItem[] => {
|
||||
if (!list) initDownloadList()
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加下载歌曲信息
|
||||
* @param downloadInfos url信息
|
||||
*/
|
||||
export const downloadInfoSave = (downloadInfos: LX.Download.ListItem[], addMusicLocationType: LX.AddMusicLocationType) => {
|
||||
if (!list) initDownloadList()
|
||||
if (addMusicLocationType == 'top') {
|
||||
let newList = [...list]
|
||||
newList.unshift(...downloadInfos)
|
||||
inertDownloadList(toDBDownloadInfo(downloadInfos), newList.slice(downloadInfos.length - 1).map((info, index) => {
|
||||
return { id: info.id, position: index }
|
||||
}))
|
||||
list = newList
|
||||
} else {
|
||||
inertDownloadList(toDBDownloadInfo(downloadInfos, list.length), [])
|
||||
list.push(...downloadInfos)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量更新列表信息
|
||||
* @param lists 列表信息
|
||||
*/
|
||||
export const downloadInfoUpdate = (lists: LX.Download.ListItem[]) => {
|
||||
updateDownloadList(toDBDownloadInfo(lists))
|
||||
if (list) {
|
||||
for (const item of lists) {
|
||||
const index = list.findIndex(info => info.id === item.id)
|
||||
if (index < 0) continue
|
||||
list.splice(index, 1, item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除下载列表
|
||||
* @param ids 歌曲id
|
||||
*/
|
||||
export const downloadInfoRemove = (ids: string[]) => {
|
||||
deleteDownloadList(ids)
|
||||
if (list) {
|
||||
for (let i = list.length; i--;) {
|
||||
let idx = ids.indexOf(list[i].id)
|
||||
if (idx < 0) continue
|
||||
list.splice(i, 1)
|
||||
ids.splice(idx, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空下载列表
|
||||
*/
|
||||
export const downloadInfoClear = () => {
|
||||
clearDownloadList()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
import { getDB } from '../../db'
|
||||
|
||||
/**
|
||||
* 创建下载列表查询语句
|
||||
* @returns 查询语句
|
||||
*/
|
||||
export const createQueryStatement = () => {
|
||||
const db = getDB()
|
||||
return db.prepare(`
|
||||
SELECT "id", "isComplate", "status", "statusText", "progress_downloaded", "progress_total", "url", "quality", "ext", "fileName", "filePath", "musicInfo", "position"
|
||||
FROM download_list
|
||||
ORDER BY "position" ASC
|
||||
`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建下载记录插入语句
|
||||
* @returns 插入语句
|
||||
*/
|
||||
export const createInsertStatement = () => {
|
||||
const db = getDB()
|
||||
return db.prepare<[LX.DBService.DownloadMusicInfo]>(`
|
||||
INSERT INTO "main"."download_list" ("id", "isComplate", "status", "statusText", "progress_downloaded", "progress_total", "url", "quality", "ext", "fileName", "filePath", "musicInfo", "position")
|
||||
VALUES (@id, @isComplate, @status, @statusText, @progress_downloaded, @progress_total, @url, @quality, @ext, @fileName, @filePath, @musicInfo, @position)`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建下载记录清空语句
|
||||
* @returns 清空语句
|
||||
*/
|
||||
export const createClearStatement = () => {
|
||||
const db = getDB()
|
||||
return db.prepare(`
|
||||
DELETE FROM "main"."download_list"
|
||||
`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建下载记录删除语句
|
||||
* @returns 删除语句
|
||||
*/
|
||||
export const createDeleteStatement = () => {
|
||||
const db = getDB()
|
||||
return db.prepare<[string]>(`
|
||||
DELETE FROM "main"."download_list"
|
||||
WHERE "id"=?
|
||||
`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建下载记录更新语句
|
||||
* @returns 更新语句
|
||||
*/
|
||||
export const createUpdateStatement = () => {
|
||||
const db = getDB()
|
||||
return db.prepare<[LX.DBService.DownloadMusicInfo]>(`
|
||||
UPDATE "main"."download_list"
|
||||
SET "isComplate"=@isComplate, "status"=@status, "statusText"=@statusText, "progress_downloaded"=@progress_downloaded, "progress_total"=@progress_total, "url"=@url, "filePath"=@filePath
|
||||
WHERE "id"=@id`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建下载记录顺序更新语句
|
||||
* @returns 更新语句
|
||||
*/
|
||||
export const createUpdatePositionStatement = () => {
|
||||
const db = getDB()
|
||||
return db.prepare<[{ id: string, position: number }]>(`
|
||||
UPDATE "main"."download_list"
|
||||
SET "position"=@position
|
||||
WHERE "id"=@id`)
|
||||
}
|
||||
Reference in New Issue
Block a user