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-17 17:09:49 +00:00
|
|
|
const AUTH_REG = /[0-9a-f]{40}/
|
|
|
|
enum PACK_ID {
|
|
|
|
img = 9,
|
|
|
|
midi = 118,
|
|
|
|
mp3 = 74,
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
void webpackContext.then((ctx) => ctx.e(PACK_ID[type])).then(console.log)
|
|
|
|
|
|
|
|
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)
|
|
|
|
if (m && m[0]) resolve(m[0])
|
|
|
|
})
|
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:29:34 +00:00
|
|
|
img: Promise.resolve('8c022bdef45341074ce876ae57a48f64b86cdcf5'),
|
|
|
|
midi: Promise.resolve('38fb9efaae51b0c83b5bb5791a698b48292129e7'),
|
|
|
|
mp3: Promise.resolve('63794e5461e4cfa046edfbdddfccc1ac16daffd2'),
|
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
|
|
|
}
|