Skip to content

Commit

Permalink
Adjust check file download_all success code
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 2395a06 commit f7c558e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export const configs = {
ton: {
host: getEnvOrExit("TON_RPC_URL", "", !isDev),
tonbag_address: getEnvOrExit("TON_BAG_ADDRESS", "EQBOOMNqG0rvNm6vFGfR4qZl48BTDw_gYefVI4DQ70t9GoPC"),
downloadPath: getEnvOrExit("TON_FILE_DOWNLOAD_PATH", "/root/downloads"),
downloadPath: getEnvOrExit("TON_FILE_DOWNLOAD_PATH", "downloads"),
storageMountPath: getEnvOrExit("TON_FILE_MOUNT_PATH", "/root"),
},
task: {
minReward: BigInt(getEnvOrExit("TASK_MIN_REWARD", "0", false)),
Expand Down
22 changes: 18 additions & 4 deletions src/merkle/tonsutils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { configs, env } from "../config";
import fs from "fs";

export type FilesItem = {
index: number;
Expand Down Expand Up @@ -94,11 +95,24 @@ export async function downloadChildTonBag(bag_id: string) {
}

export async function downloadTonBagSuccess(bag_id: string): Promise<boolean> {
const bd = await getTonBagDetails(bag_id);
if (bd) {
return bd.downloaded == bd.size;
const bagDetail = await getTonBagDetails(bag_id);
let allExist = true;
for (const file of bagDetail.files) {
const filePath = getStorageRealFilePath(bagDetail, file);
if (!fs.existsSync(filePath)) {
allExist = false;
return allExist;
}
}
return allExist;
}

export function getStorageRealFilePath(bagDetail: BagDetail, file: FilesItem): string {
if (bagDetail.path.startsWith("/")) {
return `${bagDetail.path}/${bagDetail.dir_name}${file.name}`;
} else {
return `${configs.ton.storageMountPath}/${bagDetail.path}/${bagDetail.dir_name}${file.name}`;
}
return false;
}

export async function downloadHeaderSuccess(bag_id: string): Promise<boolean> {
Expand Down
45 changes: 30 additions & 15 deletions src/service/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import {TaskState} from "../type/common";
import {sleep} from "../util/common";
import {Order} from "../dao/order";
import {
addTonBag,
addTonBag, BagDetail,
downloadChildTonBag,
downloadHeaderSuccess,
downloadTonBagSuccess,
downloadTonBagSuccess, FilesItem, getStorageRealFilePath,
getTonBagDetails
} from "../merkle/tonsutils";
import {logger} from "../util/logger";
import {Op} from "sequelize";
import {configs} from "../config";
import * as fs from "fs";

export async function downloadTorrentHeaders() {
while(true) {
Expand All @@ -34,7 +35,7 @@ export async function downloadTorrentHeaders() {
const torrentHash = order.torrent_hash;

try {
const result = await checkDownloadState(torrentHash, task);
const result = await correctDownloadFileState(torrentHash, task);
if (result) {
continue;
}
Expand Down Expand Up @@ -71,33 +72,47 @@ export async function downloadTorrentHeaders() {
}
}

async function checkDownloadState(torrentHash: string, task: any){
async function correctDownloadFileState(torrentHash: string, task: any, header: boolean = true){
const bagDetail = await getTonBagDetails(torrentHash);
if (bagDetail == null) {
return false;
} else if (bagDetail.header_loaded === true) {
if (bagDetail.downloaded > 0 && bagDetail.downloaded === bagDetail.size) {
if (header) {
await Task.model.update({
task_state: TaskState.download_torrent_success
task_state: TaskState.download_torrent_header_success
}, {
where: {
id: task.id
}
});
return true;
return true
}
await Task.model.update({
task_state: TaskState.download_torrent_header_success
}, {
where: {
id: task.id
if (bagDetail.downloaded > 0 && bagDetail.downloaded === bagDetail.size) {
// check files exist
let allExist = true;
for (const file of bagDetail.files) {
const filePath = getStorageRealFilePath(bagDetail, file);
if (!fs.existsSync(filePath)) {
allExist = false;
}
}
});
return true;
if (allExist) {
await Task.model.update({
task_state: TaskState.download_torrent_success
}, {
where: {
id: task.id
}
});
return allExist;
}
}
}
return false;
}



export async function downloadChildFiles() {
while (true) {
const tasks = await Task.model.findAll({
Expand All @@ -118,7 +133,7 @@ export async function downloadChildFiles() {
}))[0];
const torrentHash = order.torrent_hash;
try {
const result = await checkDownloadState(torrentHash, task);
const result = await correctDownloadFileState(torrentHash, task, false);
if (result) {
continue;
}
Expand Down

0 comments on commit f7c558e

Please sign in to comment.