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(store): rebrand open_omni stores to open_nami with unit tests

IGECT63d9ab89ded6
diff --git a/src/lib/api/open_nami.ts b/src/lib/api/open_nami.ts
new file mode 100644
index 00000000..a9c051c5
--- /dev/null
+++ b/src/lib/api/open_nami.ts
@@ -0,0 +1,98 @@
+import { invoke } from '@tauri-apps/api/core';
+import { listen, type UnlistenFn } from '@tauri-apps/api/event';
+
+export interface Profile {
+ url: string;
+ username?: string;
+ platform: string;
+ added_at: number;
+}
+
+export interface DownloadResult {
+ success: boolean;
+ message: string;
+ files_count: number;
+ cancelled: boolean;
+}
+
+export interface DownloadProgress {
+ message: string;
+ files_downloaded: number;
+ stage?: string | null;
+ stage_index?: number | null;
+ stage_total?: number | null;
+}
+
+export interface AppSettings {
+ output_directory: string | null;
+ cookies_file: string | null;
+}
+
+export interface DependencyStatus {
+ ok: boolean;
+ message: string;
+ gallery_dl_version: string | null;
+}
+
+export async function checkPythonDependencies(): Promise<DependencyStatus> {
+ return await invoke<DependencyStatus>('open_nami_check_python_dependencies');
+}
+
+export async function runGalleryDlDownload(
+ url: string,
+ outputDir: string,
+ cookiesFile: string | null,
+ contentType: string,
+ downloadId: string
+): Promise<DownloadResult> {
+ return await invoke<DownloadResult>('open_nami_run_gallery_dl_download', {
+ url,
+ outputDir,
+ cookiesFile,
+ contentType,
+ downloadId
+ });
+}
+
+export async function cancelDownload(downloadId: string): Promise<string> {
+ return await invoke<string>('open_nami_cancel_download', { downloadId });
+}
+
+export async function listenToDownloadProgress(
+ downloadId: string,
+ callback: (progress: DownloadProgress) => void
+): Promise<UnlistenFn> {
+ return await listen<DownloadProgress>(`download_${downloadId}`, (event) => {
+ callback(event.payload);
+ });
+}
+
+export async function saveAppSettings(
+ outputDirectory: string | null,
+ cookiesFile: string | null
+): Promise<string> {
+ return await invoke<string>('open_nami_save_app_settings', {
+ outputDirectory,
+ cookiesFile
+ });
+}
+
+export async function loadAppSettings(): Promise<AppSettings> {
+ return await invoke<AppSettings>('open_nami_load_app_settings');
+}
+
+export async function loadProfiles(platform: string): Promise<Profile[]> {
+ return await invoke<Profile[]>('open_nami_load_profiles', { platform });
+}
+
+export async function saveProfile(platform: string, url: string): Promise<string> {
+ return await invoke<string>('open_nami_save_profile', { platform, url });
+}
+
+export async function deleteProfile(platform: string, profileUrl: string): Promise<string> {
+ return await invoke<string>('open_nami_delete_profile', { platform, profileUrl });
+}
+
+export function generateDownloadId(): string {
+ return `dl_${Date.now()}_${Math.random().toString(36).slice(2, 11)}`;
+}
diff --git a/src/lib/stores/open_nami_deps_store.svelte.ts b/src/lib/stores/open_nami_deps_store.svelte.ts
new file mode 100644
index 00000000..1c54460b
--- /dev/null
+++ b/src/lib/stores/open_nami_deps_store.svelte.ts
@@ -0,0 +1,40 @@
+import { checkPythonDependencies, type DependencyStatus } from '$lib/api/open_nami';
+
+let status = $state<DependencyStatus | null>(null);
+let checking = $state(false);
+let inflight: Promise<void> | null = null;
+
+export function getDependencyStatus(): DependencyStatus | null {
+ return status;
+}
+
+export function isCheckingDependencies(): boolean {
+ return checking;
+}
+
+export async function ensureDependenciesChecked(force = false): Promise<DependencyStatus | null> {
+ if (status && !force) return status;
+ if (inflight) {
+ await inflight;
+ return status;
+ }
+
+ checking = true;
+ inflight = (async () => {
+ try {
+ status = await checkPythonDependencies();
+ } catch (err: unknown) {
+ status = {
+ ok: false,
+ message: err instanceof Error ? err.message : String(err),
+ gallery_dl_version: null
+ };
+ } finally {
+ checking = false;
+ inflight = null;
+ }
+ })();
+
+ await inflight;
+ return status;
+}
diff --git a/src/lib/stores/open_nami_download_store.svelte.ts b/src/lib/stores/open_nami_download_store.svelte.ts
new file mode 100644
index 00000000..0e56d1b0
--- /dev/null
+++ b/src/lib/stores/open_nami_download_store.svelte.ts
@@ -0,0 +1,219 @@
+import {
+ runGalleryDlDownload,
+ cancelDownload,
+ listenToDownloadProgress,
+ generateDownloadId,
+ type DownloadProgress,
+ type DownloadResult,
+} from "$lib/api/open_nami";
+
+export type StatusType = "success" | "error" | "info";
+
+export interface OpenNamiDraft {
+ url: string;
+ contentType: string;
+ selectedProfileUrl: string | null;
+}
+
+let draft = $state<OpenNamiDraft>({
+ url: "",
+ contentType: "all",
+ selectedProfileUrl: null,
+});
+
+let active = $state(false);
+let downloadId = $state<string | null>(null);
+let filesDownloaded = $state(0);
+let stage = $state<string | null>(null);
+let stageIndex = $state<number | null>(null);
+let stageTotal = $state<number | null>(null);
+let lastMessage = $state("");
+let status = $state("");
+let statusType = $state<StatusType>("info");
+let statusDetail = $state("");
+let cancelling = $state(false);
+let unlisten: (() => void) | null = null;
+
+export function getDraft(): OpenNamiDraft {
+ return draft;
+}
+
+export function setDraft(newDraft: Partial<OpenNamiDraft>) {
+ if (newDraft.url !== undefined) draft.url = newDraft.url;
+ if (newDraft.contentType !== undefined) draft.contentType = newDraft.contentType;
+ if (newDraft.selectedProfileUrl !== undefined) draft.selectedProfileUrl = newDraft.selectedProfileUrl;
+}
+
+export function isActive(): boolean {
+ return active;
+}
+
+export function getDownloadId(): string | null {
+ return downloadId;
+}
+
+export function getFilesDownloaded(): number {
+ return filesDownloaded;
+}
+
+export function getStage(): string | null {
+ return stage;
+}
+
+export function getStageIndex(): number | null {
+ return stageIndex;
+}
+
+export function getStageTotal(): number | null {
+ return stageTotal;
+}
+
+export function getLastMessage(): string {
+ return lastMessage;
+}
+
+export function getStatus(): string {
+ return status;
+}
+
+export function getStatusType(): StatusType {
+ return statusType;
+}
+
+export function getStatusDetail(): string {
+ return statusDetail;
+}
+
+export function isCancelling(): boolean {
+ return cancelling;
+}
+
+export function clearStatus() {
+ status = "";
+ statusType = "info";
+ statusDetail = "";
+}
+
+function applyProgress(progressData: DownloadProgress) {
+ if (progressData.message && progressData.message.trim()) {
+ lastMessage = progressData.message.trim();
+ }
+ if (typeof progressData.files_downloaded === "number" && progressData.files_downloaded >= 0) {
+ filesDownloaded = progressData.files_downloaded;
+ }
+ if (progressData.stage) {
+ stage = progressData.stage;
+ }
+ if (typeof progressData.stage_index === "number") {
+ stageIndex = progressData.stage_index;
+ }
+ if (typeof progressData.stage_total === "number") {
+ stageTotal = progressData.stage_total;
+ }
+}
+
+function resetProgressState() {
+ filesDownloaded = 0;
+ stage = null;
+ stageIndex = null;
+ stageTotal = null;
+ lastMessage = "";
+}
+
+export async function startDownload(
+ url: string,
+ outputDir: string,
+ cookiesFile: string,
+ contentType: string,
+): Promise<DownloadResult | null> {
+ if (active) {
+ throw new Error("A download is already in progress");
+ }
+
+ const cleanUrl = url.trim();
+ const cleanOutputDir = outputDir.trim();
+ const cleanCookiesFile = cookiesFile.trim();
+
+ if (!cleanUrl || !cleanOutputDir) {
+ status = "Please fill in all required fields";
+ statusType = "error";
+ statusDetail = "";
+ return null;
+ }
+
+ cancelling = false;
+ active = true;
+ resetProgressState();
+ status = "Starting download...";
+ statusType = "info";
+ statusDetail = "";
+
+ const id = generateDownloadId();
+ downloadId = id;
+
+ try {
+ unlisten = await listenToDownloadProgress(id, applyProgress);
+ const result = await runGalleryDlDownload(
+ cleanUrl,
+ cleanOutputDir,
+ cleanCookiesFile || null,
+ contentType,
+ id,
+ );
+
+ filesDownloaded = result.files_count;
+
+ if (result.cancelled) {
+ status = `Download cancelled — ${result.files_count} file(s) saved before stopping.`;
+ statusType = "info";
+ statusDetail = "";
+ } else if (result.success) {
+ status = `Downloaded ${result.files_count} file(s) successfully!`;
+ statusType = "success";
+ statusDetail = "";
+ } else {
+ const lines = (result.message || "Download failed").split("\n");
+ status = lines[0] || "Download failed";
+ statusType = "error";
+ statusDetail = lines.length > 1 ? lines.slice(1).join("\n") : "";
+ }
+
+ return result;
+ } catch (error: unknown) {
+ const msg = error instanceof Error ? error.message : String(error);
+ status = `Download failed: ${msg}`;
+ statusType = "error";
+ statusDetail = "";
+ return null;
+ } finally {
+ active = false;
+ cancelling = false;
+ if (unlisten) {
+ try {
+ unlisten();
+ } catch {
+ // ignore
+ }
+ unlisten = null;
+ }
+ downloadId = null;
+ }
+}
+
+export async function stopDownload() {
+ if (!active || !downloadId) return;
+ if (cancelling) return;
+
+ cancelling = true;
+ status = "Cancelling download...";
+ statusType = "info";
+
+ try {
+ await cancelDownload(downloadId);
+ } catch (error) {
+ console.error("Failed to cancel download:", error);
+ status = `Failed to cancel: ${error}`;
+ statusType = "error";
+ cancelling = false;
+ }
+}
diff --git a/src/lib/stores/open_nami_download_store.test.ts b/src/lib/stores/open_nami_download_store.test.ts
new file mode 100644
index 00000000..6fba087a
--- /dev/null
+++ b/src/lib/stores/open_nami_download_store.test.ts
@@ -0,0 +1,32 @@
+import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
+
+type OpenNamiStore = typeof import("./open_nami_download_store.svelte");
+
+let store: OpenNamiStore;
+
+beforeAll(async () => {
+ vi.stubGlobal("$state", <T>(value: T) => value);
+ store = await import("./open_nami_download_store.svelte");
+});
+
+afterAll(() => {
+ vi.unstubAllGlobals();
+});
+
+describe("open_nami_download_store", () => {
+ it("updates draft properties via setDraft without resetting others", () => {
+ store.setDraft({ url: "https://instagram.com/test", contentType: "photos" });
+ expect(store.getDraft().url).toBe("https://instagram.com/test");
+ expect(store.getDraft().contentType).toBe("photos");
+
+ store.setDraft({ selectedProfileUrl: "https://instagram.com/selected" });
+ expect(store.getDraft().url).toBe("https://instagram.com/test");
+ expect(store.getDraft().selectedProfileUrl).toBe("https://instagram.com/selected");
+ });
+
+ it("handles empty draft resets cleanly", () => {
+ store.setDraft({ url: "", selectedProfileUrl: null });
+ expect(store.getDraft().url).toBe("");
+ expect(store.getDraft().selectedProfileUrl).toBeNull();
+ });
+});
diff --git a/src/lib/stores/open_nami_settings_store.svelte.ts b/src/lib/stores/open_nami_settings_store.svelte.ts
new file mode 100644
index 00000000..1ef3b670
--- /dev/null
+++ b/src/lib/stores/open_nami_settings_store.svelte.ts
@@ -0,0 +1,51 @@
+import { saveAppSettings, loadAppSettings } from "$lib/api/open_nami";
+
+let outputDir = $state("");
+let cookiesFile = $state("");
+let loaded = $state(false);
+
+export function getOutputDir(): string {
+ return outputDir;
+}
+
+export function getCookiesFile(): string {
+ return cookiesFile;
+}
+
+export function isLoaded(): boolean {
+ return loaded;
+}
+
+function cleanPathLike(value: string): string {
+ const trimmed = value.trim();
+ if (
+ trimmed.length >= 2 &&
+ ((trimmed.startsWith('"') && trimmed.endsWith('"')) ||
+ (trimmed.startsWith("'") && trimmed.endsWith("'")))
+ ) {
+ return trimmed.slice(1, -1).trim();
+ }
+ return trimmed;
+}
+
+export async function loadSettings() {
+ try {
+ const settings = await loadAppSettings();
+ outputDir = settings.output_directory ?? "";
+ cookiesFile = settings.cookies_file ?? "";
+ } catch (error) {
+ console.error("Failed to load settings:", error);
+ } finally {
+ loaded = true;
+ }
+}
+
+export async function saveSettings(newOutputDir: string, newCookiesFile: string): Promise<void> {
+ const cleanOutputDir = cleanPathLike(newOutputDir);
+ const cleanCookiesFile = newCookiesFile.trim() ? cleanPathLike(newCookiesFile) : "";
+
+ await saveAppSettings(cleanOutputDir || null, cleanCookiesFile || null);
+
+ outputDir = cleanOutputDir;
+ cookiesFile = cleanCookiesFile;
+}