Creating a JSON database for storing metadata
This commit is contained in:
parent
15d7013924
commit
f80ea86d3b
|
@ -13,7 +13,7 @@
|
||||||
<form>
|
<form>
|
||||||
<input type="text" id="casename" name="casename" placeholder="Case Name"><br>
|
<input type="text" id="casename" name="casename" placeholder="Case Name"><br>
|
||||||
<input type="text" id="socialmedia" name="socialmedia" placeholder="Social Media"><br>
|
<input type="text" id="socialmedia" name="socialmedia" placeholder="Social Media"><br>
|
||||||
<textarea id="description" name="description" placeholder="Incident Description..." rows="10" cols="30"></textarea>
|
<textarea id="incidentdescription" name="incidentdescription" placeholder="Incident Description..." rows="10" cols="30"></textarea>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<label id="screenshot-path">Path: NONE</label><br>
|
<label id="screenshot-path">Path: NONE</label><br>
|
||||||
|
|
49
renderer.js
49
renderer.js
|
@ -13,9 +13,14 @@ const screenshot = document.getElementById('screen-shot');
|
||||||
const screenshotMsg = document.getElementById('screenshot-path');
|
const screenshotMsg = document.getElementById('screenshot-path');
|
||||||
const pathButton = document.getElementById('path-button');
|
const pathButton = document.getElementById('path-button');
|
||||||
const casenameField = document.getElementById('casename');
|
const casenameField = document.getElementById('casename');
|
||||||
|
const socialmediaField = document.getElementById('socialmedia');
|
||||||
|
const incidentdescriptionField = document.getElementById('incidentdescription');
|
||||||
|
|
||||||
|
|
||||||
var directoryPath = '';
|
var directoryPath = '';
|
||||||
var caseName = '';
|
var caseName = '';
|
||||||
|
var socialMedia = '';
|
||||||
|
var incidentDescription = '';
|
||||||
|
|
||||||
pathButton.addEventListener('click', function(event) {
|
pathButton.addEventListener('click', function(event) {
|
||||||
dialog.showOpenDialog({
|
dialog.showOpenDialog({
|
||||||
|
@ -46,25 +51,61 @@ screenshot.addEventListener('click', function(event) {
|
||||||
sources.forEach(function(source) {
|
sources.forEach(function(source) {
|
||||||
if (source.name === 'Entire Screen' || source.name === 'Entire screen' || source.name === 'Screen 1') {
|
if (source.name === 'Entire Screen' || source.name === 'Entire screen' || source.name === 'Screen 1') {
|
||||||
|
|
||||||
|
// get values from form
|
||||||
caseName = casenameField.value;
|
caseName = casenameField.value;
|
||||||
if (directoryPath === '') {
|
socialMedia = socialmediaField.value;
|
||||||
return displayError("Please Select a Path to save the screenshot to.");
|
incidentDescription = incidentdescriptionField.value;
|
||||||
}
|
|
||||||
|
if (directoryPath === '') return displayError("Please Select a Path to save the screenshot to.");
|
||||||
|
|
||||||
timestamp = new Date().getTime();
|
timestamp = new Date().getTime();
|
||||||
screenshotPath = path.join(directoryPath, caseName + '-' + timestamp + '.png');
|
screenshotFilename = caseName + '-' + timestamp + '.png';
|
||||||
|
screenshotPath = path.join(directoryPath, screenshotFilename);
|
||||||
|
|
||||||
fs.writeFile(screenshotPath, source.thumbnail.toPng(), function(error) {
|
fs.writeFile(screenshotPath, source.thumbnail.toPng(), function(error) {
|
||||||
if (error) return displayError(error.message);
|
if (error) return displayError(error.message);
|
||||||
|
|
||||||
shell.openExternal('file://' + screenshotPath);
|
shell.openExternal('file://' + screenshotPath);
|
||||||
screenshotMsg.textContent = 'Saved screenshot to: ' + screenshotPath;
|
screenshotMsg.textContent = 'Saved screenshot to: ' + screenshotPath;
|
||||||
|
|
||||||
|
// if file was written, save to db as well
|
||||||
|
updateDatabase(directoryPath, screenshotFilename, caseName, socialMedia, incidentDescription);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function updateDatabase(directoryPath, screenshotFilename, caseName, socialMedia, incidentDescription) {
|
||||||
|
// open/read db
|
||||||
|
dbPath = path.join(directoryPath, "documentit-data.json");
|
||||||
|
try {
|
||||||
|
dbContents = fs.readFileSync(dbPath, {encoding: "utf8"});
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
console.log("Unable to open database, starting new db.");
|
||||||
|
dbContents = "{}"; // file doesn't exist, create blank JSON
|
||||||
|
}
|
||||||
|
var database = JSON.parse(dbContents);
|
||||||
|
|
||||||
|
// init db if not init'd
|
||||||
|
// "cases" parent object
|
||||||
|
if (!database.hasOwnProperty('cases')) database.cases = {};
|
||||||
|
// each case is indexed by name (TODO: help users avoid typo'd case names)
|
||||||
|
if (!database.cases.hasOwnProperty(caseName)) database.cases[caseName] = {};
|
||||||
|
|
||||||
|
now = new Date(); // screenshots are indexed by unix timestamp
|
||||||
|
database.cases[caseName][now.getTime()] = {
|
||||||
|
'date': now.toLocaleString(), // human-readable duplicate of index time
|
||||||
|
'socialMedia': socialMedia,
|
||||||
|
'incidentDescription': incidentDescription,
|
||||||
|
'screenshotFilename': screenshotFilename
|
||||||
|
};
|
||||||
|
|
||||||
|
// write db. stringify with two spaces for human-readability
|
||||||
|
fs.writeFileSync(dbPath, JSON.stringify(database, null, ' '));
|
||||||
|
}
|
||||||
|
|
||||||
function displayError(message) {
|
function displayError(message) {
|
||||||
screenshotMsg.textContent = "ERROR: "+message;
|
screenshotMsg.textContent = "ERROR: "+message;
|
||||||
return console.log(message);
|
return console.log(message);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user