<commit_before>import Plunger from '../helpers/plunger';
import strLeftBack from 'underscore.string/strLeftBack';
import find from 'lodash/collection/find';
import includes from 'lodash/collection/includes';


exports.loadLayer = function (req, res, next) {
    const notFound = message => res.status(404).send({
        code: 404,
        message: message || 'Layer not found in given file package'
    });

    const datasets = req.remoteResource.archive.datasets;

    if (req.params.layerName) {
        let foundLayer = find(datasets, dataset => dataset.includes(req.params.layerName));
        if (foundLayer) {
            req.layer = foundLayer;
            return next();
        }
    }

    // Compatibility
    if (!req.params.layerName && datasets.length > 0) {
        req.layer = datasets[0];
        return next();
    }

    notFound();
};

exports.prepateFilePackageDownload = function (req, res, next) {

    const success = (layerPath, layerName) => {
        req.ogr2ogr = {};
        req.ogr2ogr.src = layerPath;
        req.ogr2ogr.fileName = layerName;
        next();
    };

    const notFound = err => res.status(404).send({
        code: 404,
        message: 'Resource currently unavailable',
        error: err
    });

    const serverError = err => res.status(500).send({
        code: 500,
        message: 'Internal server error',
        error: err
    });

    if (!req.remoteResource.available) {
        return notFound();
    }

    req.plunger = new Plunger(req.remoteResource.location, { abort: 'never' });

    req.plunger.inspect()
        .then(() => {
            if (req.plunger.isArchive() && req.method === 'GET') {
                return req.plunger.saveArchive()
                    .then(() => req.plunger.decompressArchive())
                    .then(() => req.plunger.listFiles())
                    .then(files => {
                        if (!includes(files, req.layer)) {
                            return notFound(); // Should trigger remote resource refresh
                        }
                        const layerPath = req.plunger.decompressedDirectoryPath + '/' + req.layer;
                        const layerName = strLeftBack(req.layer, '.');
                        return success(layerPath, layerName);
                    });
            }

            req.plunger.closeConnection(true);

            if (!req.plunger.isArchive()) {
                return notFound();
            }

            if (req.method === 'HEAD') {
                return success();
            }
        })
        .error(serverError);
};
<commit_msg>Fix download from archive<commit_after>import Plunger from '../helpers/plunger';
import strLeftBack from 'underscore.string/strLeftBack';
import find from 'lodash/collection/find';
import includes from 'lodash/collection/includes';


exports.loadLayer = function (req, res, next) {
    const notFound = message => res.status(404).send({
        code: 404,
        message: message || 'Layer not found in given file package'
    });

    const datasets = req.remoteResource.archive.datasets;

    if (req.params.layerName) {
        let foundLayer = find(datasets, dataset => dataset.includes(req.params.layerName));
        if (foundLayer) {
            req.layer = foundLayer;
            return next();
        }
    }

    // Compatibility
    if (!req.params.layerName && datasets.length > 0) {
        req.layer = datasets[0];
        return next();
    }

    notFound();
};

exports.prepateFilePackageDownload = function (req, res, next) {

    const success = (layerPath, layerName) => {
        req.ogr2ogr = {};
        req.ogr2ogr.src = layerPath;
        req.ogr2ogr.fileName = layerName;
        next();
    };

    const notFound = err => res.status(404).send({
        code: 404,
        message: 'Resource currently unavailable',
        error: err
    });

    const serverError = err => res.status(500).send({
        code: 500,
        message: 'Internal server error',
        error: err
    });

    if (!req.remoteResource.available) {
        return notFound();
    }

    req.plunger = new Plunger(req.remoteResource.location, { abort: 'never' });

    req.plunger.inspect()
        .then(() => {
            if (req.plunger.isArchive() && req.method === 'GET') {
                return req.plunger.saveArchive()
                    .then(() => req.plunger.decompressArchive())
                    .then(() => req.plunger.listFiles())
                    .then(files => {
                        if (!includes(files.datasets, req.layer)) {
                            return notFound(); // Should trigger remote resource refresh
                        }
                        const layerPath = req.plunger.decompressedDirectoryPath + '/' + req.layer;
                        const layerName = strLeftBack(req.layer, '.');
                        return success(layerPath, layerName);
                    });
            }

            req.plunger.closeConnection(true);

            if (!req.plunger.isArchive()) {
                return notFound();
            }

            if (req.method === 'HEAD') {
                return success();
            }
        })
        .error(serverError);
};

{'fraction_non_alphanumeric': 0.12990242969198393, 'fraction_numerical': 0.008800459154390664, 'mean_word_length': 1.773474801061008, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '498661', 'n_tokens_mistral': 1507, 'n_tokens_neox': 1374, 'n_words': 311}