<commit_before>// dependencies
var _ = require('lodash');
var fs = require('fs-extra');
var kue = require('kue');
var path = require('path');
var zlib = require('zlib');
var uuid = require('uuid');
var async = require('async');
var redis = require('redis');
var carto = require('carto');
var mapnik = require('mapnik');
var colors = require('colors');
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
var mongoose = require('mongoose');
var request = require('request');
var http = require('http-request');

// global paths
var VECTORPATH   = '/data/vector_tiles/';
var RASTERPATH   = '/data/raster_tiles/';
var GRIDPATH     = '/data/grid_tiles/';
var PROXYPATH 	 = '/data/proxy_tiles/';

var pile_settings = {
	store : 'disk' // or redis
}

module.exports = proxy = { 

	headers : {
		jpeg : 'image/jpeg',
		png : 'image/png',
		pbf : 'application/x-protobuf',
		grid : 'application/json'
	},

	serveTile : function (res, options) {

		// tile path on disk
		var tile_on_disk_path = PROXYPATH + options.provider + '/' + options.type + '/' + options.z + '/' + options.x + '/' + options.y + '.' + options.format;

		// read tile, serve
		fs.readFile(tile_on_disk_path, function (err, buffer) {
			if (err) console.error({
				err_id : 14,
				err_msg : 'serve tile',
				error : err
			});

			// error tile
			if (err) return proxy.serveErrorTile(res);

			// send tile to client
			res.writeHead(200, {'Content-Type': mile.headers[options.format]}); 
			res.end(buffer);
		});

	},

	serveErrorTile : function (res) {
		var errorTile = 'public/errorTile.png';
		fs.readFile('public/noAccessTile.png', function (err, tile) {
			res.writeHead(200, {'Content-Type': 'image/png'});
			res.end(tile);
		});
	},

	_serveTile : function (options, done) {

		// provider
		var provider = options.provider;

		// pass to provider
		if (provider == 'norkart') return proxy._getNorkartTile(options, done);
		if (provider == 'google') return proxy._getGoogleTile(options, done);

		// provider not supported err
		var err = 'Provider not supported!', provider
		if (err) console.error({
			err_id : 17,
			err_msg : 'get tile from provider',
			error : err
		});
		done(err);
	},

	_fetchTile : function (options, done) {

		// check disk first
		var tile_on_disk_folder = PROXYPATH + options.provider + '/' + options.type + '/' + options.z + '/' + options.x + '/' 
		var tile_on_disk_path = tile_on_disk_folder + options.y + '.' + options.format;

		// url, headers
		var url = options.url;
		var headers = options.headers;

		var ops = [];

		// check disk
		ops.push(function (callback) {

			fs.readFile(tile_on_disk_path, function (err, data) {
				
				// found tile on disk
				if (!err && data) return callback({
					status : 'got tile!'
				});

				// didnt find, do next
				callback(null);
			});
		});

		// get tile from http
		ops.push(function (callback) {

			// create folder
			fs.ensureDir(tile_on_disk_folder, function (err) {
				if (err) console.error({
					err_id : 15,
					err_msg : 'fetch tile',
					error : err
				});
			
				var httpOptions = {
					url: url,
					timeout : '10000',
					headers : headers
				};

				// get tile
				http.get(httpOptions, tile_on_disk_path, function (err, result) {
					if (err) console.error({
						err_id : 16,
						err_msg : 'fetch tile',
						error : 'tile_on_disk_path: ' + tile_on_disk_path
					});
					
					// got tile
					if (!err && result) return callback({
						status : 'got tile!'
					});

					// didn't get tile, something wrong					
					callback({ error: 'Could not get tile from disk nor http.' });
				});
			});
		});
		
		// run ops
		async.series(ops, function (err) {

			// some error
			if (err.error) return done(err.error);

			// done here
			done();
		});
	},

	_getGoogleTile : function (options, done) {

		// url schemes
		var google_types = {
			vector: "http://mt0.google.com/vt/",
			aerial: "https://khms1.googleapis.com/kh?v=182&hl=en-US&",
		}

		// google url
		var url = google_types[options.type] + 'x=' + options.x + '&y=' + options.y + '&z=' + options.z;

		// set url, headers
		options.url = url;
		options.headers = {
			'User-Agent' : 'Mapic Tile Proxy',
			'Referer' : 'https://mapic.io',
			'X-Message-For-Google' : 'Hi Google!',
			'X-Google-Server-API-Key' : '<KEY>'
		}

		// fetch
		proxy._fetchTile(options, done);
	},

	_getNorkartTile : function (options, done) {

		// url schemes<commit_msg>fix google tile proxy url<commit_after>// dependencies
var _ = require('lodash');
var fs = require('fs-extra');
var kue = require('kue');
var path = require('path');
var zlib = require('zlib');
var uuid = require('uuid');
var async = require('async');
var redis = require('redis');
var carto = require('carto');
var mapnik = require('mapnik');
var colors = require('colors');
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
var mongoose = require('mongoose');
var request = require('request');
var http = require('http-request');

// global paths
var VECTORPATH   = '/data/vector_tiles/';
var RASTERPATH   = '/data/raster_tiles/';
var GRIDPATH     = '/data/grid_tiles/';
var PROXYPATH 	 = '/data/proxy_tiles/';

var pile_settings = {
	store : 'disk' // or redis
}

module.exports = proxy = { 

	headers : {
		jpeg : 'image/jpeg',
		png : 'image/png',
		pbf : 'application/x-protobuf',
		grid : 'application/json'
	},

	serveTile : function (res, options) {

		// tile path on disk
		var tile_on_disk_path = PROXYPATH + options.provider + '/' + options.type + '/' + options.z + '/' + options.x + '/' + options.y + '.' + options.format;

		// read tile, serve
		fs.readFile(tile_on_disk_path, function (err, buffer) {
			if (err) console.error({
				err_id : 14,
				err_msg : 'serve tile',
				error : err
			});

			// error tile
			if (err) return proxy.serveErrorTile(res);

			// send tile to client
			res.writeHead(200, {'Content-Type': mile.headers[options.format]}); 
			res.end(buffer);
		});

	},

	serveErrorTile : function (res) {
		var errorTile = 'public/errorTile.png';
		fs.readFile('public/noAccessTile.png', function (err, tile) {
			res.writeHead(200, {'Content-Type': 'image/png'});
			res.end(tile);
		});
	},

	_serveTile : function (options, done) {

		// provider
		var provider = options.provider;

		// pass to provider
		if (provider == 'norkart') return proxy._getNorkartTile(options, done);
		if (provider == 'google') return proxy._getGoogleTile(options, done);

		// provider not supported err
		var err = 'Provider not supported!', provider
		if (err) console.error({
			err_id : 17,
			err_msg : 'get tile from provider',
			error : err
		});
		done(err);
	},

	_fetchTile : function (options, done) {

		// check disk first
		var tile_on_disk_folder = PROXYPATH + options.provider + '/' + options.type + '/' + options.z + '/' + options.x + '/' 
		var tile_on_disk_path = tile_on_disk_folder + options.y + '.' + options.format;

		// url, headers
		var url = options.url;
		var headers = options.headers;

		var ops = [];

		// check disk
		ops.push(function (callback) {

			fs.readFile(tile_on_disk_path, function (err, data) {

				// found tile on disk
				if (!err && data) return callback({
					status : 'got tile!'
				});

				// didnt find, do next
				callback(null);
			});
		});

		// get tile from http
		ops.push(function (callback) {

			// create folder
			fs.ensureDir(tile_on_disk_folder, function (err) {
				if (err) console.error({
					err_id : 15,
					err_msg : 'fetch tile',
					error : err
				});
			
				var httpOptions = {
					url: url,
					timeout : '10000',
					headers : headers
				};

				// get tile
				http.get(httpOptions, tile_on_disk_path, function (err, result) {
					// console.log('GET tile err', err);
					// console.log('GET TILE result', result);
					if (err) console.error({
						err_id : 16,
						err_msg : 'fetch tile',
						error : 'tile_on_disk_path: ' + tile_on_disk_path
					});
					
					// got tile
					if (!err && result) return callback({
						status : 'got tile!'
					});

					// didn't get tile, something wrong					
					callback({ error: 'Could not get tile from disk nor http.' });
				});
			});
		});
		
		// run ops
		async.series(ops, function (err) {

			// some error
			if (err.error) return done(err.error);

			// done here
			done();
		});
	},

	_getGoogleTile : function (options, done) {

		// url schemes
		var google_types = {
			vector: "http://mt0.google.com/vt/",
			// aerial: "https://khms1.googleapis.com/kh?v=182&hl=en-US&",
			aerial : "http://mt0.google.com/vt/lyrs=s&hl=en&",
		}

		// http://mt0.google.com/vt/lyrs=s&hl=en&x={x}&y={y}&z={z}

		// google url
		var url = google_types[options.type] + 'x=' + options.x + '&y=' + options.y + '&z=' + options.z;

		// set url, headers
		options.url = url;
		options.headers = {
			'User-Agent' : 'Mapic Tile Proxy',
			'Referer' : 'https://mapic.io',
			'X-Message-For-Google' : 'Hi Google!',
			'X-Google-Server-API-Key' : '<KEY>'
		}

		// fetch
		proxy._fetchTile(options, done);
	},

	_getNorkartTile : function (options, done) {

		// url schemes
{'fraction_non_alphanumeric': 0.1685886875343218, 'fraction_numerical': 0.0054914881933003845, 'mean_word_length': 2.876543209876543, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, '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': '50997', 'n_tokens_mistral': 3719, 'n_tokens_neox': 3112, 'n_words': 768}