2020-05-18 22:44:45 +00:00
|
|
|
|
2020-11-21 05:38:10 +00:00
|
|
|
import { saveAs, assertRes } from './utils'
|
2020-11-24 07:51:43 +00:00
|
|
|
import { ScoreInfo } from './scoreinfo'
|
2020-05-18 22:44:45 +00:00
|
|
|
|
2020-11-24 07:59:19 +00:00
|
|
|
const MSCZ_BUF_SYM = Symbol('msczBufferP')
|
2020-05-18 22:44:45 +00:00
|
|
|
|
2020-11-24 07:51:43 +00:00
|
|
|
export const fetchMscz = async (scoreinfo: ScoreInfo): Promise<ArrayBuffer> => {
|
2020-11-24 07:59:19 +00:00
|
|
|
let msczBufferP = scoreinfo.store.get(MSCZ_BUF_SYM) as Promise<ArrayBuffer> | undefined
|
|
|
|
|
2020-05-18 22:44:45 +00:00
|
|
|
if (!msczBufferP) {
|
2020-11-14 18:37:46 +00:00
|
|
|
const url = scoreinfo.msczCidUrl
|
2020-05-18 22:44:45 +00:00
|
|
|
msczBufferP = (async (): Promise<ArrayBuffer> => {
|
2020-11-14 18:37:46 +00:00
|
|
|
const r0 = await fetch(url)
|
2020-11-21 05:38:10 +00:00
|
|
|
assertRes(r0)
|
2020-11-17 16:13:16 +00:00
|
|
|
const { Key } = await r0.json()
|
|
|
|
const r = await fetch(`https://ipfs.infura.io/ipfs/${Key as string}`)
|
2020-11-21 05:38:10 +00:00
|
|
|
assertRes(r)
|
2020-05-18 22:44:45 +00:00
|
|
|
const data = await r.arrayBuffer()
|
|
|
|
return data
|
|
|
|
})()
|
2020-11-24 07:59:19 +00:00
|
|
|
scoreinfo.store.set(MSCZ_BUF_SYM, msczBufferP)
|
2020-05-18 22:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return msczBufferP
|
|
|
|
}
|
|
|
|
|
2020-11-24 07:51:43 +00:00
|
|
|
export const downloadMscz = async (scoreinfo: ScoreInfo): Promise<void> => {
|
|
|
|
const data = new Blob([await fetchMscz(scoreinfo)])
|
2020-05-18 22:44:45 +00:00
|
|
|
const filename = scoreinfo.fileName
|
|
|
|
saveAs(data, `${filename}.mscz`)
|
|
|
|
}
|