@devries-cameron20 / t3code Mirror · read-only / commits / b0aa468aa1
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 09:30 UTC

fix(web): copy and save images embedded as data URLs (#23621)

Co-authored-by: maria-rcks <maria@kuuro.net>

Kayzel committed Nov 1, 2026 at 03:28 UTC b0aa468aa18ca4195d08362a83a9c6235114633a
2 files changed +92
apps/web/src/components/media/mediaContent.test.ts new
+57
@@ -0,0 +1,57 @@
1 +import { expect, it, vi } from "vite-plus/test";
2 +
3 +import { readMediaPng } from "./mediaContent";
4 +
5 +const PNG_BYTES = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
6 +
7 +it("copies a data URL image without fetching it", async () => {
8 + // The desktop renderer's connect-src rejects data: URLs the way fetch rejects a CORS failure.
9 + using fetch = vi.spyOn(globalThis, "fetch").mockRejectedValue(new TypeError("Failed to fetch"));
10 + const src = `data:image/png;base64,${btoa(String.fromCharCode(...PNG_BYTES))}`;
11 +
12 + const png = await readMediaPng(src);
13 +
14 + expect(fetch).not.toHaveBeenCalled();
15 + expect(png.type).toBe("image/png");
16 + expect([...new Uint8Array(await png.arrayBuffer())]).toEqual(PNG_BYTES);
17 +});
18 +
19 +it("decodes percent-escaped bytes in a non-base64 data URL", async () => {
20 + const escaped = PNG_BYTES.map((byte) => `%${byte.toString(16).padStart(2, "0")}`).join("");
21 + const src = `data:image/png,${escaped}`;
22 +
23 + const png = await readMediaPng(src);
24 +
25 + expect([...new Uint8Array(await png.arrayBuffer())]).toEqual(PNG_BYTES);
26 +});
27 +
28 +it("accepts any case in the scheme and base64 marker, and escapes in base64", async () => {
29 + const base64 = btoa(String.fromCharCode(...PNG_BYTES)).replaceAll("=", "%3D");
30 +
31 + const png = await readMediaPng(`DATA:image/png;BASE64,${base64}`);
32 +
33 + expect([...new Uint8Array(await png.arrayBuffer())]).toEqual(PNG_BYTES);
34 +});
35 +
36 +it("ignores the URL fragment when decoding a data URL", async () => {
37 + const src = `data:image/png;base64,${btoa(String.fromCharCode(...PNG_BYTES))}#preview`;
38 +
39 + const png = await readMediaPng(src);
40 +
41 + expect([...new Uint8Array(await png.arrayBuffer())]).toEqual(PNG_BYTES);
42 +});
43 +
44 +it.each(["data:image/png;base64,%%%", "data:image/png"])(
45 + "reports malformed inline media: %s",
46 + async (src) => {
47 + await expect(readMediaPng(src)).rejects.toThrow("This inline media could not be decoded.");
48 + },
49 +);
50 +
51 +it("keeps remote media on the fetch path", async () => {
52 + const png = new Blob([new Uint8Array(PNG_BYTES)], { type: "image/png" });
53 + using fetch = vi.spyOn(globalThis, "fetch").mockResolvedValue(new Response(png));
54 +
55 + expect(await readMediaPng("https://example.com/image.png")).toEqual(png);
56 + expect(fetch).toHaveBeenCalledWith("https://example.com/image.png");
57 +});
apps/web/src/components/media/mediaContent.ts
+35
@@ -6,7 +6,42 @@ export function resolveProtocolRelativeMediaUrl(src: string): string {
6 return `${protocol}${src}`;
7 }
8
9 +// Data URL escapes are raw bytes, not UTF-8, so `%89` must stay one byte.
10 +function decodePercentBytes(payload: string) {
11 + const encoder = new TextEncoder();
12 + return Uint8Array.from(
13 + payload
14 + .split(/(%[0-9a-f]{2})/i)
15 + .flatMap((part) =>
16 + /^%[0-9a-f]{2}$/i.test(part)
17 + ? [Number.parseInt(part.slice(1), 16)]
18 + : [...encoder.encode(part)],
19 + ),
20 + );
21 +}
22 +
23 +/** Decodes inline media locally; desktop connect-src omits `data:`, so fetch() cannot read it. */
24 +function readDataUrlBlob(src: string): Blob {
25 + // A fragment is never part of the payload; a literal `#` in data must be escaped as `%23`.
26 + const url = src.split("#", 1)[0] ?? src;
27 + const comma = url.indexOf(",");
28 + const [mimeType = "", ...parameters] = url.slice("data:".length, comma).split(";");
29 + const payload = url.slice(comma + 1);
30 + try {
31 + if (comma < 0) throw new Error("Missing data URL payload.");
32 + const isBase64 = parameters.some((parameter) => parameter.trim().toLowerCase() === "base64");
33 + // Base64 text is ASCII, so any escape that is not valid UTF-8 is malformed anyway.
34 + const bytes = isBase64
35 + ? Uint8Array.from(atob(decodeURIComponent(payload)), (char) => char.charCodeAt(0))
36 + : decodePercentBytes(payload);
37 + return new Blob([bytes], { type: mimeType || "text/plain" });
38 + } catch (cause) {
39 + throw new Error("This inline media could not be decoded.", { cause });
40 + }
41 +}
42 +
43 async function readMediaBlob(src: string): Promise<Blob> {
44 + if (/^data:/i.test(src)) return readDataUrlBlob(src);
45 let response: Response;
46 try {
47 response = await fetch(src);