-
Notifications
You must be signed in to change notification settings - Fork 427
RI-7799: Fix download report for electron #5293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,41 @@ | ||
| import { CustomHeaders } from 'uiSrc/constants/api' | ||
|
|
||
| /** | ||
| * Triggers a file download from a URL by creating a temporary link element | ||
| * Triggers a file download from a URL using fetch with proper headers | ||
| * This is necessary for Electron app where window ID authentication is required | ||
| * @param url The full URL to download from | ||
| */ | ||
| export const triggerDownloadFromUrl = (url: string): void => { | ||
| export const triggerDownloadFromUrl = async (url: string): Promise<void> => { | ||
| const headers: Record<string, string> = {} | ||
|
|
||
| // Add window ID header for Electron app authentication | ||
| if (window.windowId) { | ||
| headers[CustomHeaders.WindowId] = window.windowId | ||
| } | ||
|
Check warning on line 14 in redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts
|
||
|
|
||
| const response = await fetch(url, { headers }) | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error(`Download failed: ${response.statusText}`) | ||
| } | ||
|
Check warning on line 20 in redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts
|
||
|
|
||
| // Extract filename from Content-Disposition header | ||
| const contentDisposition = response.headers.get('content-disposition') || '' | ||
|
Check warning on line 23 in redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts
|
||
| const filenameMatch = contentDisposition.match(/filename="?([^";\n]+)"?/) | ||
| const filename = filenameMatch?.[1] || 'download' | ||
|
Check warning on line 25 in redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts
|
||
|
|
||
| // Convert response to blob and trigger download | ||
| const blob = await response.blob() | ||
| const blobUrl = URL.createObjectURL(blob) | ||
|
|
||
| const link = document.createElement('a') | ||
| link.href = url | ||
| link.href = blobUrl | ||
| link.download = filename | ||
| link.style.display = 'none' | ||
| document.body.appendChild(link) | ||
| link.click() | ||
| document.body.removeChild(link) | ||
|
|
||
| // Clean up the blob URL | ||
| URL.revokeObjectURL(blobUrl) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Blob URL revoked immediately may fail download
The
URL.revokeObjectURL(blobUrl)is called synchronously right afterlink.click(). However,click()only initiates the download and returns immediately - it doesn't wait for the download to complete or even start. In some browsers or environments (including Electron, which this PR specifically targets), the blob URL may be revoked before the browser has finished reading the blob data, causing the download to fail silently. A common solution is to delay the revocation usingsetTimeoutor defer it until after a brief period.