<commit_before>var async = require('async'),
    common = require('../common'),
    config = require('../config'),
    child_process = require('child_process'),
    db = require('../db'),
    formidable = require('formidable'),
    fs = require('fs'),
    hooks = require('../hooks'),
    im = require('imagemagick'),
    path = require('path'),
    util = require('util'),
    winston = require('winston');

var image_attrs = ('src thumb dims size MD5 hash imgnm spoiler realthumb vint'
		+ ' apng').split(' ');

function is_image(image) {
	return image && (image.src || image.vint);
};

hooks.hook('extractPost', function (post, cb) {
	if (!is_image(post))
		return cb(null);
	var image = {};
	image_attrs.forEach(function (key) {
		if (key in post) {
			image[key] = post[key];
			delete post[key];
		}
	});
	if (image.dims.split)
		image.dims = image.dims.split(',');
	image.size = parseInt(image.size);
	delete image.hash;
	post.image = image;
	cb(null);
});

hooks.hook('inlinePost', function (info, cb) {
	var post = info.dest, image = info.src.image;
	if (!image)
		return cb(null);
	image_attrs.forEach(function (key) {
		if (key in image)
			post[key] = image[key];
	});
	cb(null);
});

function get_thumb_specs(w, h, pinky) {
	var QUALITY = config[pinky ? 'PINKY_QUALITY' : 'THUMB_QUALITY'];
	var bound = config[pinky ? 'PINKY_DIMENSIONS' : 'THUMB_DIMENSIONS'];
	var r = Math.max(w / bound[0], h / bound[1], 1);
	var bg = pinky ? '#d6daf0' : '#eef2ff';
	return {dims: [Math.round(w/r), Math.round(h/r)], quality: QUALITY,
			bg_color: bg, bound: bound};
}

exports.ImageUpload = function (db, status) {
	this.db = db;
	this.statusCallback = status;
};

var IU = exports.ImageUpload.prototype;

var validFields = ['client_id', 'spoiler', 'op'];

IU.status = function (msg) {
	this.form_call('upload_status', msg);
};

IU.handle_request = function (req, resp, board) {
	this.board = board;
	this.resp = resp;
	var len = parseInt(req.headers['content-length'], 10);
	if (len > 0 && len > config.IMAGE_FILESIZE_MAX + (20*1024))
		return this.failure('File is too large.');

	var form = new formidable.IncomingForm();
	form.uploadDir = config.MEDIA_DIRS.tmp;
	form.maxFieldsSize = 50 * 1024;
	form.hash = 'md5';
	form.onPart = function (part) {
		if (part.filename && part.name == 'image')
			form.handlePart(part);
		else if (!part.filename && validFields.indexOf(part.name) >= 0)
			form.handlePart(part);
		else
			this._error('Superfluous field.');
	};
	var self = this;
	form.once('error', function (err) {
		winston.error('formidable: ' + err);
		self.failure('Upload request problem.');
	});
	form.once('aborted', function (err) {
		self.failure('Upload was aborted.');
	});
	this.lastProgress = -1;<commit_msg>Just ignore superfluous POST fields<commit_after>var async = require('async'),
    common = require('../common'),
    config = require('../config'),
    child_process = require('child_process'),
    db = require('../db'),
    formidable = require('formidable'),
    fs = require('fs'),
    hooks = require('../hooks'),
    im = require('imagemagick'),
    path = require('path'),
    util = require('util'),
    winston = require('winston');

var image_attrs = ('src thumb dims size MD5 hash imgnm spoiler realthumb vint'
		+ ' apng').split(' ');

function is_image(image) {
	return image && (image.src || image.vint);
};

hooks.hook('extractPost', function (post, cb) {
	if (!is_image(post))
		return cb(null);
	var image = {};
	image_attrs.forEach(function (key) {
		if (key in post) {
			image[key] = post[key];
			delete post[key];
		}
	});
	if (image.dims.split)
		image.dims = image.dims.split(',');
	image.size = parseInt(image.size);
	delete image.hash;
	post.image = image;
	cb(null);
});

hooks.hook('inlinePost', function (info, cb) {
	var post = info.dest, image = info.src.image;
	if (!image)
		return cb(null);
	image_attrs.forEach(function (key) {
		if (key in image)
			post[key] = image[key];
	});
	cb(null);
});

function get_thumb_specs(w, h, pinky) {
	var QUALITY = config[pinky ? 'PINKY_QUALITY' : 'THUMB_QUALITY'];
	var bound = config[pinky ? 'PINKY_DIMENSIONS' : 'THUMB_DIMENSIONS'];
	var r = Math.max(w / bound[0], h / bound[1], 1);
	var bg = pinky ? '#d6daf0' : '#eef2ff';
	return {dims: [Math.round(w/r), Math.round(h/r)], quality: QUALITY,
			bg_color: bg, bound: bound};
}

exports.ImageUpload = function (db, status) {
	this.db = db;
	this.statusCallback = status;
};

var IU = exports.ImageUpload.prototype;

var validFields = ['client_id', 'spoiler', 'op'];

IU.status = function (msg) {
	this.form_call('upload_status', msg);
};

IU.handle_request = function (req, resp, board) {
	this.board = board;
	this.resp = resp;
	var len = parseInt(req.headers['content-length'], 10);
	if (len > 0 && len > config.IMAGE_FILESIZE_MAX + (20*1024))
		return this.failure('File is too large.');

	var form = new formidable.IncomingForm();
	form.uploadDir = config.MEDIA_DIRS.tmp;
	form.maxFieldsSize = 50 * 1024;
	form.hash = 'md5';
	form.onPart = function (part) {
		if (part.filename && part.name == 'image')
			form.handlePart(part);
		else if (!part.filename && validFields.indexOf(part.name) >= 0)
			form.handlePart(part);
	};
	var self = this;
	form.once('error', function (err) {
		winston.error('formidable: ' + err);
		self.failure('Upload request problem.');
	});
	form.once('aborted', function (err) {
		self.failure('Upload was aborted.');
	});
	this.lastProgress = -1;
{'fraction_non_alphanumeric': 0.18341013824884791, 'fraction_numerical': 0.009216589861751152, 'mean_word_length': 4.07577174929841, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '4841450', 'n_tokens_mistral': 2067, 'n_tokens_neox': 1974, 'n_words': 450}