<commit_before>var logger = require('koa-logger');
var serve = require('koa-static');
var parse = require('co-busboy');
var koa = require('koa');
var fs = require('fs');
var app = koa();
var os = require('os');
var path = require('path');
var rootPath = path.join(__dirname, '..');
console.log(rootPath);

// logging
//app.use(logger());
// redirect to homepage
app.use(function*(next) {
  yield next;
  if (this.body || !this.idempotent) return;
  console.log('error this:', this);
  //this.redirect(path.join('/src/'));
});

// serve static files
app.use(serve(rootPath));

// handle uploads
app.use(function*(next) {
  // ignore non-POSTs
  if ('POST' != this.method) return yield next;

  if (!this.request.is('multipart/*')) return yield next;
  console.log('multipart upload');
  // multipart upload
  var parts = parse(this);
  var part;

  try {
    while (part = yield parts) {
      var tempFileName = `${Date.now()}_${Math.floor(Math.random()*1000000)}.jpg`;
      if (part.length) { // fields
        console.log(part);
      }
      else { // stream
        var stream = fs.createWriteStream(path.join(rootPath, 'src_server', 'uploads', tempFileName));
        part.pipe(stream);
        console.log('uploading %s -> %s', part.filename, stream.path);
      }
    }
    this.status = 200;
  } catch (exception) {
    console.log(exception);
    this.status = 500;
  }
});


app.listen(3000);
console.log('listening on port 3000');
<commit_msg>refactor<commit_after>'use strict';

const fs = require('fs');
const path = require('path');
// const os = require('os');
const koa = require('koa');
const serve = require('koa-static');
const logger = require('koa-logger');
const parse = require('co-busboy');

const rootPath = path.join(__dirname, '..');
const app = koa();
// logging
//app.use(logger());

// redirect to homepage
app.use(function*(next) {
  yield next;
  if (this.body || !this.idempotent) return;
  console.log('error this:', this);
  //this.redirect(path.join('/src/'));
});

// serve static files
app.use(serve(rootPath));

// handle uploads
app.use(function*(next) {
  // ignore non-POSTs
  if (this.method !== 'POST' && !this.request.is('multipart/*')) return yield next;
  console.log('multipart upload');
  // multipart upload
  var parts = parse(this), part;

  try {
    while (part = yield parts) {
      let fileName = `${Date.now()}_${Math.floor(Math.random()*1000000)}.jpg`;
      let writePath = path.join(rootPath, 'src_server', 'uploads', fileName);
      if (part.length) { // fields
        console.log(part);
      } else { // stream
        let stream = fs.createWriteStream(writePath);
        part.pipe(stream);
        console.log('uploading %s -> %s', part.filename, stream.path);
      }
    }
    this.status = 200;
  } catch (exception) {
    console.log(exception);
    this.status = 500;
  }
});


app.listen(3000);
console.log('listening on port 3000');

{'fraction_non_alphanumeric': 0.1880371261601925, 'fraction_numerical': 0.014437951185974561, 'mean_word_length': 3.3891402714932126, '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': '1265072', 'n_tokens_mistral': 996, 'n_tokens_neox': 950, 'n_words': 221}