Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/frontend/terminal/Terminal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -164,6 +165,7 @@ import {
useTranslation,
invokeAction,
usePluginApi,
usePluginApiFor,
useSlotContributions,
getCustomKeybindings,
runKeybindingAction,
Expand Down Expand Up @@ -259,6 +261,7 @@ const TerminalInner = forwardRef<TerminalHandle, SSHTerminalProps>(
) {
const { t } = useTranslation();
const api = usePluginApi();
const remoteApi = usePluginApiFor("remote");
const { instance: terminal, ref: xtermRef } = useXTerm();
const commandHistoryContext = useCommandHistory();
const confirm = useConfirm();
Expand Down Expand Up @@ -3842,7 +3845,12 @@ const TerminalInner = forwardRef<TerminalHandle, SSHTerminalProps>(
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 {
Expand Down
14 changes: 14 additions & 0 deletions src/frontend/terminal/terminal-image-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(
sessionOrigin: "local" | "remote",
localApi: T,
remoteApi: T,
): T {
return sessionOrigin === "remote" ? remoteApi : localApi;
}
18 changes: 17 additions & 1 deletion tests/frontend/terminal/terminal-image-upload-metadata.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down Expand Up @@ -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);
});
});