Update download fn

This commit is contained in:
PJ 2022-02-16 13:07:57 +01:00
parent a3661d15ba
commit bd86eb4cce
No known key found for this signature in database
GPG Key ID: F345964979FA8971
1 changed files with 10 additions and 21 deletions

View File

@ -3,6 +3,9 @@ import { createReadStream, createWriteStream } from "fs";
import got from "got"; import got from "got";
import { extract } from "tar-fs"; import { extract } from "tar-fs";
import { IPFS_GATEWAY, IPFS_INFURA_API, SKYNET_PORTAL } from "./consts"; 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); const client = new SkynetClient(SKYNET_PORTAL);
@ -23,29 +26,15 @@ export async function isDirectory(cid: string): Promise<boolean> {
return isDir return isDir
} }
export async function download(cid: string, destination: string, directory: boolean): Promise<boolean> { export async function download(cid: string, destination: string, directory: boolean): Promise<void> {
const url = directory ? `${IPFS_INFURA_API}/api/v0/get?arg=${cid}&archive=true` : `${IPFS_GATEWAY}/${cid}`; const url = directory ? `${IPFS_INFURA_API}/api/v0/get?arg=${cid}&archive=true` : `${IPFS_GATEWAY}/${cid}`;
console.log('downloading from url', url) console.log('downloading from url', url)
const pipeline = promisify(stream.pipeline);
return new Promise((resolve, reject) => { await pipeline(
const downloadStream = got.stream(url); got.stream(url),
downloadStream.on("error", (error) => { createWriteStream(destination)
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);
});
} }
export async function extractArchive(src: string, dst: string) { export async function extractArchive(src: string, dst: string) {