Skip to content

Instantly share code, notes, and snippets.

@trevorfoskett
Created March 31, 2020 20:30
Show Gist options
  • Save trevorfoskett/1dbd4b8d194e497cb7e32d1417ff1952 to your computer and use it in GitHub Desktop.
Save trevorfoskett/1dbd4b8d194e497cb7e32d1417ff1952 to your computer and use it in GitHub Desktop.
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;
}
// Upload to Google drive
async function upload(drive, path, fileName, folderId) {
var fileMetadata = {
'name': `${fileName}.tdf3.html`,
parents: [folderId]
};
var media = {
mimeType: 'text/html',
body: await encrypt(path, fileName)
};
drive.files.create({
resource: fileMetadata,
media: media,
fields: 'id'
}, function (err, file) {
if (err) {
console.error(err);
console.log(`${fileName} - Error.`);
} else {
console.log(`${fileName} - Success.`);
}
});
}
// Watch the source folder for changes
async function watchFiles(auth) {
if (store.get('save_location') !== '') {
console.log(`Watching ${store.get('save_location')}`);
var email = getCreds()[0];
var appId = getCreds()[1];
console.log(email, appId);
client = new Virtru.Client({email, appId});
const drive = google.drive({version: 'v3', auth});
const watcher = chokidar.watch(store.get('save_location'), {
persistent: true,
ignoreInitial: true
});
watcher.on('add', async path => {
console.log(path);
var fileName = path.split("/")[(path.split("/")).length-1];
upload(drive, path, fileName, folderId);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment