Skip to content

Instantly share code, notes, and snippets.

const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');
// If modifying these scopes, delete token.json.
const SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly'];
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
const TOKEN_PATH = 'token.json';
var fileMetadata = {
'name': 'photo.jpg'
};
var media = {
mimeType: 'image/jpeg',
body: fs.createReadStream('files/photo.jpg')
};
drive.files.create({
resource: fileMetadata,
media: media,
@trevorfoskett
trevorfoskett / changeQuickstart.js
Created November 22, 2019 06:46
Change the scope to allow upload and replace 'listFiles()' function.
//const SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly'];
const SCOPES = ['https://www.googleapis.com/auth/drive'];
//function listFiles(auth) {...}
function upload(auth) {
var fileMetadata = {
'name': 'photo.jpg'
};
var media = {
mimeType: 'image/jpeg',
@trevorfoskett
trevorfoskett / encryptAll.js
Created November 22, 2019 06:51
Virtru SDK sample code to encrypt all files in a directory.
const Virtru = require('virtru-sdk');
var fs = require('fs');
const email = 'some-email';
const appId = 'some-appid';
const sourceDir = 'some-input-dir';
const destDir = 'some-output-dir';
// Initialize the client.
const client = new Virtru.Client({email, appId});
@trevorfoskett
trevorfoskett / googleSecrets.js
Last active November 22, 2019 06:56
Load Google Auth secrets, and call virtruStart() function.
function virtruStart(auth) {
promises = fs.readdirSync('./encrypt-in/').filter(function(x) {
return x !== '.DS_Store';
}).map(fileName => encrypt(fileName, auth)); // calling encrypt() on each file.
Promise.all(promises).then(() =>
console.log(`Encrypted & Uploaded:`));
}
// *** Google Auth ***
// Load client secrets from a local file.
@trevorfoskett
trevorfoskett / encryptForDrive.js
Last active November 22, 2019 07:00
Basic Virtru encryption function.
async function encrypt(fileName){
const encryptParams = new Virtru.EncryptParamsBuilder()
.withFileSource(`./encrypt-in/${fileName}`)
.withDisplayFilename(fileName)
.build();
ct = await client.encrypt(encryptParams);
var ctString = await ct.toString();
}
@trevorfoskett
trevorfoskett / encryptUpload.js
Created November 22, 2019 07:01
Added upload functionality to the encrypt function.
async function encrypt(fileName, auth){
const encryptParams = new Virtru.EncryptParamsBuilder()
.withFileSource(`./encrypt-in/${fileName}`)
.withDisplayFilename(fileName)
.build();
ct = await client.encrypt(encryptParams);
var ctString = await ct.toString();
const drive = google.drive({version: 'v3', auth});
@trevorfoskett
trevorfoskett / listDownload.js
Created November 22, 2019 07:13
List, then download encrypted files in Drive by ID.
drive.files.list({
fields: 'nextPageToken, files(id, name)',
q: `'${folderId}' in parents and name contains "tdf3.html" and trashed = false`
}, (err, res) => {
if (err) return console.log('The API returned an error: ' + err);
// Generate list of files.
const files = res.data.files;
if (files.length) {
files.map((file) => {
// Get name and ID of each file.
@trevorfoskett
trevorfoskett / streamToTmp
Last active November 22, 2019 07:33
Set the download location for the stream data to /tmp.
...
var dest = fs.createWriteStream(`/tmp/${uFileName}`); // download encrypted content to /tmp
drive.files.get({
fileId: fileId,
alt: 'media'
}, {
responseType: 'stream'
}...
...
@trevorfoskett
trevorfoskett / uniqueFileName.js
Created November 22, 2019 07:18
Assign a unique filename to each file.
...
var fileName = file.name;
// Assign a unique name (uFileName) to each file (fileName
// + randnum)to ensure client will not attempt to use same
//set of keys for multiple files of the same name.
var uFileName = `${fileName}|${Math.random()}`;
// Set temporary storage location in /tmp/
var dest = fs.createWriteStream(`/tmp/${uFileName}`);
...