Download multiple times if filesize is 0

This commit is contained in:
PJ 2022-02-16 13:21:46 +01:00
parent bd86eb4cce
commit 72cad4ff5c
No known key found for this signature in database
GPG Key ID: F345964979FA8971
2 changed files with 18 additions and 13 deletions

View File

@ -130,12 +130,6 @@ async function reuploadFile(cid: string, recordsDB: Collection<IRecord>): 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 {

View File

@ -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<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}`;
console.log('downloading from url', url)
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) {