attachement: Handle downloading files failure.

In case of any failure, the app will fall back to the previous download
functionality which is to show a download dialog when a user clicks on 
file attachments.
This commit is contained in:
Abhigyan Khaund
2018-07-23 21:07:51 +05:30
committed by Akash Nimare
parent c0ec292090
commit 3342d7da91
2 changed files with 37 additions and 2 deletions

View File

@@ -246,8 +246,35 @@ app.on('ready', () => {
page.session.once('will-download', (event, item) => {
const filePath = path.join(downloadPath, item.getFilename());
item.setSavePath(filePath);
item.once('done', () => {
page.send('downloadFileCompleted', filePath, item.getFilename());
item.on('updated', (event, state) => {
switch (state) {
case 'interrupted' : {
// Can interrupted to due to network error, cancel download then
console.log('Download interrupted, cancelling and fallback to dialog download.');
item.cancel();
break;
}
case 'progressing': {
if (item.isPaused()) {
item.cancel();
}
// This event can also be used to show progres in percentage in future.
break;
}
default: {
console.info('Unknown updated state of download item');
}
}
});
item.once('done', (event, state) => {
if (state === 'completed') {
page.send('downloadFileCompleted', item.getSavePath(), item.getFilename());
} else {
console.log('Download failed state: ', state);
page.send('downloadFileFailed');
}
// To stop item for listening to updated events of this file
item.removeAllListeners('updated');
});
});
});

View File

@@ -39,6 +39,14 @@ function handleExternalLink(event) {
downloadNotification.onclick = () => {
shell.openItem(filePath);
};
ipcRenderer.removeAllListeners('downloadFileFailed');
});
ipcRenderer.once('downloadFileFailed', () => {
// Automatic download failed, so show save dialog prompt and download
// through webview
this.$el.downloadURL(url);
ipcRenderer.removeAllListeners('downloadFileCompleted');
});
return;
}