<commit_before>var rekuire = require('rekuire');
var util = require('util');
var config = require('config');
var _ = require('underscore');
var rest = rekuire('src/util/rest');
var QR = rekuire('src/models/qr');
var request = require('request');
var Canvas = new require('canvas');
var qrcode = require('qrcode');
var fs = require('fs');

module.exports.getAllQRs = function(req, res) {
    QR.find({}).sort({_id: 1}).exec(function(err, qrs) {
        res.send(err ? 404 : qrs);
    });
}

module.exports.newQR = function(req, res) {

    function pad(n, width, z) {
        z = z || '0';
        n = n + '';
        return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
    }

    QR.count({}, function (err, count){
        QR.create({humanId: 'UK' + pad(count, 10)}, function (err, qr) {
            res.send(err ? 404 : qr);
        });
    });
}

module.exports.getQR = function(req, res) {
    QR.findOne({humanId: req.param('id')}, function(err, qr) {
        res.send(qr ? qr : 404);
    });
}

module.exports.getQRImage = function(req, res) {
    var margin = req.param('print') ? 10 : 0;

    QR.findOne({humanId: req.param('id')}, function(err, qr) {
        if (!qr) {
            res.send(404);
            return;
        }
        qrcode.draw(qr.id, {scale: 10, margin: margin}, function(error, canvas) {
            ctx = canvas.getContext('2d');

            if (req.param('print')) {
                ctx.font = 'italic 20pt Calibri';
                ctx.fillStyle = 'black';
                ctx.textAlign = 'center';
                //ctx.rotate(90*Math.PI/180);
                ctx.fillText(qr.humanId, canvas.width / 2, canvas.height - 10);
                //ctx.rotate(-90*Math.PI/180);
            }

            function sendImage() {
                res.type('jpg');
                canvas.jpegStream({bufsize: 4096, quality: 100, progressive: false}).pipe(res);
            }

            var brand = req.param('brand');

            var brands = {
                wds: __dirname + '/../data/wds.gif',
                wds2: __dirname + '/../data/wds.png',
                apple: __dirname + '/../data/apple.png'
            }

            if (brands[brand]) {
                var data = fs.readFileSync(brands[brand]);
                var rect = {
                    center: {
                        x: canvas.width / 2,
                        y: canvas.height / 2
                    },
                    width: (canvas.width - margin * 10) / 3,
                    height: (canvas.height - margin * 10) / 3
                };

                ctx.beginPath();
                ctx.rect(rect.center.x - rect.width / 2, rect.center.y - rect.height / 2, rect.width, rect.height);
                ctx.fillStyle = 'white';
                ctx.fill();
                var img = new Canvas.Image;
                img.src = data;
                ctx.drawImage(img, rect.center.x - rect.width / 2 + 5, rect.center.y - rect.height / 2 + 5, rect.width - 10, rect.height - 10);
            }
            
            sendImage();
        });
    });
}<commit_msg>Fixed aspect ratio of the brand<commit_after>var rekuire = require('rekuire');
var util = require('util');
var config = require('config');
var _ = require('underscore');
var rest = rekuire('src/util/rest');
var QR = rekuire('src/models/qr');
var request = require('request');
var Canvas = new require('canvas');
var qrcode = require('qrcode');
var fs = require('fs');

module.exports.getAllQRs = function(req, res) {
    QR.find({}).sort({_id: 1}).exec(function(err, qrs) {
        res.send(err ? 404 : qrs);
    });
}

module.exports.newQR = function(req, res) {

    function pad(n, width, z) {
        z = z || '0';
        n = n + '';
        return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
    }

    QR.count({}, function (err, count){
        QR.create({humanId: 'UK' + pad(count, 10)}, function (err, qr) {
            res.send(err ? 404 : qr);
        });
    });
}

module.exports.getQR = function(req, res) {
    QR.findOne({humanId: req.param('id')}, function(err, qr) {
        res.send(qr ? qr : 404);
    });
}

module.exports.getQRImage = function(req, res) {
    var margin = req.param('print') ? 10 : 0;

    QR.findOne({humanId: req.param('id')}, function(err, qr) {
        if (!qr) {
            res.send(404);
            return;
        }
        qrcode.draw(qr.id, {scale: 10, margin: margin}, function(error, canvas) {
            ctx = canvas.getContext('2d');

            if (req.param('print')) {
                ctx.font = 'italic 20pt Calibri';
                ctx.fillStyle = 'black';
                ctx.textAlign = 'center';
                //ctx.rotate(90*Math.PI/180);
                ctx.fillText(qr.humanId, canvas.width / 2, canvas.height - 10);
                //ctx.rotate(-90*Math.PI/180);
            }

            function sendImage() {
                res.type('jpg');
                canvas.jpegStream({bufsize: 4096, quality: 100, progressive: false}).pipe(res);
            }

            var brand = req.param('brand');

            var brands = {
                wds: __dirname + '/../data/wds.gif',
                wds2: __dirname + '/../data/wds.png',
                apple: __dirname + '/../data/apple.png'
            }

            if (brands[brand]) {
                var data = fs.readFileSync(brands[brand]);
                var rect = {
                    center: {
                        x: canvas.width / 2,
                        y: canvas.height / 2
                    },
                    width: (canvas.width - margin * 10) / 3,
                    height: (canvas.height - margin * 10) / 3
                };

                ctx.beginPath();
                ctx.rect(rect.center.x - rect.width / 2, rect.center.y - rect.height / 2, rect.width, rect.height);
                ctx.fillStyle = 'white';
                ctx.fill();
                var img = new Canvas.Image;
                img.src = data;
                var ratio = img.height / img.width;
                var iwidth = rect.width - 10;
                var iheight = iwidth * ratio;
                var ix = rect.center.x - iwidth / 2;
                var iy = rect.center.y - iheight / 2;
                ctx.drawImage(img, ix, iy, iwidth, iheight);
            }

            sendImage();
        });
    });
}
{'fraction_non_alphanumeric': 0.1601378878094641, 'fraction_numerical': 0.019429645879034785, 'mean_word_length': 1.5199368337939203, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '77143', 'n_tokens_mistral': 2146, 'n_tokens_neox': 1922, 'n_words': 429}