123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- import Logger from "@/util/logger.ts";
- import { loadDb} from "@/common/db/db.ts";
- import {handle, PromiseResult} from "@/util/promiseHandle.ts";
- import {AppDbName} from "@/types/appConfig.ts";
- import {Knex} from "knex";
- import {MusicScanSetting, MusicTableName} from "@/types/musicType.ts";
- let logger = Logger.logger('music_db', 'info');
- async function _initScanConfigTable(db : Knex): PromiseResult<boolean> {
- let [err, hasTable] = await handle(
- db.schema.hasTable(MusicTableName.music_scan_setting)
- )
- if (err) {
- err = err as Error;
- logger.error(`[音频扫描库] ${err.message}`)
- return [new Error('音频扫描库初始化失败'), false]
- }
- if (hasTable) {
- return [null, true];
- }
- let [createErr, _res] = await handle(db?.schema.createTable('scanConfig', (table) => {
- // 初始化扫描配置
- logger.error(`[初始化音频扫描库]`)
- table.string('name')
- table.string('path')
- table.boolean('scanSubDir')
- table.boolean('isFileRepeat')
- }))
- if (createErr) {
- createErr = createErr as Error;
- logger.error(`[初始化磁贴表失败] ${createErr.message}`)
- return [createErr, false];
- }
- logger.info('[初始化音频扫描配置成功]')
- return [null, true];
- }
- async function _initPlayListTable(db : Knex): PromiseResult<boolean> {
- let [err, hasTable] = await handle(
- db.schema.hasTable(MusicTableName.music_scan_setting)
- )
- if (err) {
- err = err as Error;
- logger.error(`[歌单初始化] ${err.message}`)
- return [new Error('歌单初始化'), false]
- }
- if (hasTable) {
- return [null, true];
- }
- let [createErr, _res] = await handle(
- db.schema.createTable(MusicTableName.music_play_list, (table) => {
- logger.error(`[初始化音频播放列表库]`)
- table.increments('id').primary()
- table.string('name')
- table.string('icon')
- table.string('cover')
- table.string('description')
- table.integer('playCount')
- table.integer('trackCount')
- table.integer('createTime')
- table.boolean('isTagSearch')
- table.integer('lastPlayTime')
- table.boolean('isSync')
- table.boolean('isPublic')
- table.boolean('isLike')
- }))
- if (createErr) {
- createErr = createErr as Error;
- logger.error(`[初始化歌单失败] ${createErr.message}`)
- return [createErr, false];
- }
- // 添加默认歌单
- await db.insert({
- name: '我的喜爱',
- icon: 'favorite',
- cover: 'favorite',
- description: '我的喜爱',
- playCount: 0,
- trackCount: 0,
- createTime: Date.now(),
- isTagSearch: false,
- lastPlayTime: Date.now(),
- isSync: false,
- isPublic: false,
- isLike: true,
- }).into(MusicTableName.music_play_list)
- logger.info('[初始化我的喜爱歌单成功]')
- return [null, true];
- }
- async function _initSongsTable(db : Knex): PromiseResult<boolean>
- {
- let [err, hasTable] = await handle(
- db.schema.hasTable(MusicTableName.music_songs)
- )
- if (err) {
- err = err as Error;
- logger.error(`[音频表初始化] ${err.message}`)
- return [new Error('音频表初始化'), false]
- }
- if (hasTable) {
- return [null, true];
- }
- let [createErr, _res] = await handle(
- db.schema.createTable(MusicTableName.music_songs, (table) => {
- logger.error(`[初始化音频库]`)
- table.increments('id').primary()
- table.string('name')
- table.string('artists')
- table.string('album')
- table.string('cover')
- table.integer('duration')
- table.boolean('isLike')
- table.string('origin')
- table.integer('type')
- table.boolean('isLocal')
- table.string('filePath')
- table.string('lyricPath')
- table.string('tags')
- table.integer('playCount')
- }))
- if (createErr) {
- createErr = createErr as Error;
- logger.error(`[初始化音频库失败] ${createErr.message}`)
- return [createErr, false];
- }
- logger.info('[初始化音频库成功]')
- return [null, true];
- }
- async function _initPlaylistSongs(db : Knex): PromiseResult<boolean>
- {
- let [err, hasTable] = await handle(
- db.schema.hasTable(MusicTableName.music_play_list_songs)
- )
- if (err) {
- err = err as Error;
- logger.error(`[歌单歌曲表初始化] ${err.message}`)
- return [new Error('歌单歌曲表初始化'), false]
- }
- if (hasTable) {
- return [null, true];
- }
- let [createErr, _res] = await handle(
- db.schema.createTable(MusicTableName.music_play_list_songs, (table) => {
- logger.error(`[初始化歌单歌曲库]`)
- table.increments('id').primary()
- table.integer('musicId')
- table.integer('playListId')
- table.integer('order')
- }))
- if (createErr) {
- createErr = createErr as Error;
- logger.error(`[初始化歌单歌曲库失败] ${createErr.message}`)
- return [createErr, false];
- }
- logger.info('[初始化歌单歌曲库成功]')
- return [null, true];
- }
- export async function initMusicData() : PromiseResult<boolean>
- {
- let db = loadDb(AppDbName.music_db)
- if(!db){
- logger.error('数据库初始化失败')
- return [new Error('音乐数据库初始化失败'), false]
- }
- let [err, res] = await _initScanConfigTable(db);
- if (err) {
- err = err as Error;
- logger.error(`[初始化扫描库失败] ${err.message}`)
- return [err, false];
- }
- [err, res] = await _initPlayListTable(db);
- if (err) {
- err = err as Error;
- logger.error(`[初始化播放列表库失败] ${err.message}`)
- return [err, false];
- }
- [err, res] = await _initSongsTable(db);
- if (err) {
- err = err as Error;
- logger.error(`[初始化音频库失败] ${err.message}`)
- return [err, false];
- }
- [err, res] = await _initPlaylistSongs(db);
- if (err) {
- err = err as Error;
- logger.error(`[初始化歌单歌曲库失败] ${err.message}`)
- return [err, false];
- }
- logger.info('[初始化音频库成功]')
- return [null, res]
- }
- // 根据扫描地址获取扫描配置
- export async function getScanConfigByPath(path: string) : PromiseResult<MusicScanSetting[]>
- {
- let db = loadDb(AppDbName.music_db)
- if(!db){
- logger.error('数据库初始化失败')
- return [new Error('音乐数据库初始化失败'), null]
- }
- let [err, res] = await handle(
- db.select('name', 'path', 'scanSubDir', 'isFileRepeat')
- .from(MusicTableName.music_scan_setting)
- .where('path', path)
- )
- if (err) {
- err = err as Error;
- logger.error(`[获取扫描配置失败] ${err.message}`)
- return [err, null];
- }
- return [null, res as MusicScanSetting[]];
- }
- export async function getScanConfig() : PromiseResult<MusicScanSetting[]>
- {
- let db = loadDb(AppDbName.music_db)
- if(!db){
- logger.error('数据库初始化失败')
- return [new Error('音乐数据库初始化失败'), null]
- }
- let [err, res] = await handle(
- db.select('name', 'path', 'scanSubDir', 'isFileRepeat')
- .from(MusicTableName.music_scan_setting)
- )
- if (err) {
- err = err as Error;
- logger.error(`[获取扫描配置失败] ${err.message}`)
- return [err, null];
- }
- return [null, res as MusicScanSetting[]];
- }
- export async function addScanConfig(scanConfig: MusicScanSetting) : PromiseResult<boolean>
- {
- let db = loadDb(AppDbName.music_db)
- if(!db){
- logger.error('数据库初始化失败')
- return [new Error('音乐数据库初始化失败'), false]
- }
- let [err, _res] = await handle(
- db.insert(scanConfig).into(MusicTableName.music_scan_setting)
- )
- if (err) {
- err = err as Error;
- logger.error(`[添加扫描配置失败] ${err.message}`)
- return [err, false];
- }
- return [null, true];
- }
- export async function updateScanConfig(scanConfig: MusicScanSetting) : PromiseResult<boolean>
- {
- let db = loadDb(AppDbName.music_db)
- if(!db){
- logger.error('数据库初始化失败')
- return [new Error('音乐数据库初始化失败'), false]
- }
- let [err, _res] = await handle(
- db.update(scanConfig).into(MusicTableName.music_scan_setting)
- )
- if (err) {
- err = err as Error;
- }
- return [err, true];
- }
- export async function deleteScanConfig(path: string) : PromiseResult<boolean>
- {
- let db = loadDb(AppDbName.music_db)
- if(!db){
- logger.error('数据库初始化失败')
- return [new Error('音乐数据库初始化失败'), false]
- }
- let [err, _res] = await handle(
- db.delete().from(MusicTableName.music_scan_setting).where('path', path)
- )
- if (err) {
- err= err as Error;
- }
- return [err, true];
- }
|