musescore-downloader/src/file.ts

94 lines
2.6 KiB
TypeScript
Raw Normal View History

2020-10-26 18:53:55 +00:00
/* eslint-disable no-extend-native */
import scoreinfo from './scoreinfo'
2020-11-17 17:09:49 +00:00
import { onPackLoad, webpackContext } from './webpack-hook'
type FileType = 'img' | 'mp3' | 'midi'
2020-11-18 23:35:45 +00:00
const AUTH_REG = /,((\d+\.\..+?)?function\(\)\{var \w=Array.prototype.slice.*?)\)(\[|\.then)/
const PACK_ID_REG = /\((\{.*?"\})\[\w\]\|\|\w\)/
const packIdPromise: Promise<Record<FileType, (string | number)>> = webpackContext.then((ctx) => {
const ids = {
img: '9',
midi: '',
mp3: '',
}
try {
const fn = ctx.e.toString()
const packsData = fn.match(PACK_ID_REG)[1] as string
// eslint-disable-next-line no-new-func, @typescript-eslint/no-implied-eval
const packs = Function(`return (${packsData})`)() as { [id: string]: string }
Object.entries(packs).forEach(([id, name]) => {
if (name.includes('audio') && !ids['mp3']) ids['mp3'] = id
if (name.includes('piano_roll') && !ids['midi']) ids['midi'] = id
})
} catch (err) {
console.error(err)
}
return ids
})
2020-11-17 17:09:49 +00:00
2020-10-22 20:52:33 +00:00
/**
* I know this is super hacky.
*/
2020-11-18 23:35:45 +00:00
const magicHookConstr = async (type: FileType) => {
const packId = await packIdPromise
2020-11-17 17:09:49 +00:00
// request pack
// eslint-disable-next-line no-void, @typescript-eslint/no-unsafe-return
2020-11-18 23:35:45 +00:00
void webpackContext.then((ctx) => ctx.e(packId[type]))
2020-11-17 17:09:49 +00:00
return new Promise<string>((resolve) => {
onPackLoad((pack) => {
2020-11-18 23:35:45 +00:00
if (pack[0].includes(packId[type]) || pack[0].includes(+packId[type])) {
2020-11-17 17:09:49 +00:00
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]
2020-11-18 19:00:43 +00:00
try {
// eslint-disable-next-line no-new-func, @typescript-eslint/no-implied-eval
const magic = Function(`return (${code})`)()
resolve(magic)
} catch (err) {
console.error(err)
}
2020-11-18 18:57:56 +00:00
}
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-11-13 23:27:28 +00:00
const { info } = await r.json()
return info.url as string
}