<commit_before>var express = require('express');
var router = express.Router();
var config = require('config');
var multer = require('multer');
var gridfsStorage = require('gridfs-storage-engine')({ url: config.database.connection });
var upload = multer({ storage: gridfsStorage });
var mongo = require('mongodb');
var database = undefined;
var gfs = undefined;
var Grid = require('gridfs-stream');
var httpError = require('../helper/httpError');
var uuid = require('node-uuid');
var moment = require('moment');

// connect to MongoDB
mongo.MongoClient.connect(config.database.connection, function(err, db) {
    if (err) {
        throw err;
    }

    database = db;

    gfs = new Grid(database, mongo);
});

router.post('/upload', upload.array('files'), function(req, res, next) {
    var fileIds = [ req.files[0].gridfsEntry._id ];

    insertUpload(fileIds, function(err, result, upload) {
        if (err) {
            return next(httpError.createError(500, err));
        }

        res.status(201).json({
            token: upload.token,
            expirationDate: upload.expirationDate
        });
    });
});

router.get('/files/:token', function(req, res, next) {
    database
        .collection('uploads')
        .find({ token: req.param('token') })
        .limit(1)
        .next(function(err, upload) {
            if (err) {
                return next(httpError.createError(500, err));
            }

            var now = new Date();

            if (now > upload.expirationDate) { // expired
                res.status(404);
            }
            else {
                var fileId = upload.files[0];

                gfs
                    .files
                    .find({ _id: fileId })
                    .limit(1)
                    .next(function(err, file) {
                        if (err) {
                            return next(httpError.createError(500, err));
                        }

                        // stream download
                        res.setHeader('Content-disposition', 'attachment; filename=' + file.filename);
                        res.setHeader('Content-type', file.contentType);

                        var readStream = gfs.createReadStream({ _id: fileId });
                        readStream.pipe(res);
                    });
            }
        });
});

function insertUpload(fileIds, callback) {
    var currentDate = new Date();
    var upload = {
        token: uuid.v4(),<commit_msg>renamed "api/files" route to "api/download" replaced deprecated functions from express.js API set content-length in HTTP header of download<commit_after>var express = require('express');
var router = express.Router();
var config = require('config');
var multer = require('multer');
var gridfsStorage = require('gridfs-storage-engine')({ url: config.database.connection });
var upload = multer({ storage: gridfsStorage });
var mongo = require('mongodb');
var database = undefined;
var gfs = undefined;
var Grid = require('gridfs-stream');
var httpError = require('../helper/httpError');
var uuid = require('node-uuid');
var moment = require('moment');

// connect to MongoDB
mongo.MongoClient.connect(config.database.connection, function(err, db) {
    if (err) {
        throw err;
    }

    database = db;

    gfs = new Grid(database, mongo);
});

router.post('/upload', upload.array('files'), function(req, res, next) {
    var fileIds = [ req.files[0].gridfsEntry._id ];

    insertUpload(fileIds, function(err, result, upload) {
        if (err) {
            return next(httpError.createError(500, err));
        }

        res.status(201).json({
            token: upload.token,
            expirationDate: upload.expirationDate
        });
    });
});

router.get('/download/:token', function(req, res, next) {
    database
        .collection('uploads')
        .find({ token: req.params.token })
        .limit(1)
        .next(function(err, upload) {
            if (err) {
                return next(httpError.createError(500, err));
            }

            var now = new Date();

            if (now > upload.expirationDate) { // expired
                res.status(404);
            }
            else {
                var fileId = upload.files[0];

                gfs
                    .files
                    .find({ _id: fileId })
                    .limit(1)
                    .next(function(err, file) {
                        if (err) {
                            return next(httpError.createError(500, err));
                        }

                        // stream download
                        res.setHeader('Content-disposition', 'attachment; filename=' + file.filename);
                        res.setHeader('Content-type', file.contentType);
                        res.setHeader('Content-length', file.length);

                        var readStream = gfs.createReadStream({ _id: fileId });
                        readStream.pipe(res);
                    });
            }
        });
});

function insertUpload(fileIds, callback) {
    var currentDate = new Date();
    var upload = {
        token: uuid.v4(),
{'fraction_non_alphanumeric': 0.13481394895772453, 'fraction_numerical': 0.007792713812585233, 'mean_word_length': 1.8116100766703176, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7339681', 'n_tokens_mistral': 1423, 'n_tokens_neox': 1350, 'n_words': 316}