From 2e52ec6c6833c653010783cb8a4acd57dfd750ed Mon Sep 17 00:00:00 2001 From: Stephen Oney Date: Sat, 1 Aug 2015 00:48:20 -0400 Subject: [PATCH] Sends all records to a directory --- lib/mkdirp.js | 12 +++++++ lib/voice-assist.js | 73 ++++++++++++++++++++++++++++++++++------ lib/voiceRecorder.js | 36 +++++--------------- lib/workspaceSnapshot.js | 61 +++++++++++++++++++++++++++++++++ package.json | 3 +- 5 files changed, 146 insertions(+), 39 deletions(-) create mode 100644 lib/mkdirp.js create mode 100644 lib/workspaceSnapshot.js diff --git a/lib/mkdirp.js b/lib/mkdirp.js new file mode 100644 index 0000000..9052ecb --- /dev/null +++ b/lib/mkdirp.js @@ -0,0 +1,12 @@ +var mkdirpLib = require('mkdirp'); +module.exports = function(path) { + return new Promise(function(resolve, reject) { + mkdirpLib(path, function(err) { + if(err) { + reject(err); + } else { + resolve(path); + } + }); + }); +}; diff --git a/lib/voice-assist.js b/lib/voice-assist.js index 57eb453..85947fe 100644 --- a/lib/voice-assist.js +++ b/lib/voice-assist.js @@ -1,5 +1,8 @@ var EventEmitter = require('events').EventEmitter; +var mkdirp = require('./mkdirp'); +var fs = require('fs'); var RecordingBarView = require('./RecordingBarView'); +var STORAGE_DIRECTORY = __dirname + '/recordings/'; var recorder = new EventEmitter(); recorder._recording = false; @@ -58,30 +61,78 @@ recorder.serialize = function() { var recordingBarView; var voiceRecorder = require('./voiceRecorder'); var editorRecorder = require('./editorRecorder'); +var workspaceSnapshot = require('./workspaceSnapshot'); var modalPanel; +var uid; recorder.on('start', function() { - voiceRecorder.start(); - editorRecorder.start(); + uid = guid(); + voiceRecorder.start(uid); + editorRecorder.start(uid); + workspaceSnapshot.start(uid); modalPanel.show(); }).on('stop', function() { - var changelogPromise = editorRecorder.stop(); - var promises = voiceRecorder.stop(), + var changelogPromise = editorRecorder.stop(uid); + var promises = voiceRecorder.stop(uid), wavPromise = promises.wav, transcriptPromise = promises.transcript; - Promise.all([wavPromise, transcriptPromise, changelogPromise]).then(function(info) { + var zipPromise = workspaceSnapshot.stop(uid); + Promise.all([wavPromise, transcriptPromise, changelogPromise, zipPromise]).then(function(info) { var wavFilename = info[0], transcript = info[1], - changelog = info[2]; - console.log("Wav file: ", wavFilename); - console.log("Transcript: ", transcript); - console.log("Changes: ", changelog) + changelog = info[2], + zip = info[3]; + + var recordingInfo = { + wav: wavFilename, + transcript: transcript, + changelog: changelog, + zip: zip + }; + + var folder = STORAGE_DIRECTORY + uid + '/', + filename = 'recording.json', + fullFilename = folder + filename; + + return mkdirp(folder).then(function() { + return new Promise(function(resolve, reject) { + var infoString; + try { + infoString = JSON.stringify(recordingInfo); + } catch(e) { + reject(e); + return; + } + + fs.writeFile(fullFilename, infoString, function(err) { + if(err) { + reject(err); + } else { + resolve(fullFilename); + } + }) + }); + }); + }).then(function(infoFile) { + console.log(infoFile); + }, function(err) { + console.error(err.stack); }); modalPanel.hide(); }).on('cancel', function() { - editorRecorder.cancel(); - voiceRecorder.cancel(); + editorRecorder.cancel(uid); + voiceRecorder.cancel(uid); + workspaceSnapshot.cancel(uid); modalPanel.hide(); }) +function guid() { + function s4() { + return Math .floor((1 + Math.random()) * 0x10000) + .toString(16) + .substring(1); + } + return s4() + s4() + '-' + s4() + '-' + s4() + '-' + + s4() + '-' + s4() + s4() + s4(); +} module.exports = recorder; diff --git a/lib/voiceRecorder.js b/lib/voiceRecorder.js index b662b1c..281ce0a 100644 --- a/lib/voiceRecorder.js +++ b/lib/voiceRecorder.js @@ -1,7 +1,7 @@ var _ = require('./vendor/underscore'), RecordRTC = require('recordrtc'), fs = require('fs'), - mkdirpLib = require('mkdirp'); + mkdirp = require('./mkdirp'); var RECORDING_STORAGE_DIRECTORY = __dirname + '/recordings/'; @@ -10,16 +10,6 @@ recognitionEngine.continue = false; recognitionEngine.interimResults = false; recognitionEngine.maxResults = 1; -function guid() { - function s4() { - return Math .floor((1 + Math.random()) * 0x10000) - .toString(16) - .substring(1); - } - return s4() + s4() + '-' + s4() + '-' + s4() + '-' + - s4() + '-' + s4() + s4() + s4(); -} - function getUserMedia() { return new Promise(function(resolve, reject) { try { @@ -36,21 +26,12 @@ function getUserMedia() { }); } -function mkdirp(path) { - return new Promise(function(resolve, reject) { - mkdirpLib(path, function(err) { - if(err) { - reject(err); - } else { - resolve(path); - } - }); - }); -} var transcriptPromise, audioStreamPromise, recorder; +var question_id; module.exports = { - start: function () { + start: function (uid) { + question_id = uid; transcriptPromise = new Promise(function(resolve, reject) { recognitionEngine.start(); recognitionEngine.onresult = function(event) { @@ -70,7 +51,7 @@ module.exports = { return audioStream; }); }, - stop: function() { + stop: function(uid) { recognitionEngine.stop(); var wavPromise = audioStreamPromise.then(function(audioStream) { return new Promise(function(resolve, reject) { @@ -89,10 +70,11 @@ module.exports = { fr.readAsBinaryString(blob); }); }).then(function(binaryContent) { - var filename = guid() + ".wav", - fullFilename = RECORDING_STORAGE_DIRECTORY + filename; + var folder = RECORDING_STORAGE_DIRECTORY + uid + '/', + filename = 'audio.wav', + fullFilename = folder + filename; - return mkdirp(RECORDING_STORAGE_DIRECTORY).then(function() { + return mkdirp(folder).then(function() { return new Promise(function(resolve, reject) { fs.writeFile(fullFilename, binaryContent, "binary", function(err) { if(err) { diff --git a/lib/workspaceSnapshot.js b/lib/workspaceSnapshot.js new file mode 100644 index 0000000..2823a31 --- /dev/null +++ b/lib/workspaceSnapshot.js @@ -0,0 +1,61 @@ +var _ = require('./vendor/underscore'), + archiver = require('archiver'), + mkdirp = require('./mkdirp'), + fs = require('fs'); + +var archive; +var WORKSPACE_STORAGE_DIRECTORY = __dirname + '/recordings/'; +var output; +var zipPromise; +var filename; + +module.exports = { + start: function (uid) { + var rootDirectories = atom.project.getDirectories(), + directoryPaths = _.map(rootDirectories, function(dir) { + return dir.getPath(); + }); + + var folder = WORKSPACE_STORAGE_DIRECTORY + uid + '/', + filename = 'workspace.zip', + fullFilename = folder + filename; + + zipPromise = mkdirp(folder).then(function() { + output = fs.createWriteStream(fullFilename); + + var outputClosePromise = new Promise(function(resolve, reject) { + output.on('close', function() { + resolve(fullFilename); + }); + }); + + archive = archiver.create('zip', {}); + archive.pipe(output); + + _.each(directoryPaths, function(path) { + archive.directory(path); + }) + + archive.finalize(); + return outputClosePromise; + }, function(e) { + console.error(e.stack); + }); + }, + stop: function() { + return zipPromise; + }, + cancel: function(uid) { + var folder = WORKSPACE_STORAGE_DIRECTORY + uid + '/', + filename = 'workspace.zip', + fullFilename = folder + filename; + + return zipPromise.then(function(filename) { + return new Promise(function(resolve) { + fs.unlink(filename, function() { + resolve(); + }); + }); + }) + } +}; diff --git a/package.json b/package.json index 25876dd..f1e8365 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "mkdirp": "latest", "atom-helpers": "latest", "jquery": "latest", - "diff": "latest" + "diff": "latest", + "archiver": "latest" } }