feat: implement initial download method

This commit is contained in:
Derrick Hammer 2023-06-23 03:29:14 -04:00
parent d340447aba
commit 7c07211356
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 41 additions and 1 deletions

View File

@ -1 +1,41 @@
export function downloadObject(cid: string) {} import { getActivePortals } from "#portal.js";
import { ErrTuple } from "#types.js";
import { decodeCid, getVerifiableStream } from "@lumeweb/libportal";
const NO_PORTALS_ERROR = [null, "no active portals"] as ErrTuple;
export async function downloadObject(cid: string): Promise<ErrTuple> {
const activePortals = getActivePortals();
if (!activePortals.size) {
return NO_PORTALS_ERROR;
}
for (const portal of activePortals) {
if (!(await portal.isLoggedIn())) {
try {
await portal.register();
} catch {
continue;
}
await portal.login();
}
let stream, proof;
try {
stream = await portal.downloadFile(cid);
proof = await portal.downloadProof(cid);
} catch {
continue;
}
return [
await getVerifiableStream(decodeCid(cid).hash, proof, stream),
null,
];
}
return NO_PORTALS_ERROR;
}