<commit_before>var fs = require('fs');
var imops = require('imops');
var ogger = require('./ogger');
var path = require('path');

// TODO: Separate middleware from other logic.
module.exports = function(app, config) {

  var cacheDir = config.server.cache;
  var resourcesDir = config.server.resources;

  function utilExist(variable) {
    return (!(typeof variable === 'undefined' || variable === null || variable === ""));
  }

  // Create folders if they don't exist.
  if(!(fs.existsSync(cacheDir)))                {fs.mkdirSync(cacheDir);}
  if(!(fs.existsSync(cacheDir + '/image')))     {fs.mkdirSync(cacheDir + '/image');}
  if(!(fs.existsSync(cacheDir + '/sound')))     {fs.mkdirSync(cacheDir + '/sound');}
  if(!(fs.existsSync(resourcesDir)))            {fs.mkdirSync(resourcesDir);}
  if(!(fs.existsSync(resourcesDir + '/image'))) {fs.mkdirSync(resourcesDir + '/image');}
  if(!(fs.existsSync(resourcesDir +'/sound')))  {fs.mkdirSync(resourcesDir + '/sound');}

  // Resize image using imops.
  function resizeImage(sizeString, path, res){
    if(!(fs.existsSync(cacheDir  + '/image/' + sizeString))) {
      fs.mkdirSync(cacheDir  + '/image/' + sizeString);
    }
    var size = sizeString.split('x');
    var gi = new imops.image();
    gi.read(resourcesDir + '/image/' + path);

    var newHeight = gi.height / gi.width * size[0];
    var scaledImage;
    if (newHeight < size[1]) {
      scaledImage = gi.scale(size[0], newHeight);
    } else {
      scaledImage = gi.scale(gi.width / gi.height * size[1], size[1]);
    }

    scaledImage.write(cacheDir + '/image/' + sizeString + '/' + path);
    if (!pipeFile(cacheDir + '/image/' + sizeString + '/' + path,res)) {
      res.end('Error');
    }
  }
<commit_msg>fixed 500 errors due to resizeimage<commit_after>var fs = require('fs');
var imops = require('imops');
var ogger = require('./ogger');
var path = require('path');

// TODO: Separate middleware from other logic.
module.exports = function(app, config) {

  var cacheDir = config.server.cache;
  var resourcesDir = config.server.resources;

  function utilExist(variable) {
    return (!(typeof variable === 'undefined' || variable === null || variable === ""));
  }

  // Create folders if they don't exist.
  if(!(fs.existsSync(cacheDir)))                {fs.mkdirSync(cacheDir);}
  if(!(fs.existsSync(cacheDir + '/image')))     {fs.mkdirSync(cacheDir + '/image');}
  if(!(fs.existsSync(cacheDir + '/sound')))     {fs.mkdirSync(cacheDir + '/sound');}
  if(!(fs.existsSync(resourcesDir)))            {fs.mkdirSync(resourcesDir);}
  if(!(fs.existsSync(resourcesDir + '/image'))) {fs.mkdirSync(resourcesDir + '/image');}
  if(!(fs.existsSync(resourcesDir +'/sound')))  {fs.mkdirSync(resourcesDir + '/sound');}

  // Resize image using imops.
  function resizeImage(sizeString, path, res){
    if(!(fs.existsSync(cacheDir  + '/image/' + sizeString))) {
      fs.mkdirSync(cacheDir  + '/image/' + sizeString);
    }
    var size = sizeString.split('x');
    var gi = new imops.image();
    gi.read(resourcesDir + '/image/' + path);

    var newHeight = gi.height / gi.width * size[0];
    var scaledImage;
    if (newHeight < size[1]) {
      scaledImage = gi.scale(size[0], newHeight);
    } else {
      scaledImage = gi.scale(gi.width / gi.height * size[1], size[1]);
    }
  	
    if (scaledImage !== null)
    {
      scaledImage.write(cacheDir + '/image/' + sizeString + '/' + path);
      if (!pipeFile(cacheDir + '/image/' + sizeString + '/' + path,res)) {
        res.end('Error');
      }
    }
  }

{'fraction_non_alphanumeric': 0.18946474086661003, 'fraction_numerical': 0.0036816765788728408, 'mean_word_length': 3.3231334149326806, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2179502', 'n_tokens_mistral': 1189, 'n_tokens_neox': 1145, 'n_words': 238}