<commit_before>var express = require("express");
var Mustache = require('mustache');
var resumeToText = require('resume-to-text');
var path = require('path');
var resumeToHTML = require('resume-to-html');
var resumeToPDF = require('resume-to-pdf');
var resumeToMarkdown = require('resume-to-markdown');
var bodyParser = require('body-parser');
var bcrypt = require('bcrypt-nodejs');
var gravatar = require('gravatar');
var app = express();

var postmark = require("postmark")(process.env.POSTMARK_API_KEY);


var MongoClient = require('mongodb').MongoClient;
var mongo = require('mongodb');
app.use(bodyParser());
var fs = require('fs');
var guid = (function() {
    function s4() {
        return Math.floor((1 + Math.random()) * 0x10000)
            .toString(16)
            .substring(1);
    }
    return function() {
        return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
            s4() + '-' + s4() + s4() + s4();
    };
})();

function S4() {
    return Math.floor((1 + Math.random()) * 0x10000)
        .toString(16)
        .substring(1);
};

MongoClient.connect(process.env.MONGOHQ_URL, function(err, db) {
    app.all('/*', function(req, res, next) {
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "X-Requested-With");
        next();
    });

    var renderHomePage = function(req, res) {
        var homeTemplate = fs.readFileSync(path.resolve(__dirname, 'home.template'), 'utf8');
        db.collection('users').find({}).toArray(function(err, docs) {
            var usernameArray = [];
            docs.forEach(function(doc) {
                usernameArray.push({
                    username: doc.username,
                    gravatar: gravatar.url(doc.email, {
                        s: '80',
                        r: 'pg',
                        d: '404'
                    })
                });
            });
            var page = Mustache.render(homeTemplate, {
                usernames: usernameArray
            });
            res.send(page);
        });

    };

    var renderResume = function(req, res) {
        var uid = req.params.uid;
        var format = req.params.format || 'html';
        console.log(format);
        db.collection('resumes').findOne({
            'jsonresume.username': uid,
        }, function(err, resume) {

            var content = '';
            switch (format) {
                case 'json':
                    content = JSON.stringify(resume, undefined, 4);
                    res.set({
                        'Content-Type': 'text/plain',
                        'Content-Length': content.length
                    });

                    res.send(content);
                    break;
                case 'txt':
                    content = resumeToText(resume, function(plainText) {
                        res.set({
                            'Content-Type': 'text/plain',
                            'Content-Length': plainText.length
                        });
                        res.send(200, plainText);
                    });
                    break
                case 'md':
                    resumeToMarkdown(resume, function(markdown, errs) {
                        res.set({
                            'Content-Type': 'text/plain',
                            'Content-Length': markdown.length
                        });
                        res.send(markdown);
                    })
                    break;
                case 'pdf':
                    resumeToPDF(resume, function(err, buffer) {
                        if (err) return console.log(err);
                        res.contentType("application/pdf");
                        res.send(buffer);
                    });
                    break;
                default:
                    console.log('def')
                    resumeToHTML(resume, function(content, errs) {
                        var resumeTemplate = fs.readFileSync(path.resolve(__dirname, 'layout.template'), 'utf8');

                        var page = Mustache.render(resumeTemplate, {
                            output: content,
                            resume: resume,
                            username: uid
                        });
                        res.send(page);
                    });
            }
        });
    };

    app.get('/', renderHomePage);
    app.get('/:uid.:format', renderResume);
    app.get('/:uid', renderResume);

    app.post('/resume', function(req, res) {
        var password = <PASSWORD>.body.password;
        var email = req.body.email;
        console.log(req.body);
        if (!req.body.guest) {
            db.collection('users').findOne({
                'email': email
            }, function(err, user) {
                if (user && bcrypt.compareSync(password, user.hash)) {
                    var resume = req.body && req.body.resume || {};
                    resume.jsonresume = {
                        username: user.username
                    };
                    console.log('inserted');
                    db.collection('resumes').update({
                        'jsonresume.username': user.username
                    }, resume, {
                        upsert: true,
                        safe: true
                    }, function(err, resume) {
                        res.send({
                            url: 'http://registry.jsonresume.org/' + user.username
                        });
                    });
                } else {
                    console.log('deleted');
                    res.send({
                        message: 'ERRORRRSSSS'
                    });
                }
            });
        } else if (req.body.pProtected !== false) {

            res.send({
                url: "hahahahahahahha"
            });

        } else {
            var guestUsername = S4() + S4();
            var resume = req.body && req.body.resume || {};
            resume.jsonresume = {
                username: guestUsername
            };
            console.log('inserted');
            db.collection('resumes').insert(resume, {
                safe: true
            }, function(err, resume) {
                res.send({
                    url: 'http://registry.jsonresume.org/' + guestUsername
                });
            });
        }
    });<commit_msg>if registered and password protected<commit_after>var express = require("express");
var Mustache = require('mustache');
var resumeToText = require('resume-to-text');
var path = require('path');
var resumeToHTML = require('resume-to-html');
var resumeToPDF = require('resume-to-pdf');
var resumeToMarkdown = require('resume-to-markdown');
var bodyParser = require('body-parser');
var bcrypt = require('bcrypt-nodejs');
var gravatar = require('gravatar');
var app = express();

var postmark = require("postmark")(process.env.POSTMARK_API_KEY);


var MongoClient = require('mongodb').MongoClient;
var mongo = require('mongodb');
app.use(bodyParser());
var fs = require('fs');
var guid = (function() {
    function s4() {
        return Math.floor((1 + Math.random()) * 0x10000)
            .toString(16)
            .substring(1);
    }
    return function() {
        return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
            s4() + '-' + s4() + s4() + s4();
    };
})();

function S4() {
    return Math.floor((1 + Math.random()) * 0x10000)
        .toString(16)
        .substring(1);
};

MongoClient.connect(process.env.MONGOHQ_URL, function(err, db) {
    app.all('/*', function(req, res, next) {
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "X-Requested-With");
        next();
    });

    var renderHomePage = function(req, res) {
        var homeTemplate = fs.readFileSync(path.resolve(__dirname, 'home.template'), 'utf8');
        db.collection('users').find({}).toArray(function(err, docs) {
            var usernameArray = [];
            docs.forEach(function(doc) {
                usernameArray.push({
                    username: doc.username,
                    gravatar: gravatar.url(doc.email, {
                        s: '80',
                        r: 'pg',
                        d: '404'
                    })
                });
            });
            var page = Mustache.render(homeTemplate, {
                usernames: usernameArray
            });
            res.send(page);
        });

    };

    var renderResume = function(req, res) {
        var uid = req.params.uid;
        var format = req.params.format || 'html';
        console.log(format);
        db.collection('resumes').findOne({
            'jsonresume.username': uid,
        }, function(err, resume) {

            var content = '';
            switch (format) {
                case 'json':
                    content = JSON.stringify(resume, undefined, 4);
                    res.set({
                        'Content-Type': 'text/plain',
                        'Content-Length': content.length
                    });

                    res.send(content);
                    break;
                case 'txt':
                    content = resumeToText(resume, function(plainText) {
                        res.set({
                            'Content-Type': 'text/plain',
                            'Content-Length': plainText.length
                        });
                        res.send(200, plainText);
                    });
                    break
                case 'md':
                    resumeToMarkdown(resume, function(markdown, errs) {
                        res.set({
                            'Content-Type': 'text/plain',
                            'Content-Length': markdown.length
                        });
                        res.send(markdown);
                    })
                    break;
                case 'pdf':
                    resumeToPDF(resume, function(err, buffer) {
                        if (err) return console.log(err);
                        res.contentType("application/pdf");
                        res.send(buffer);
                    });
                    break;
                default:
                    console.log('def')
                    resumeToHTML(resume, function(content, errs) {
                        var resumeTemplate = fs.readFileSync(path.resolve(__dirname, 'layout.template'), 'utf8');

                        var page = Mustache.render(resumeTemplate, {
                            output: content,
                            resume: resume,
                            username: uid
                        });
                        res.send(page);
                    });
            }
        });
    };

    app.get('/', renderHomePage);
    app.get('/:uid.:format', renderResume);
    app.get('/:uid', renderResume);

    app.post('/resume', function(req, res) {
        var password = req.body.password;
        var email = req.body.email;
        console.log(req.body);
        if (!req.body.guest) {
            db.collection('users').findOne({
                'email': email
            }, function(err, user) {
                if (user && bcrypt.compareSync(password, user.hash)) {
                    var resume = req.body && req.body.resume || {};
                    resume.jsonresume = {
                        username: user.username
                    };
                    console.log('inserted');
                    db.collection('resumes').update({
                        'jsonresume.username': user.username
                    }, resume, {
                        upsert: true,
                        safe: true
                    }, function(err, resume) {
                        res.send({
                            url: 'http://registry.jsonresume.org/' + user.username
                        });
                    });
                } else {
                    console.log('deleted');
                    res.send({
                        message: 'ERRORRRSSSS'
                    });
                }
            });
        } else if (req.body.pProtected !== false) {

            db.collection('users').findOne({
                'email': email
            }, function(err, user) {
                if (user && bcrypt.compareSync(password, user.hash)) {
                    var resume = req.body && req.body.resume || {};
                    resume.jsonresume = {
                        username: user.username,
                        pProtected: req.body.pProtected
                    };
                    console.log('inserted');
                    db.collection('resumes').update({
                        'jsonresume.username': user.username
                    }, resume, {
                        upsert: true,
                        safe: true
                    }, function(err, resume) {
                        res.send({
                            url: 'http://registry.jsonresume.org/' + user.username
                        });
                    });
                } else {
                    console.log('deleted');
                    res.send({
                        message: 'ERRORRRSSSS'
                    });
                }
            });

        } else {
            var guestUsername = S4() + S4();
            var resume = req.body && req.body.resume || {};
            resume.jsonresume = {
                username: guestUsername
            };
            console.log('inserted');
            db.collection('resumes').insert(resume, {
                safe: true
            }, function(err, resume) {
                res.send({
                    url: 'http://registry.jsonresume.org/' + guestUsername
                });
            });
        }
    });
{'fraction_non_alphanumeric': 0.13221049587971664, 'fraction_numerical': 0.0062165678762469275, 'mean_word_length': 1.1718995290423861, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, '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': '5843658', 'n_tokens_mistral': 3558, 'n_tokens_neox': 3373, 'n_words': 623}