Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Tell a non-text Markdown file apart from a connection failure

Bugfixer 24adb422b2189
diff --git a/src/components/MarkdownFilePreview.dom.test.ts b/src/components/MarkdownFilePreview.dom.test.ts
index 8b03a04be6..d9eb9f123c 100644
--- a/src/components/MarkdownFilePreview.dom.test.ts
+++ b/src/components/MarkdownFilePreview.dom.test.ts
@@ -40,7 +40,15 @@ describe("Markdown file preview errors and encodings", () => {
it("does not show a UTF-16 file as garbage", async () => {
const bytes = new Uint8Array([0xff, 0xfe, 0x23, 0x00, 0x20, 0x00, 0x41, 0x00, 0xe7, 0x00]);
await open(async () => new Response(bytes));
- expect(alertText()).toContain("Could not open this file");
+ expect(alertText()).toContain("not plain text");
+ expect(alertText()).not.toContain("Check your connection");
+ });
+
+ it("does not show a UTF-16 file without a byte order mark either", async () => {
+ // Every byte here is valid UTF-8 (NUL included), so only the NUL check stops it.
+ await open(async () => new Response(new Uint8Array([0x23, 0x00, 0x20, 0x00, 0x41, 0x00])));
+ expect(alertText()).toContain("not plain text");
+ expect(host.querySelector("[data-markdown-file]")).toBeNull();
});
it("shows the server's own reason instead of blaming the connection", async () => {
diff --git a/src/components/MarkdownFilePreview.tsx b/src/components/MarkdownFilePreview.tsx
index fe066960e5..fe236e9370 100644
--- a/src/components/MarkdownFilePreview.tsx
+++ b/src/components/MarkdownFilePreview.tsx
@@ -12,7 +12,7 @@ export const MARKDOWN_PREVIEW_MAX_BYTES = 2 * 1024 * 1024;
export default function MarkdownFileContent({ path, name, message }: { path: string; name: string; message: MessageAttachmentContext }) {
const [attempt, setAttempt] = useState(0);
const [source, setSource] = useState<string | null>(null);
- const [error, setError] = useState<"size" | "load" | { reason: string } | null>(null);
+ const [error, setError] = useState<"size" | "load" | "encoding" | { reason: string } | null>(null);
const [showSource, setShowSource] = useState(false);
const save = useLocalFileSave(path, name, message);
useEffect(() => {
@@ -31,14 +31,14 @@ export default function MarkdownFileContent({ path, name, message }: { path: str
if (controller.signal.aborted) return;
// A refusal from the server says why; only a request that got no answer (TypeError) is the connection.
if (!answered && reason instanceof Error && !(reason instanceof TypeError)) setError({ reason: reason.message });
- else setError(reason instanceof Error && reason.message === "size" ? "size" : "load");
+ else setError(reason instanceof Error && (reason.message === "size" || reason.message === "encoding") ? reason.message : "load");
}
})();
return () => controller.abort();
}, [path, message.threadId, message.messageId, attempt]);
// Save and Retry stay on screen while loading too, so a stalled request never leaves only a spinner; they wrap so a long name can't push Retry off a narrow dialog.
const recovery = <><div className="mt-3 flex flex-wrap justify-center gap-2"><button type="button" className="inline-flex max-w-full min-w-0 items-center gap-1.5 rounded-lg border border-hairline px-3 py-2 hover:bg-raised" disabled={save.state === "saving"} onClick={() => void save.save()}><Download size={14} className="shrink-0" /><span className="min-w-0 break-all">{t("attach.saveAria", { name })}</span></button><button type="button" className="rounded-lg border border-hairline px-3 py-2 hover:bg-raised" onClick={() => setAttempt(attempt + 1)}>{t("chat.retry")}</button></div>{save.state === "failed" && <p role="alert" className="mt-2 text-[12px] text-danger">{save.reason}</p>}</>;
- if (error) return <div className="p-8 text-center text-sm"><p role="alert" className="text-ink-secondary">{typeof error === "object" ? t("markdown.refused", { reason: error.reason }) : t(error === "size" ? "markdown.tooLarge" : "markdown.loadFailed")}</p>{recovery}</div>;
+ if (error) return <div className="p-8 text-center text-sm"><p role="alert" className="text-ink-secondary">{typeof error === "object" ? t("markdown.refused", { reason: error.reason }) : t(error === "size" ? "markdown.tooLarge" : error === "encoding" ? "markdown.notText" : "markdown.loadFailed")}</p>{recovery}</div>;
if (source === null) return <div className="p-12 text-center text-sm text-ink-secondary"><span role="status" className="inline-flex items-center gap-2"><LoaderCircle size={16} className="animate-spin" />{t("markdown.loading")}</span>{recovery}</div>;
return <div className="flex flex-col gap-2">
<div className="flex items-center justify-end gap-1">
diff --git a/src/components/TableFilePreview.tsx b/src/components/TableFilePreview.tsx
index e2f7586d04..17f75793b6 100644
--- a/src/components/TableFilePreview.tsx
+++ b/src/components/TableFilePreview.tsx
@@ -27,8 +27,10 @@ export async function readTableResponse(response: Response, signal: AbortSignal,
}
} finally { await reader.cancel().catch(() => {}); reader.releaseLock(); }
const bytes = new Uint8Array(await new Blob(parts as BlobPart[]).arrayBuffer());
+ // NUL is valid UTF-8, so UTF-16 without a byte order mark would pass the UTF-8 check: look for it first.
+ if (fallback && bytes.includes(0)) throw new Error("encoding");
try { return new TextDecoder("utf-8", { fatal: true }).decode(bytes); } catch (error) {
- if (!fallback || bytes.includes(0)) throw error;
+ if (!fallback) throw error;
return new TextDecoder(fallback).decode(bytes);
}
}
diff --git a/src/locales/en.json b/src/locales/en.json
index 42df858d23..0ca509ea63 100644
--- a/src/locales/en.json
+++ b/src/locales/en.json
@@ -161,6 +161,7 @@
"markdown.tooLarge": "This file is too large for an in-app preview (2 MB). Download the original to view it.",
"markdown.loadFailed": "Could not open this file. Check your connection and try again. UTF-8 text files are supported.",
"markdown.refused": "Could not open this file: {reason}",
+ "markdown.notText": "This file is not plain text the preview can show. Download the original to open it in another app.",
"markdown.rendered": "View formatted",
"table.loading": "Opening table…",
"table.tooLarge": "This file is too large for an in-app preview (10 MB, 100,000 rows, 200 columns or 500,000 cells). Download the original to view it all.",
diff --git a/src/locales/pt-br.json b/src/locales/pt-br.json
index 2ffd7b116e..9c39c29da7 100644
--- a/src/locales/pt-br.json
+++ b/src/locales/pt-br.json
@@ -144,6 +144,7 @@
"markdown.tooLarge": "Este arquivo é grande demais para a prévia no app (2 MB). Baixe o original para vê-lo.",
"markdown.loadFailed": "Não foi possível abrir este arquivo. Verifique sua conexão e tente novamente. Arquivos de texto UTF-8 são compatíveis.",
"markdown.refused": "Não foi possível abrir este arquivo: {reason}",
+ "markdown.notText": "Este arquivo não é um texto simples que a prévia consiga mostrar. Baixe o original para abri-lo em outro app.",
"markdown.rendered": "Ver formatado",
"table.loading": "Abrindo a tabela…",
"table.tooLarge": "Este arquivo é grande demais para a prévia no app (10 MB, 100.000 linhas, 200 colunas ou 500.000 células). Baixe o original para ver tudo.",