From 997343fd5d8da28e87daed55a12ba1199ee8a54d Mon Sep 17 00:00:00 2001 From: KayleCoder Date: Wed, 31 Jul 2024 11:10:58 +0800 Subject: [PATCH] Fix bug Signed-off-by: KayleCoder --- src/merkle/tonsutils.ts | 39 ++++++++++++++------------------------- src/service/storage.ts | 3 +++ 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/merkle/tonsutils.ts b/src/merkle/tonsutils.ts index dd7f29d..1875b45 100644 --- a/src/merkle/tonsutils.ts +++ b/src/merkle/tonsutils.ts @@ -41,8 +41,20 @@ export function sleep(time: number) { const baseUrl = configs.tonStorageUtilsApi export async function getTonBagDetails(bag_id: string) { return fetch(`${baseUrl}/api/v1/details?bag_id=${bag_id}`) - .then((res) => res.json()) - .then((item) => item as BagDetail); + .then((res) => { + if (res.status == 200 || res.status == 404) { + return res.json() + } else { + throw new Error("Call storage api failed") + } + }) + .then((item) => { + const bagId = item.bag_id + if (bagId === undefined) { + return null; + } + return item as BagDetail; + }); } export async function addTonBag({ @@ -67,29 +79,6 @@ export async function addTonBag({ }); } -export async function downloadTonBag(bag_id: string, waitCompleted: boolean = false) { - await addTonBag({ bag_id }); - let bd: BagDetail; - // check header - while (true) { - await sleep(1); - bd = await getTonBagDetails(bag_id); - if (bd.header_loaded) { - break; - } - } - // down all - await addTonBag({ bag_id, files: bd.files.map((f) => f.index), donwload_all: true }); - // check all - while (waitCompleted) { - await sleep(2000); - bd = await getTonBagDetails(bag_id); - if (bd.downloaded == bd.size) { - return true; - } - } - return true -} export async function downloadChildTonBag(bag_id: string) { const bd = await getTonBagDetails(bag_id); diff --git a/src/service/storage.ts b/src/service/storage.ts index 12052b6..cc4149b 100644 --- a/src/service/storage.ts +++ b/src/service/storage.ts @@ -71,6 +71,9 @@ export async function downloadTorrentHeaders() { async function checkDownloadState(torrentHash: string, task: any){ const bagDetail = await getTonBagDetails(torrentHash); + if (bagDetail == null) { + return false; + } if (bagDetail.downloaded === bagDetail.size) { await Task.model.update({ task_state: TaskState.download_torrent_success