Created
April 14, 2022 23:35
-
-
Save MCarlomagno/f55238da8a8ed2675e96084770806928 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const untar = await require("js-untar"); | |
async download(file: PersssistFile) { | |
const iterable = this.ipfs.get(file.filePath); | |
var chunks: Uint8Array[] = []; | |
// we need to use a for await for downloading | |
// the buffer in chunks. | |
for await (const b of iterable) { | |
chunks.push(b); | |
} | |
// the result is a tar file, so we need to find a way to | |
// untar the file from the fronted, in my case I did it with untar lib. | |
const tarball = new Blob(chunks, { type: 'application/x-tar' }) | |
const tarAsArrayBuffer = await tarball.arrayBuffer(); | |
const result = await this.untar(tarAsArrayBuffer); | |
// finally we create the blob and download it. | |
const resultFile = new Blob([result[0].buffer], { type: file.fileType }) | |
var url = window.URL.createObjectURL(resultFile); | |
this.downloadURL(url, file.fileName); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment