Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

1.7 KB
Raw Blame
import { isTauriRuntime } from "@/lib/backend/tauriRuntime";

export class DictionaryFileExistsError extends Error {
  constructor() {
    super("exists");
    this.name = "DictionaryFileExistsError";
  }
}

export async function chooseDataDictionaryPath(suggestedName: string): Promise<string | null> {
  if (!isTauriRuntime()) return suggestedName;
  const { save } = await import("@tauri-apps/plugin-dialog");
  return save({ defaultPath: suggestedName, filters: [{ name: "PDF", extensions: ["pdf"] }] });
}

/**
 * Suggested name is only the save dialog's defaultPath. The write uses the path
 * `save()` just returned. Tauri's dialog grant covers that exact path, not a
 * typed path or a timestamp appended after the dialog closes.
 */
export async function saveDataDictionaryFile(suggestedName: string, content: Uint8Array, options: { overwrite: boolean; grantedPath?: string }): Promise<string | null> {
  if (isTauriRuntime()) {
    const fs = await import("@tauri-apps/plugin-fs");
    const granted = options.grantedPath ?? (await chooseDataDictionaryPath(suggestedName));
    if (!granted) return null;
    if (!options.overwrite && (await fs.exists(granted))) throw new DictionaryFileExistsError();
    await fs.writeFile(granted, content);
    return granted;
  }

  const downloadName = suggestedName.split(/[/\\]/).pop() || suggestedName;
  const blob = new Blob([content.buffer.slice(content.byteOffset, content.byteOffset + content.byteLength) as ArrayBuffer], { type: "application/pdf" });
  const url = URL.createObjectURL(blob);
  const anchor = document.createElement("a");
  anchor.href = url;
  anchor.download = downloadName;
  anchor.click();
  URL.revokeObjectURL(url);
  return downloadName;
}