Skip to content

Instantly share code, notes, and snippets.

@trevorfoskett
trevorfoskett / encryptAndUpload.js
Created March 31, 2020 20:30
Chokidar calls the upload function, which includes a call of the encrypt function.
// Virtru encrypt function.
async function encrypt(path, fileName) {
const encryptParams = new Virtru.EncryptParamsBuilder()
.withFileSource(path)
.withDisplayFilename(fileName)
.build();
ct = await client.encrypt(encryptParams);
var ctString = await ct.toString();
return ctString;
}
@trevorfoskett
trevorfoskett / createWatcher.js
Created March 31, 2020 20:28
Creating a folder watcher with Chokidar
// Watch the source folder for changes
async function watchFiles(auth) {
if (store.get('save_location') !== '') {
const watcher = chokidar.watch(store.get('save_location'), {
persistent: true,
ignoreInitial: true // ignores files that already exist in this folder
});
watcher.on('add', async path => {
// code to execute when a new file is added
@trevorfoskett
trevorfoskett / createSettingsUnderTray.js
Created March 31, 2020 20:24
Creating a settings window that will display beneath the Virtru icon in the tray.
// Create an icon in the tray.
const createTray = () => {
tray = new Tray('./images/virtru-mark-black.png');
tray.on('click', function (event) {
toggleWindow()
});
}
// Determine where the tray icon is located so the
// window can be positioned directly below it.
@trevorfoskett
trevorfoskett / settingsPage.js
Created February 27, 2020 17:10
Create a new window containing the settings page.
function createSettings() {
settingsPage = new BrowserWindow({
parent: win,
width: 500,
height: 310,
webPreferences: {
nodeIntegration: true
}
})
@trevorfoskett
trevorfoskett / buildPolicy_.js
Created February 27, 2020 17:08
Construct policy object based on user input.
ipcMain.on('disable-reshare-on', (event) => {
disableResharing = true;
console.log('Disable Resharing: ' + disableResharing);
})
ipcMain.on('disable-reshare-off', (event) => {
disableResharing = false;
console.log('Disable Resharing: ' + disableResharing);
})
@trevorfoskett
trevorfoskett / policyInput.js
Created February 27, 2020 17:00
Accept user input from policy toggles and send information to the main thread.
// Send Disable Reshare information to Main process when button toggled.
const disableReshareBtn = document.getElementById('disable-reshare-toggle');
disableReshareBtn.addEventListener('click', (event) => {
console.log('clicked - disable reshare');
if (disableReshareBtn.checked) {
ipcRenderer.send('disable-reshare-on');
} else {
ipcRenderer.send('disable-reshare-off');
}
})
@trevorfoskett
trevorfoskett / encryptFromDialog.js
Created February 27, 2020 16:29
Pass selected file to a Virtru encrypt function.
async function encrypt(filePath, fileName) {
const encryptParams = new Virtru.EncryptParamsBuilder()
.withFileSource(filePath)
.withDisplayFilename(fileName)
.build();
ct = await client.encrypt(encryptParams);
await ct.toFile(`${filePath}.tdf3.html`);
}
ipcMain.on('open-file-dialog', async (event) => {
@trevorfoskett
trevorfoskett / openDialogMain.js
Created February 27, 2020 16:18
Main thread registers the button click event and opens the file dialog to select filepaths.
ipcMain.on('open-file-dialog', async (event) => {
var paths = dialog.showOpenDialogSync(win, {
properties: ['openFile', 'multiSelections']
});
})
@trevorfoskett
trevorfoskett / buttonClickInRenderer.js
Created February 27, 2020 16:14
Send the "open-file-dialog" message to the main process.
const selectDirBtn = document.getElementById('select-files');
selectDirBtn.addEventListener('click', (event) => {
ipcRenderer.send('open-file-dialog');
})
@trevorfoskett
trevorfoskett / quickStart.js
Created February 27, 2020 16:06
Quickstart Electron code to generate app & window.
// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const path = require('path')
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {