<commit_before>// Routes for the tile server. Suitable for HTTP cacheable content with a
// long TTL.
var _ = require('underscore')._,
    Step = require('step'),
    fs = require('fs'),
    path = require('path'),
    Tile = require('tilelive').Tile,
    models = require('../mvc/models');

module.exports = function(app, settings) {
    app.enable('jsonp callback');
    app.error(Error.HTTP.handler(settings));

    // Route middleware. Validates an mbtiles file specified in a tile or
    // download route.
    var validateTileset = function(req, res, next) {
        req.model = req.model || {};
        req.model.options = req.model.options || {};
        var model = new models.Tileset(
            { id: req.params[0] },
            req.model.options
        );
        model.fetch({
            success: function(model) {
                res.mapfile = model.filepath(settings.tiles);
                next();
            },
            error: function(model, err) {
                next(err);
            }
        });
    };

    // Load HTTP headers specific to the requested mbtiles file.
    var loadMapFileHeaders = function(req, res, next) {
        var headers = {};
        if (res.mapfile) {
            fs.stat(res.mapfile, function(err, stat) {
                if (!err) {
                    res.mapfile_headers = {
                        'Last-Modified': stat.mtime,
                        'E-Tag': stat.size + '-' + Number(stat.mtime)
                    }
                    return next();
                }
            });
        }
    }

    // If "download" feature is enabled, add route equivalent to
    // `/download/:tileset` except with handling for `:tileset` parameters that may
    // contain a `.` character.
    if (settings.features && settings.features.download) {
        var download = /^\/download\/([\w+|\d+|.|-]*)?.mbtiles/;
        app.get(download, validateTileset, function(req, res, next) {
            res.sendfile(res.mapfile, function(err, path) {
                return err && next(err);
            });
        });
    }

    // Route equivalent to `/1.0.0/:tileset/:z/:x/:y.:format` except with handling
    // for `:tileset` parameters that may contain a `.` character.
    var tile = /^\/1.0.0\/([\w+|\d+|.|-]*)?\/([-]?\d+)\/([-]?\d+)\/([-]?\d+).(png|jpg|jpeg)/;
    app.get(tile, validateTileset, loadMapFileHeaders, function(req, res, next) {
        var tile = new Tile({
            type: 'mbtiles',
            datasource: res.mapfile,
            format: req.params[4],
            xyz: [req.params[2], req.params[3], req.params[1]]
        });
        tile.render(function(err, data) {
            if (!err) {<commit_msg>Apply default headers to downloads.<commit_after>// Routes for the tile server. Suitable for HTTP cacheable content with a
// long TTL.
var _ = require('underscore')._,
    Step = require('step'),
    fs = require('fs'),
    path = require('path'),
    Tile = require('tilelive').Tile,
    models = require('../mvc/models');

module.exports = function(app, settings) {
    app.enable('jsonp callback');
    app.error(Error.HTTP.handler(settings));

    // Route middleware. Validates an mbtiles file specified in a tile or
    // download route.
    var validateTileset = function(req, res, next) {
        req.model = req.model || {};
        req.model.options = req.model.options || {};
        var model = new models.Tileset(
            { id: req.params[0] },
            req.model.options
        );
        model.fetch({
            success: function(model) {
                res.mapfile = model.filepath(settings.tiles);
                next();
            },
            error: function(model, err) {
                next(err);
            }
        });
    };

    // Load HTTP headers specific to the requested mbtiles file.
    var loadMapFileHeaders = function(req, res, next) {
        var headers = {};
        if (res.mapfile) {
            fs.stat(res.mapfile, function(err, stat) {
                if (!err) {
                    res.mapfile_headers = {
                        'Last-Modified': stat.mtime,
                        'E-Tag': stat.size + '-' + Number(stat.mtime)
                    }
                    return next();
                }
            });
        }
    }

    // If "download" feature is enabled, add route equivalent to
    // `/download/:tileset` except with handling for `:tileset` parameters that may
    // contain a `.` character.
    if (settings.features && settings.features.download) {
        var download = /^\/download\/([\w+|\d+|.|-]*)?.mbtiles/;
        app.get(download, validateTileset, function(req, res, next) {
            _(res.headers).extend(settings.header_defaults);
            res.sendfile(res.mapfile, function(err, path) {
                return err && next(err);
            });
        });
    }

    // Route equivalent to `/1.0.0/:tileset/:z/:x/:y.:format` except with handling
    // for `:tileset` parameters that may contain a `.` character.
    var tile = /^\/1.0.0\/([\w+|\d+|.|-]*)?\/([-]?\d+)\/([-]?\d+)\/([-]?\d+).(png|jpg|jpeg)/;
    app.get(tile, validateTileset, loadMapFileHeaders, function(req, res, next) {
        var tile = new Tile({
            type: 'mbtiles',
            datasource: res.mapfile,
            format: req.params[4],
            xyz: [req.params[2], req.params[3], req.params[1]]
        });
        tile.render(function(err, data) {
            if (!err) {
{'fraction_non_alphanumeric': 0.15955303169078586, 'fraction_numerical': 0.004030042132258656, 'mean_word_length': 1.763157894736842, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3671550', 'n_tokens_mistral': 1669, 'n_tokens_neox': 1623, 'n_words': 371}