<commit_before>// Store, retrieve, and delete metadata envelopes.

var
  config = require('./config'),
  connection = require('./connection'),
  logging = require('./logging');

var log = logging.getLogger(config.content_log_level());

/**
 * @description Retrieve content from the store by content ID.
 */
exports.retrieve = function (req, res, next) {
  log.debug("Requesting content ID: [" + req.params.id + "]");

  var source = connection.client.download({
    container: config.content_container(),
    remote: encodeURIComponent(req.params.id)
  });

  // This directly sends the response to the caller, long term we'll
  // probably not want to do this, but it allows the prototype to get functional
  //
  source.pipe(res);

  next();
};

/**
 * @description Store new content into the content service.
 */
exports.store = function (req, res, next) {
  log.info("Storing content with ID: [" + req.params.id + "]");

  var dest = connection.client.upload({
    container: config.content_container(),
    remote: encodeURIComponent(req.params.id)
  });

  dest.on('success', function () {<commit_msg>Inject registered assets during envelope retrieval.<commit_after>// Store, retrieve, and delete metadata envelopes.

var
  async = require('async'),
  config = require('./config'),
  connection = require('./connection'),
  logging = require('./logging');

var log = logging.getLogger(config.content_log_level());

/**
 * @description Download the raw metadata envelope from Cloud Files.
 */
function download_content(content_id, callback) {
  var source = connection.client.download({
    container: config.content_container(),
    remote: encodeURIComponent(req.params.id)
  });
  var chunks = [];

  source.on('error', function (err) {
    callback(err);
  });

  source.on('data', function (chunk) {
    chunks.push(chunk);
  });

  source.on('end', function () {
    var
      complete = Buffer.concat(chunks),
      envelope = JSON.parse(complete);

    callback(null, envelope);
  });
}

/**
 * @description Inject asset variables included from the /assets endpoint into
 *   an outgoing metadata envelope.
 */
function inject_asset_vars(envelope, callback) {
  log.debug("Collecting asset variables to inject into the envelope.");

  connection.db.collection("layout_assets").find().toArray(function (err, asset_vars) {
    if (err) {
      callback(err);
      return;
    }

    log.debug("Injecting " + asset_vars.length + " variables into the envelope.");

    var assets = {};

    asset_vars.forEach(function (asset_var) {
      assets[asset_var.key] = asset_var.public_url;
    });

    envelope.assets = assets;

    callback(null, envelope);
  });
}

/**
 * @description Retrieve content from the store by content ID.
 */
exports.retrieve = function (req, res, next) {
  log.debug("Requesting content ID: [" + req.params.id + "]");

  async.waterfall([
    async.apply(download_content, req.params.id),
    inject_asset_vars
  ], function (err, envelope) {
    if (err) {
      log.error("Failed to retrieve a metadata envelope", err);

      res.status(err.statusCode || 500);
      res.send();
      next();

      return;
    }

    res.status(200);
    res.json(envelope);
    next();
  });
};

/**
 * @description Store new content into the content service.
 */
exports.store = function (req, res, next) {
  log.info("Storing content with ID: [" + req.params.id + "]");

  var dest = connection.client.upload({
    container: config.content_container(),
    remote: encodeURIComponent(req.params.id)
  });

  dest.on('success', function () {
{'fraction_non_alphanumeric': 0.14766040907817315, 'fraction_numerical': 0.0016811431773606051, 'mean_word_length': 3.5419847328244276, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3461070', 'n_tokens_mistral': 1057, 'n_tokens_neox': 1036, 'n_words': 275}