From 72cad4ff5ceee92999259b80850d54c6c0a4d635 Mon Sep 17 00:00:00 2001 From: PJ Date: Wed, 16 Feb 2022 13:21:46 +0100 Subject: [PATCH] Download multiple times if filesize is 0 --- packages/ipfs-api/src/server.ts | 6 ------ packages/ipfs-api/src/utils.ts | 25 ++++++++++++++++++------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/packages/ipfs-api/src/server.ts b/packages/ipfs-api/src/server.ts index 2863954b..f6345117 100644 --- a/packages/ipfs-api/src/server.ts +++ b/packages/ipfs-api/src/server.ts @@ -130,12 +130,6 @@ async function reuploadFile(cid: string, recordsDB: Collection): Promis throw error; } - // sleep 3s - // TODO: hacky addition because I saw some 0-byte uploads - await sleep(3000) - const fileStats = fs.statSync(filePath) - console.log('file size', fileStats.size) - // upload the file let skylink; try { diff --git a/packages/ipfs-api/src/utils.ts b/packages/ipfs-api/src/utils.ts index c6a364c2..086ff0b7 100644 --- a/packages/ipfs-api/src/utils.ts +++ b/packages/ipfs-api/src/utils.ts @@ -1,5 +1,5 @@ import { SkynetClient } from "@skynetlabs/skynet-nodejs"; -import { createReadStream, createWriteStream } from "fs"; +import { createReadStream, createWriteStream, statSync } from "fs"; import got from "got"; import { extract } from "tar-fs"; import { IPFS_GATEWAY, IPFS_INFURA_API, SKYNET_PORTAL } from "./consts"; @@ -29,12 +29,23 @@ export async function isDirectory(cid: string): 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) - const pipeline = promisify(stream.pipeline); - await pipeline( - got.stream(url), - createWriteStream(destination) - ); + let fileSize = 0; + let attempts = 0; + + while (fileSize === 0 && attempts <= 3) { + attempts++ + + console.log('downloading from url', url, ' attempt ', attempts) + const pipeline = promisify(stream.pipeline); + await pipeline( + got.stream(url), + createWriteStream(destination) + ); + + const fileStats = statSync(destination) + fileSize = fileStats.size + console.log('file size', fileSize) + } } export async function extractArchive(src: string, dst: string) {