Skip to content

Instantly share code, notes, and snippets.

@MCarlomagno
Created April 14, 2022 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MCarlomagno/f55238da8a8ed2675e96084770806928 to your computer and use it in GitHub Desktop.
Save MCarlomagno/f55238da8a8ed2675e96084770806928 to your computer and use it in GitHub Desktop.
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