downloadFiles: Fix issue of showing two Save As dialog box.

Currently, there are two dialog boxes shown while downloading files (in Ubuntu). One by default behavior of electron and other by the dialog box for save as feature.
This PR fixes this issue by using electron's save as dialog box.

Fixes: #947.
This commit is contained in:
Abhigyan Khaund
2020-05-01 17:55:46 +05:30
committed by GitHub
parent d9afee3330
commit 82421d843a
2 changed files with 18 additions and 27 deletions

View File

@@ -5,7 +5,7 @@ import {setAutoLaunch} from './startup';
import windowStateKeeper from 'electron-window-state';
import path from 'path';
import fs from 'fs';
import electron, {app, ipcMain, session, dialog} from 'electron';
import electron, {app, ipcMain, session} from 'electron';
import * as AppMenu from './menu';
import * as BadgeSettings from '../renderer/js/pages/preference/badge-settings';
@@ -323,21 +323,11 @@ app.on('ready', () => {
ipcMain.on('downloadFile', (_event: Electron.IpcMainEvent, url: string, downloadPath: string) => {
page.downloadURL(url);
page.session.once('will-download', async (_event: Event, item) => {
let setFilePath: string;
let shortFileName: string;
if (ConfigUtil.getConfigItem('promptDownload', false)) {
const showDialogOptions: object = {
defaultPath: path.join(downloadPath, item.getFilename())
};
const result = await dialog.showSaveDialog(mainWindow, showDialogOptions);
if (result.canceled) {
item.cancel();
return;
}
setFilePath = result.filePath;
shortFileName = path.basename(setFilePath);
item.setSaveDialogOptions(showDialogOptions);
} else {
const getTimeStamp = (): number => {
const date = new Date();
@@ -354,12 +344,10 @@ app.on('ready', () => {
// Update the name and path of the file if it already exists
const updatedFilePath = path.join(downloadPath, formatFile(filePath));
setFilePath = fs.existsSync(filePath) ? updatedFilePath : filePath;
shortFileName = fs.existsSync(filePath) ? formatFile(filePath) : item.getFilename();
const setFilePath: string = fs.existsSync(filePath) ? updatedFilePath : filePath;
item.setSavePath(setFilePath);
}
item.setSavePath(setFilePath);
const updatedListener = (_event: Event, state: string): void => {
switch (state) {
case 'interrupted': {
@@ -387,10 +375,10 @@ app.on('ready', () => {
item.on('updated', updatedListener);
item.once('done', (_event: Event, state) => {
if (state === 'completed') {
page.send('downloadFileCompleted', item.getSavePath(), shortFileName);
page.send('downloadFileCompleted', item.getSavePath(), path.basename(item.getSavePath()));
} else {
console.log('Download failed state:', state);
page.send('downloadFileFailed');
page.send('downloadFileFailed', state);
}
// To stop item for listening to updated events of this file

View File

@@ -34,19 +34,22 @@ export default function handleExternalLink(this: WebView, event: Electron.NewWin
}
});
ipcRenderer.once('downloadFileFailed', () => {
ipcRenderer.once('downloadFileFailed', (_event: Event, state: string) => {
// Automatic download failed, so show save dialog prompt and download
// through webview
// Only do this if it is the automatic download, otherwise show an error (so we aren't showing two save
// prompts right after each other)
if (ConfigUtil.getConfigItem('promptDownload', false)) {
// We need to create a "new Notification" to display it, but just `Notification(...)` on its own
// doesn't work
new Notification('Download Complete', { // eslint-disable-line no-new
body: 'Download failed'
});
} else {
this.$el.downloadURL(url.href);
// Check that the download is not cancelled by user
if (state !== 'cancelled') {
if (ConfigUtil.getConfigItem('promptDownload', false)) {
// We need to create a "new Notification" to display it, but just `Notification(...)` on its own
// doesn't work
new Notification('Download Complete', { // eslint-disable-line no-new
body: 'Download failed'
});
} else {
this.$el.downloadURL(url.href);
}
}
ipcRenderer.removeAllListeners('downloadFileCompleted');