<commit_before>import fs from 'fs';
import os from 'os';
import path from 'path';
import querystring from 'querystring';

import multiparty from 'multiparty';
/**
 * parse data by form
 * @param  {} http []
 * @return {}      []
 */
think.middleware('parse_form_payload', http => {
  let multiReg = /^multipart\/(form-data|related);\s*boundary=(?:"([^"]+)"|([^;]+))$/i;
  //file upload by form or FormData
  if (!multiReg.test(http.req.headers['content-type'])) {
    return;
  }

  let deferred = think.defer();
  let uploadDir = think.config('post.file_upload_path') || (os.tmpdir() + '/thinkjs_upload');
  if (uploadDir) {
    think.mkdir(uploadDir);
  }
  let postConfig = think.config('post');
  let form = new multiparty.Form({
    maxFieldsSize: postConfig.max_fields_size,
    maxFields: postConfig.max_fields,
    maxFilesSize: postConfig.max_file_size,
    uploadDir: uploadDir
  });
  //support for file with multiple="multiple"
  let files = http._file;
  form.on('file', (name, value) => {
    if (name in files) {
      if (!think.isArray(files[name])) {
        files[name] = [files[name]];
      }
      files[name].push(value);
    }else{
      files[name] = value;
    }
  });
  form.on('field', (name, value) => {
    http._post[name] = value;
  });
  form.on('close', () => {
    deferred.resolve(http);
  });
  form.on('error', () => {
    http.res.statusCode = 400;
    http.end();
  });
  form.parse(http.req);
  return deferred.promise;
});


/**
 * parse upload file by ajax
 * @param  {} http []
 * @return {}      []
 */
think.middleware('parse_single_file_payload', http => {
  let filename = http.req.headers[think.config('post.single_file_header')];
  if(!filename){
    return;
  }
  let deferred = think.defer();
  let uploadDir = think.config('post.file_upload_path') || (os.tmpdir() + '/thinkjs_upload');
  if (uploadDir) {
    think.mkdir(uploadDir);
  }
  let name = think.uuid(20);
  let filepath = uploadDir + '/' + name + path.extname(filename).slice(0, 5);
  let stream = fs.createWriteStream(filepath);
  http.req.pipe(stream);
  stream.on('error', () => {
    http.res.statusCode = 400;
    http.end();
  });
  stream.on('close', () => {
    http._file.file = {
      fieldName: 'file',
      originalFilename: filename,
      path: filepath,
      size: fs.statSync(filepath).size
    };
    deferred.resolve();
  });
  return deferred.promise;
});


/**
 * parse payload
 * @param  {Object} http
 * @return {}         []
 */
think.middleware('parse_json_payload', http => {
  let types = http.config('post.json_content_type');
  if (types.indexOf(http._type) === -1) {
    return;
  }
  return http.getPayload().then(payload => {
    try{
      http._post = JSON.parse(payload);
    }catch(e){}
  });
});


/**
 * parse payload by querystring<commit_msg>replace get content-type by http.type() method<commit_after>import fs from 'fs';
import os from 'os';
import path from 'path';
import querystring from 'querystring';

import multiparty from 'multiparty';
/**
 * parse data by form
 * @param  {} http []
 * @return {}      []
 */
think.middleware('parse_form_payload', http => {
  let multiReg = /^multipart\/(form-data|related);\s*boundary=(?:"([^"]+)"|([^;]+))$/i;
  //file upload by form or FormData
  if (!multiReg.test(http.req.headers['content-type'])) {
    return;
  }

  let deferred = think.defer();
  let uploadDir = think.config('post.file_upload_path') || (os.tmpdir() + '/thinkjs_upload');
  if (uploadDir) {
    think.mkdir(uploadDir);
  }
  let postConfig = think.config('post');
  let form = new multiparty.Form({
    maxFieldsSize: postConfig.max_fields_size,
    maxFields: postConfig.max_fields,
    maxFilesSize: postConfig.max_file_size,
    uploadDir: uploadDir
  });
  //support for file with multiple="multiple"
  let files = http._file;
  form.on('file', (name, value) => {
    if (name in files) {
      if (!think.isArray(files[name])) {
        files[name] = [files[name]];
      }
      files[name].push(value);
    }else{
      files[name] = value;
    }
  });
  form.on('field', (name, value) => {
    http._post[name] = value;
  });
  form.on('close', () => {
    deferred.resolve(http);
  });
  form.on('error', () => {
    http.res.statusCode = 400;
    http.end();
  });
  form.parse(http.req);
  return deferred.promise;
});


/**
 * parse upload file by ajax
 * @param  {} http []
 * @return {}      []
 */
think.middleware('parse_single_file_payload', http => {
  let filename = http.req.headers[think.config('post.single_file_header')];
  if(!filename){
    return;
  }
  let deferred = think.defer();
  let uploadDir = think.config('post.file_upload_path') || (os.tmpdir() + '/thinkjs_upload');
  if (uploadDir) {
    think.mkdir(uploadDir);
  }
  let name = think.uuid(20);
  let filepath = uploadDir + '/' + name + path.extname(filename).slice(0, 5);
  let stream = fs.createWriteStream(filepath);
  http.req.pipe(stream);
  stream.on('error', () => {
    http.res.statusCode = 400;
    http.end();
  });
  stream.on('close', () => {
    http._file.file = {
      fieldName: 'file',
      originalFilename: filename,
      path: filepath,
      size: fs.statSync(filepath).size
    };
    deferred.resolve();
  });
  return deferred.promise;
});


/**
 * parse payload
 * @param  {Object} http
 * @return {}         []
 */
think.middleware('parse_json_payload', http => {
  let types = http.config('post.json_content_type');
  if (types.indexOf(http.type()) === -1) {
    return;
  }
  return http.getPayload().then(payload => {
    try{
      http._post = JSON.parse(payload);
    }catch(e){}
  });
});


/**
 * parse payload by querystring
{'fraction_non_alphanumeric': 0.18614564831261102, 'fraction_numerical': 0.003907637655417406, 'mean_word_length': 3.2338345864661653, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '4594039', 'n_tokens_mistral': 1967, 'n_tokens_neox': 1914, 'n_words': 374}