<commit_before>var IconModel = require('../models/icon')
  , path = require('path')
  , util = require('util')
  , fs = require('fs-extra')
  , async = require('async')
  , moment = require('moment')
  , access = require('../access')
  , config = require('../config.json');

var iconBase = config.server.iconBaseDirectory;

function Icon(form, type, variant) {
  this._form = form;
  this._type = type;
  this._variant = variant;
};

Icon.prototype.createIconPath = function(icon, name) {
  var ext = path.extname(name);
  var iconPath = this._form._id.toString();
  if (icon._type) {
    iconPath = path.join(iconPath, icon._type);
    if (this._variant) {
      iconPath = path.join(iconPath, this._variant + ext);
    } else {
      iconPath = path.join(iconPath, "default" + ext);
    }
  } else {
    iconPath = path.join(iconPath, "default" + ext);
  }

  return iconPath;
}

Icon.prototype.getIcon = function(callback) {
  var conditions = {
    formId: this._form._id,
    type: this._type || null,
    variant: this._variant || null
  };

  IconModel.getIcon(conditions, function(err, icon) {
    if (err) callback(err);

    callback(null, path.join(iconBase, icon.relativePath));
  });
}

Icon.prototype.create = function(icon, callback) {
  var relativePath = this.createIconPath(this, icon.name);
  var newIcon = {
    formId: this._form._id,
    type: this._type || null,
    variant: this._variant || null,
    relativePath: relativePath
  }

  var iconPath = path.join(iconBase, relativePath);
  fs.mkdirp(path.dirname(iconPath), function(err) {
    fs.rename(icon.path, iconPath, function(err) {
      if (err) return callback(err);
      IconModel.create(newIcon, callback);
    });
  });
}

Icon.prototype.delete = function(callback) {
  var icon = {
    formId: this._form._id,
    type: this._type || null,
    variant: this._variant || null
  }

  IconModel.remove(icon, function(err, removedIcon) {
    if (err) return callback(err);

    var file = path.join(iconBase, removedIcon.relativePath);
    fs.remove(file, function(err) {
      if (err) {
        console.error("Could not remove attachment file " + file + ". ", err);
      }
    });

    callback(err, icon);
  });
}

module.exports = Icon;
<commit_msg>return 404 if icon not found<commit_after>var IconModel = require('../models/icon')
  , path = require('path')
  , util = require('util')
  , fs = require('fs-extra')
  , async = require('async')
  , moment = require('moment')
  , access = require('../access')
  , config = require('../config.json');

var iconBase = config.server.iconBaseDirectory;

function Icon(form, type, variant) {
  this._form = form;
  this._type = type;
  this._variant = variant;
};

Icon.prototype.createIconPath = function(icon, name) {
  var ext = path.extname(name);
  var iconPath = this._form._id.toString();
  if (icon._type) {
    iconPath = path.join(iconPath, icon._type);
    if (this._variant) {
      iconPath = path.join(iconPath, this._variant + ext);
    } else {
      iconPath = path.join(iconPath, "default" + ext);
    }
  } else {
    iconPath = path.join(iconPath, "default" + ext);
  }

  return iconPath;
}

Icon.prototype.getIcon = function(callback) {
  var conditions = {
    formId: this._form._id,
    type: this._type || null,
    variant: this._variant || null
  };

  IconModel.getIcon(conditions, function(err, icon) {
    if (err || !icon) return callback(err);

    callback(null, path.join(iconBase, icon.relativePath));
  });
}

Icon.prototype.create = function(icon, callback) {
  var relativePath = this.createIconPath(this, icon.name);
  var newIcon = {
    formId: this._form._id,
    type: this._type || null,
    variant: this._variant || null,
    relativePath: relativePath
  }

  var iconPath = path.join(iconBase, relativePath);
  fs.mkdirp(path.dirname(iconPath), function(err) {
    fs.rename(icon.path, iconPath, function(err) {
      if (err) return callback(err);
      IconModel.create(newIcon, callback);
    });
  });
}

Icon.prototype.delete = function(callback) {
  var icon = {
    formId: this._form._id,
    type: this._type || null,
    variant: this._variant || null
  }

  IconModel.remove(icon, function(err, removedIcon) {
    if (err) return callback(err);

    var file = path.join(iconBase, removedIcon.relativePath);
    fs.remove(file, function(err) {
      if (err) {
        console.error("Could not remove attachment file " + file + ". ", err);
      }
    });

    callback(err, icon);
  });
}

module.exports = Icon;

{'fraction_non_alphanumeric': 0.1550920789882405, 'fraction_numerical': 0.0006656312402928778, 'mean_word_length': 3.281101614434948, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3334083', 'n_tokens_mistral': 1485, 'n_tokens_neox': 1467, 'n_words': 317}