<commit_before>/**
 * @preserve Copyright 2012 <NAME> & Modilabs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*jslint browser:true, devel:true, jquery:true, smarttabs:true sub:true*/

/**
 * FileManager Class for PERSISTENT file storage, used to storage file inputs for later submission
 * Could be expanded, if ever needed, to use TEMPORARY file storage as well
 *
 * @constructor
 */
function FileManager(){
	"use strict";

	var fs, requestQuota, requestFileSystem, errorHandler, setCurrentQuotaUsed, dirName, dirPrefix,
		currentQuota = null,
		currentQuotaUsed = null,
		DEFAULTBYTESREQUESTED = 10 * 1024 * 1024;

	this.getCurrentQuota = function(){return currentQuota;}; //REMOVE, IS TEMPORARY
	this.getCurrentQuotaUsed = function(){return currentQuotaUsed;};
	this.getFS = function(){return fs;};
	
	/**
	 * Initializes the File Manager
	 * @param  {string}					directory name of directory to store files in
	 * @param  {Object.<string, Function>} callbacks callback functions (error, and success)
	 * @return {boolean}					returns true/false if File API is supported by browser
	 */
	this.init = function(directory, callbacks){
		if (this.isSupported()){
			var that = this;
			dirName = directory;
			dirPrefix = '/'+directory+'/';

			setCurrentQuotaUsed();
			requestQuota(
				DEFAULTBYTESREQUESTED,
				{
					success: function(grantedBytes){
						requestFileSystem(
							grantedBytes,
							{
								success: function(fsys){
									fs = fsys;
									that.createDir(
										dirName,
										callbacks
									);
								},
								error: errorHandler
							}
						);
					},
					error: errorHandler
				}
			);
			//TODO: move this to final success eventhandler?
			$(document).on('submissionsuccess', function(ev, recordName, instanceID){
				that.deleteDir(instanceID);
			});
			return true;
		}
		else{
			return false;
		}
	};

	/**
	 * Checks if File API is supported by browser
	 * @return {boolean}
	 */
	this.isSupported = function(){
		window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
		window.resolveLocalFileSystemURL = window.resolveLocalFileSystemURL || window.webkitResolveLocalFileSystemURL;
		window.storageInfo = window.storageInfo || window.webkitStorageInfo;
		return (typeof window.requestFileSystem !== 'undefined' && typeof window.resolveLocalFileSystemURL !== 'undefined' && window.storageInfo !== 'undefined');
	};

	/**
	 * Requests PERSISTENT file storage (may prompt user) asynchronously
	 * @param  {number}						bytesRequested the storage size in bytes that is requested
	 * @param  {Object.<string, Function>}	callbacks      callback functions (error, and success)
	 */
	requestQuota = function(bytesRequested, callbacks){
		window.storageInfo.requestQuota(
			window.storageInfo.PERSISTENT,
			bytesRequested ,
			callbacks.success,
			callbacks.error
		);
	};
	
	/**
	 * requests filesystem
	 * @param  {number} bytes		when called by requestQuota for PERSISTENt storage this is the number of bytes granted
	 * @param  {Object.<string, Function>} callbacks   callback functions (error, and success)
	 */
	requestFileSystem = function(bytes, callbacks){
		console.log('requesting file system');
		console.log('quota for persistent storage granted in MegaBytes: '+bytes/(1024 * 1024));
		if (bytes > 0){
			currentQuota = bytes;
			window.requestFileSystem(
				window.storageInfo.PERSISTENT,
				bytes,
				callbacks.success,
				callbacks.error
			);
		}
		else{

		}
	};

	/**
	 * generic error handler
	 * @param  {Error} e [description]
	 */
	errorHandler = function(e){
		//console.error('error occurred: ', e);
		throw('error occurred: '+e.message);
	};

	/**
	 * Requests the amount of storage used (asynchronously) and sets variable (EXPERIMENTAL/UNSTABLE API)
	 */
	setCurrentQuotaUsed = function(){
		if (typeof window.storageInfo.queryUsageAndQuota !== 'undefined'){
			window.storageInfo.queryUsageAndQuota(
				window.storageInfo.PERSISTENT,
				function(quotaUsed){
					currentQuotaUsed = quotaUsed;
				},
				errorHandler
			);
		}
		else {
			console.error('browser does not support queryUsageAndQuota');
		}
	};
<commit_msg>show trace when error occurs<commit_after>/**
 * @preserve Copyright 2012 <NAME> & Modilabs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*jslint browser:true, devel:true, jquery:true, smarttabs:true sub:true*/

/**
 * FileManager Class for PERSISTENT file storage, used to storage file inputs for later submission
 * Could be expanded, if ever needed, to use TEMPORARY file storage as well
 *
 * @constructor
 */
function FileManager(){
	"use strict";

	var fs, requestQuota, requestFileSystem, errorHandler, setCurrentQuotaUsed, dirName, dirPrefix,
		currentQuota = null,
		currentQuotaUsed = null,
		DEFAULTBYTESREQUESTED = 10 * 1024 * 1024;

	this.getCurrentQuota = function(){return currentQuota;}; //REMOVE, IS TEMPORARY
	this.getCurrentQuotaUsed = function(){return currentQuotaUsed;};
	this.getFS = function(){return fs;};
	
	/**
	 * Initializes the File Manager
	 * @param  {string}					directory name of directory to store files in
	 * @param  {Object.<string, Function>} callbacks callback functions (error, and success)
	 * @return {boolean}					returns true/false if File API is supported by browser
	 */
	this.init = function(directory, callbacks){
		if (this.isSupported()){
			var that = this;
			dirName = directory;
			dirPrefix = '/'+directory+'/';

			setCurrentQuotaUsed();
			requestQuota(
				DEFAULTBYTESREQUESTED,
				{
					success: function(grantedBytes){
						requestFileSystem(
							grantedBytes,
							{
								success: function(fsys){
									fs = fsys;
									that.createDir(
										dirName,
										callbacks
									);
								},
								error: errorHandler
							}
						);
					},
					error: errorHandler
				}
			);
			//TODO: move this to final success eventhandler?
			$(document).on('submissionsuccess', function(ev, recordName, instanceID){
				that.deleteDir(instanceID);
			});
			return true;
		}
		else{
			return false;
		}
	};

	/**
	 * Checks if File API is supported by browser
	 * @return {boolean}
	 */
	this.isSupported = function(){
		window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
		window.resolveLocalFileSystemURL = window.resolveLocalFileSystemURL || window.webkitResolveLocalFileSystemURL;
		window.storageInfo = window.storageInfo || window.webkitStorageInfo;
		return (typeof window.requestFileSystem !== 'undefined' && typeof window.resolveLocalFileSystemURL !== 'undefined' && window.storageInfo !== 'undefined');
	};

	/**
	 * Requests PERSISTENT file storage (may prompt user) asynchronously
	 * @param  {number}						bytesRequested the storage size in bytes that is requested
	 * @param  {Object.<string, Function>}	callbacks      callback functions (error, and success)
	 */
	requestQuota = function(bytesRequested, callbacks){
		window.storageInfo.requestQuota(
			window.storageInfo.PERSISTENT,
			bytesRequested ,
			callbacks.success,
			callbacks.error
		);
	};
	
	/**
	 * requests filesystem
	 * @param  {number} bytes		when called by requestQuota for PERSISTENt storage this is the number of bytes granted
	 * @param  {Object.<string, Function>} callbacks   callback functions (error, and success)
	 */
	requestFileSystem = function(bytes, callbacks){
		console.log('requesting file system');
		console.log('quota for persistent storage granted in MegaBytes: '+bytes/(1024 * 1024));
		if (bytes > 0){
			currentQuota = bytes;
			window.requestFileSystem(
				window.storageInfo.PERSISTENT,
				bytes,
				callbacks.success,
				callbacks.error
			);
		}
		else{

		}
	};

	/**
	 * generic error handler
	 * @param  {Error} e [description]
	 */
	errorHandler = function(e){
		console.error('error occurred: ', e);
		if (typeof console.trace !== 'undefined') console.trace();
	};

	/**
	 * Requests the amount of storage used (asynchronously) and sets variable (EXPERIMENTAL/UNSTABLE API)
	 */
	setCurrentQuotaUsed = function(){
		if (typeof window.storageInfo.queryUsageAndQuota !== 'undefined'){
			window.storageInfo.queryUsageAndQuota(
				window.storageInfo.PERSISTENT,
				function(quotaUsed){
					currentQuotaUsed = quotaUsed;
				},
				errorHandler
			);
		}
		else {
			console.error('browser does not support queryUsageAndQuota');
		}
	};

{'fraction_non_alphanumeric': 0.10684902233144566, 'fraction_numerical': 0.005769847205898066, 'mean_word_length': 3.6176615688209175, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5238174', 'n_tokens_mistral': 3227, 'n_tokens_neox': 2653, 'n_words': 798}