Skip to content

Commit 5d8ec6e

Browse files
Fix issues found while testing changes for large file download. #3369
1 parent e711a1b commit 5d8ec6e

3 files changed

Lines changed: 37 additions & 14 deletions

File tree

runtime/src/js/downloader.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,29 @@ function updateProgress(callerWindow) {
6363
setProgress.call(callerWindow, progress);
6464
}
6565

66+
async function fileDownloadPath(callerWindow, options, prompt=true) {
67+
let filePath = path.join(app.getPath('downloads'), options.defaultPath);
68+
// prompt is true when the user has set the preference to prompt for download location
69+
if(prompt) {
70+
const result = await dialog.showSaveDialog(callerWindow, {
71+
title: 'Save File',
72+
...options,
73+
});
74+
75+
if (result.canceled) {
76+
return;
77+
}
78+
filePath = result.filePath;
79+
}
80+
return filePath;
81+
}
82+
6683
export function setupDownloader() {
6784
// Listen for the renderer's request to show the open dialog
6885
ipcMain.handle('get-download-path', async (event, options, prompt=true) => {
6986
try {
70-
let filePath = path.join(app.getPath('downloads'), options.defaultPath);
7187
const callerWindow = BrowserWindow.fromWebContents(event.sender);
72-
// prompt is true when the user has set the preference to prompt for download location
73-
if(prompt) {
74-
const result = await dialog.showSaveDialog(callerWindow, {
75-
title: 'Save File',
76-
...options,
77-
});
78-
79-
if (result.canceled) {
80-
return;
81-
}
82-
filePath = result.filePath;
83-
}
84-
88+
const filePath = await fileDownloadPath(callerWindow, options, prompt);
8589
downloadQueue[filePath] = new DownloadItem(filePath, () => {
8690
updateProgress(callerWindow);
8791
}, () => {
@@ -118,4 +122,13 @@ export function setupDownloader() {
118122
openFile && shell.openPath(filePath);
119123
}
120124
});
125+
126+
// non-streaming direct download
127+
ipcMain.handle('download-base64-url', async (event, base64url, options, prompt=true, openFile=false) => {
128+
const callerWindow = BrowserWindow.fromWebContents(event.sender);
129+
const filePath = await fileDownloadPath(callerWindow, options, prompt);
130+
const buffer = Buffer.from(base64url.split(',')[1], 'base64');
131+
fs.writeFileSync(filePath, buffer);
132+
openFile && shell.openPath(filePath);
133+
});
121134
}

runtime/src/js/pgadmin_preload.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ contextBridge.exposeInMainWorld('electronUI', {
2929
downloadDataSaveChunk: (...args) => ipcRenderer.send('download-data-save-chunk', ...args),
3030
downloadDataSaveTotal: (...args) => ipcRenderer.send('download-data-save-total', ...args),
3131
downloadDataSaveEnd: (...args) => ipcRenderer.send('download-data-save-end', ...args),
32+
downloadBase64UrlData: (...args) => ipcRenderer.invoke('download-base64-url', ...args)
3233
});

web/pgadmin/static/js/download_utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ import { getBrowser, toPrettySize } from './utils';
1414
// This function is used to download the base64 data
1515
// and create a link to download the file.
1616
export async function downloadBase64UrlData(downloadUrl, fileName) {
17+
if(getBrowser().name == 'Electron') {
18+
const {automatically_open_downloaded_file, prompt_for_download_location} = usePreferences.getState().getPreferencesForModule('misc');
19+
// In Electron, we use the electronUI to download the file.
20+
await window.electronUI.downloadBase64UrlData(downloadUrl, {
21+
defaultPath: fileName,
22+
}, prompt_for_download_location, automatically_open_downloaded_file);
23+
return;
24+
}
25+
// In other browsers, we create a link to download the file.
1726
let link = document.createElement('a');
1827
link.setAttribute('href', downloadUrl);
1928
link.setAttribute('download', fileName);

0 commit comments

Comments
 (0)