<commit_before>var fs = require('fs')
  , crypto = require('crypto')
  ,ffmpeg  = require('fluent-ffmpeg');

var CanvasCapture = function() {
  return function(req, res, next) {
    var app = req.app;

    /**
     * Generates and sends back a new captureId.
     */
    app.get('/capture', function(request, response) {
      crypto.randomBytes(24, function(ex, buf) {
        var token = buf.toString('hex');
        response.end(token);
      });
    });

    /**
     * Receives a frame from the capture.
     */
    app.post('/capture/:id/frame/:n', function(request, response) {
      // Verify Content-Type before proceeding
      var mime = request.headers['content-type'] || '';
      mime = mime.split(';')[0];
      if (mime.split(';')[0] !== 'image/png') {
        response.end();
        return;
      }

      var dataURL = '';
      request.setEncoding('utf8');
      request.on('data', function(chunk) {
        dataURL += chunk;
      });
      request.on('end', function() {
        var buffer = new Buffer(dataURL, 'base64');

        var id = request.params.id;
        var frameN = request.params.n;

        if (!fs.existsSync('captures/')) {
          fs.mkdirSync('captures/', 0777);
        }
        if (!fs.existsSync('captures/' + id + '/')) {
          fs.mkdirSync('captures/' + id + '/');
        }
        fs.writeFile('captures/' + id + '/'  + frameN + '.png', buffer, 'binary', function(err) {
        });
        response.end();
      });
    });

    /**
     * Pipes the video for download.
     */
    app.get('/capture/:id/canvas.mp4', function(request, response) {
      response.writeHead(200, { 
        'Content-Type':'video/mp4' 
      });

      var filePath = 'captures/' + request.params.id + '/canvas.mp4';
      var fileStream = fs.createReadStream(filePath);
      fileStream.pipe(response);
    });

    /**
     * Triggers ffmpeg to render the captured frames into a video.
     */
    app.post('/capture/:id/render', function(request, response) {
      var fps = request.body.fps || 60;
      var proc = new ffmpeg({
        source: 'captures/' + request.params.id + '/%d.png',
        timeout: 30,
        priority: 0
      })
      .withFpsInput(fps)
      .onProgress(function(progress) {
      })
      .saveToFile('captures/' + request.params.id + '/canvas.mp4', function(retcode, error) {<commit_msg>Removing .png frames once the rendering is complete<commit_after>var fs = require('fs')
  , crypto = require('crypto')
  , ffmpeg = require('fluent-ffmpeg');

var CanvasCapture = function() {
  return function(req, res, next) {
    var app = req.app;

    /**
     * Generates and sends back a new captureId.
     */
    app.get('/capture', function(request, response) {
      crypto.randomBytes(24, function(ex, buf) {
        var token = buf.toString('hex');
        response.end(token);
      });
    });

    /**
     * Receives a frame from the capture.
     */
    app.post('/capture/:id/frame/:n', function(request, response) {
      // Verify Content-Type before proceeding
      var mime = request.headers['content-type'] || '';
      mime = mime.split(';')[0];
      if (mime.split(';')[0] !== 'image/png') {
        response.end();
        return;
      }

      var dataURL = '';
      request.setEncoding('utf8');
      request.on('data', function(chunk) {
        dataURL += chunk;
      });
      request.on('end', function() {
        var buffer = new Buffer(dataURL, 'base64');

        var id = request.params.id;
        var frameN = request.params.n;

        if (!fs.existsSync('captures/')) {
          fs.mkdirSync('captures/', 0777);
        }
        if (!fs.existsSync('captures/' + id + '/')) {
          fs.mkdirSync('captures/' + id + '/');
        }
        fs.writeFile('captures/' + id + '/'  + frameN + '.png', buffer, 'binary', function(err) {
        });
        response.end();
      });
    });

    /**
     * Pipes the video for download.
     */
    app.get('/capture/:id/canvas.mp4', function(request, response) {
      response.writeHead(200, { 
        'Content-Type':'video/mp4' 
      });

      var filePath = 'captures/' + request.params.id + '/canvas.mp4';
      var fileStream = fs.createReadStream(filePath);
      fileStream.pipe(response);
    });

    /**
     * Triggers ffmpeg to render the captured frames into a video.
     */
    app.post('/capture/:id/render', function(request, response) {
      var fps = request.body.fps || 60;
      var proc = new ffmpeg({
        source: 'captures/' + request.params.id + '/%d.png',
        timeout: 30,
        priority: 0
      })
      .withFpsInput(fps)
      .onProgress(function(progress) {
      })
      .saveToFile('captures/' + request.params.id + '/canvas.mp4', function(retcode, error) {
        // Remove .png frames to free up space
        fs.readdir('captures/' + request.params.id, function(err, files) {
          if (err) return;
          for (var i = 0; i < files.length; i++) {
            var file = files[i];
            if (file.slice(-4) !== '.png') continue;

            fs.unlink('captures/' + request.params.id + '/' + file);
          }
        });
{'fraction_non_alphanumeric': 0.17289719626168223, 'fraction_numerical': 0.009345794392523364, 'mean_word_length': 2.100181050090525, 'pattern_counts': {'":': 0, '<': 4, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2716878', 'n_tokens_mistral': 1665, 'n_tokens_neox': 1600, 'n_words': 336}