<commit_before>/*! imgcache.js v0.1 
   Copyright 2012 <NAME>

   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.
*/

var ImgCache = {
	// options to override before using the library (but after loading this script!)
	options: {
		debug: false,			/* write to console? */
		localCacheFolder: 'imgcache',	/* name of the cache folder */
		useDataURI: false,		/* use src="data:.."? otherwise will use src="filesystem:.." */
		chromeQuota: 10*1024*1024,	/* allocated cache space : here 10Mb */
		usePersistentCache: true
	}
};

(function($) {

	var old_src_attr = 'data-old-src';

	/** Helpers *****************************************************************/
	// with a little help from http://code.google.com/p/js-uri/
	var URI = function(str) {
	    if (!str) str = "";
	    // Based on the regex in RFC2396 Appendix B.
	    var parser = /^(?:([^:\/?\#]+):)?(?:\/\/([^\/?\#]*))?([^?\#]*)(?:\?([^\#]*))?(?:\#(.*))?/;
	    var result = str.match(parser);
	    this.scheme    = result[1] || null;
	    this.authority = result[2] || null;
	    this.path      = result[3] || null;
	    this.query     = result[4] || null;
	    this.fragment  = result[5] || null;
	};

	var is_cordova = function() {
		return (typeof(cordova) !== 'undefined' || typeof(phonegap) !== 'undefined');
	};

	var logging = function(str) {
		if (ImgCache.options.debug)
			console.log(str);
	};

	// returns lower cased filename from full URI
	var URIGetFileName = function(fullpath) {
		if (!fullpath)
			return;
		//TODO: there must be a better way here.. (url encoded strings fail)
		var idx = fullpath.lastIndexOf("/");
		if (!idx)
			return;
		return fullpath.substr(idx + 1).toLowerCase();
	};

	// returns lower cased path from full URI
	var URIGetPath = function(str) {
		if (!str)
			return;
		var uri = URI(str);
		return uri.path.toLowerCase();
	};
	/****************************************************************************/

	// if no local_root set, set relative path
	var _getCachedFilePath = function(img_src, local_root) {
		var filename = URIGetFileName(img_src);
		//TODO: we should here return the file path with subfolders matching its url to avoid problem when two files have same name
		// 	for now not done because that would mean be sure that the subfolders are created (recursively) before writing the file
		//	An example of how to recursively create subfolders:
		// 	http://www.html5rocks.com/en/tutorials/file/filesystem/
		// var path = URIGetPath(img_src);
		//return (local_root ? local_root + '/' : '') + path + '/' + filename;
		return (local_root ? local_root + '/' : '') + filename;
	};

	var _setNewImgPath = function($img, new_src, old_src) {
		$img.attr('src', new_src);
		// store previous url in case we need to reload it
		$img.attr(old_src_attr, old_src);
	};

	var _createCacheDir = function(callback) {
		if (!ImgCache.filesystem)
			return;

		var _fail = function(error) {
			logging('Failed to get/create local cache directory: ' + error.code);
		};
		var _getDirSuccess = function(dirEntry) {
			ImgCache.dirEntry = dirEntry;
			logging('Local cache folder opened: ' + dirEntry.fullPath);
			if (callback) callback();
		};
		ImgCache.filesystem.root.getDirectory(ImgCache.options.localCacheFolder, {create: true, exclusive: false}, _getDirSuccess, _fail);	
	};

	// This is a wrapper for phonegap's FileTransfer object in order to implement the same feature
	// in Chrome (and possibly extra browsers in the future)
	var FileTransferWrapper = function(filesystem) {
		if (is_cordova()) {
			// PHONEGAP
			this.fileTransfer = new FileTransfer();
		}
		this.filesystem = filesystem;	// only useful for CHROME
	};
	FileTransferWrapper.prototype.download = function(uri, localPath, success_callback, error_callback) {
		// PHONEGAP
		if (this.fileTransfer) return this.fileTransfer.download(uri, localPath, success_callback, error_callback);

		var filesystem = this.filesystem;

		// CHROME - browsers
		var xhr = new XMLHttpRequest();
		xhr.open('GET', uri, true);
		xhr.responseType = 'arraybuffer';
		xhr.onload = function(event){
			if (xhr.response && (xhr.status == 200 || xhr.status == 0)) {
				filesystem.root.getFile(localPath, { create:true }, function(fileEntry) {
					fileEntry.createWriter(function(writer){

						writer.onerror = error_callback;
						writer.onwriteend = function() { success_callback(fileEntry);  };

						var BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder;
						var bb = new BlobBuilder();
						bb.append(xhr.response);
						var mime_type = xhr.getResponseHeader('Content-type');
						writer.write(bb.getBlob(mime_type), error_callback);

					}, error_callback);
				}, error_callback);
			} else {
				//TODO: error_callback(error)
				logging('Image ' + uri + ' could not be downloaded - status: ' + xhr.status);
			}
		};
		xhr.send();
	};

	// toURL for html5, toURI for cordova...
	var _getFileEntryURL = function(entry) {
		return entry.toURL ? entry.toURL() : entry.toURI();
	}

	ImgCache.init = function(init_callback) {
		ImgCache.init_callback = init_callback;

		var _gotFS = function(filesystem) {
			logging('LocalFileSystem opened');

			// store filesystem handle
			ImgCache.filesystem = filesystem;

			_createCacheDir(ImgCache.init_callback);
		};
		var _fail = function(error) {
			logging('Failed to initialise LocalFileSystem ' + error.code);
		};
		if (is_cordova()) {
			// PHONEGAP
			var persistence = (ImgCache.options.usePersistentCache ? LocalFileSystem.PERSISTENT : LocalFileSystem.TEMPORARY);
			window.requestFileSystem(persistence, 0, _gotFS, _fail);<commit_msg>added logging on xhr error<commit_after>/*! imgcache.js v0.1 
   Copyright 2012 <NAME>

   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.
*/

var ImgCache = {
	// options to override before using the library (but after loading this script!)
	options: {
		debug: false,			/* write log (to console)? */
		localCacheFolder: 'imgcache',	/* name of the cache folder */
		useDataURI: false,		/* use src="data:.."? otherwise will use src="filesystem:.." */
		chromeQuota: 10*1024*1024,	/* allocated cache space : here 10Mb */
		usePersistentCache: true	/* false: use temporary cache storage */
	}
};

(function($) {

	var old_src_attr = 'data-old-src';

	/** Helpers *****************************************************************/
	// with a little help from http://code.google.com/p/js-uri/
	var URI = function(str) {
	    if (!str) str = "";
	    // Based on the regex in RFC2396 Appendix B.
	    var parser = /^(?:([^:\/?\#]+):)?(?:\/\/([^\/?\#]*))?([^?\#]*)(?:\?([^\#]*))?(?:\#(.*))?/;
	    var result = str.match(parser);
	    this.scheme    = result[1] || null;
	    this.authority = result[2] || null;
	    this.path      = result[3] || null;
	    this.query     = result[4] || null;
	    this.fragment  = result[5] || null;
	};

	var is_cordova = function() {
		return (typeof(cordova) !== 'undefined' || typeof(phonegap) !== 'undefined');
	};

	var logging = function(str) {
		if (ImgCache.options.debug)
			console.log(str);
	};

	// returns lower cased filename from full URI
	var URIGetFileName = function(fullpath) {
		if (!fullpath)
			return;
		//TODO: there must be a better way here.. (url encoded strings fail)
		var idx = fullpath.lastIndexOf("/");
		if (!idx)
			return;
		return fullpath.substr(idx + 1).toLowerCase();
	};

	// returns lower cased path from full URI
	var URIGetPath = function(str) {
		if (!str)
			return;
		var uri = URI(str);
		return uri.path.toLowerCase();
	};
	/****************************************************************************/

	// if no local_root set, set relative path
	var _getCachedFilePath = function(img_src, local_root) {
		var filename = URIGetFileName(img_src);
		//TODO: we should here return the file path with subfolders matching its url to avoid problem when two files have same name
		// 	for now not done because that would mean be sure that the subfolders are created (recursively) before writing the file
		//	An example of how to recursively create subfolders:
		// 	http://www.html5rocks.com/en/tutorials/file/filesystem/
		// var path = URIGetPath(img_src);
		//return (local_root ? local_root + '/' : '') + path + '/' + filename;
		return (local_root ? local_root + '/' : '') + filename;
	};

	var _setNewImgPath = function($img, new_src, old_src) {
		$img.attr('src', new_src);
		// store previous url in case we need to reload it
		$img.attr(old_src_attr, old_src);
	};

	var _createCacheDir = function(callback) {
		if (!ImgCache.filesystem)
			return;

		var _fail = function(error) {
			logging('Failed to get/create local cache directory: ' + error.code);
		};
		var _getDirSuccess = function(dirEntry) {
			ImgCache.dirEntry = dirEntry;
			logging('Local cache folder opened: ' + dirEntry.fullPath);
			if (callback) callback();
		};
		ImgCache.filesystem.root.getDirectory(ImgCache.options.localCacheFolder, {create: true, exclusive: false}, _getDirSuccess, _fail);	
	};

	// This is a wrapper for phonegap's FileTransfer object in order to implement the same feature
	// in Chrome (and possibly extra browsers in the future)
	var FileTransferWrapper = function(filesystem) {
		if (is_cordova()) {
			// PHONEGAP
			this.fileTransfer = new FileTransfer();
		}
		this.filesystem = filesystem;	// only useful for CHROME
	};
	FileTransferWrapper.prototype.download = function(uri, localPath, success_callback, error_callback) {
		// PHONEGAP
		if (this.fileTransfer) return this.fileTransfer.download(uri, localPath, success_callback, error_callback);

		var filesystem = this.filesystem;

		// CHROME - browsers
		var xhr = new XMLHttpRequest();
		xhr.open('GET', uri, true);
		xhr.responseType = 'arraybuffer';
		xhr.onload = function(event){
			if (xhr.response && (xhr.status == 200 || xhr.status == 0)) {
				filesystem.root.getFile(localPath, { create:true }, function(fileEntry) {
					fileEntry.createWriter(function(writer){

						writer.onerror = error_callback;
						writer.onwriteend = function() { success_callback(fileEntry);  };

						var BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder;
						var bb = new BlobBuilder();
						bb.append(xhr.response);
						var mime_type = xhr.getResponseHeader('Content-type');
						writer.write(bb.getBlob(mime_type), error_callback);

					}, error_callback);
				}, error_callback);
			} else {
				//TODO: error_callback(error)
				logging('Image ' + uri + ' could not be downloaded - status: ' + xhr.status);
			}
		};
		xhr.onerror = function() {
			logging('XHR error - Image ' + uri + ' could not be downloaded - status: ' + xhr.status);
		};
		xhr.send();
	};

	// toURL for html5, toURI for cordova...
	var _getFileEntryURL = function(entry) {
		return entry.toURL ? entry.toURL() : entry.toURI();
	}

	ImgCache.init = function(init_callback) {
		ImgCache.init_callback = init_callback;

		var _gotFS = function(filesystem) {
			logging('LocalFileSystem opened');

			// store filesystem handle
			ImgCache.filesystem = filesystem;

			_createCacheDir(ImgCache.init_callback);
		};
		var _fail = function(error) {
			logging('Failed to initialise LocalFileSystem ' + error.code);
		};
		if (is_cordova()) {
			// PHONEGAP
			var persistence = (ImgCache.options.usePersistentCache ? LocalFileSystem.PERSISTENT : LocalFileSystem.TEMPORARY);
			window.requestFileSystem(persistence, 0, _gotFS, _fail);
{'fraction_non_alphanumeric': 0.1541332035369514, 'fraction_numerical': 0.006327573618885374, 'mean_word_length': 3.6433145009416195, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3045547', 'n_tokens_mistral': 4250, 'n_tokens_neox': 3788, 'n_words': 1081}