fix: restore database import in embedded desktop mode (#1060)

This commit is contained in:
ZacharyZcR
2026-07-16 11:52:53 +08:00
committed by GitHub
parent 39d40db8c6
commit 71a63ae5e8
3 changed files with 99 additions and 23 deletions
+54
View File
@@ -0,0 +1,54 @@
import { describe, expect, it } from "vitest";
import { getDatabaseTransferUrl } from "./database-transfer-url";
const productionLocation = {
protocol: "https:",
host: "termix.example.com",
hostname: "termix.example.com",
port: "",
};
describe("getDatabaseTransferUrl", () => {
it("uses the embedded backend for Electron without a remote server", () => {
expect(
getDatabaseTransferUrl("import", {
electron: true,
configuredServerUrl: null,
location: productionLocation,
}),
).toBe("http://localhost:30001/database/import");
});
it("uses the configured Electron server without duplicate slashes", () => {
expect(
getDatabaseTransferUrl("export", {
electron: true,
configuredServerUrl: "https://termix.example.com/",
location: productionLocation,
}),
).toBe("https://termix.example.com/database/export");
});
it("uses the browser base path in production", () => {
expect(
getDatabaseTransferUrl("import", {
electron: false,
location: productionLocation,
}),
).toBe("https://termix.example.com/database/import");
});
it("uses the backend development port for the Vite frontend", () => {
expect(
getDatabaseTransferUrl("export", {
electron: false,
location: {
protocol: "http:",
host: "localhost:5173",
hostname: "localhost",
port: "5173",
},
}),
).toBe("http://localhost:30001/database/export");
});
});
+33
View File
@@ -0,0 +1,33 @@
import { getBasePath } from "./base-path";
type BrowserLocation = Pick<
Location,
"protocol" | "host" | "hostname" | "port"
>;
interface DatabaseTransferUrlOptions {
electron: boolean;
configuredServerUrl?: string | null;
location: BrowserLocation;
}
export function getDatabaseTransferUrl(
operation: "export" | "import",
{ electron, configuredServerUrl, location }: DatabaseTransferUrlOptions,
): string {
if (electron) {
const serverUrl = configuredServerUrl || "http://localhost:30001";
return `${serverUrl.replace(/\/$/, "")}/database/${operation}`;
}
const development =
location.port === "5173" ||
location.hostname === "localhost" ||
location.hostname === "127.0.0.1";
if (development) {
return `http://localhost:30001/database/${operation}`;
}
return `${location.protocol}//${location.host}${getBasePath()}/database/${operation}`;
}
+12 -23
View File
@@ -33,6 +33,7 @@ import {
getCommandHistoryEnabled,
updateCommandHistoryEnabled,
isElectron,
getConfiguredServerUrl,
getUserRoles,
} from "@/main-axios";
import {
@@ -69,7 +70,7 @@ import {
type AdminUser,
} from "./AdminManagementSections";
import { toast } from "sonner";
import { getBasePath } from "@/lib/base-path";
import { getDatabaseTransferUrl } from "@/lib/database-transfer-url";
import {
AdminDatabaseSection,
AdminGeneralSettingsSection,
@@ -717,17 +718,11 @@ export function AdminSettingsPanel() {
async function handleExportDatabase() {
setExportLoading(true);
try {
const isDev =
!isElectron() &&
(window.location.port === "5173" ||
window.location.hostname === "localhost" ||
window.location.hostname === "127.0.0.1");
const apiUrl = isElectron()
? `${window.configuredServerUrl}/database/export`
: isDev
? `http://localhost:30001/database/export`
: `${window.location.protocol}//${window.location.host}${getBasePath()}/database/export`;
const apiUrl = getDatabaseTransferUrl("export", {
electron: isElectron(),
configuredServerUrl: getConfiguredServerUrl(),
location: window.location,
});
const response = await fetch(apiUrl, {
method: "POST",
@@ -769,17 +764,11 @@ export function AdminSettingsPanel() {
}
setImportLoading(true);
try {
const isDev =
!isElectron() &&
(window.location.port === "5173" ||
window.location.hostname === "localhost" ||
window.location.hostname === "127.0.0.1");
const apiUrl = isElectron()
? `${window.configuredServerUrl}/database/import`
: isDev
? `http://localhost:30001/database/import`
: `${window.location.protocol}//${window.location.host}${getBasePath()}/database/import`;
const apiUrl = getDatabaseTransferUrl("import", {
electron: isElectron(),
configuredServerUrl: getConfiguredServerUrl(),
location: window.location,
});
const formData = new FormData();
formData.append("file", importFile);