mirror of
https://github.com/Termix-SSH/Termix.git
synced 2026-07-16 05:13:36 +00:00
fix: restore database import in embedded desktop mode (#1060)
This commit is contained in:
@@ -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");
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -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}`;
|
||||||
|
}
|
||||||
@@ -33,6 +33,7 @@ import {
|
|||||||
getCommandHistoryEnabled,
|
getCommandHistoryEnabled,
|
||||||
updateCommandHistoryEnabled,
|
updateCommandHistoryEnabled,
|
||||||
isElectron,
|
isElectron,
|
||||||
|
getConfiguredServerUrl,
|
||||||
getUserRoles,
|
getUserRoles,
|
||||||
} from "@/main-axios";
|
} from "@/main-axios";
|
||||||
import {
|
import {
|
||||||
@@ -69,7 +70,7 @@ import {
|
|||||||
type AdminUser,
|
type AdminUser,
|
||||||
} from "./AdminManagementSections";
|
} from "./AdminManagementSections";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { getBasePath } from "@/lib/base-path";
|
import { getDatabaseTransferUrl } from "@/lib/database-transfer-url";
|
||||||
import {
|
import {
|
||||||
AdminDatabaseSection,
|
AdminDatabaseSection,
|
||||||
AdminGeneralSettingsSection,
|
AdminGeneralSettingsSection,
|
||||||
@@ -717,17 +718,11 @@ export function AdminSettingsPanel() {
|
|||||||
async function handleExportDatabase() {
|
async function handleExportDatabase() {
|
||||||
setExportLoading(true);
|
setExportLoading(true);
|
||||||
try {
|
try {
|
||||||
const isDev =
|
const apiUrl = getDatabaseTransferUrl("export", {
|
||||||
!isElectron() &&
|
electron: isElectron(),
|
||||||
(window.location.port === "5173" ||
|
configuredServerUrl: getConfiguredServerUrl(),
|
||||||
window.location.hostname === "localhost" ||
|
location: window.location,
|
||||||
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 response = await fetch(apiUrl, {
|
const response = await fetch(apiUrl, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
@@ -769,17 +764,11 @@ export function AdminSettingsPanel() {
|
|||||||
}
|
}
|
||||||
setImportLoading(true);
|
setImportLoading(true);
|
||||||
try {
|
try {
|
||||||
const isDev =
|
const apiUrl = getDatabaseTransferUrl("import", {
|
||||||
!isElectron() &&
|
electron: isElectron(),
|
||||||
(window.location.port === "5173" ||
|
configuredServerUrl: getConfiguredServerUrl(),
|
||||||
window.location.hostname === "localhost" ||
|
location: window.location,
|
||||||
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 formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append("file", importFile);
|
formData.append("file", importFile);
|
||||||
|
|||||||
Reference in New Issue
Block a user