diff --git a/src/frontend/terminal/Terminal.tsx b/src/frontend/terminal/Terminal.tsx index f3b5268..9620f50 100644 --- a/src/frontend/terminal/Terminal.tsx +++ b/src/frontend/terminal/Terminal.tsx @@ -60,6 +60,7 @@ import { useCommandHistory } from "./command-history/CommandHistoryContext"; import { getAndroidHardwareKeySequence } from "./android-hardware-keyboard"; import { buildImageUploadFormData, + selectImageUploadApi, type TerminalImageUploadSource, } from "./terminal-image-upload"; import { createTerminalNativePasteHandler } from "./terminal-native-paste"; @@ -164,6 +165,7 @@ import { useTranslation, invokeAction, usePluginApi, + usePluginApiFor, useSlotContributions, getCustomKeybindings, runKeybindingAction, @@ -259,6 +261,7 @@ const TerminalInner = forwardRef( ) { const { t } = useTranslation(); const api = usePluginApi(); + const remoteApi = usePluginApiFor("remote"); const { instance: terminal, ref: xtermRef } = useXTerm(); const commandHistoryContext = useCommandHistory(); const confirm = useConfirm(); @@ -3842,7 +3845,12 @@ const TerminalInner = forwardRef( hostConfig.instanceId ?? "", source, ); - const response = await api.post("/image-upload", form, { + const uploadApi = selectImageUploadApi( + sessionOriginRef.current, + api, + remoteApi, + ); + const response = await uploadApi.post("/image-upload", form, { headers: { "Content-Type": undefined }, }); const { shellPath } = response.data as { diff --git a/src/frontend/terminal/terminal-image-upload.ts b/src/frontend/terminal/terminal-image-upload.ts index e92688c..3775b73 100644 --- a/src/frontend/terminal/terminal-image-upload.ts +++ b/src/frontend/terminal/terminal-image-upload.ts @@ -13,3 +13,17 @@ export function buildImageUploadFormData( form.append("clientUploadTimestamp", clientUploadTimestamp); return form; } + +/** + * The API client an image upload goes to. The backend stores the image through + * the terminal's SSH session, so the upload has to reach the backend that holds + * that session: the linked server for a session opened there from the desktop + * app, the local backend otherwise. + */ +export function selectImageUploadApi( + sessionOrigin: "local" | "remote", + localApi: T, + remoteApi: T, +): T { + return sessionOrigin === "remote" ? remoteApi : localApi; +} diff --git a/tests/frontend/terminal/terminal-image-upload-metadata.test.ts b/tests/frontend/terminal/terminal-image-upload-metadata.test.ts index c0576f0..4765444 100644 --- a/tests/frontend/terminal/terminal-image-upload-metadata.test.ts +++ b/tests/frontend/terminal/terminal-image-upload-metadata.test.ts @@ -1,5 +1,8 @@ import { describe, expect, it } from "vitest"; -import { buildImageUploadFormData } from "../../../src/frontend/terminal/terminal-image-upload"; +import { + buildImageUploadFormData, + selectImageUploadApi, +} from "../../../src/frontend/terminal/terminal-image-upload"; describe("buildImageUploadFormData", () => { it("tags a picked-file upload with source=file and a client timestamp", () => { @@ -38,3 +41,16 @@ describe("buildImageUploadFormData", () => { expect(form.get("clientUploadTimestamp")).toBe("2026-08-15T12:00:00.000Z"); }); }); + +describe("selectImageUploadApi", () => { + const localApi = { name: "local" }; + const remoteApi = { name: "remote" }; + + it("uploads to the linked server when the session was opened there", () => { + expect(selectImageUploadApi("remote", localApi, remoteApi)).toBe(remoteApi); + }); + + it("uploads to the local backend when the session is local", () => { + expect(selectImageUploadApi("local", localApi, remoteApi)).toBe(localApi); + }); +});