var db = require('../lib/db');
var baseurl = require('../lib/baseurl');
var path = require('path');
var QR = require('qr').Encoder;
var qr = new QR();

/*
 * Fetch a single clip
 */
exports.fetch = function(req, res, next) {
  var hash = req.params.hash;

  db.fetchItemByHash(hash, function(err, item) {
    if (err) return res.send(500);
    if (!item) return res.send(404);

    req.store.item = item;
    next();
  });
};


/*
 * Generate QR code
 */
exports.qr = function(req, res, next) {
  var item = req.store.item;

  if (!(item.type === 'ipa' || item.type === 'apk')) return next();

  baseurl.set(req.protocol, req.get('host'));

  qr.on('end', function onEncodingComplete(png) {
    res.locals.qrImage = 'data:image/png;base64,' + png.toString('base64');
    next();
  });

  qr.encode([baseurl.get(), item.basenameWithoutExt].join('/'), null, {
    background_color: 'F8F5EE',
    margin: 2,
    dot_size: 5
  });
};


/*
 * Handle clip downloading
 */
exports.send = function(req, res, next) {
  var item = req.store.item;

  if ('image' === item.type) {
    res.sendfile(path.join(process.cwd(), item.relativePathLong));
  } else {
    res.download(path.join(process.cwd(), item.relativePathLong), item.originalName);
  }

};
@@ -129,26 +129,8 @@
 er;%0A
-var qr = new QR();
 %0A%0A/*
@@ -490,32 +490,53 @@
  req.store.item;
+%0A  var qr = new QR();
 %0A%0A  if (!(item.t

Fix misplaced object