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 <noreply@anthropic.com>

* 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 <noreply@anthropic.com>

* 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 <noreply@anthropic.com>

---------

Co-authored-by: Ruben Fiszel <ruben@windmill.dev>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Raunak Burrows
2026-11-19 09:59:40 +01:00
committed by GitHub
co-authored by Claude Opus 5.5 Ruben Fiszel
parent 9f3f820e6c
commit 905a92ab32
2 changed files with 74 additions and 5 deletions
@@ -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<unknown> {
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 @@
>
</div>
{:else}
{#if tooBigFilename && resultApiPath}
<div class="flex">
<Button
onclick={() => downloadTooBigFile(resultApiPath!, tooBigFilename!)}
disabled={downloadingTooBigFile}
startIcon={{ icon: Download }}
variant="subtle"
unifiedSize="sm"
>
Download {tooBigFilename}
</Button>
</div>
{/if}
{#if largeObject}
<div class="text-xs text-emphasis"
>{#if resultApiPath && shouldDownloadViaClient()}
+10 -4
View File
@@ -17,12 +17,15 @@ export function shouldDownloadViaClient(): boolean {
return Boolean(OpenAPI.TOKEN || OpenAPI.HEADERS || OpenAPI.USERNAME)
}
export async function fetchViaClient(apiPath: string): Promise<Response> {
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<void> {
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