-
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): new electron bridge (#290)
* feat(electron): add downloaded data * chore(data): change soil moisture color map * feat(electron): download progress * feat(electron): improve security by disabling node integration * fix(typos): typos
- Loading branch information
Showing
30 changed files
with
375 additions
and
190 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,33 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const {app} = require('electron'); | ||
|
||
const {getDownloadedIds} = require('./get-downloaded-ids'); | ||
|
||
/** | ||
* Removes the folder matching the given id from the offline directoy | ||
*/ | ||
module.exports = function deleteId(browserWindow, id) { | ||
// check if id contains '/', '\', '..' or ':' | ||
if (id.match(/:|\/|\\|\.\./)) { | ||
throw new Error('deleteId: Invalid id'); | ||
} | ||
|
||
const downloadsPath = app.getPath('downloads'); | ||
const pathToDelete = path.join(downloadsPath, id); | ||
|
||
if (!fs.statSync(pathToDelete).isDirectory() || id.length < 5) { | ||
throw new Error('deleteId: Path to delete does not exist', pathToDelete); | ||
} | ||
|
||
console.log('Deleting', pathToDelete); | ||
|
||
fs.rmdir(pathToDelete, {recursive: true}, err => { | ||
if (!err) { | ||
browserWindow.webContents.send( | ||
'offline-update', | ||
JSON.stringify(getDownloadedIds()) | ||
); | ||
} | ||
}); | ||
}; |
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,22 @@ | ||
const fs = require('fs'); | ||
const {app} = require('electron'); | ||
|
||
/** | ||
* Get downloaded Ids from the downloads folder content | ||
*/ | ||
module.exports.getDownloadedIds = function() { | ||
const dirContent = fs | ||
.readdirSync(app.getPath('downloads'), { | ||
withFileTypes: true | ||
}) | ||
.filter(entry => entry.isDirectory()) | ||
.map(entry => entry.name); | ||
|
||
const layers = dirContent.filter(name => !name.startsWith('story')); | ||
const stories = dirContent.filter(name => name.startsWith('story')); | ||
|
||
return { | ||
layers, | ||
stories | ||
}; | ||
}; |
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,33 @@ | ||
const path = require('path'); | ||
const {contextBridge, remote, ipcRenderer} = require('electron'); | ||
const app = remote.app; | ||
|
||
// Returns the currently set "Downloads" folder joined with the given path parts | ||
function getDownloadsPath(...parts) { | ||
return path.join(app.getPath('downloads'), ...parts); | ||
} | ||
|
||
// Downloads the content at the given URL | ||
// the download will be handled by the electron 'will-download' handler) | ||
function downloadUrl(url) { | ||
remote.getCurrentWebContents().downloadURL(url); | ||
} | ||
|
||
// Delete the offline folder of the given layer or story id | ||
function deleteId(id) { | ||
const deleteRemoteFn = remote.require('./download-delete'); | ||
const browserWindow = remote.BrowserWindow.getFocusedWindow(); | ||
deleteRemoteFn(browserWindow, id); | ||
} | ||
|
||
// The context of the preload script and the browser windows context are both | ||
// isolated for security reasons (contextIsolation: true). | ||
// That's why we have to expose values and functions via the context bridge. | ||
// see https://www.electronjs.org/docs/tutorial/security#3-enable-context-isolation-for-remote-content | ||
contextBridge.exposeInMainWorld('cfs', { | ||
isElectron: true, | ||
getDownloadsPath, | ||
downloadUrl, | ||
deleteId, | ||
addIpcListener: (channel, callback) => ipcRenderer.on(channel, callback) | ||
}); |
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,17 @@ | ||
import {DownloadProgress} from '../types/download-progress'; | ||
|
||
export const SET_DOWNLOAD_PROGRESS = 'SET_DOWNLOAD_PROGRESS'; | ||
|
||
export interface SetDownloadProgressAction { | ||
type: typeof SET_DOWNLOAD_PROGRESS; | ||
progress: DownloadProgress; | ||
} | ||
|
||
const setDownloadProgressAction = ( | ||
progress: DownloadProgress | ||
): SetDownloadProgressAction => ({ | ||
type: SET_DOWNLOAD_PROGRESS, | ||
progress | ||
}); | ||
|
||
export default setDownloadProgressAction; |
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
Oops, something went wrong.