-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(electron): add basic download functionality (#277)
- Loading branch information
Showing
13 changed files
with
153 additions
and
31 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const zip = require('cross-zip'); | ||
const {app} = require('electron'); | ||
|
||
/** | ||
* Intercepts all browser downloads in the given window | ||
*/ | ||
module.exports.addDownloadHandler = function(browserWindow) { | ||
browserWindow.webContents.session.on('will-download', (_, item) => { | ||
const downloadsPath = app.getPath('downloads'); | ||
const tmpFilePath = path.join(downloadsPath, `${Date.now()}.zip`); | ||
item.setSavePath(tmpFilePath); | ||
|
||
console.log(`Downloading file ${item.getURL()} to ${item.savePath}`); | ||
|
||
item.on('updated', (event, state) => { | ||
if (state === 'interrupted') { | ||
console.log('Download is interrupted but can be resumed'); | ||
} else if (state === 'progressing') { | ||
if (item.isPaused()) { | ||
console.log('Download is paused'); | ||
} else { | ||
console.log(`Received bytes: ${item.getReceivedBytes()}`); | ||
} | ||
} | ||
}); | ||
|
||
item.once('done', (event, state) => { | ||
if (state === 'completed') { | ||
console.log('Download successfully', item.savePath); | ||
zip.unzipSync(item.savePath, downloadsPath); | ||
fs.unlinkSync(item.savePath); | ||
} else { | ||
console.log(`Download failed: ${state}`, item.savePath); | ||
} | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* Webpack will only include this module when building for web | ||
*/ | ||
|
||
export function isElectron() { | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* Webpack will only include this module when building for electron | ||
*/ | ||
|
||
import {remote} from 'electron'; | ||
|
||
const webContents = remote.getCurrentWebContents(); | ||
|
||
export function isElectron() { | ||
return true; | ||
} | ||
|
||
export function downloadUrl(url: string) { | ||
webContents.downloadURL(url); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const path = require('path'); | ||
const webConfigFn = require('./webpack.config.js'); | ||
|
||
module.exports = (env, {mode} = {}) => { | ||
const config = webConfigFn(env, mode); | ||
|
||
config.target = 'electron-renderer'; | ||
config.resolve.alias.electronHelpers = path.resolve( | ||
__dirname, | ||
'./src/scripts/libs/electron-helpers.ts' | ||
); | ||
|
||
return config; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters