2022-03-30 00:53:05 +08:00
|
|
|
import { db, Tables } from './db'
|
2022-04-16 21:14:03 +08:00
|
|
|
import type { FetchTracksResponse } from '@/shared/api/Track'
|
2022-04-09 00:28:37 +08:00
|
|
|
import { app } from 'electron'
|
2022-03-19 17:03:29 +08:00
|
|
|
import { Request, Response } from 'express'
|
2022-04-16 13:30:25 +08:00
|
|
|
import log from './log'
|
2022-03-19 17:03:29 +08:00
|
|
|
import fs from 'fs'
|
|
|
|
import * as musicMetadata from 'music-metadata'
|
2022-04-16 21:14:03 +08:00
|
|
|
import { APIs, APIsParams, APIsResponse } from '../shared/CacheAPIs'
|
2022-05-01 19:53:25 +08:00
|
|
|
import { TablesStructures } from './db'
|
2022-03-19 17:03:29 +08:00
|
|
|
|
2022-04-12 01:48:14 +08:00
|
|
|
class Cache {
|
|
|
|
constructor() {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
2022-04-29 19:16:34 +08:00
|
|
|
async set(api: string, data: any, query: any = {}) {
|
2022-04-12 01:48:14 +08:00
|
|
|
switch (api) {
|
|
|
|
case APIs.UserPlaylist:
|
|
|
|
case APIs.UserAccount:
|
|
|
|
case APIs.Personalized:
|
|
|
|
case APIs.RecommendResource:
|
2022-04-16 21:14:03 +08:00
|
|
|
case APIs.UserAlbums:
|
|
|
|
case APIs.UserArtists:
|
2022-04-12 01:48:14 +08:00
|
|
|
case APIs.Likelist: {
|
|
|
|
if (!data) return
|
|
|
|
db.upsert(Tables.AccountData, {
|
|
|
|
id: api,
|
|
|
|
json: JSON.stringify(data),
|
2022-04-16 16:50:34 +08:00
|
|
|
updatedAt: Date.now(),
|
2022-04-12 01:48:14 +08:00
|
|
|
})
|
|
|
|
break
|
|
|
|
}
|
2022-04-16 21:14:03 +08:00
|
|
|
case APIs.Track: {
|
2022-04-12 01:48:14 +08:00
|
|
|
if (!data.songs) return
|
|
|
|
const tracks = (data as FetchTracksResponse).songs.map(t => ({
|
|
|
|
id: t.id,
|
|
|
|
json: JSON.stringify(t),
|
2022-03-30 16:54:11 +08:00
|
|
|
updatedAt: Date.now(),
|
|
|
|
}))
|
2022-04-12 01:48:14 +08:00
|
|
|
db.upsertMany(Tables.Track, tracks)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
case APIs.Album: {
|
|
|
|
if (!data.album) return
|
|
|
|
data.album.songs = data.songs
|
|
|
|
db.upsert(Tables.Album, {
|
|
|
|
id: data.album.id,
|
|
|
|
json: JSON.stringify(data.album),
|
|
|
|
updatedAt: Date.now(),
|
|
|
|
})
|
|
|
|
break
|
|
|
|
}
|
2022-04-16 21:14:03 +08:00
|
|
|
case APIs.Playlist: {
|
2022-04-12 01:48:14 +08:00
|
|
|
if (!data.playlist) return
|
|
|
|
db.upsert(Tables.Playlist, {
|
|
|
|
id: data.playlist.id,
|
|
|
|
json: JSON.stringify(data),
|
|
|
|
updatedAt: Date.now(),
|
|
|
|
})
|
|
|
|
break
|
|
|
|
}
|
2022-04-16 21:14:03 +08:00
|
|
|
case APIs.Artist: {
|
2022-04-12 01:48:14 +08:00
|
|
|
if (!data.artist) return
|
|
|
|
db.upsert(Tables.Artist, {
|
|
|
|
id: data.artist.id,
|
|
|
|
json: JSON.stringify(data),
|
|
|
|
updatedAt: Date.now(),
|
|
|
|
})
|
|
|
|
break
|
|
|
|
}
|
|
|
|
case APIs.ArtistAlbum: {
|
|
|
|
if (!data.hotAlbums) return
|
|
|
|
db.createMany(
|
|
|
|
Tables.Album,
|
|
|
|
data.hotAlbums.map((a: Album) => ({
|
|
|
|
id: a.id,
|
|
|
|
json: JSON.stringify(a),
|
|
|
|
updatedAt: Date.now(),
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
const modifiedData = {
|
|
|
|
...data,
|
|
|
|
hotAlbums: data.hotAlbums.map((a: Album) => a.id),
|
|
|
|
}
|
|
|
|
db.upsert(Tables.ArtistAlbum, {
|
|
|
|
id: data.artist.id,
|
|
|
|
json: JSON.stringify(modifiedData),
|
|
|
|
updatedAt: Date.now(),
|
|
|
|
})
|
|
|
|
break
|
|
|
|
}
|
|
|
|
case APIs.Lyric: {
|
|
|
|
if (!data.lrc) return
|
|
|
|
db.upsert(Tables.Lyric, {
|
|
|
|
id: query.id,
|
|
|
|
json: JSON.stringify(data),
|
|
|
|
updatedAt: Date.now(),
|
|
|
|
})
|
|
|
|
break
|
|
|
|
}
|
|
|
|
case APIs.CoverColor: {
|
|
|
|
if (!data.id || !data.color) return
|
|
|
|
if (/^#([a-fA-F0-9]){3}$|[a-fA-F0-9]{6}$/.test(data.color) === false) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
db.upsert(Tables.CoverColor, {
|
|
|
|
id: data.id,
|
|
|
|
color: data.color,
|
|
|
|
})
|
2022-03-30 16:54:11 +08:00
|
|
|
}
|
2022-04-05 23:02:59 +08:00
|
|
|
}
|
2022-03-19 17:03:29 +08:00
|
|
|
}
|
|
|
|
|
2022-04-16 21:14:03 +08:00
|
|
|
get<T extends keyof APIsParams>(api: T, params: any): any {
|
2022-04-12 01:48:14 +08:00
|
|
|
switch (api) {
|
|
|
|
case APIs.UserPlaylist:
|
|
|
|
case APIs.UserAccount:
|
|
|
|
case APIs.Personalized:
|
|
|
|
case APIs.RecommendResource:
|
|
|
|
case APIs.Likelist: {
|
|
|
|
const data = db.find(Tables.AccountData, api)
|
|
|
|
if (data?.json) return JSON.parse(data.json)
|
|
|
|
break
|
|
|
|
}
|
2022-04-16 21:14:03 +08:00
|
|
|
case APIs.Track: {
|
|
|
|
const ids: number[] = params?.ids
|
|
|
|
.split(',')
|
|
|
|
.map((id: string) => Number(id))
|
2022-04-12 01:48:14 +08:00
|
|
|
if (ids.length === 0) return
|
2022-03-24 14:23:04 +08:00
|
|
|
|
2022-04-16 21:14:03 +08:00
|
|
|
if (ids.includes(NaN)) return
|
2022-03-24 14:23:04 +08:00
|
|
|
|
2022-04-12 01:48:14 +08:00
|
|
|
const tracksRaw = db.findMany(Tables.Track, ids)
|
2022-03-30 00:53:05 +08:00
|
|
|
|
2022-04-12 01:48:14 +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)
|
|
|
|
})
|
2022-03-30 16:54:11 +08:00
|
|
|
return {
|
|
|
|
code: 200,
|
2022-04-12 01:48:14 +08:00
|
|
|
songs: tracks,
|
|
|
|
privileges: {},
|
2022-03-30 16:54:11 +08:00
|
|
|
}
|
2022-04-12 01:48:14 +08:00
|
|
|
}
|
|
|
|
case APIs.Album: {
|
2022-04-16 21:14:03 +08:00
|
|
|
if (isNaN(Number(params?.id))) return
|
|
|
|
const data = db.find(Tables.Album, params.id)
|
2022-04-12 01:48:14 +08:00
|
|
|
if (data?.json)
|
|
|
|
return {
|
|
|
|
resourceState: true,
|
|
|
|
songs: [],
|
|
|
|
code: 200,
|
|
|
|
album: JSON.parse(data.json),
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2022-04-16 21:14:03 +08:00
|
|
|
case APIs.Playlist: {
|
|
|
|
if (isNaN(Number(params?.id))) return
|
|
|
|
const data = db.find(Tables.Playlist, params.id)
|
2022-04-12 01:48:14 +08:00
|
|
|
if (data?.json) return JSON.parse(data.json)
|
|
|
|
break
|
|
|
|
}
|
2022-04-16 21:14:03 +08:00
|
|
|
case APIs.Artist: {
|
|
|
|
if (isNaN(Number(params?.id))) return
|
|
|
|
const data = db.find(Tables.Artist, params.id)
|
2022-04-12 01:48:14 +08:00
|
|
|
if (data?.json) return JSON.parse(data.json)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
case APIs.ArtistAlbum: {
|
2022-04-16 21:14:03 +08:00
|
|
|
if (isNaN(Number(params?.id))) return
|
2022-03-30 16:54:11 +08:00
|
|
|
|
2022-04-16 21:14:03 +08:00
|
|
|
const artistAlbumsRaw = db.find(Tables.ArtistAlbum, params.id)
|
2022-04-12 01:48:14 +08:00
|
|
|
if (!artistAlbumsRaw?.json) return
|
|
|
|
const artistAlbums = JSON.parse(artistAlbumsRaw.json)
|
2022-03-30 16:54:11 +08:00
|
|
|
|
2022-04-12 01:48:14 +08:00
|
|
|
const albumsRaw = db.findMany(Tables.Album, artistAlbums.hotAlbums)
|
|
|
|
if (albumsRaw.length !== artistAlbums.hotAlbums.length) return
|
|
|
|
const albums = albumsRaw.map(a => JSON.parse(a.json))
|
2022-03-30 16:54:11 +08:00
|
|
|
|
2022-04-12 01:48:14 +08:00
|
|
|
artistAlbums.hotAlbums = artistAlbums.hotAlbums.map((id: number) =>
|
|
|
|
albums.find(a => a.id === id)
|
|
|
|
)
|
|
|
|
return artistAlbums
|
|
|
|
}
|
|
|
|
case APIs.Lyric: {
|
2022-04-16 21:14:03 +08:00
|
|
|
if (isNaN(Number(params?.id))) return
|
|
|
|
const data = db.find(Tables.Lyric, params.id)
|
2022-04-12 01:48:14 +08:00
|
|
|
if (data?.json) return JSON.parse(data.json)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
case APIs.CoverColor: {
|
2022-04-16 21:14:03 +08:00
|
|
|
if (isNaN(Number(params?.id))) return
|
|
|
|
return db.find(Tables.CoverColor, params.id)?.color
|
2022-04-12 01:48:14 +08:00
|
|
|
}
|
2022-04-05 23:02:59 +08:00
|
|
|
}
|
2022-03-19 17:03:29 +08:00
|
|
|
}
|
|
|
|
|
2022-04-12 01:48:14 +08:00
|
|
|
getForExpress(api: string, req: Request) {
|
|
|
|
// Get track detail cache
|
2022-04-16 21:14:03 +08:00
|
|
|
if (api === APIs.Track) {
|
2022-04-12 01:48:14 +08:00
|
|
|
const cache = this.get(api, req.query)
|
|
|
|
if (cache) {
|
2022-04-16 13:30:25 +08:00
|
|
|
log.debug(`[cache] Cache hit for ${req.path}`)
|
2022-04-12 01:48:14 +08:00
|
|
|
return cache
|
|
|
|
}
|
2022-03-19 17:03:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 01:48:14 +08:00
|
|
|
getAudio(fileName: string, res: Response) {
|
|
|
|
if (!fileName) {
|
|
|
|
return res.status(400).send({ error: 'No filename provided' })
|
|
|
|
}
|
|
|
|
const id = Number(fileName.split('-')[0])
|
2022-03-19 17:03:29 +08:00
|
|
|
|
2022-04-12 01:48:14 +08:00
|
|
|
try {
|
|
|
|
const path = `${app.getPath('userData')}/audio_cache/${fileName}`
|
|
|
|
const audio = fs.readFileSync(path)
|
|
|
|
if (audio.byteLength === 0) {
|
|
|
|
db.delete(Tables.Audio, id)
|
|
|
|
fs.unlinkSync(path)
|
|
|
|
return res.status(404).send({ error: 'Audio not found' })
|
|
|
|
}
|
2022-04-30 02:08:25 +08:00
|
|
|
res
|
|
|
|
.status(206)
|
|
|
|
.setHeader('Accept-Ranges', 'bytes')
|
|
|
|
.setHeader('Connection', 'keep-alive')
|
2022-05-01 19:53:25 +08:00
|
|
|
.setHeader(
|
|
|
|
'Content-Range',
|
|
|
|
`bytes 0-${audio.byteLength - 1}/${audio.byteLength}`
|
|
|
|
)
|
2022-04-30 02:08:25 +08:00
|
|
|
.send(audio)
|
2022-04-12 01:48:14 +08:00
|
|
|
} catch (error) {
|
|
|
|
res.status(500).send({ error })
|
2022-03-19 17:03:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-01 19:53:25 +08:00
|
|
|
async setAudio(buffer: Buffer, { id, url }: { id: number; url: string }) {
|
2022-04-12 01:48:14 +08:00
|
|
|
const path = `${app.getPath('userData')}/audio_cache`
|
2022-03-19 17:03:29 +08:00
|
|
|
|
2022-04-12 01:48:14 +08:00
|
|
|
try {
|
|
|
|
fs.statSync(path)
|
|
|
|
} catch (e) {
|
|
|
|
fs.mkdirSync(path)
|
|
|
|
}
|
2022-03-19 17:03:29 +08:00
|
|
|
|
2022-04-12 01:48:14 +08:00
|
|
|
const meta = await musicMetadata.parseBuffer(buffer)
|
2022-05-01 19:53:25 +08:00
|
|
|
const br =
|
|
|
|
meta?.format?.codec === 'OPUS' ? 165000 : meta.format.bitrate ?? 0
|
|
|
|
const type =
|
|
|
|
{
|
|
|
|
'MPEG 1 Layer 3': 'mp3',
|
|
|
|
'Ogg Vorbis': 'ogg',
|
|
|
|
AAC: 'm4a',
|
|
|
|
FLAC: 'flac',
|
|
|
|
OPUS: 'opus',
|
|
|
|
}[meta.format.codec ?? ''] ?? 'unknown'
|
|
|
|
|
|
|
|
let source: TablesStructures[Tables.Audio]['source'] = 'unknown'
|
|
|
|
if (url.includes('googlevideo.com')) source = 'youtube'
|
|
|
|
if (url.includes('126.net')) source = 'netease'
|
|
|
|
if (url.includes('migu.cn')) source = 'migu'
|
|
|
|
if (url.includes('kuwo.cn')) source = 'kuwo'
|
|
|
|
if (url.includes('bilivideo.com')) source = 'bilibili'
|
|
|
|
// TODO: missing kugou qq joox
|
2022-03-19 17:03:29 +08:00
|
|
|
|
2022-05-01 19:53:25 +08:00
|
|
|
fs.writeFile(`${path}/${id}-${br}.${type}`, buffer, error => {
|
2022-04-12 01:48:14 +08:00
|
|
|
if (error) {
|
2022-04-16 13:30:25 +08:00
|
|
|
return log.error(`[cache] cacheAudio failed: ${error}`)
|
2022-04-12 01:48:14 +08:00
|
|
|
}
|
2022-04-16 13:30:25 +08:00
|
|
|
log.info(`Audio file ${id}-${br}.${type} cached!`)
|
2022-03-19 17:03:29 +08:00
|
|
|
|
2022-04-12 01:48:14 +08:00
|
|
|
db.upsert(Tables.Audio, {
|
|
|
|
id,
|
|
|
|
br,
|
2022-05-01 19:53:25 +08:00
|
|
|
type: type as TablesStructures[Tables.Audio]['type'],
|
2022-04-12 01:48:14 +08:00
|
|
|
source,
|
2022-05-01 19:53:25 +08:00
|
|
|
updatedAt: Date.now(),
|
2022-04-12 01:48:14 +08:00
|
|
|
})
|
2022-03-19 17:03:29 +08:00
|
|
|
|
2022-04-16 13:30:25 +08:00
|
|
|
log.info(`[cache] cacheAudio ${id}-${br}.${type}`)
|
2022-04-12 01:48:14 +08:00
|
|
|
})
|
|
|
|
}
|
2022-03-19 17:03:29 +08:00
|
|
|
}
|
2022-04-12 01:48:14 +08:00
|
|
|
|
|
|
|
export default new Cache()
|