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: add download management functionality with progress tracking and completion handling

Tonhoda8aa169b1ee
diff --git a/src/lib/stores/download-store.svelte.ts b/src/lib/stores/download-store.svelte.ts
new file mode 100644
index 00000000..84f4579f
--- /dev/null
+++ b/src/lib/stores/download-store.svelte.ts
@@ -0,0 +1,125 @@
+export type DownloadStatus = "downloading" | "complete" | "error";
+
+export type DownloadItem = {
+ courseId: number;
+ courseName: string;
+ percent: number;
+ currentModule: string;
+ currentPage: string;
+ status: DownloadStatus;
+ error?: string;
+ startedAt: number;
+ bytesDownloaded: number;
+ lastUpdateAt: number;
+ speed: number;
+ totalPages: number;
+ completedPages: number;
+ totalModules: number;
+ currentModuleIndex: number;
+};
+
+const SPEED_SMOOTHING = 0.3;
+
+let downloads = $state(new Map<number, DownloadItem>());
+
+export function getDownloads(): Map<number, DownloadItem> {
+ return downloads;
+}
+
+export function getActiveCount(): number {
+ let count = 0;
+ for (const item of downloads.values()) {
+ if (item.status === "downloading") count++;
+ }
+ return count;
+}
+
+export function upsertProgress(
+ courseId: number,
+ courseName: string,
+ percent: number,
+ currentModule: string,
+ currentPage: string,
+ downloadedBytes: number,
+ totalPages: number,
+ completedPages: number,
+ totalModules: number,
+ currentModuleIndex: number,
+) {
+ const now = Date.now();
+ const existing = downloads.get(courseId);
+
+ let speed = 0;
+ if (existing && existing.bytesDownloaded > 0 && downloadedBytes > existing.bytesDownloaded) {
+ const dt = (now - existing.lastUpdateAt) / 1000;
+ if (dt > 0.1) {
+ const instantSpeed = (downloadedBytes - existing.bytesDownloaded) / dt;
+ speed = existing.speed > 0
+ ? existing.speed * (1 - SPEED_SMOOTHING) + instantSpeed * SPEED_SMOOTHING
+ : instantSpeed;
+ } else {
+ speed = existing.speed;
+ }
+ }
+
+ downloads.set(courseId, {
+ courseId,
+ courseName,
+ percent,
+ currentModule,
+ currentPage,
+ status: "downloading",
+ startedAt: existing?.startedAt ?? now,
+ bytesDownloaded: downloadedBytes,
+ lastUpdateAt: now,
+ speed,
+ totalPages,
+ completedPages,
+ totalModules,
+ currentModuleIndex,
+ });
+ downloads = new Map(downloads);
+}
+
+export function markComplete(courseName: string, success: boolean, error?: string) {
+ for (const [id, item] of downloads) {
+ if (item.courseName === courseName) {
+ downloads.set(id, {
+ ...item,
+ percent: success ? 100 : item.percent,
+ status: success ? "complete" : "error",
+ error,
+ lastUpdateAt: Date.now(),
+ speed: 0,
+ });
+ downloads = new Map(downloads);
+ break;
+ }
+ }
+}
+
+export function formatBytes(bytes: number): string {
+ if (bytes < 1024) return `${bytes} B`;
+ if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
+ if (bytes < 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
+ return `${(bytes / (1024 * 1024 * 1024)).toFixed(2)} GB`;
+}
+
+export function formatSpeed(bytesPerSec: number): string {
+ if (bytesPerSec <= 0) return "0 KB/s";
+ if (bytesPerSec < 1024 * 1024) return `${(bytesPerSec / 1024).toFixed(0)} KB/s`;
+ return `${(bytesPerSec / (1024 * 1024)).toFixed(1)} MB/s`;
+}
+
+export function formatEta(item: DownloadItem): string {
+ if (item.percent <= 0 || item.speed <= 0) return "Calculando...";
+ const elapsed = (item.lastUpdateAt - item.startedAt) / 1000;
+ if (elapsed < 2) return "Calculando...";
+ const remaining = elapsed * (100 - item.percent) / item.percent;
+ if (!isFinite(remaining) || remaining < 0) return "Calculando...";
+ if (remaining < 60) return `~${Math.ceil(remaining)} seg`;
+ if (remaining < 3600) return `~${Math.ceil(remaining / 60)} min`;
+ const hours = Math.floor(remaining / 3600);
+ const mins = Math.ceil((remaining % 3600) / 60);
+ return `~${hours}h ${mins}min`;
+}