<commit_before>const express = require("express");
const webshot = require("webshot");
const im = require("imagemagick-stream");
const fs = require("fs");
const app  = express();
const PORT = 9100;

const options = {
  streamType: "jpeg",
  defaultWhiteBackground: true
};

function fileName(url, resize, options) {
  var name = (url + resize).replace(new RegExp(`[",/;\[\*\.\:\|\=\?\%]`, "g"), "_").replace("\\", "_").replace("\]", "_");
  return name + "." + options.streamType;
}

module.exports = function (cluster) {
  app.get("/", function (req, res) {
    const url = req.query.url;
    const resize = req.query.resize;
    const filePath = __dirname + "/screens/" + fileName(url, resize, options);
    if (url && resize) {
      fs.stat(filePath, function(err) {
        if (err) {
          const file = fs.createWriteStream(filePath, { encoding: "binary" });
          webshot(url, options)
            .pipe(im().resize(resize).quality(99))
            .on("data", function(data) {
              file.write(data.toString("binary"), "binary");
              res.write(data);
            })
            .on("error", function (webshotError) {
              fs.unlink(filePath, function (rmError) {
                if (rmError) {
                  console.error(filePath, "rmError", rmError, "webshotError", webshotError);
                  res.status(500);
                } else {
                  console.error(filePath, "error", webshotError);
                  res.status(500);
                }
              });
            })
            .on("end", function() {
              res.end();
            });
        } else {
          res.sendFile(filePath);
        }
      });
    } else {
      console.error("url or resize missing", url, resize);
      res.status(400).end("url or resize missing");
    }
  });

  var server = app.listen(PORT, () => {
    var host = server.address().address;
    var port = server.address().port;
    console.log(`App worker ${cluster.worker.id} listening at http://${host}:${port}`);
  });

};<commit_msg>Refactoring: use middlewares<commit_after>const express = require("express");
const webshot = require("webshot");
const im = require("imagemagick-stream");
const fs = require("fs");
const app  = express();
const PORT = 9100;

const options = {
  streamType: "jpeg",
  defaultWhiteBackground: true
};

function fileName(url, resize, options) {
  var name = (url + resize).replace(new RegExp(`[",/;\[\*\.\:\|\=\?\%]`, "g"), "_").replace("\\", "_").replace("\]", "_");
  return name + "." + options.streamType;
}

function cache(req, res, next) {
  const filePath = req._filePath;
  fs.stat(filePath, function(err, stat) {
    console.log(stat);
    if (err) next();
    else res.sendFile(filePath);
  });
}

function validator(req, res, next) {
  const url = req.query.url;
  const resize = req.query.resize;
  if (url && resize) {
    req._url = url;
    req._resize = resize;
    req._filePath = __dirname + "/screens/" + fileName(url, resize, options);
    next();
  } else {
    console.error("url or resize missing", url, resize);
    res.status(400).end("url or resize missing");
  }
}

module.exports = function (cluster) {
  app.get("/", validator, cache, function (req, res) {
    const url = req._url;
    const resize = req._resize;
    const filePath = req._filePath;
    const file = fs.createWriteStream(filePath, { encoding: "binary" });
    webshot(url, options)
      .pipe(im().resize(resize).quality(99))
      .on("data", function(data) {
        file.write(data.toString("binary"), "binary");
        res.write(data);
      })
      .on("error", function (webshotError) {
        fs.unlink(filePath, function (rmError) {
          if (rmError) {
            console.error(filePath, "rmError", rmError, "webshotError", webshotError);
            res.status(500);
          } else {
            console.error(filePath, "error", webshotError);
            res.status(500);
          }
        });
      })
      .on("end", function() {
        res.end();
      });
  });

  var server = app.listen(PORT, () => {
    var host = server.address().address;
    var port = server.address().port;
    console.log(`App worker ${cluster.worker.id} listening at http://${host}:${port}`);
  });

};
{'fraction_non_alphanumeric': 0.18055881662362055, 'fraction_numerical': 0.00704390702042733, 'mean_word_length': 2.497536945812808, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '7666983', 'n_tokens_mistral': 1348, 'n_tokens_neox': 1306, 'n_words': 289}