Skip to content

Commit

Permalink
Fix bug
Browse files Browse the repository at this point in the history
Signed-off-by: KayleCoder <[email protected]>
  • Loading branch information
KayleCoder committed Jul 31, 2024
1 parent 294e811 commit 997343f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
39 changes: 14 additions & 25 deletions src/merkle/tonsutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/service/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 997343f

Please sign in to comment.