From bd86eb4cce426e75204f171bd7111eb4c731759c Mon Sep 17 00:00:00 2001 From: PJ Date: Wed, 16 Feb 2022 13:07:57 +0100 Subject: [PATCH] Update download fn --- packages/ipfs-api/src/utils.ts | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/packages/ipfs-api/src/utils.ts b/packages/ipfs-api/src/utils.ts index 612cfc27..c6a364c2 100644 --- a/packages/ipfs-api/src/utils.ts +++ b/packages/ipfs-api/src/utils.ts @@ -3,6 +3,9 @@ import { createReadStream, createWriteStream } from "fs"; import got from "got"; import { extract } from "tar-fs"; import { IPFS_GATEWAY, IPFS_INFURA_API, SKYNET_PORTAL } from "./consts"; +import { promisify } from 'node:util'; +import stream from 'node:stream'; + const client = new SkynetClient(SKYNET_PORTAL); @@ -23,29 +26,15 @@ export async function isDirectory(cid: string): Promise { return isDir } -export async function download(cid: string, destination: string, directory: boolean): Promise { +export async function download(cid: string, destination: string, directory: boolean): Promise { const url = directory ? `${IPFS_INFURA_API}/api/v0/get?arg=${cid}&archive=true` : `${IPFS_GATEWAY}/${cid}`; + console.log('downloading from url', url) - - return new Promise((resolve, reject) => { - const downloadStream = got.stream(url); - downloadStream.on("error", (error) => { - console.error(`Download fail ${url}: ${error.message}`); - }); - - const fileWriterStream = createWriteStream(destination); - fileWriterStream - .on("error", (error) => { - console.error(`Could not write file to system: ${error.message}`); - reject(error); - }) - .on("finish", () => { - console.log(`File downloaded to ${destination}`); - resolve(true); - }); - - downloadStream.pipe(fileWriterStream); - }); + const pipeline = promisify(stream.pipeline); + await pipeline( + got.stream(url), + createWriteStream(destination) + ); } export async function extractArchive(src: string, dst: string) {