diff --git a/src/config/index.ts b/src/config/index.ts index 0371034..4e81c64 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -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)), diff --git a/src/merkle/tonsutils.ts b/src/merkle/tonsutils.ts index 180c77c..280785c 100644 --- a/src/merkle/tonsutils.ts +++ b/src/merkle/tonsutils.ts @@ -1,4 +1,5 @@ import { configs, env } from "../config"; +import fs from "fs"; export type FilesItem = { index: number; @@ -94,11 +95,24 @@ export async function downloadChildTonBag(bag_id: string) { } export async function downloadTonBagSuccess(bag_id: string): Promise { - 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 { diff --git a/src/service/storage.ts b/src/service/storage.ts index b6e1d23..2dc6274 100644 --- a/src/service/storage.ts +++ b/src/service/storage.ts @@ -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) { @@ -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; } @@ -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({ @@ -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; }