feat: check if downloading data exists

This commit is contained in:
Hachi-R 2024-11-06 19:29:51 -03:00
parent f555890b4c
commit 5d59a15bbf

View file

@ -68,66 +68,69 @@ export class PythonInstance {
public static async getStatus() { public static async getStatus() {
const response = await this.rpc.get<LibtorrentPayload | null>("/status"); const response = await this.rpc.get<LibtorrentPayload | null>("/status");
if (response.data?.downloading === null) return null;
try { if (response.data?.downloading) {
const { try {
progress, const {
numPeers, progress,
numSeeds, numPeers,
downloadSpeed, numSeeds,
bytesDownloaded, downloadSpeed,
fileSize,
folderName,
status,
gameId,
} = response.data?.downloading!;
this.downloadingGameId = gameId;
const isDownloadingMetadata =
status === LibtorrentStatus.DownloadingMetadata;
const isCheckingFiles = status === LibtorrentStatus.CheckingFiles;
if (!isDownloadingMetadata && !isCheckingFiles) {
const update: QueryDeepPartialEntity<Game> = {
bytesDownloaded, bytesDownloaded,
fileSize, fileSize,
folderName,
status,
gameId,
} = response.data.downloading;
this.downloadingGameId = gameId;
const isDownloadingMetadata =
status === LibtorrentStatus.DownloadingMetadata;
const isCheckingFiles = status === LibtorrentStatus.CheckingFiles;
if (!isDownloadingMetadata && !isCheckingFiles) {
const update: QueryDeepPartialEntity<Game> = {
bytesDownloaded,
fileSize,
progress,
status: "active",
};
await gameRepository.update(
{ id: gameId },
{
...update,
folderName,
}
);
}
if (
progress === 1 &&
!isCheckingFiles &&
status !== LibtorrentStatus.Seeding
) {
this.downloadingGameId = -1;
}
return {
numPeers,
numSeeds,
downloadSpeed,
timeRemaining: calculateETA(fileSize, bytesDownloaded, downloadSpeed),
isDownloadingMetadata,
isCheckingFiles,
progress, progress,
status: "active", gameId,
}; } as DownloadProgress;
} catch (err) {
await gameRepository.update( return null;
{ id: gameId },
{
...update,
folderName,
}
);
} }
if (
progress === 1 &&
!isCheckingFiles &&
status !== LibtorrentStatus.Seeding
) {
this.downloadingGameId = -1;
}
return {
numPeers,
numSeeds,
downloadSpeed,
timeRemaining: calculateETA(fileSize, bytesDownloaded, downloadSpeed),
isDownloadingMetadata,
isCheckingFiles,
progress,
gameId,
} as DownloadProgress;
} catch (err) {
return null;
} }
return null;
} }
static async pauseDownload() { static async pauseDownload() {
@ -136,7 +139,7 @@ export class PythonInstance {
action: "pause", action: "pause",
game_id: this.downloadingGameId, game_id: this.downloadingGameId,
} as PauseDownloadPayload) } as PauseDownloadPayload)
.catch(() => {}); .catch(() => { });
this.downloadingGameId = -1; this.downloadingGameId = -1;
} }
@ -168,7 +171,7 @@ export class PythonInstance {
action: "cancel", action: "cancel",
game_id: gameId, game_id: gameId,
} as CancelDownloadPayload) } as CancelDownloadPayload)
.catch(() => {}); .catch(() => { });
this.downloadingGameId = -1; this.downloadingGameId = -1;
} }