Skip to content

Open-source download manager & offline media toolbox for YouTube, music, social media, learning platforms & 1,800+ sites. Built with Tauri 2.0, Rust & Svelte.

feat: enhance GenericDownloadItem with speed, downloadedBytes, totalBytes, and etaSeconds; update related functions for improved progress tracking

Tonho7126021434fb
diff --git a/src/lib/stores/download-store.svelte.ts b/src/lib/stores/download-store.svelte.ts
index 047f9748..d4fb8c30 100644
--- a/src/lib/stores/download-store.svelte.ts
+++ b/src/lib/stores/download-store.svelte.ts
@@ -25,6 +25,10 @@ export type CourseDownloadItem = BaseItem & {
export type GenericDownloadItem = BaseItem & {
kind: "generic";
platform: string;
+ speed: number;
+ downloadedBytes: number;
+ totalBytes: number | null;
+ etaSeconds: number | null;
filePath?: string;
fileCount?: number;
};
@@ -142,16 +146,29 @@ export function upsertGenericProgress(
title: string,
platform: string,
percent: number,
+ speedBytesPerSec: number,
+ downloadedBytes: number,
+ totalBytes: number | null,
+ etaSeconds: number | null,
) {
const now = Date.now();
const existing = downloads.get(id);
+ let speed = speedBytesPerSec;
+ if (existing && existing.kind === "generic" && existing.speed > 0 && speedBytesPerSec > 0) {
+ speed = existing.speed * (1 - SPEED_SMOOTHING) + speedBytesPerSec * SPEED_SMOOTHING;
+ }
+
downloads.set(id, {
kind: "generic",
id,
name: title,
platform,
percent,
+ speed,
+ downloadedBytes,
+ totalBytes,
+ etaSeconds,
status: "downloading",
startedAt: existing?.startedAt ?? now,
lastUpdateAt: now,
@@ -166,7 +183,7 @@ export function removeDownload(id: number) {
}
}
-export function markGenericComplete(id: number, success: boolean, error?: string, filePath?: string, fileCount?: number) {
+export function markGenericComplete(id: number, success: boolean, error?: string, filePath?: string, fileCount?: number, totalBytes?: number | null) {
const item = downloads.get(id);
if (!item || item.kind !== "generic") return;
@@ -177,6 +194,9 @@ export function markGenericComplete(id: number, success: boolean, error?: string
error,
filePath,
fileCount,
+ totalBytes: totalBytes ?? item.totalBytes,
+ speed: 0,
+ etaSeconds: null,
lastUpdateAt: Date.now(),
});
downloads = new Map(downloads);
@@ -209,3 +229,24 @@ export function getEtaI18n(item: CourseDownloadItem): I18nValue {
const mins = Math.ceil((remaining % 3600) / 60);
return { key: "downloads.eta_hours", params: { h: hours, m: mins } };
}
+
+export function getGenericEtaI18n(item: GenericDownloadItem): I18nValue {
+ if (item.etaSeconds != null && isFinite(item.etaSeconds) && item.etaSeconds >= 0) {
+ const remaining = item.etaSeconds;
+ if (remaining < 60) return { key: "downloads.eta_seconds", params: { n: Math.ceil(remaining) } };
+ if (remaining < 3600) return { key: "downloads.eta_minutes", params: { n: Math.ceil(remaining / 60) } };
+ const hours = Math.floor(remaining / 3600);
+ const mins = Math.ceil((remaining % 3600) / 60);
+ return { key: "downloads.eta_hours", params: { h: hours, m: mins } };
+ }
+ if (item.percent <= 0) return { key: "downloads.eta_calculating" };
+ const elapsed = (item.lastUpdateAt - item.startedAt) / 1000;
+ if (elapsed < 2) return { key: "downloads.eta_calculating" };
+ const remaining = elapsed * (100 - item.percent) / item.percent;
+ if (!isFinite(remaining) || remaining < 0) return { key: "downloads.eta_calculating" };
+ if (remaining < 60) return { key: "downloads.eta_seconds", params: { n: Math.ceil(remaining) } };
+ if (remaining < 3600) return { key: "downloads.eta_minutes", params: { n: Math.ceil(remaining / 60) } };
+ const hours = Math.floor(remaining / 3600);
+ const mins = Math.ceil((remaining % 3600) / 60);
+ return { key: "downloads.eta_hours", params: { h: hours, m: mins } };
+}