Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: Use dynamic filename for "Download image" context menu (#8408)

Daizu5cd856909e0e
diff --git a/frontend/src/components/editor/cell/cell-context-menu.tsx b/frontend/src/components/editor/cell/cell-context-menu.tsx
index e45489b0fa..c78a341b00 100644
--- a/frontend/src/components/editor/cell/cell-context-menu.tsx
+++ b/frontend/src/components/editor/cell/cell-context-menu.tsx
@@ -26,6 +26,7 @@ import { isOutputEmpty } from "@/core/cells/outputs";
import { goToDefinitionAtCursorPosition } from "@/core/codemirror/go-to-definition/utils";
import { sendToPanelManager } from "@/core/vscode/vscode-bindings";
import { copyToClipboard } from "@/utils/copy";
+import { getImageExtension } from "@/utils/filenames";
import { Logger } from "@/utils/Logger";
import type { ActionButton } from "../actions/types";
import {
@@ -154,8 +155,9 @@ export const CellActionsContextMenu = ({
handle: () => {
if (imageRightClicked) {
const link = document.createElement("a");
- link.download = "image.png";
link.href = imageRightClicked.src;
+ const ext = getImageExtension(imageRightClicked.src) || "png";
+ link.download = `image.${ext}`;
link.click();
}
},
diff --git a/frontend/src/utils/__tests__/filenames.test.ts b/frontend/src/utils/__tests__/filenames.test.ts
index 400d1e5881..3b6f340f0d 100644
--- a/frontend/src/utils/__tests__/filenames.test.ts
+++ b/frontend/src/utils/__tests__/filenames.test.ts
@@ -2,7 +2,7 @@
import { describe, expect, it } from "vitest";
import { EDGE_CASE_FILENAMES } from "../../__tests__/mocks";
-import { Filenames } from "../filenames";
+import { Filenames, getImageExtension } from "../filenames";
describe("Filenames", () => {
it("should convert filename to markdown", () => {
@@ -45,3 +45,56 @@ describe("Filenames", () => {
expect(typeof withoutExt).toBe("string");
});
});
+
+describe("getImageExtension", () => {
+ it("should return correct extensions for common image URLs", () => {
+ expect(getImageExtension("https://example.com/image.png")).toBe("png");
+ expect(getImageExtension("https://example.com/image.jpeg")).toBe("jpeg");
+ expect(getImageExtension("https://example.com/image.JPEG?param=1")).toBe(
+ "jpeg",
+ );
+ expect(getImageExtension("https://example.com/image.gif")).toBe("gif");
+ expect(getImageExtension("https://example.com/image.svg")).toBe("svg");
+ expect(getImageExtension("https://example.com/image.webp")).toBe("webp");
+ expect(getImageExtension("https://example.com/image.avif")).toBe("avif");
+ expect(getImageExtension("https://example.com/image.bmp")).toBe("bmp");
+ expect(getImageExtension("https://example.com/image.tiff")).toBe("tiff");
+ });
+
+ it("should return correct extensions for data URIs", () => {
+ expect(getImageExtension("data:image/png;base64,...")).toBe("png");
+ expect(getImageExtension("data:image/jpeg;base64,...")).toBe("jpeg");
+ expect(getImageExtension("data:image/svg+xml;base64,...")).toBe("svg");
+ });
+
+ it("should return correct extensions for relative URLs resolved with window.location.href", () => {
+ // Mock window.location
+ const originalLocation = window.location;
+ Object.defineProperty(window, "location", {
+ value: new URL("https://example.com/some/path/index.html"),
+ writable: true,
+ });
+
+ expect(getImageExtension("../assets/image.png")).toBe("png");
+ expect(getImageExtension("image.jpeg")).toBe("jpeg");
+ expect(getImageExtension("/root/image.gif")).toBe("gif");
+
+ // Restore original window.location
+ Object.defineProperty(window, "location", {
+ value: originalLocation,
+ writable: true,
+ });
+ });
+
+ it("should return undefined for URLs without recognized image extensions", () => {
+ expect(
+ getImageExtension("https://example.com/document.pdf"),
+ ).toBeUndefined();
+ expect(
+ getImageExtension("https://example.com/archive.tar.gz"),
+ ).toBeUndefined();
+ expect(getImageExtension("https://example.com/image")).toBeUndefined(); // No extension
+ expect(getImageExtension("image")).toBeUndefined();
+ expect(getImageExtension("")).toBeUndefined();
+ });
+});
diff --git a/frontend/src/utils/filenames.ts b/frontend/src/utils/filenames.ts
index 6c91268ec1..48b96dd717 100644
--- a/frontend/src/utils/filenames.ts
+++ b/frontend/src/utils/filenames.ts
@@ -30,3 +30,45 @@ export const Filenames = {
return `${Filenames.withoutExtension(filename)}.${extension}`;
},
};
+
+const IMAGE_EXTENSIONS: Record<string, string> = {
+ png: "png",
+ jpg: "jpg",
+ jpeg: "jpeg",
+ gif: "gif",
+ webp: "webp",
+ avif: "avif",
+ bmp: "bmp",
+ tiff: "tiff",
+ svg: "svg",
+ "svg+xml": "svg",
+};
+
+/**
+ * Infers the file extension from an image source string (src).
+ * If the extension cannot be determined, it returns `undefined`.
+ *
+ * @param src - The image source string (a URL or a data URI).
+ * @returns The inferred file extension (e.g., "png", "jpeg", "svg"),
+ * or `undefined` if it cannot be determined.
+ *
+ * @example
+ * getImageExtension("https://example.com/image.png"); // Returns "png"
+ * getImageExtension("../assets/image.gif"); // Returns "gif"
+ * getImageExtension("data:image/svg+xml;base64,..."); // Returns "svg"
+ * getImageExtension("https://example.com/image"); // Returns undefined
+ */
+export function getImageExtension(src: string): string | undefined {
+ const dataUriMatch = src.match(/^data:image\/([^,;]+)/);
+ if (dataUriMatch) {
+ return IMAGE_EXTENSIONS[dataUriMatch[1]];
+ }
+
+ try {
+ const url = new URL(src, window.location.href);
+ const ext = url.pathname.split(".").pop()?.toLowerCase() ?? "";
+ return IMAGE_EXTENSIONS[ext];
+ } catch {
+ return undefined;
+ }
+}