2022-03-30 00:53:05 +08:00
|
|
|
import { db, Tables } from './db'
|
|
|
|
import type { FetchTracksResponse } from '../renderer/api/track'
|
2022-03-19 17:03:29 +08:00
|
|
|
import { app, ipcMain } from 'electron'
|
|
|
|
import { Request, Response } from 'express'
|
|
|
|
import logger from './logger'
|
|
|
|
import fs from 'fs'
|
|
|
|
import * as musicMetadata from 'music-metadata'
|
|
|
|
|
|
|
|
export async function setCache(api: string, data: any, query: any) {
|
|
|
|
switch (api) {
|
2022-03-30 00:53:05 +08:00
|
|
|
case 'user/playlist':
|
2022-03-19 17:03:29 +08:00
|
|
|
case 'user/account':
|
|
|
|
case 'personalized':
|
2022-03-30 00:53:05 +08:00
|
|
|
case 'recommend/resource':
|
2022-03-19 17:03:29 +08:00
|
|
|
case 'likelist': {
|
|
|
|
if (!data) return
|
2022-03-30 00:53:05 +08:00
|
|
|
console.log(api)
|
|
|
|
db.upsert(Tables.ACCOUNT_DATA, {
|
|
|
|
id: api,
|
|
|
|
json: JSON.stringify(data),
|
|
|
|
updateAt: Date.now(),
|
|
|
|
})
|
2022-03-19 17:03:29 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
case 'song/detail': {
|
2022-03-30 00:53:05 +08:00
|
|
|
console.log('dsdadasdas')
|
2022-03-19 17:03:29 +08:00
|
|
|
if (!data.songs) return
|
2022-03-30 00:53:05 +08:00
|
|
|
const tracks = (data as FetchTracksResponse).songs.map(t => ({
|
|
|
|
id: t.id,
|
|
|
|
json: JSON.stringify(t),
|
|
|
|
updatedAt: Date.now(),
|
|
|
|
}))
|
|
|
|
db.upsertMany(Tables.TRACK, tracks)
|
2022-03-19 17:03:29 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
case 'album': {
|
|
|
|
if (!data.album) return
|
2022-03-21 02:03:25 +08:00
|
|
|
data.album.songs = (data as FetchTracksResponse).songs
|
2022-03-30 00:53:05 +08:00
|
|
|
db.upsert(Tables.ALBUM, {
|
|
|
|
id: data.album.id,
|
|
|
|
json: JSON.stringify(data),
|
|
|
|
updatedAt: Date.now(),
|
|
|
|
})
|
2022-03-19 17:03:29 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
case 'playlist/detail': {
|
|
|
|
if (!data.playlist) return
|
2022-03-30 00:53:05 +08:00
|
|
|
db.upsert(Tables.PLAYLIST, {
|
|
|
|
id: data.playlist.id,
|
|
|
|
json: JSON.stringify(data),
|
|
|
|
updatedAt: Date.now(),
|
|
|
|
})
|
2022-03-19 17:03:29 +08:00
|
|
|
break
|
|
|
|
}
|
2022-03-23 01:21:22 +08:00
|
|
|
case 'artists': {
|
|
|
|
if (!data.artist) return
|
2022-03-30 00:53:05 +08:00
|
|
|
db.upsert(Tables.ARTIST, {
|
|
|
|
id: data.artist.id,
|
|
|
|
json: JSON.stringify(data),
|
|
|
|
updatedAt: Date.now(),
|
|
|
|
})
|
2022-03-23 01:21:22 +08:00
|
|
|
break
|
|
|
|
}
|
2022-03-19 17:03:29 +08:00
|
|
|
case 'artist/album': {
|
|
|
|
if (!data.hotAlbums) return
|
2022-03-30 00:53:05 +08:00
|
|
|
db.upsert(Tables.ARTIST_ALBUMS, {
|
|
|
|
id: data.artist.id,
|
|
|
|
json: JSON.stringify(data),
|
|
|
|
updatedAt: Date.now(),
|
|
|
|
})
|
2022-03-19 17:03:29 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-30 00:53:05 +08:00
|
|
|
export function getCache(api: string, query: any): any {
|
2022-03-19 17:03:29 +08:00
|
|
|
switch (api) {
|
|
|
|
case 'user/account':
|
2022-03-30 00:53:05 +08:00
|
|
|
case 'user/playlist':
|
2022-03-19 17:03:29 +08:00
|
|
|
case 'personalized':
|
2022-03-30 00:53:05 +08:00
|
|
|
case 'recommend/resource':
|
2022-03-19 17:03:29 +08:00
|
|
|
case 'likelist': {
|
2022-03-30 00:53:05 +08:00
|
|
|
const data = db.find(Tables.ACCOUNT_DATA, api)
|
2022-03-19 17:03:29 +08:00
|
|
|
if (data?.json) return JSON.parse(data.json)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
case 'song/detail': {
|
|
|
|
const ids: string[] = query?.ids.split(',')
|
2022-03-24 14:23:04 +08:00
|
|
|
if (ids.length === 0) return
|
|
|
|
|
|
|
|
let isIDsValid = true
|
|
|
|
ids.forEach(id => {
|
|
|
|
if (id === '' || isNaN(Number(id))) isIDsValid = false
|
|
|
|
})
|
|
|
|
if (!isIDsValid) return
|
|
|
|
|
2022-03-30 00:53:05 +08:00
|
|
|
const tracksRaw = db.findMany(Tables.TRACK, ids)
|
|
|
|
|
2022-03-19 17:03:29 +08:00
|
|
|
if (tracksRaw.length !== ids.length) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const tracks = ids.map(id => {
|
|
|
|
const track = tracksRaw.find(t => t.id === Number(id)) as any
|
|
|
|
return JSON.parse(track.json)
|
|
|
|
})
|
|
|
|
|
|
|
|
return {
|
|
|
|
code: 200,
|
|
|
|
songs: tracks,
|
|
|
|
privileges: {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 'album': {
|
2022-03-24 14:23:04 +08:00
|
|
|
if (isNaN(Number(query?.id))) return
|
2022-03-30 00:53:05 +08:00
|
|
|
const data = db.find(Tables.ALBUM, query.id)
|
|
|
|
console.log(data)
|
|
|
|
if (data?.json) return JSON.parse(data.json)
|
2022-03-19 17:03:29 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
case 'playlist/detail': {
|
2022-03-24 14:23:04 +08:00
|
|
|
if (isNaN(Number(query?.id))) return
|
2022-03-30 00:53:05 +08:00
|
|
|
const data = db.find(Tables.PLAYLIST, query.id)
|
|
|
|
if (data?.json) return JSON.parse(data.json)
|
2022-03-19 17:03:29 +08:00
|
|
|
break
|
|
|
|
}
|
2022-03-23 01:21:22 +08:00
|
|
|
case 'artists': {
|
2022-03-24 14:23:04 +08:00
|
|
|
if (isNaN(Number(query?.id))) return
|
2022-03-30 00:53:05 +08:00
|
|
|
const data = db.find(Tables.ARTIST, query.id)
|
|
|
|
if (data?.json) return JSON.parse(data.json)
|
2022-03-23 01:21:22 +08:00
|
|
|
break
|
|
|
|
}
|
2022-03-19 17:03:29 +08:00
|
|
|
case 'artist/album': {
|
2022-03-24 14:23:04 +08:00
|
|
|
if (isNaN(Number(query?.id))) return
|
2022-03-30 00:53:05 +08:00
|
|
|
const data = db.find(Tables.ARTIST_ALBUMS, query.id)
|
|
|
|
if (data?.json) return JSON.parse(data.json)
|
2022-03-19 17:03:29 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getCacheForExpress(api: string, req: Request) {
|
|
|
|
// Get track detail cache
|
|
|
|
if (api === 'song/detail') {
|
|
|
|
const cache = getCache(api, req.query)
|
|
|
|
if (cache) {
|
|
|
|
logger.info(`[cache] Cache hit for ${req.path}`)
|
|
|
|
return cache
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get audio cache if API is song/detail
|
|
|
|
if (api === 'song/url') {
|
2022-03-30 00:53:05 +08:00
|
|
|
const cache = db.find(Tables.AUDIO, Number(req.query.id))
|
2022-03-19 17:03:29 +08:00
|
|
|
if (!cache) return
|
|
|
|
|
|
|
|
const audioFileName = `${cache.id}-${cache.br}.${cache.type}`
|
|
|
|
|
|
|
|
const isAudioFileExists = fs.existsSync(
|
|
|
|
`${app.getPath('userData')}/audio_cache/${audioFileName}`
|
|
|
|
)
|
|
|
|
if (!isAudioFileExists) return
|
|
|
|
|
|
|
|
logger.info(`[cache] Audio cache hit for ${req.path}`)
|
|
|
|
|
|
|
|
return {
|
|
|
|
data: [
|
|
|
|
{
|
|
|
|
source: cache.source,
|
|
|
|
id: cache.id,
|
|
|
|
url: `http://127.0.0.1:42710/yesplaymusic/audio/${audioFileName}`,
|
|
|
|
br: cache.br,
|
|
|
|
size: 0,
|
|
|
|
md5: '',
|
|
|
|
code: 200,
|
|
|
|
expi: 0,
|
|
|
|
type: cache.type,
|
|
|
|
gain: 0,
|
|
|
|
fee: 8,
|
|
|
|
uf: null,
|
|
|
|
payed: 0,
|
|
|
|
flag: 4,
|
|
|
|
canExtend: false,
|
|
|
|
freeTrialInfo: null,
|
|
|
|
level: 'standard',
|
|
|
|
encodeType: cache.type,
|
|
|
|
freeTrialPrivilege: {
|
|
|
|
resConsumable: false,
|
|
|
|
userConsumable: false,
|
|
|
|
listenType: null,
|
|
|
|
},
|
|
|
|
freeTimeTrialPrivilege: {
|
|
|
|
resConsumable: false,
|
|
|
|
userConsumable: false,
|
|
|
|
type: 0,
|
|
|
|
remainTime: 0,
|
|
|
|
},
|
|
|
|
urlSource: 0,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
code: 200,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getAudioCache(fileName: string, res: Response) {
|
|
|
|
if (!fileName) {
|
|
|
|
return res.status(400).send({ error: 'No filename provided' })
|
|
|
|
}
|
|
|
|
const id = Number(fileName.split('-')[0])
|
|
|
|
|
|
|
|
try {
|
|
|
|
const path = `${app.getPath('userData')}/audio_cache/${fileName}`
|
|
|
|
const audio = fs.readFileSync(path)
|
|
|
|
if (audio.byteLength === 0) {
|
2022-03-30 00:53:05 +08:00
|
|
|
db.delete(Tables.AUDIO, id)
|
2022-03-19 17:03:29 +08:00
|
|
|
fs.unlinkSync(path)
|
|
|
|
return res.status(404).send({ error: 'Audio not found' })
|
|
|
|
}
|
|
|
|
res.send(audio)
|
|
|
|
} catch (error) {
|
|
|
|
res.status(500).send({ error })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache audio info local folder
|
|
|
|
export async function cacheAudio(
|
|
|
|
buffer: Buffer,
|
|
|
|
{ id, source }: { id: number; source: string }
|
|
|
|
) {
|
|
|
|
const path = `${app.getPath('userData')}/audio_cache`
|
|
|
|
|
|
|
|
try {
|
|
|
|
fs.statSync(path)
|
|
|
|
} catch (e) {
|
|
|
|
fs.mkdirSync(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
const meta = await musicMetadata.parseBuffer(buffer)
|
|
|
|
const br = meta.format.bitrate
|
|
|
|
const type = {
|
|
|
|
'MPEG 1 Layer 3': 'mp3',
|
|
|
|
'Ogg Vorbis': 'ogg',
|
|
|
|
AAC: 'm4a',
|
|
|
|
FLAC: 'flac',
|
|
|
|
unknown: 'unknown',
|
|
|
|
}[meta.format.codec ?? 'unknown']
|
|
|
|
|
|
|
|
await fs.writeFile(`${path}/${id}-${br}.${type}`, buffer, error => {
|
|
|
|
if (error) {
|
|
|
|
return logger.error(`[cache] cacheAudio failed: ${error}`)
|
|
|
|
}
|
|
|
|
logger.info(`Audio file ${id}-${br}.${type} cached!`)
|
|
|
|
|
2022-03-30 00:53:05 +08:00
|
|
|
db.upsert(Tables.AUDIO, {
|
|
|
|
id,
|
|
|
|
br,
|
|
|
|
type,
|
|
|
|
source,
|
|
|
|
updateAt: Date.now(),
|
2022-03-19 17:03:29 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
logger.info(`[cache] cacheAudio ${id}-${br}.${type}`)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
ipcMain.on('getApiCacheSync', (event, args) => {
|
|
|
|
const { api, query } = args
|
2022-03-30 00:53:05 +08:00
|
|
|
const data = getCache(api, query)
|
2022-03-19 17:03:29 +08:00
|
|
|
event.returnValue = data
|
|
|
|
})
|