|
| 1 | +////////////////////////////////////////////////////////////////////////// |
| 2 | +// |
| 3 | +// pgAdmin 4 - PostgreSQL Tools |
| 4 | +// |
| 5 | +// Copyright (C) 2013 - 2025, The pgAdmin Development Team |
| 6 | +// This software is released under the PostgreSQL Licence |
| 7 | +// |
| 8 | +////////////////////////////////////////////////////////////////////////// |
| 9 | + |
| 10 | +import usePreferences from '../../preferences/static/js/store'; |
| 11 | +import { callFetch, parseApiError } from './api_instance'; |
| 12 | +import { getBrowser, toPrettySize } from './utils'; |
| 13 | + |
| 14 | +const DownloadUtils = { |
| 15 | + downloadViaLink: function (url, fileName) { |
| 16 | + const link = document.createElement('a'); |
| 17 | + link.href = url; |
| 18 | + link.download = fileName; |
| 19 | + document.body.appendChild(link); |
| 20 | + link.click(); |
| 21 | + document.body.removeChild(link); |
| 22 | + }, |
| 23 | + |
| 24 | + downloadBlob: function (blob, fileName) { |
| 25 | + const urlCreator = window.URL || window.webkitURL; |
| 26 | + const downloadUrl = urlCreator.createObjectURL(blob); |
| 27 | + |
| 28 | + this.downloadViaLink(downloadUrl, fileName); |
| 29 | + window.URL.revokeObjectURL(downloadUrl); |
| 30 | + }, |
| 31 | + |
| 32 | + downloadTextData: function (textData, fileName, fileType) { |
| 33 | + const respBlob = new Blob([textData], {type : fileType}); |
| 34 | + this.downloadBlob(respBlob, fileName); |
| 35 | + }, |
| 36 | + |
| 37 | + downloadBase64UrlData: function (downloadUrl, fileName) { |
| 38 | + this.downloadViaLink(downloadUrl, fileName); |
| 39 | + }, |
| 40 | + |
| 41 | + downloadFileStream: async function (allOptions, fileName, fileType, onProgress) { |
| 42 | + const data = []; |
| 43 | + const response = await callFetch(allOptions.url, allOptions.options); |
| 44 | + if(!response.ok) { |
| 45 | + throw new Error(parseApiError(await response.json())); |
| 46 | + } |
| 47 | + if (!response.body) { |
| 48 | + throw new Error(response.statusText); |
| 49 | + } |
| 50 | + |
| 51 | + const reader = response.body.getReader(); |
| 52 | + |
| 53 | + let done = false; |
| 54 | + let receivedLength = 0; // received bytes |
| 55 | + while (!done) { |
| 56 | + const { value, done: doneReading } = await reader.read(); |
| 57 | + done = doneReading; |
| 58 | + if (value) { |
| 59 | + data.push(value); |
| 60 | + receivedLength += value.length; |
| 61 | + onProgress?.(toPrettySize(receivedLength, 'B', 2)); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + const blob = new Blob(data, {type: fileType}); |
| 66 | + this.downloadBlob(blob, fileName); |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | +// If we are in Electron, we will use the Electron API to download files. |
| 71 | +if(getBrowser().name == 'Electron') { |
| 72 | + DownloadUtils.downloadTextData = async (textData, fileName, _fileType) =>{ |
| 73 | + const {automatically_open_downloaded_file, prompt_for_download_location} = usePreferences.getState().getPreferencesForModule('misc'); |
| 74 | + await window.electronUI.downloadTextData(textData, { |
| 75 | + defaultPath: fileName, |
| 76 | + }, prompt_for_download_location, automatically_open_downloaded_file); |
| 77 | + }; |
| 78 | + |
| 79 | + DownloadUtils.downloadBase64UrlData = async (downloadUrl, fileName) => { |
| 80 | + const {automatically_open_downloaded_file, prompt_for_download_location} = usePreferences.getState().getPreferencesForModule('misc'); |
| 81 | + await window.electronUI.downloadBase64UrlData(downloadUrl, { |
| 82 | + defaultPath: fileName, |
| 83 | + }, prompt_for_download_location, automatically_open_downloaded_file); |
| 84 | + }; |
| 85 | + |
| 86 | + DownloadUtils.downloadFileStream = async (allOptions, fileName, _fileType, onProgress)=>{ |
| 87 | + const {automatically_open_downloaded_file, prompt_for_download_location} = usePreferences.getState().getPreferencesForModule('misc'); |
| 88 | + const filePath = await window.electronUI.getDownloadStreamPath({ |
| 89 | + defaultPath: fileName, |
| 90 | + }, prompt_for_download_location); |
| 91 | + |
| 92 | + // If the user cancels the download, we will not proceed |
| 93 | + if(!filePath) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + const response = await callFetch(allOptions.url, allOptions.options); |
| 98 | + if(!response.ok) { |
| 99 | + throw new Error(parseApiError(await response.json())); |
| 100 | + } |
| 101 | + if (!response.body) { |
| 102 | + throw new Error(response.statusText); |
| 103 | + } |
| 104 | + |
| 105 | + const contentLength = response.headers.get('Content-Length'); |
| 106 | + window.electronUI.downloadStreamSaveTotal(filePath, contentLength ? parseInt(contentLength, 10) : null); |
| 107 | + |
| 108 | + const reader = response.body.getReader(); |
| 109 | + |
| 110 | + let done = false; |
| 111 | + let receivedLength = 0; // received bytes |
| 112 | + while (!done) { |
| 113 | + const { value, done: doneReading } = await reader.read(); |
| 114 | + done = doneReading; |
| 115 | + if (value) { |
| 116 | + window.electronUI.downloadStreamSaveChunk(filePath, value); |
| 117 | + receivedLength += value.length; |
| 118 | + onProgress?.(toPrettySize(receivedLength, 'B', 2)); |
| 119 | + } |
| 120 | + } |
| 121 | + window.electronUI.downloadStreamSaveEnd(filePath, automatically_open_downloaded_file); |
| 122 | + }; |
| 123 | +} |
| 124 | + |
| 125 | +export default DownloadUtils; |
0 commit comments