2020-10-26 18:53:55 +00:00
|
|
|
/* eslint-disable no-extend-native */
|
2020-10-18 11:21:25 +00:00
|
|
|
|
|
|
|
import scoreinfo from './scoreinfo'
|
2020-11-17 17:09:49 +00:00
|
|
|
import { onPackLoad, webpackContext } from './webpack-hook'
|
2020-10-18 11:21:25 +00:00
|
|
|
|
|
|
|
type FileType = 'img' | 'mp3' | 'midi'
|
|
|
|
|
2020-11-18 18:57:56 +00:00
|
|
|
const AUTH_REG = /,(function\(\)\{var \w=Array.prototype.slice.*?)\)\[/
|
2020-11-17 17:09:49 +00:00
|
|
|
enum PACK_ID {
|
2020-11-18 18:57:56 +00:00
|
|
|
img = 'F/w+',
|
|
|
|
midi = 'yihj',
|
|
|
|
mp3 = '2oYV',
|
2020-11-17 17:09:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 20:52:33 +00:00
|
|
|
/**
|
|
|
|
* I know this is super hacky.
|
|
|
|
*/
|
2020-11-17 17:09:49 +00:00
|
|
|
const magicHookConstr = (type: FileType) => {
|
|
|
|
// request pack
|
|
|
|
// eslint-disable-next-line no-void, @typescript-eslint/no-unsafe-return
|
2020-11-18 18:57:56 +00:00
|
|
|
void webpackContext.then((ctx) => ctx.e(PACK_ID[type]))
|
2020-11-17 17:09:49 +00:00
|
|
|
|
|
|
|
return new Promise<string>((resolve) => {
|
|
|
|
onPackLoad((pack) => {
|
|
|
|
if (pack[0].includes(PACK_ID[type])) {
|
|
|
|
Object.values(pack[1]).forEach((mod) => {
|
|
|
|
const m = mod.toString().match(AUTH_REG)
|
2020-11-18 18:57:56 +00:00
|
|
|
if (m) {
|
|
|
|
const code = m[1]
|
|
|
|
// eslint-disable-next-line no-new-func, @typescript-eslint/no-implied-eval
|
|
|
|
const magic = Function(`return (${code})`)()
|
|
|
|
resolve(magic)
|
|
|
|
}
|
2020-11-17 17:09:49 +00:00
|
|
|
})
|
2020-11-13 23:27:28 +00:00
|
|
|
}
|
2020-11-17 17:09:49 +00:00
|
|
|
})
|
2020-11-05 21:47:50 +00:00
|
|
|
})
|
2020-11-17 17:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const magics: Record<FileType, Promise<string>> = {
|
2020-11-18 18:57:56 +00:00
|
|
|
img: magicHookConstr('img'),
|
|
|
|
midi: magicHookConstr('midi'),
|
|
|
|
mp3: magicHookConstr('mp3'),
|
2020-11-17 17:09:49 +00:00
|
|
|
}
|
2020-10-22 20:52:33 +00:00
|
|
|
|
2020-11-13 23:27:28 +00:00
|
|
|
const getApiUrl = (type: FileType, index: number): string => {
|
2020-11-17 17:09:49 +00:00
|
|
|
return `/api/jmuse?id=${scoreinfo.id}&type=${type}&index=${index}&v2=1`
|
2020-11-13 23:27:28 +00:00
|
|
|
}
|
2020-10-22 20:52:33 +00:00
|
|
|
|
2020-11-13 23:27:28 +00:00
|
|
|
const getApiAuth = async (type: FileType, index: number): Promise<string> => {
|
2020-11-17 17:09:49 +00:00
|
|
|
// eslint-disable-next-line no-void
|
|
|
|
void index
|
|
|
|
return magics[type]
|
2020-11-13 23:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getFileUrl = async (type: FileType, index = 0): Promise<string> => {
|
|
|
|
const url = getApiUrl(type, index)
|
|
|
|
const auth = await getApiAuth(type, index)
|
|
|
|
|
|
|
|
const r = await fetch(url, {
|
|
|
|
headers: {
|
|
|
|
Authorization: auth,
|
|
|
|
},
|
2020-10-18 11:21:25 +00:00
|
|
|
})
|
2020-11-13 23:27:28 +00:00
|
|
|
|
|
|
|
const { info } = await r.json()
|
|
|
|
return info.url as string
|
2020-10-18 11:21:25 +00:00
|
|
|
}
|