Files
windmill/frontend/src/lib/utils/downloadFile.ts
T
905a92ab32 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>
2026-11-19 09:59:40 +01:00

50 lines
1.7 KiB
TypeScript

import { OpenAPI } from '$lib/gen'
import { getHeaders } from '$lib/gen/core/request'
import { sendUserToast } from '$lib/toast'
/**
* When OpenAPI is configured to authenticate via headers (TOKEN, basic auth,
* or arbitrary HEADERS) we cannot rely on a plain `<a href>` browser
* navigation because the browser does not attach those headers. In that case
* fetch the file via the OpenAPI client (which carries the configured auth)
* and trigger a download from a blob URL. Cookie-only auth still works with
* a plain link.
*
* `apiPath` should be the path relative to OpenAPI.BASE, starting with `/`
* (e.g. `/w/foo/job_helpers/download_s3_file?file_key=...`).
*/
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> {
let response: Response
try {
response = await fetchViaClient(apiPath)
} catch (e) {
sendUserToast(`Download failed: ${e}`, true)
return
}
if (!response.ok) {
sendUserToast(`Download failed: ${response.status} ${response.statusText}`, true)
return
}
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
a.download = filename
document.body.appendChild(a)
a.click()
a.remove()
URL.revokeObjectURL(blobUrl)
}