From fdbce4abec36e6ecaa614a2c68664b9ea82a8539 Mon Sep 17 00:00:00 2001 From: banbxio Date: Wed, 2 Apr 2025 01:57:47 +0800 Subject: [PATCH] feat(fetch): enhance download task management with status and error tracking --- frontend/src/api/fetch.tsx | 4 ++- frontend/src/components/prompts/Fetch.vue | 40 ++++++++++++++++++----- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/frontend/src/api/fetch.tsx b/frontend/src/api/fetch.tsx index 68dc02c3..780d26b3 100644 --- a/frontend/src/api/fetch.tsx +++ b/frontend/src/api/fetch.tsx @@ -20,7 +20,7 @@ export async function fetchUrlFile( return taskID; } -type DownloadTask = { +export type DownloadTask = { filename: string; pathname: string; progress: number; @@ -28,6 +28,8 @@ type DownloadTask = { taskID: string; totalSize: number; url: string; + status: string; + error: string; }; export async function queryDownloadTask(taskID: string): Promise { diff --git a/frontend/src/components/prompts/Fetch.vue b/frontend/src/components/prompts/Fetch.vue index 5e54333e..cf4bbdc0 100644 --- a/frontend/src/components/prompts/Fetch.vue +++ b/frontend/src/components/prompts/Fetch.vue @@ -50,14 +50,17 @@ {{ t("buttons.create") }} -
+
-
- {{ Math.floor((progress * 100)) + "%" }} +
+ {{ Math.floor(currentTask.progress * 100) + "%" }}
@@ -67,10 +70,11 @@ import { useLayoutStore } from "@/stores/layout.ts"; import { useFileStore } from "@/stores/file.ts"; import { useI18n } from "vue-i18n"; -import { computed, inject, onMounted, ref, watch } from "vue"; +import { computed, inject, onMounted, reactive, ref, watch } from "vue"; import url from "@/utils/url.ts"; import { fetcher as api } from "@/api"; import { useRoute } from "vue-router"; +import type { DownloadTask } from "@/api/fetch.tsx"; const $showError = inject("$showError")!; @@ -83,10 +87,28 @@ const { t } = useI18n(); const fetchUrl = ref(""); const saveName = ref(""); const taskID = ref(""); -const progress = ref(0); +const currentTask = reactive({ + error: "", + filename: "", + pathname: "", + progress: 0, + savedSize: 0, + status: "", + taskID: "", + totalSize: 0, + url: "", +}); const isDownloading = computed(() => { - return taskID.value !== "" && progress.value < 1 && progress.value > 0; + return ( + taskID.value !== "" && + taskID.value === currentTask.taskID && + currentTask.status === "downloading" + ); +}); + +const isFailed = computed(() => { + return currentTask.status === "error"; }); watch(fetchUrl, (value) => { @@ -139,8 +161,8 @@ onMounted(() => { const task = await api.queryDownloadTask(taskID.value); if (!task) return; console.log("fetch task info", task); - progress.value = task.progress; - if (task.progress >= 1) { + Object.assign(currentTask, task); + if (currentTask.status === "completed") { taskID.value = ""; } }, 1000);