From 905a92ab32a45b694b99dd60bb0f0244a20ed2e7 Mon Sep 17 00:00:00 2001 From: Raunak Burrows Date: Thu, 19 Nov 2026 08:59:40 +0000 Subject: [PATCH] fix(frontend): download a large file result as its file, not as JSON (#12507) * fix(frontend): download a large file result as its file, not as JSON A result over the backend's inline size cap reaches the result viewer as 'WINDMILL_TOO_BIG', so a big { file: { content, filename } } result fell through to the generic "Download as JSON" link and saved result.json. Fetch just file.filename through json_path to recognise it, and decode file.content into the named file when the user downloads it. Fixes #6287 * fix(frontend): handle a top-level file result and save large files via a blob A file result is either { file, filename } or { file: { content, filename } }. Look up the name at `filename` first and fall back to `file.filename`, and read the content from `file` for both shapes. Download through an object URL instead of a data: URL, which browsers cap below a large file's size. The blob download is shared with downloadViaClient. * fix: offer a large result's file only when the result has one, and keep the JSON download Co-Authored-By: Claude Opus 5.5 * fix: read a large result's root file only on download, and decode it without a per-character array Co-Authored-By: Claude Opus 5.5 * fix: offer a large result's file only for the nested shape, and report a failed download response Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Ruben Fiszel Co-authored-by: Claude Opus 5.5 --- .../src/lib/components/DisplayResult.svelte | 65 ++++++++++++++++++- frontend/src/lib/utils/downloadFile.ts | 14 ++-- 2 files changed, 74 insertions(+), 5 deletions(-) diff --git a/frontend/src/lib/components/DisplayResult.svelte b/frontend/src/lib/components/DisplayResult.svelte index f04c48faa0..95d7e0ab2b 100644 --- a/frontend/src/lib/components/DisplayResult.svelte +++ b/frontend/src/lib/components/DisplayResult.svelte @@ -7,7 +7,13 @@ import { parseDbtRun } from '$lib/components/dbt/parseDbtRun' import { copyToClipboard, parseS3Object, roughSizeOfObject } from '$lib/utils' import ExpandableImage from '$lib/components/common/image/ExpandableImage.svelte' - import { downloadViaClient, shouldDownloadViaClient } from '$lib/utils/downloadFile' + import { + downloadBlob, + downloadViaClient, + fetchViaClient, + shouldDownloadViaClient + } from '$lib/utils/downloadFile' + import { sendUserToast } from '$lib/toast' import { appendViewToken } from '$lib/viewToken' import { Badge, Button, Drawer, DrawerContent } from './common' import { @@ -261,6 +267,50 @@ ) let resultDownloadName = $derived(`${filename ?? 'result'}.json`) + // A result over the backend's inline cap arrives as 'WINDMILL_TOO_BIG'. Ask the backend for + // `file.filename` alone to tell a `{ file: { content, filename } }` result. The root + // `{ file, filename }` shape is a file only when those are its sole keys, which cannot be + // checked without reading the large value, so it keeps the JSON download. + let tooBigFilename: string | undefined = $state(undefined) + let downloadingTooBigFile = $state(false) + async function fetchResultAt(apiPath: string, jsonPath: string): Promise { + const r = await fetchViaClient( + `${apiPath}${apiPath.includes('?') ? '&' : '?'}json_path=${jsonPath}` + ) + if (!r.ok) throw new Error(`${r.status} ${r.statusText}`) + return r.json() + } + $effect(() => { + tooBigFilename = undefined + const apiPath = result === 'WINDMILL_TOO_BIG' ? resultApiPath : undefined + if (!apiPath) return + let stale = false + fetchResultAt(apiPath, 'file.filename').then( + (name) => { + if (!stale && typeof name === 'string') tooBigFilename = name + }, + () => {} + ) + return () => (stale = true) + }) + async function downloadTooBigFile(apiPath: string, name: string) { + downloadingTooBigFile = true + try { + const content = await fetchResultAt(apiPath, 'file.content') + if (typeof content !== 'string' || !content) throw new Error('the result has no file content') + const bin = atob(content) + // Filled byte by byte: a per-character array is several times the file's size + const bytes = new Uint8Array(bin.length) + for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i) + // A blob, not a data: URL, which browsers cap well below a large file's size + downloadBlob(new Blob([bytes]), name) + } catch (e) { + sendUserToast(`Download failed: ${e instanceof Error ? e.message : e}`, true) + } finally { + downloadingTooBigFile = false + } + } + function checkIfS3(result: any, keys: string[]) { return keys.includes('s3') && typeof result.s3 === 'string' } @@ -1327,6 +1377,19 @@ > {:else} + {#if tooBigFilename && resultApiPath} +
+ +
+ {/if} {#if largeObject}
{#if resultApiPath && shouldDownloadViaClient()} diff --git a/frontend/src/lib/utils/downloadFile.ts b/frontend/src/lib/utils/downloadFile.ts index fe37ee5f2c..ebd87f9f67 100644 --- a/frontend/src/lib/utils/downloadFile.ts +++ b/frontend/src/lib/utils/downloadFile.ts @@ -17,12 +17,15 @@ export function shouldDownloadViaClient(): boolean { return Boolean(OpenAPI.TOKEN || OpenAPI.HEADERS || OpenAPI.USERNAME) } +export async function fetchViaClient(apiPath: string): Promise { + const headers = await getHeaders(OpenAPI, { method: 'GET', url: apiPath }) + return fetch(`${OpenAPI.BASE}${apiPath}`, { headers, credentials: OpenAPI.CREDENTIALS }) +} + export async function downloadViaClient(apiPath: string, filename: string): Promise { - const url = `${OpenAPI.BASE}${apiPath}` let response: Response try { - const headers = await getHeaders(OpenAPI, { method: 'GET', url: apiPath }) - response = await fetch(url, { headers, credentials: OpenAPI.CREDENTIALS }) + response = await fetchViaClient(apiPath) } catch (e) { sendUserToast(`Download failed: ${e}`, true) return @@ -31,7 +34,10 @@ export async function downloadViaClient(apiPath: string, filename: string): Prom sendUserToast(`Download failed: ${response.status} ${response.statusText}`, true) return } - const blob = await response.blob() + downloadBlob(await response.blob(), filename) +} + +export function downloadBlob(blob: Blob, filename: string): void { const blobUrl = URL.createObjectURL(blob) const a = document.createElement('a') a.href = blobUrl