'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _nodeBits = require('node-bits');

exports.default = function () {
  return {
    store: function store(file) {
      return file.data.toString('base64');
    },

    getImage: function getImage(req, res, db) {
      var model = req.query.model;
      if (!model) {
        res.status(404).send('You must provide model for this configuration.');
        return;
      }

      var field = req.query.field;
      if (!field) {
        res.status(404).send('You must provide field for this configuration.');
        return;
      }

      var id = req.query.id;
      if (!id) {
        res.status(404).send('You must provide id for this configuration.');
        return;
      }

      db.findById(model, id).then(function (item) {
        var data = item[field];
        if (!data) {
          res.send();
          return;
        }

        var img = new Buffer(data, 'base64');
        res.writeHead(200, {
          'Content-Type': 'image/png',
          'Content-Length': img.length
        });
        res.end(img);
      }).catch(function (err) {
        (0, _nodeBits.logError)(err);
        res.status(500).send(err);
      });
    }
  };
};