musescore-downloader/src/mscz.ts

27 lines
719 B
TypeScript
Raw Normal View History

2020-05-18 22:44:45 +00:00
import { saveAs } from './utils'
import scoreinfo from './scoreinfo'
let msczBufferP: Promise<ArrayBuffer> | undefined
export const fetchMscz = async (): Promise<ArrayBuffer> => {
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-17 16:13:16 +00:00
const { Key } = await r0.json()
const r = await fetch(`https://ipfs.infura.io/ipfs/${Key as string}`)
2020-05-18 22:44:45 +00:00
const data = await r.arrayBuffer()
return data
})()
}
return msczBufferP
}
export const downloadMscz = async (): Promise<void> => {
const data = new Blob([await fetchMscz()])
const filename = scoreinfo.fileName
saveAs(data, `${filename}.mscz`)
}