<commit_before>/**
 * file_downloader.js for LabJack Switchboard.  Provides Kipling with the ability
 * to download files to a computer's downloads directory and provide the user
 * with feedback as to when that file is completed, as well as the downloads 
 * intermediate progress (with speed, time remaining, progress etc.).
 *
 * This library is meant to be included on startup of k3 and is name-space safe.
 *
 * @author <NAME> (LabJack, 2014)
**/

// Require external npm libraries
var q = require('q');
var request = require('request');
var async = require('async');
var dict = require('dict');
var statusBar = require ('status-bar');
var handlebars = require('handlebars');

// Require nodejs internal libraries to perform get requests.
var http = require('http');
var https = require('https');

// Require remaining nodejs libraries
var path = require('path');
var fs = require('fs');


/**
 * define an object in which all variables will be defined in to help reduce
 * name space clutter.
 * @return {[type]} [description]
 */
function fileDownloaderUtility() {
	this.isInitialized = false;
	this.isDebugMode = (typeof($) === 'undefined');

	this.htmlID = '';
	this.htmlEl = null;
	this.downloadID = '';
	this.downloadEl = null;
	this.closeID = '';
	this.closeEl = null;

	var dlTemplate = '' +
	'<div id="{{newID}}">' +
	'<div id="lvmProgressBar" class="curProgressBar progress">' +
	'<div class="bar" style="width: 0%;"></div>' +
	'</div>' +
	'<table>' +
	'<tr>' +
	'<td>File Name:</td>' +
	'<td id="fileName" class="curFileName">{{fileName}}</td>' +
	'</tr>' +
	'<tr>' +
	'<td>Size:</td>' +
	'<td id="fileSize" class="curFileSize">{{fileSize}}</td>' +
	'</tr>' +
	'<tr>' +
	'<td>Speed:</td>' +
	'<td id="downloadSpeed" class="curDownloadSpeed"></td>' +
	'</tr>' +
	'<tr>' +
	'<td>Time Remaining:</td>' +
	'<td id="timeRemaining" class="curTimeRemaining"></td>' +
	'</tr>' +
	'</table>' +
	'<button id="showInFileButton" class="showInFileButton btn btn-mini btn-link">Show In Finder</button>' +
	'</div>';
	this.downloadTemplate = handlebars.compile(dlTemplate);

	this.setDebugMode = function(val) {
		if(val)
			self.isDebugMode = true;
		else
			self.isDebugMode = false;
	};
	var isDefined = function(obj) {
		if (typeof(obj) !== 'undefined') {
			return true;
		} else {
			return false;
		}
	};

	var getWindowsDownloadsPath = function() {
		var userDirectory = process.env.HOMEPATH;
		if (isDefined(userDirectory)) {
			var downloadsDir = userDirectory + '\\Downloads';
			if (fs.existsSync(downloadsDir)) {
				return downloadsDir;
			}
			downloadsDir = userDirectory + '\\My Documents\\Downloads';
			if (fs.existsSync(downloadsDir)) {
				return downloadsDir;
			}
		}
		return '';
	};
	var getNixDownloadsPath = function() {
		var userDirectory = process.env.HOME;
		if (isDefined(userDirectory)) {
			var downloadsDir = userDirectory + '/Downloads';
			if (fs.existsSync(downloadsDir)) {
				return downloadsDir;
			}
		}
		return '';
	};
	var windowsPath = getWindowsDownloadsPath();
	var nixPath = getNixDownloadsPath();
	var defaultDownloadDirectory = {
		'linux': nixPath,
		'linux2': nixPath,
		'sunos': nixPath,
		'solaris': nixPath,
		'freebsd': nixPath,
		'openbsd': nixPath,
		'darwin': nixPath,
		'mac': nixPath,
		'win32': windowsPath,
	}[process.platform];
	var formatSize = function(numBytes) {
		var kb = Math.pow(2,10);
		var mb = Math.pow(2,20);
		var gb = Math.pow(2,30);
		if (numBytes < kb) {
			return numBytes.toString() + 'Bytes';
		} else if (numBytes < mb) {
			return (numBytes/kb).toPrecision(2) + 'KB';
		} else if (numBytes < gb) {
			return (numBytes/mb).toPrecision(2) + 'MB';
		} else {
			return (numBytes/gb).toPrecision(2) + 'GB';
		}
	};
	var onStartDefaultFunc = function(info) {
		var pageElements = null;
		if(self.isInitialized) {<commit_msg>Customized the "Show in xxx" file download button to be like google chrome for each operating system.<commit_after>/**
 * file_downloader.js for LabJack Switchboard.  Provides Kipling with the ability
 * to download files to a computer's downloads directory and provide the user
 * with feedback as to when that file is completed, as well as the downloads 
 * intermediate progress (with speed, time remaining, progress etc.).
 *
 * This library is meant to be included on startup of k3 and is name-space safe.
 *
 * @author <NAME> (LabJack, 2014)
**/

// Require external npm libraries
var q = require('q');
var request = require('request');
var async = require('async');
var dict = require('dict');
var statusBar = require ('status-bar');
var handlebars = require('handlebars');

// Require nodejs internal libraries to perform get requests.
var http = require('http');
var https = require('https');

// Require remaining nodejs libraries
var path = require('path');
var fs = require('fs');


/**
 * define an object in which all variables will be defined in to help reduce
 * name space clutter.
 * @return {[type]} [description]
 */
function fileDownloaderUtility() {
	this.isInitialized = false;
	this.isDebugMode = (typeof($) === 'undefined');

	this.htmlID = '';
	this.htmlEl = null;
	this.downloadID = '';
	this.downloadEl = null;
	this.closeID = '';
	this.closeEl = null;

	var fileBrowserButtonText = {
		'linux': 'Show in file browser',
		'linux2': 'Show in file browser',
		'sunos': 'Show in file browser',
		'solaris': 'Show in file browser',
		'freebsd': 'Show in file browser',
		'openbsd': 'Show in file browser',
		'darwin': 'Show in file browser',
		'mac': 'Show in finder',
		'win32': 'Show in folder'
	}[process.platform];

	var dlTemplate = '' +
	'<div id="{{newID}}">' +
	'<div id="lvmProgressBar" class="curProgressBar progress">' +
	'<div class="bar" style="width: 0%;"></div>' +
	'</div>' +
	'<table>' +
	'<tr>' +
	'<td>File Name:</td>' +
	'<td id="fileName" class="curFileName">{{fileName}}</td>' +
	'</tr>' +
	'<tr>' +
	'<td>Size:</td>' +
	'<td id="fileSize" class="curFileSize">{{fileSize}}</td>' +
	'</tr>' +
	'<tr>' +
	'<td>Speed:</td>' +
	'<td id="downloadSpeed" class="curDownloadSpeed"></td>' +
	'</tr>' +
	'<tr>' +
	'<td>Time Remaining:</td>' +
	'<td id="timeRemaining" class="curTimeRemaining"></td>' +
	'</tr>' +
	'</table>' +
	'<button id="showInFileButton" class="showInFileButton btn btn-mini btn-link">' +
		fileBrowserButtonText +
	'</button>' +
	'</div>';
	this.downloadTemplate = handlebars.compile(dlTemplate);

	this.setDebugMode = function(val) {
		if(val)
			self.isDebugMode = true;
		else
			self.isDebugMode = false;
	};
	var isDefined = function(obj) {
		if (typeof(obj) !== 'undefined') {
			return true;
		} else {
			return false;
		}
	};

	var getWindowsDownloadsPath = function() {
		var userDirectory = process.env.HOMEPATH;
		if (isDefined(userDirectory)) {
			var downloadsDir = userDirectory + '\\Downloads';
			if (fs.existsSync(downloadsDir)) {
				return downloadsDir;
			}
			downloadsDir = userDirectory + '\\My Documents\\Downloads';
			if (fs.existsSync(downloadsDir)) {
				return downloadsDir;
			}
		}
		return '';
	};
	var getNixDownloadsPath = function() {
		var userDirectory = process.env.HOME;
		if (isDefined(userDirectory)) {
			var downloadsDir = userDirectory + '/Downloads';
			if (fs.existsSync(downloadsDir)) {
				return downloadsDir;
			}
		}
		return '';
	};
	var windowsPath = getWindowsDownloadsPath();
	var nixPath = getNixDownloadsPath();
	var defaultDownloadDirectory = {
		'linux': nixPath,
		'linux2': nixPath,
		'sunos': nixPath,
		'solaris': nixPath,
		'freebsd': nixPath,
		'openbsd': nixPath,
		'darwin': nixPath,
		'mac': nixPath,
		'win32': windowsPath,
	}[process.platform];

	var formatSize = function(numBytes) {
		var kb = Math.pow(2,10);
		var mb = Math.pow(2,20);
		var gb = Math.pow(2,30);
		if (numBytes < kb) {
			return numBytes.toString() + 'Bytes';
		} else if (numBytes < mb) {
			return (numBytes/kb).toPrecision(2) + 'KB';
		} else if (numBytes < gb) {
			return (numBytes/mb).toPrecision(2) + 'MB';
		} else {
			return (numBytes/gb).toPrecision(2) + 'GB';
		}
	};
	var onStartDefaultFunc = function(info) {
		var pageElements = null;
		if(self.isInitialized) {
{'fraction_non_alphanumeric': 0.17015220888503899, 'fraction_numerical': 0.0055686177453285485, 'mean_word_length': 4.141221374045801, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 55, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4923129', 'n_tokens_mistral': 2999, 'n_tokens_neox': 2763, 'n_words': 720}