<commit_before>class Manga {
	constructor(id, data) {
		this.id = id;

		this.type = data.mangaType;

		this.titles = {
			english: data.titles.en || null,
			romanji: data.titles.en_jp || null,
			canonical: data.canonicalTitle || null,
			abbrivated: data.abbrivatedTitles || [],
		}

		this.synopsis = data.synopsis || null;

		this.startDate = data.startDate || null;
		this.endDate = data.endDate || null;

		this.images = {
			posterImage: data.posterImage || null,
			coverImage: data.coverImage || null,
		}
	}

	get url() {
		return `https://kitsu.io/anime/${this.id}/`
	}
}

module.exports = Manga;<commit_msg>Added rating property to the Manga Class<commit_after>class Manga {
	constructor(id, data) {
		this.id = id;

		this.type = data.mangaType;

		this.titles = {
			english: data.titles.en || null,
			romanji: data.titles.en_jp || null,
			canonical: data.canonicalTitle || null,
			abbrivated: data.abbrivatedTitles || [],
		}

		this.synopsis = data.synopsis || null;

		this.startDate = data.startDate || null;
		this.endDate = data.endDate || null;

		this.rating = parseFloat(data.averageRating) || null;

		this.images = {
			posterImage: data.posterImage || null,
			coverImage: data.coverImage || null,
		}
	}

	get url() {
		return `https://kitsu.io/anime/${this.id}/`
	}
}

module.exports = Manga;<|endoftext|>


<commit_before>/*
    The Conflict of Interest (COI) module of Kuali Research
    Copyright © 2005-2016 Kuali, Inc.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
*/

import { allowedRoles } from '../middleware/role-check';
import wrapAsync from './wrap-async';
import pjson from '../../package.json';

let getAuthorizationInfo;
try {
  const extensions = require('research-extensions').default;
  getAuthorizationInfo = extensions.getAuthorizationInfo;
} catch (e) {
  getAuthorizationInfo = (dbInfo) => { //eslint-disable-line no-unused-vars
    return {
      researchCoreUrl: process.env.RESEARCH_CORE_URL || 'https://uit.kuali.dev/res'
    };
  };
}

export const init = app => {
  app.get('/api/coi/userinfo', allowedRoles('ANY'), wrapAsync(async (req, res) => {
    res.send({
      firstName: req.userInfo.firstName,
      lastName: req.userInfo.lastName,
      coiRole: req.userInfo.coiRole,
      mock: req.userInfo.mock,
      researchCoreUrl: getAuthorizationInfo(req.dbInfo).researchCoreUrl,
      version: pjson.version
    });
  }));
};
<commit_msg>Pull package.json version from environment variable instead of relative path<commit_after>/*
    The Conflict of Interest (COI) module of Kuali Research
    Copyright © 2005-2016 Kuali, Inc.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
*/

import { allowedRoles } from '../middleware/role-check';
import wrapAsync from './wrap-async';

let getAuthorizationInfo;
try {
  const extensions = require('research-extensions').default;
  getAuthorizationInfo = extensions.getAuthorizationInfo;
} catch (e) {
  getAuthorizationInfo = (dbInfo) => { //eslint-disable-line no-unused-vars
    return {
      researchCoreUrl: process.env.RESEARCH_CORE_URL || 'https://uit.kuali.dev/res'
    };
  };
}

export const init = app => {
  app.get('/api/coi/userinfo', allowedRoles('ANY'), wrapAsync(async (req, res) => {
    res.send({
      firstName: req.userInfo.firstName,
      lastName: req.userInfo.lastName,
      coiRole: req.userInfo.coiRole,
      mock: req.userInfo.mock,
      researchCoreUrl: getAuthorizationInfo(req.dbInfo).researchCoreUrl,
      version: process.env.npm_package_version
    });
  }));
};
<|endoftext|>


<commit_before>var React = global.React || require('react'),
    _ = require('underscore'),
    regForms = {},
    mockForm = require('./form'),
    defaultForm = '_DEFAULT_FORM',
    objectAssign = require('./utils').objectAssign,

Mixin = {
    focus: function () {
        // if (!this.refs.focus) {
        //    throw new Error('should have a child component with ref name `input`, most of the case ref is set on ErrorMsgLabel');
        // }
        React.findDOMNode(this.refs.focus) &&
            React.findDOMNode(this.refs.focus).focus();
    },
    getInitialState: function () {
        return {
            _formsyExValue: '',
            _formsyExErrorMsgs: []
        };
    },
    componentWillMount: function () {
        var component = this,
            regToFormName;
        if (component.props.isAttached === true ||
            component.props.name === undefined ) {
            return undefined;
        }
        if (component.props.attachToForm) {
            component.props.attachToForm(component);
        }
        else {
            regToFormName = component.props.regForm ? component.props.regForm : defaultForm;
            if (regForms[regToFormName] === undefined) {
                regForms[regToFormName] = mockForm();
            }
            regForms[regToFormName].register(component);
        }
    },
    componentWillUnmount: function () {
        var component = this;
        if (!component.props.isAttached) {
            return undefined;
        }
        var regToFormName = component.props.regForm ? component.props.regForm : defaultForm;
        regForms[regToFormName] ? (regForms[regToFormName].unregister(component)) : undefined ;
    },
    validate: function (value) {
        var _value = value !== undefined ? value : this.state._formValue,
            validators = this.getValidators(),
            formsyExErrorMsgs,
            isContinue = true,
            res;
        validators &&
            (formsyExErrorMsgs = _.map(validators,
                function (validator) {
                    if (isContinue === false) {
                        return '';
                    }
                    res = validator(_value) || {};
                    if (res.continue === false) {
                        isContinue = false;
                    }
                    if (res.res === false) {
                        return res.msg;
                    }
                    return '';
                }
            ));
        formsyExErrorMsgs = _.filter(
            formsyExErrorMsgs,
            function (formsyExErrorMsg) {
                return true && formsyExErrorMsg;
            }
        )
        this.setState(
            {
                _formValue: _value,
                _formsyExErrorMsgs: formsyExErrorMsgs
            }
        );
        return formsyExErrorMsgs.length === 0;
    },
    getValue: function () {
        return this.state._formValue;
    },
    getErrorMsg: function () {
        return this.state._formsyExErrorMsgs.length === 1 ?
                this.state._formsyExErrorMsgs[0] : this.state._formsyExErrorMsgs;
    },
    getValidators: function () {
        var propertyValidator =  this.validators && this.validators.call ?
            [this.validators] :
            (this.validators || []),
            validators = propertyValidator.concat(this.props.validators || []);
        return validators;
    }
},
FormMixin = objectAssign({}, Mixin, {
    attachToForm: function (component) {
        this.register(component);
    },
    detachFromForm: function (component) {
        this.unregister(component);
    },
    regInputs: function (formName, children) {
         React.Children.forEach(
            children,
            function (child) {
                if (
                    child.props.regForm !== undefined &&
                    child.props.regForm !== formName
                   ) {
                    // components are registered to another form
                    return undefined;
                }

                if (child.props.children) { // DFS to explore child first
                    this.regInputs(formName, child.props.children);
                }

                if (child.props.name === undefined) {
                    return undefined;
                }

                child.props.attachToForm = this.attachToForm;

            }.bind(this)
        );
    },
    componentWillMount: function () {
        var component = this;
        var formName = component.props.name ? component.props.name : defaultForm;
        if (regForms[formName]) {
            component.inputs = regForms[formName].inputs;
            component.childId = regForms[formName].childId;
        }
        regForms[formName] = component;
        this.regInputs(formName, component.props.children);
    },
    componentWillUnmount: function () {
        var component = this;
        // form object
        var formName = component.props.name ? component.props.name : defaultForm;
        delete regForms[formName];
    }
});
module.exports = Mixin;
module.exports.FormMixin = FormMixin;
<commit_msg>element detach<commit_after>var React = global.React || require('react'),
    _ = require('underscore'),
    regForms = {},
    mockForm = require('./form'),
    defaultForm = '_DEFAULT_FORM',
    objectAssign = require('./utils').objectAssign,

Mixin = {
    focus: function () {
        React.findDOMNode(this.refs.focus) &&
            React.findDOMNode(this.refs.focus).focus();
    },
    getInitialState: function () {
        return {
            _formsyExValue: '',
            _formsyExErrorMsgs: []
        };
    },
    componentWillMount: function () {
        var component = this,
            regToFormName;
        if (component.props.isAttached === true ||
            component.props.name === undefined ) {
            return undefined;
        }
        if (component.props.attachToForm) {
            component.props.attachToForm(component);
        } else {
            regToFormName = component.props.regForm ? component.props.regForm : defaultForm;
            if (regForms[regToFormName] === undefined) {
                regForms[regToFormName] = mockForm();
            }
            regForms[regToFormName].register(component);
        }
    },
    componentWillUnmount: function () {
        var component = this;
        if (component.props.isAttached === false) {
            return undefined;
        }
        if (component.props.detachFromForm) {
            component.props.detachFromForm(component);
        } else {
            var regToFormName = component.props.regForm ? component.props.regForm : defaultForm;
            regForms[regToFormName] ? (regForms[regToFormName].unregister(component)) : undefined ;

        }
    },
    validate: function (value) {
        var _value = value !== undefined ? value : this.state._formValue,
            validators = this.getValidators(),
            formsyExErrorMsgs,
            isContinue = true,
            res;
        validators &&
            (formsyExErrorMsgs = _.map(validators,
                function (validator) {
                    if (isContinue === false) {
                        return '';
                    }
                    res = validator(_value) || {};
                    if (res.continue === false) {
                        isContinue = false;
                    }
                    if (res.res === false) {
                        return res.msg;
                    }
                    return '';
                }
            ));
        formsyExErrorMsgs = _.filter(
            formsyExErrorMsgs,
            function (formsyExErrorMsg) {
                return true && formsyExErrorMsg;
            }
        )
        this.setState(
            {
                _formValue: _value,
                _formsyExErrorMsgs: formsyExErrorMsgs
            }
        );
        return formsyExErrorMsgs.length === 0;
    },
    getValue: function () {
        return this.state._formValue;
    },
    getErrorMsg: function () {
        return this.state._formsyExErrorMsgs.length === 1 ?
                this.state._formsyExErrorMsgs[0] : this.state._formsyExErrorMsgs;
    },
    getValidators: function () {
        var propertyValidator =  this.validators && this.validators.call ?
            [this.validators] :
            (this.validators || []),
            validators = propertyValidator.concat(this.props.validators || []);
        return validators;
    }
},
FormMixin = objectAssign({}, Mixin, {
    attachToForm: function (component) {
        this.register(component);
    },
    detachFromForm: function (component) {
        this.unregister(component);
    },
    regInputs: function (formName, children) {
         React.Children.forEach(
            children,
            function (child) {
                if (
                    child.props.regForm !== undefined &&
                    child.props.regForm !== formName
                   ) {
                    // components are registered to another form
                    return undefined;
                }

                if (child.props.children) { // DFS to explore child first
                    this.regInputs(formName, child.props.children);
                }

                if (child.props.name === undefined) {
                    return undefined;
                }

                child.props.attachToForm = this.attachToForm;
                child.props.detachFromForm = this.detachFromForm;

            }.bind(this)
        );
    },
    componentWillMount: function () {
        var component = this;
        var formName = component.props.name ? component.props.name : defaultForm;
        if (regForms[formName]) {
            component.inputs = regForms[formName].inputs;
            component.childId = regForms[formName].childId;
        }
        regForms[formName] = component;
        this.regInputs(formName, component.props.children);
    },
    componentWillUnmount: function () {
        var component = this;
        // form object
        var formName = component.props.name ? component.props.name : defaultForm;
        delete regForms[formName];
    }
});
module.exports = Mixin;
module.exports.FormMixin = FormMixin;
<|endoftext|>


<commit_before>'use strict';

const MongoClient = require('mongodb').MongoClient;
process.env.TZ = 'Europe/London';
const fs = require('fs');
const readline = require('readline');

const aggregate = function(newObject, oldObject, property) {
    if (newObject[property] === undefined) {
        newObject[property] = {};
        newObject[property][oldObject] = 1;
    } else if (newObject[property][oldObject] === undefined) {
        newObject[property][oldObject] = 1;
    } else {
        newObject[property][oldObject]++;
    }
};

const sortSanitizeAndFilter = (obj, limit) => {
    let arr = [];
    for (let row in obj) {
        arr.push({ k: row, v: obj[row] });
    }
    arr.sort((a, b) => { return b.v - a.v; });
    arr = arr.slice(0, limit);
    let nobj = {};
    for (let idx of arr) {
        let clean = idx.k;
        clean = clean.split(".").join("-");
        clean = clean.split("$").join("-");
        if (idx.v >= 0) {
            nobj[clean] = idx.v;
        }
    }
    return nobj;
};

const arrange = (inserts, article, database, cps, bulk, log, cb) => {

    if (cps === undefined) {
        cps = database.collection('cps');
        bulk = cps.initializeUnorderedBulkOp();
    }

    if (article % 1000 === 0 && article !== 0) {
        log.a("Shipping 1000 rows", 1, 'MongoBulk');
        bulk.execute().then(r => {
            bulk = cps.initializeUnorderedBulkOp();
            log.a({ stage: 'continue', index: article, nInserted: r.nInserted, nMatched: r.nMatched, nModified: r.nModified, nUpserted: r.nUpserted }, 1, 'MongoBulk');
            execute(inserts, article, database, cps, bulk, log, cb);
        }, e => {
            console.log(e);
            log.a('Error With Mongo Bulk', 3, 'MongoBulk');
            execute(inserts, article, database, cps, bulk, log, cb);
        });

    } else if (article < inserts.length) {
        //more articles
        execute(inserts, article, database, cps, bulk, log, cb);
    } else {
        bulk.execute().then(r => {
            log.a({ stage: 'end', index: article, nInserted: r.nInserted, nMatched: r.nMatched, nModified: r.nModified, nUpserted: r.nUpserted }, 1, 'MongoBulk');
            cb();
        }, e => {
            console.log(e);
            log.a('Error With Mongo Bulk', 3, 'MongoBulk');
            cb();
        });
    }
};

const execute = (inserts, article, database, cps, bulk, log, cb) => {
    bulk.find({ _id: inserts[article].id }).upsert().update({ $push: { analytics: inserts[article].pushed } });
    article++;
    arrange(inserts, article, database, cps, bulk, log, cb);
};

module.exports = class Mongo {
    constructor(app) {
        let self = this;
        self.app = app;
        self.environment = app.environment;
        if (self.app.config.output.mongo && self.app.config.output.mongo.database) {
            self.databasename = self.app.config.output.mongo.database;
            self.region = self.app.config.output.mongo.region;
        } else {
            throw new Error('No Mongo config found');
        }
    }

    getServers(cb) {
        let self = this;

        self.app.c.a("Get Enviroment Servers", 1, 'Mongo');

        if (self.environment === 'sandbox') {
            self.connection = 'mongodb://' + self.local + '/' + self.databasename;
            self.app.c.a("Servers: " + self.connection, 1, 'Mongo');
            cb(null);

        } else if (self.environment === 'live') {

            let filter = { "Filters": [{ "Name": "tag:Name", "Values": ["live-mongos"] }, { "Name": "instance-state-name", "Values": ["running"] }, { "Name": "tag:BBCEnvironment", "Values": ['live'] }] };
            const AWS = require('aws-sdk');
            const ec2 = new AWS.EC2({ "region": self.region });
            ec2.describeInstances(filter, function(err, res) {
                if (err) {
                    cb(err);
                } else if (res.Reservations.length > 0) {
                    self.connection = 'mongodb://';
                    res.Reservations.forEach(function(x, idx) {
                        self.connection += x.Instances[0].PrivateIpAddress + ':27017';
                        if (idx < res.Reservations.length - 1) { self.connection += ','; }
                    });

                    self.connection += '/' + self.databasename;
                    self.app.c.a("Servers: " + self.connection, 1, 'Mongo');
                    cb(null);
                } else {
                    cb(new Error("no mongo instances found"));
                }
            });
        } else {
            let filter = { "Filters": [{ "Name": "tag:Name", "Values": ["live-mongos"] }, { "Name": "instance-state-name", "Values": ["running"] }, { "Name": "tag:BBCEnvironment", "Values": ['live'] }] };
            const AWS = require('aws-sdk');
            const ec2 = new AWS.EC2({ "region": self.region });
            ec2.describeInstances(filter, function(err, res) {
                if (err) {
                    cb(err);
                } else if (res.Reservations.length > 0) {
                    self.connection = 'mongodb://';
                    res.Reservations.forEach(function(x, idx) {
                        self.connection += x.Instances[0].PrivateIpAddress + ':27017';
                        if (idx < res.Reservations.length - 1) { self.connection += ','; }
                    });

                    self.connection += '/' + self.databasename;
                    self.app.c.a("Servers: " + self.connection, 1, 'Mongo');
                    cb(null);
                } else {
                    cb(new Error("no mongo instances found"));
                }
            });
        }
    }

    connect(cb) {
        //enviroment modules here to specify which mongo to connect to
        let self = this;
        MongoClient.connect(self.connection, function(err, db) {
            if (err) cb(err);
            self.app.c.a("Connected to MongoDB");
            self.database = db;
            cb(null);
        });
    }

    disconnect() {
        this.database.close();
        this.app.c.a("Disconneted from MongoDB");
    }

    create() {
        let self = this;
        self.file = {};
    }

    ship(cb) {

        let self = this;
        self.app.c.a("Agregating Documents for Mongo");

        let reader = fs.createReadStream('mongo.tmp');
        let stream = readline.createInterface({
            input: reader,
            terminal: false
        });

        let articles = {};

        stream.on('line', (line) => {
            line = JSON.parse(line);

            //remove pages we don't care for... index, business
            if (line.bbc_page === "Index") {
                return;
            }

            //remove any documents we feel are not storys - hack until we fix poundlock
            line.bbc_cps = parseInt(line.bbc_cps);
            if (line.bbc_cps < 10000000 || line.bbc_cps > 99999999) {
                return;
            }

            //bucket datetime into 15 min blocks
            line.date = new Date(line.date);
            let mins = line.date.getMinutes();
            if (mins >= 0 && mins < 15) {
                line.date.setMinutes(0, 0, 0);
            } else if (mins >= 15 && mins < 30) {
                line.date.setMinutes(15, 0, 0);
            } else if (mins >= 30 && mins < 45) {
                line.date.setMinutes(30, 0, 0);
            } else {
                line.date.setMinutes(45, 0, 0);
            }

            //create time string
            let time = line.date.toISOString();

            //views
            if (articles[line.bbc_cps] === undefined) {
                articles[line.bbc_cps] = {};
                articles[line.bbc_cps][time] = {};
                articles[line.bbc_cps][time].views = 1;
                articles[line.bbc_cps][time].date = line.date;
            } else if (articles[line.bbc_cps][time] === undefined) {
                articles[line.bbc_cps][time] = {};
                articles[line.bbc_cps][time].views = 1;
                articles[line.bbc_cps][time].date = line.date;
            } else {
                articles[line.bbc_cps][time].views++;
            }

            let doc = articles[line.bbc_cps][time];

            if (line.loc_cont) { aggregate(doc, line.loc_cont, 'continent'); }

            if (line.loc_country) { aggregate(doc, line.loc_country, 'country'); }

            if (line.loc_city !== undefined && line.loc_country === 'GB') { aggregate(doc, line.loc_city, 'city'); }

            if (line.device_browser) { aggregate(doc, line.device_browser, 'browser'); }

            if (line.device_platform) { aggregate(doc, line.device_platform, 'platform'); }

            if (line.device_family) { aggregate(doc, line.device_family, 'device'); }

            if (line.device_os) { aggregate(doc, line.device_os, 'os'); }

            if (line.ref_type) { aggregate(doc, line.ref_type, 'entry'); }

            if (line.bbc_app) { aggregate(doc, line.bbc_app, 'app'); }

            switch (line.ref_type) {
                case 'bbc':
                    {
                        if (line.ref_name !== 'homepage') {
                            aggregate(doc, line.ref_page, 'bbc_' + line.ref_name.slice(0, 10).split(".").join("-").split("$").join("-"));
                        }

                        break;
                    }
                case 'search':
                    {
                        aggregate(doc, line.ref_name, 'ref_search');
                        if (line.ref_term) {
                            aggregate(doc, line.ref_term, 'ref_term');
                        }
                        break;
                    }
                case 'social':
                    {
                        aggregate(doc, line.ref_name, 'ref_social');
                        break;
                    }
                case 'news':
                    {
                        aggregate(doc, line.ref_name, 'ref_news');
                        break;
                    }
                case 'email':
                    {
                        aggregate(doc, line.ref_name, 'ref_email');
                        break;
                    }
                default:
                    {
                        if (line.ref_host) {
                            aggregate(doc, line.ref_host, 'ref_unknown');
                        }
                    }
            }
        });

        stream.on('close', () => {

            self.app.c.a("Completed Agregating Documents for Mongo");

            let documents = [];

            for (let id in articles) {
                for (let time in articles[id]) {
                    for (let property in articles[id][time]) {
                        if (property !== 'views' && property !== 'date') {
                            articles[id][time][property] = sortSanitizeAndFilter(articles[id][time][property], 10);
                        }
                    }
                }
            }

            //if the array already exists we want to inc the values,
            //if it doesn't exist, we need to create it by pushing the values to the array.

            let pusher = [];
            for (let id in articles) {
                for (let time in articles[id]) {
                    let incs = {};
                    let pushed;
                    properties:
                        //prepare the increments
                        for (let prop in articles[id][time]) {
                            if (prop === 'date') {
                                continue properties;
                            }
                            //check if it is deep
                            if (typeof articles[id][time][prop] === 'object') {
                                for (let subprop in articles[id][time][prop]) {
                                    incs["analytics.$." + prop + "." + subprop] = articles[id][time][prop][subprop];
                                }
                            } else {
                                incs["analytics.$." + prop] = articles[id][time][prop];
                            }
                        }
                        //prepare the push
                    pusher.push({ id: parseInt(id), date: articles[id][time].date, inc: incs, pushed: articles[id][time] });
                }
            }

            for (let i = 0; i < pusher.length; i++) {
                fs.appendFileSync('mongotest.tmp', JSON.stringify(pusher[i]) + '\n');
            }

            self.app.c.a("Shipping " + pusher.length + " Documents to Mongo");
            arrange(pusher, 0, self.database, undefined, undefined, self.app.c, cb);
        });
    }
};<commit_msg>allow local<commit_after>'use strict';

const MongoClient = require('mongodb').MongoClient;
process.env.TZ = 'Europe/London';
const fs = require('fs');
const readline = require('readline');

const aggregate = function(newObject, oldObject, property) {
    if (newObject[property] === undefined) {
        newObject[property] = {};
        newObject[property][oldObject] = 1;
    } else if (newObject[property][oldObject] === undefined) {
        newObject[property][oldObject] = 1;
    } else {
        newObject[property][oldObject]++;
    }
};

const sortSanitizeAndFilter = (obj, limit) => {
    let arr = [];
    for (let row in obj) {
        arr.push({ k: row, v: obj[row] });
    }
    arr.sort((a, b) => { return b.v - a.v; });
    arr = arr.slice(0, limit);
    let nobj = {};
    for (let idx of arr) {
        let clean = idx.k;
        clean = clean.split(".").join("-");
        clean = clean.split("$").join("-");
        if (idx.v >= 0) {
            nobj[clean] = idx.v;
        }
    }
    return nobj;
};

const arrange = (inserts, article, database, cps, bulk, log, cb) => {

    if (cps === undefined) {
        cps = database.collection('cps');
        bulk = cps.initializeUnorderedBulkOp();
    }

    if (article % 1000 === 0 && article !== 0) {
        log.a("Shipping 1000 rows", 1, 'MongoBulk');
        bulk.execute().then(r => {
            bulk = cps.initializeUnorderedBulkOp();
            log.a({ stage: 'continue', index: article, nInserted: r.nInserted, nMatched: r.nMatched, nModified: r.nModified, nUpserted: r.nUpserted }, 1, 'MongoBulk');
            execute(inserts, article, database, cps, bulk, log, cb);
        }, e => {
            console.log(e);
            log.a('Error With Mongo Bulk', 3, 'MongoBulk');
            execute(inserts, article, database, cps, bulk, log, cb);
        });

    } else if (article < inserts.length) {
        //more articles
        execute(inserts, article, database, cps, bulk, log, cb);
    } else {
        bulk.execute().then(r => {
            log.a({ stage: 'end', index: article, nInserted: r.nInserted, nMatched: r.nMatched, nModified: r.nModified, nUpserted: r.nUpserted }, 1, 'MongoBulk');
            cb();
        }, e => {
            console.log(e);
            log.a('Error With Mongo Bulk', 3, 'MongoBulk');
            cb();
        });
    }
};

const execute = (inserts, article, database, cps, bulk, log, cb) => {
    bulk.find({ _id: inserts[article].id }).upsert().update({ $push: { analytics: inserts[article].pushed } });
    article++;
    arrange(inserts, article, database, cps, bulk, log, cb);
};

module.exports = class Mongo {
    constructor(app) {
        let self = this;
        self.app = app;
        self.environment = app.environment;
        if (self.app.config.output.mongo && self.app.config.output.mongo.database) {
            self.databasename = self.app.config.output.mongo.database;
            self.local = self.app.config.output.mongo.local;
            self.region = self.app.config.output.mongo.region;
        } else {
            throw new Error('No Mongo config found');
        }
    }

    getServers(cb) {
        let self = this;

        self.app.c.a("Get Enviroment Servers", 1, 'Mongo');

        if (self.environment === 'sandbox') {
            self.connection = 'mongodb://' + self.local + '/' + self.databasename;
            self.app.c.a("Servers: " + self.connection, 1, 'Mongo');
            cb(null);

        } else if (self.environment === 'live') {

            let filter = { "Filters": [{ "Name": "tag:Name", "Values": ["live-mongos"] }, { "Name": "instance-state-name", "Values": ["running"] }, { "Name": "tag:BBCEnvironment", "Values": ['live'] }] };
            const AWS = require('aws-sdk');
            const ec2 = new AWS.EC2({ "region": self.region });
            ec2.describeInstances(filter, function(err, res) {
                if (err) {
                    cb(err);
                } else if (res.Reservations.length > 0) {
                    self.connection = 'mongodb://';
                    res.Reservations.forEach(function(x, idx) {
                        self.connection += x.Instances[0].PrivateIpAddress + ':27017';
                        if (idx < res.Reservations.length - 1) { self.connection += ','; }
                    });

                    self.connection += '/' + self.databasename;
                    self.app.c.a("Servers: " + self.connection, 1, 'Mongo');
                    cb(null);
                } else {
                    cb(new Error("no mongo instances found"));
                }
            });
        } else {
            let filter = { "Filters": [{ "Name": "tag:Name", "Values": ["live-mongos"] }, { "Name": "instance-state-name", "Values": ["running"] }, { "Name": "tag:BBCEnvironment", "Values": ['live'] }] };
            const AWS = require('aws-sdk');
            const ec2 = new AWS.EC2({ "region": self.region });
            ec2.describeInstances(filter, function(err, res) {
                if (err) {
                    cb(err);
                } else if (res.Reservations.length > 0) {
                    self.connection = 'mongodb://';
                    res.Reservations.forEach(function(x, idx) {
                        self.connection += x.Instances[0].PrivateIpAddress + ':27017';
                        if (idx < res.Reservations.length - 1) { self.connection += ','; }
                    });

                    self.connection += '/' + self.databasename;
                    self.app.c.a("Servers: " + self.connection, 1, 'Mongo');
                    cb(null);
                } else {
                    cb(new Error("no mongo instances found"));
                }
            });
        }
    }

    connect(cb) {
        //enviroment modules here to specify which mongo to connect to
        let self = this;
        MongoClient.connect(self.connection, function(err, db) {
            if (err) cb(err);
            self.app.c.a("Connected to MongoDB");
            self.database = db;
            cb(null);
        });
    }

    disconnect() {
        this.database.close();
        this.app.c.a("Disconneted from MongoDB");
    }

    create() {
        let self = this;
        self.file = {};
    }

    ship(cb) {

        let self = this;
        self.app.c.a("Agregating Documents for Mongo");

        let reader = fs.createReadStream('mongo.tmp');
        let stream = readline.createInterface({
            input: reader,
            terminal: false
        });

        let articles = {};

        stream.on('line', (line) => {
            line = JSON.parse(line);

            //remove pages we don't care for... index, business
            if (line.bbc_page === "Index") {
                return;
            }

            //remove any documents we feel are not storys - hack until we fix poundlock
            line.bbc_cps = parseInt(line.bbc_cps);
            if (line.bbc_cps < 10000000 || line.bbc_cps > 99999999) {
                return;
            }

            //bucket datetime into 15 min blocks
            line.date = new Date(line.date);
            let mins = line.date.getMinutes();
            if (mins >= 0 && mins < 15) {
                line.date.setMinutes(0, 0, 0);
            } else if (mins >= 15 && mins < 30) {
                line.date.setMinutes(15, 0, 0);
            } else if (mins >= 30 && mins < 45) {
                line.date.setMinutes(30, 0, 0);
            } else {
                line.date.setMinutes(45, 0, 0);
            }

            //create time string
            let time = line.date.toISOString();

            //views
            if (articles[line.bbc_cps] === undefined) {
                articles[line.bbc_cps] = {};
                articles[line.bbc_cps][time] = {};
                articles[line.bbc_cps][time].views = 1;
                articles[line.bbc_cps][time].date = line.date;
            } else if (articles[line.bbc_cps][time] === undefined) {
                articles[line.bbc_cps][time] = {};
                articles[line.bbc_cps][time].views = 1;
                articles[line.bbc_cps][time].date = line.date;
            } else {
                articles[line.bbc_cps][time].views++;
            }

            let doc = articles[line.bbc_cps][time];

            if (line.loc_cont) { aggregate(doc, line.loc_cont, 'continent'); }

            if (line.loc_country) { aggregate(doc, line.loc_country, 'country'); }

            if (line.loc_city !== undefined && line.loc_country === 'GB') { aggregate(doc, line.loc_city, 'city'); }

            if (line.device_browser) { aggregate(doc, line.device_browser, 'browser'); }

            if (line.device_platform) { aggregate(doc, line.device_platform, 'platform'); }

            if (line.device_family) { aggregate(doc, line.device_family, 'device'); }

            if (line.device_os) { aggregate(doc, line.device_os, 'os'); }

            if (line.ref_type) { aggregate(doc, line.ref_type, 'entry'); }

            if (line.bbc_app) { aggregate(doc, line.bbc_app, 'app'); }

            switch (line.ref_type) {
                case 'bbc':
                    {
                        if (line.ref_name !== 'homepage') {
                            aggregate(doc, line.ref_page, 'bbc_' + line.ref_name.slice(0, 10).split(".").join("-").split("$").join("-"));
                        }

                        break;
                    }
                case 'search':
                    {
                        aggregate(doc, line.ref_name, 'ref_search');
                        if (line.ref_term) {
                            aggregate(doc, line.ref_term, 'ref_term');
                        }
                        break;
                    }
                case 'social':
                    {
                        aggregate(doc, line.ref_name, 'ref_social');
                        break;
                    }
                case 'news':
                    {
                        aggregate(doc, line.ref_name, 'ref_news');
                        break;
                    }
                case 'email':
                    {
                        aggregate(doc, line.ref_name, 'ref_email');
                        break;
                    }
                default:
                    {
                        if (line.ref_host) {
                            aggregate(doc, line.ref_host, 'ref_unknown');
                        }
                    }
            }
        });

        stream.on('close', () => {

            self.app.c.a("Completed Agregating Documents for Mongo");

            let documents = [];

            for (let id in articles) {
                for (let time in articles[id]) {
                    for (let property in articles[id][time]) {
                        if (property !== 'views' && property !== 'date') {
                            articles[id][time][property] = sortSanitizeAndFilter(articles[id][time][property], 10);
                        }
                    }
                }
            }

            //if the array already exists we want to inc the values,
            //if it doesn't exist, we need to create it by pushing the values to the array.

            let pusher = [];
            for (let id in articles) {
                for (let time in articles[id]) {
                    let incs = {};
                    let pushed;
                    properties:
                        //prepare the increments
                        for (let prop in articles[id][time]) {
                            if (prop === 'date') {
                                continue properties;
                            }
                            //check if it is deep
                            if (typeof articles[id][time][prop] === 'object') {
                                for (let subprop in articles[id][time][prop]) {
                                    incs["analytics.$." + prop + "." + subprop] = articles[id][time][prop][subprop];
                                }
                            } else {
                                incs["analytics.$." + prop] = articles[id][time][prop];
                            }
                        }
                        //prepare the push
                    pusher.push({ id: parseInt(id), date: articles[id][time].date, inc: incs, pushed: articles[id][time] });
                }
            }

            for (let i = 0; i < pusher.length; i++) {
                fs.appendFileSync('mongotest.tmp', JSON.stringify(pusher[i]) + '\n');
            }

            self.app.c.a("Shipping " + pusher.length + " Documents to Mongo");
            arrange(pusher, 0, self.database, undefined, undefined, self.app.c, cb);
        });
    }
};<|endoftext|>


<commit_before>'use strict';

const QueueEmptyError = require('./errors/QueueEmptyError');

class Queue {

  /*
  * Class constructor with the required `key` parameter withc represents the
  * name of the list used by this schedule.
  */

  constructor(redis, key) {
    this.redis = redis;
    this.key = key;
    this.running = false;
    this.timeout = null;
  }

  /*
  * Starts the heartbit of the schedule.
  *
  * ```
  * let schedule = new Schedule('main');
  * schedule.start();
  * ```
  */

  start() {
    if (this.running) return;

    this.running = true;
    this.tick();
  }

  /*
  * Private method which is called on every heartbit of the schedule.
  */

  tick() {
    if (!this.running) return;

    return this.redis.rpoplpush(this.key, `${this.key}:processing`).then(value => {
      if (!value) throw new QueueEmptyError();

      let data = this.decodeValue(value);
      return require.main.require(data.path).apply(this, data.args).then(res => value);
    }).then(value => {
      return this.redis.lrem(`${this.key}:processing`, '-0', value);
    }).then(this.tick.bind(this)).catch(this.handleError.bind(this));
  }

  /*
  * Stops the heartbit of the schedule.
  *
  * ```
  * let schedule = new Schedule('main');
  * schedule.stop();
  * ```
  */

  stop() {
    clearTimeout(this.timeout);
    this.running = false;
  }

  /*
  * Private method which handles class errors.
  */

  handleError(err) {
    if (!(err instanceof QueueEmptyError)) console.log(err);

    clearTimeout(this.timeout);
    this.timeout = setTimeout(this.tick.bind(this), 1000);
  }

  /*
  * Returns serialized value which can be stored in redis.
  */

  encodeValue(data) {
    let path = data.path;
    let args = data.args;
    return JSON.stringify({path, args});
  }

  /*
  * Returns unserialized value.
  */

  decodeValue(value) {
    return JSON.parse(value);
  }

  /*
  * Places a new job on the processing list.
  *
  * ```
  * let queue = new Queue('main');
  * queue.enqueue({path: './jobs/MyJob', args: [100]});
  * ```
  */

  enqueue(data) {
    let path = data.path;
    let args = data.args;
    let value = this.encodeValue({path, args});
    return this.redis.lpush(this.key, value);
  }

  /*
  * Removes a job from the processing list. Not that if a job is enqueued
  * multiple times then multiple values will be deleted.
  *
  * ```
  * let queue = new Queue('main');
  * queue.enqueue({path: './jobs/MyJob', args: [100]});
  * ```
  */

  dequeue(data) {
    let path = data.path;
    let args = data.args;
    let value = this.encodeValue({path, args});
    return this.redis.lrem(this.key, '-0', value);
  }
}

module.exports = Queue;
<commit_msg>pass array of paths option where Queue should look for jobs<commit_after>'use strict';

const path = require('path');
const QueueEmptyError = require('./errors/QueueEmptyError');

class Queue {

  /*
  * Class constructor with the required `key` parameter withc represents the
  * name of the list used by this schedule.
  */

  constructor(redis, key, options) {
    this.redis = redis;
    this.key = key;
    this.running = false;
    this.timeout = null;
    this.paths = [process.cwd()];

    if (options && Array.isArray(options.paths)) this.paths = this.paths.concat(options.paths);
  }

  /*
  * Starts the heartbit of the schedule.
  *
  * ```
  * let schedule = new Schedule('main');
  * schedule.start();
  * ```
  */

  start() {
    if (this.running) return;

    this.running = true;
    this.tick();
  }

  /*
  * Private method which is called on every heartbit of the schedule.
  */

  tick() {
    if (!this.running) return;

    return this.redis.rpoplpush(this.key, `${this.key}:processing`).then(value => {
      if (!value) throw new QueueEmptyError();

      let data = this.decodeValue(value);
      let locs = this.paths.concat([data.path]);
      let dest = path.resolve.apply(null, locs);
      return require.main.require(dest).apply(this, data.args).then(res => value);
    }).then(value => {
      return this.redis.lrem(`${this.key}:processing`, '-0', value);
    }).then(this.tick.bind(this)).catch(this.handleError.bind(this));
  }

  /*
  * Stops the heartbit of the schedule.
  *
  * ```
  * let schedule = new Schedule('main');
  * schedule.stop();
  * ```
  */

  stop() {
    clearTimeout(this.timeout);
    this.running = false;
  }

  /*
  * Private method which handles class errors.
  */

  handleError(err) {
    if (!(err instanceof QueueEmptyError)) console.log(err);

    clearTimeout(this.timeout);
    this.timeout = setTimeout(this.tick.bind(this), 1000);
  }

  /*
  * Returns serialized value which can be stored in redis.
  */

  encodeValue(data) {
    let path = data.path;
    let args = data.args;
    return JSON.stringify({path, args});
  }

  /*
  * Returns unserialized value.
  */

  decodeValue(value) {
    return JSON.parse(value);
  }

  /*
  * Places a new job on the processing list.
  *
  * ```
  * let queue = new Queue('main');
  * queue.enqueue({path: './jobs/MyJob', args: [100]});
  * ```
  */

  enqueue(data) {
    let path = data.path;
    let args = data.args;
    let value = this.encodeValue({path, args});
    return this.redis.lpush(this.key, value);
  }

  /*
  * Removes a job from the processing list. Not that if a job is enqueued
  * multiple times then multiple values will be deleted.
  *
  * ```
  * let queue = new Queue('main');
  * queue.enqueue({path: './jobs/MyJob', args: [100]});
  * ```
  */

  dequeue(data) {
    let path = data.path;
    let args = data.args;
    let value = this.encodeValue({path, args});
    return this.redis.lrem(this.key, '-0', value);
  }
}

module.exports = Queue;
<|endoftext|>


<commit_before>import React, { PropTypes, findDOMNode } from 'react';
import LogMonitorEntry from './LogMonitorEntry';
import LogMonitorButton from './LogMonitorButton';
import * as themes from './themes';

const styles = {
  container: {
    fontFamily: 'monospace',
    position: 'relative',
    overflowY: 'hidden',
    width: '100%',
    height: '100%'
  },
  buttonBar: {
    height: 26,
    marginBottom: 1,
    paddingLeft: 2
  },
  elements: {
    position: 'absolute',
    left: 0,
    right: 0,
    top: 27,
    bottom: 0,
    overflowX: 'hidden',
    overflowY: 'auto'
  }
};

export default class LogMonitor {
  constructor() {
    window.addEventListener('keydown', ::this.handleKeyPress);
  }

  static propTypes = {
    computedStates: PropTypes.array.isRequired,
    currentStateIndex: PropTypes.number.isRequired,
    monitorState: PropTypes.object.isRequired,
    stagedActions: PropTypes.array.isRequired,
    skippedActions: PropTypes.object.isRequired,
    reset: PropTypes.func.isRequired,
    commit: PropTypes.func.isRequired,
    rollback: PropTypes.func.isRequired,
    sweep: PropTypes.func.isRequired,
    toggleAction: PropTypes.func.isRequired,
    jumpToState: PropTypes.func.isRequired,
    setMonitorState: PropTypes.func.isRequired,
    select: PropTypes.func.isRequired
  };

  static defaultProps = {
    select: (state) => state,
    monitorState: { isVisible: true },
    theme: 'ocean'
  };

  componentWillReceiveProps(nextProps) {
    const node = findDOMNode(this.refs.elements);
    if (!node) {
      this.scrollDown = true;
    } else if (
      this.props.stagedActions.length < nextProps.stagedActions.length
    ) {
      const scrollableNode = node.parentElement;
      const { scrollTop, offsetHeight, scrollHeight } = scrollableNode;

      this.scrollDown = Math.abs(
        scrollHeight - (scrollTop + offsetHeight)
      ) < 20;
    } else {
      this.scrollDown = false;
    }
  }

  componentDidUpdate() {
    const node = findDOMNode(this.refs.elements);
    if (!node) {
      return;
    }

    if (this.scrollDown) {
      const scrollableNode = node.parentElement;
      const { offsetHeight, scrollHeight } = scrollableNode;
      scrollableNode.scrollTop = scrollHeight - offsetHeight;
      this.scrollDown = false;
    }
  }

  handleRollback() {
    this.props.rollback();
  }

  handleSweep() {
    this.props.sweep();
  }

  handleCommit() {
    this.props.commit();
  }

  handleToggleAction(index) {
    this.props.toggleAction(index);
  }

  handleReset() {
    this.props.reset();
  }

  handleKeyPress(event) {
    const { monitorState } = this.props;

    if (event.ctrlKey && event.keyCode === 72) { // Ctrl+H
      event.preventDefault();
      this.props.setMonitorState({
        ...monitorState,
        isVisible: !monitorState.isVisible
      });
    }
  }

  render() {
    const elements = [];
    const { monitorState, skippedActions, stagedActions, computedStates, select } = this.props;
    let theme;
    if (typeof this.props.theme === 'string') {
      if (typeof themes[this.props.theme] !== 'undefined') {
        theme = themes[this.props.theme];
      } else {
        console.warn('DevTools theme ' + this.props.theme + ' not found, defaulting to ocean');
        theme = themes.ocean;
      }
    } else {
      theme = this.props.theme;
    }
    if (!monitorState.isVisible) {
      return null;
    }

    for (let i = 0; i < stagedActions.length; i++) {
      const action = stagedActions[i];
      const { state, error } = computedStates[i];

      elements.push(
        <LogMonitorEntry key={i}
                         index={i}
                         theme={theme}
                         select={select}
                         action={action}
                         state={state}
                         collapsed={skippedActions[i]}
                         error={error}
                         onActionClick={::this.handleToggleAction} />
      );
    }

    return (

          <LogMonitorButton theme={theme} onClick={::this.handleReset}>Reset</LogMonitorButton>
          {computedStates.length > 1 &&
            <LogMonitorButton theme={theme} onClick={::this.handleRollback}>Revert</LogMonitorButton>
          }
          {Object.keys(skippedActions).some(key => skippedActions[key]) &&
            <LogMonitorButton theme={theme} onClick={::this.handleSweep}>Sweep</LogMonitorButton>
          }
          {computedStates.length > 1 &&
            <LogMonitorButton theme={theme} onClick={::this.handleCommit}>Commit</LogMonitorButton>
          }

          {elements}

    );
  }
}
<commit_msg>restored scroll functionality to default<commit_after>import React, { PropTypes, findDOMNode } from 'react';
import LogMonitorEntry from './LogMonitorEntry';
import LogMonitorButton from './LogMonitorButton';
import * as themes from './themes';

const styles = {
  container: {
    fontFamily: 'monospace',
    position: 'relative',
    overflowY: 'hidden',
    width: '100%',
    height: '100%'
  },
  buttonBar: {
    height: 26,
    marginBottom: 1,
    paddingLeft: 2
  },
  elements: {
    position: 'absolute',
    left: 0,
    right: 0,
    top: 27,
    bottom: 0,
    overflowX: 'hidden',
    overflowY: 'auto'
  }
};

export default class LogMonitor {
  constructor() {
    window.addEventListener('keydown', ::this.handleKeyPress);
  }

  static propTypes = {
    computedStates: PropTypes.array.isRequired,
    currentStateIndex: PropTypes.number.isRequired,
    monitorState: PropTypes.object.isRequired,
    stagedActions: PropTypes.array.isRequired,
    skippedActions: PropTypes.object.isRequired,
    reset: PropTypes.func.isRequired,
    commit: PropTypes.func.isRequired,
    rollback: PropTypes.func.isRequired,
    sweep: PropTypes.func.isRequired,
    toggleAction: PropTypes.func.isRequired,
    jumpToState: PropTypes.func.isRequired,
    setMonitorState: PropTypes.func.isRequired,
    select: PropTypes.func.isRequired
  };

  static defaultProps = {
    select: (state) => state,
    monitorState: { isVisible: true },
    theme: 'ocean'
  };

  componentWillReceiveProps(nextProps) {
    const node = findDOMNode(this.refs.elements);
    if (!node) {
      this.scrollDown = true;
    } else if (
      this.props.stagedActions.length < nextProps.stagedActions.length
    ) {
      const { scrollTop, offsetHeight, scrollHeight } = node;

      this.scrollDown = Math.abs(
        scrollHeight - (scrollTop + offsetHeight)
      ) < 20;
    } else {
      this.scrollDown = false;
    }
  }

  componentDidUpdate() {
    const node = findDOMNode(this.refs.elements);
    if (!node) {
      return;
    }

    if (this.scrollDown) {
      const { offsetHeight, scrollHeight } = node;
      node.scrollTop = scrollHeight - offsetHeight;
      this.scrollDown = false;
    }
  }

  handleRollback() {
    this.props.rollback();
  }

  handleSweep() {
    this.props.sweep();
  }

  handleCommit() {
    this.props.commit();
  }

  handleToggleAction(index) {
    this.props.toggleAction(index);
  }

  handleReset() {
    this.props.reset();
  }

  handleKeyPress(event) {
    const { monitorState } = this.props;

    if (event.ctrlKey && event.keyCode === 72) { // Ctrl+H
      event.preventDefault();
      this.props.setMonitorState({
        ...monitorState,
        isVisible: !monitorState.isVisible
      });
    }
  }

  render() {
    const elements = [];
    const { monitorState, skippedActions, stagedActions, computedStates, select } = this.props;
    let theme;
    if (typeof this.props.theme === 'string') {
      if (typeof themes[this.props.theme] !== 'undefined') {
        theme = themes[this.props.theme];
      } else {
        console.warn('DevTools theme ' + this.props.theme + ' not found, defaulting to ocean');
        theme = themes.ocean;
      }
    } else {
      theme = this.props.theme;
    }
    if (!monitorState.isVisible) {
      return null;
    }

    for (let i = 0; i < stagedActions.length; i++) {
      const action = stagedActions[i];
      const { state, error } = computedStates[i];

      elements.push(
        <LogMonitorEntry key={i}
                         index={i}
                         theme={theme}
                         select={select}
                         action={action}
                         state={state}
                         collapsed={skippedActions[i]}
                         error={error}
                         onActionClick={::this.handleToggleAction} />
      );
    }

    return (

          <LogMonitorButton theme={theme} onClick={::this.handleReset}>Reset</LogMonitorButton>
          {computedStates.length > 1 &&
            <LogMonitorButton theme={theme} onClick={::this.handleRollback}>Revert</LogMonitorButton>
          }
          {Object.keys(skippedActions).some(key => skippedActions[key]) &&
            <LogMonitorButton theme={theme} onClick={::this.handleSweep}>Sweep</LogMonitorButton>
          }
          {computedStates.length > 1 &&
            <LogMonitorButton theme={theme} onClick={::this.handleCommit}>Commit</LogMonitorButton>
          }

          {elements}

    );
  }
}
<|endoftext|>


<commit_before>'use babel';
'use strict';

import glob from 'glob';
import path from 'path';

import CommandRegistry from './CommandRegistry';

/**
 * Handles loading all commands from the given Bot's commandsDir
 * @class CommandLoader
 * @param {Bot} bot - Bot instance
 */
export default class CommandLoader
{
	constructor(bot)
	{
		/** @type {Bot} */
		this._bot = bot;
	}

	/**
	 * Load or reload all commands from the base commands directory and the
	 * user-specified {@link Bot#commandsDir} directory and stores them in
	 * the Bot's {@link CommandRegistry} instance ({@link Bot#commands})
	 * @memberof CommandLoader
	 * @instance
	 */
	loadCommands()
	{
		if (this._bot.commands.size > 0) this._bot.commands = new CommandRegistry();
		let commandFiles = [];
		commandFiles.push(...glob.sync(`${path.join(__dirname, './base')}/**/*.js`));
		commandFiles.push(...glob.sync(`${this._bot.commandsDir}/**/*.js`));
		let loadedCommands = 0;
		commandFiles.forEach(fileName =>
		{
			let commandLocation = fileName.replace('.js', '');
			delete require.cache[require.resolve(commandLocation)];
			let Command = require(commandLocation).default;
			let command = new Command(this._bot);
			if (this._bot.disableBase.includes(command.name)) return;
			command.classloc = commandLocation;
			if (command.overloads)
			{
				if (!this._bot.commands.has(command.overloads)) // eslint-disable-line curly
					throw new Error(`Command "${command.overloads}" does not exist to be overloaded.`);
				this._bot.commands.delete(command.overloads);
				this._bot.commands.register(command, command.name);
			}
			else
			{
				this._bot.commands.register(command, command.name);
			}
			if (!command.overloads)
			{
				loadedCommands++;
				console.log(`Command '${command.name}' loaded.`); // eslint-disable-line no-console
			}
			else
			{
				console.log(`Command '${command.name}' loaded, overloading command '${command.overloads}'.`); // eslint-disable-line no-console
			}
		});
		console.log(`Loaded ${loadedCommands} total commands in ${this._bot.commands.groups.length} groups.`); // eslint-disable-line no-console
	}

	/**
	 * Reload the given command in the Bot's {@link CommandRegistry} ({@link Bot#commands})
	 * @memberof CommandLoader
	 * @instance
	 * @param {string} nameOrAlias - {@link Command#name} or {@link Command#aliases} alias
	 * @returns {boolean}
	 */
	reloadCommand(nameOrAlias)
	{
		let name = this._bot.commands.findByNameOrAlias(nameOrAlias).name;
		if (!name) return false;
		let commandLocation = this._bot.commands.get(name).classloc;
		delete require.cache[require.resolve(commandLocation)];
		let Command = require(commandLocation).default;
		let command = new Command(this._bot);
		command.classloc = commandLocation;
		this._bot.commands.register(command, command.name, true);
		console.log(`Command '${command.name}' reloaded.`); // eslint-disable-line no-console
		return true;
	}
}
<commit_msg>Clean Up CommandLoader a bit<commit_after>'use babel';
'use strict';

import glob from 'glob';
import path from 'path';

import CommandRegistry from './CommandRegistry';

/**
 * Handles loading all commands from the given Bot's commandsDir
 * @class CommandLoader
 * @param {Bot} bot - Bot instance
 */
export default class CommandLoader
{
	constructor(bot)
	{
		/** @type {Bot} */
		this._bot = bot;
	}

	/**
	 * Load or reload all commands from the base commands directory and the
	 * user-specified {@link Bot#commandsDir} directory and stores them in
	 * the Bot's {@link CommandRegistry} instance ({@link Bot#commands})
	 * @memberof CommandLoader
	 * @instance
	 */
	loadCommands()
	{
		if (this._bot.commands.size > 0) this._bot.commands = new CommandRegistry();
		let commandFiles = [];
		commandFiles.push(...glob.sync(`${path.join(__dirname, './base')}/**/*.js`));
		commandFiles.push(...glob.sync(`${this._bot.commandsDir}/**/*.js`));
		let loadedCommands = 0;
		commandFiles.forEach(fileName =>
		{
			const commandLocation = fileName.replace('.js', '');
			delete require.cache[require.resolve(commandLocation)];
			const Command = require(commandLocation).default;
			const _command = new Command(this._bot);
			if (this._bot.disableBase.includes(_command.name)) return;
			_command._classloc = commandLocation;
			if (_command.overloads)
			{
				if (!this._bot.commands.has(_command.overloads)) // eslint-disable-line curly
					throw new Error(`Command "${_command.overloads}" does not exist to be overloaded.`);
				this._bot.commands.delete(_command.overloads);
				this._bot.commands.register(_command, _command.name);
				console.log(`Command '${_command.name}' loaded, overloading command '${_command.overloads}'.`); // eslint-disable-line no-console
			}
			else
			{
				this._bot.commands.register(_command, _command.name);
				loadedCommands++;
				console.log(`Command '${_command.name}' loaded.`); // eslint-disable-line no-console
			}
		});
		console.log(`Loaded ${loadedCommands} total commands in ${this._bot.commands.groups.length} groups.`); // eslint-disable-line no-console
	}

	/**
	 * Reload the given command in the Bot's {@link CommandRegistry} ({@link Bot#commands})
	 * @memberof CommandLoader
	 * @instance
	 * @param {string} nameOrAlias - {@link Command#name} or {@link Command#aliases} alias
	 * @returns {boolean}
	 */
	reloadCommand(nameOrAlias)
	{
		const name = this._bot.commands.findByNameOrAlias(nameOrAlias).name;
		if (!name) return false;
		const commandLocation = this._bot.commands.get(name)._classloc;
		delete require.cache[require.resolve(commandLocation)];
		const Command = require(commandLocation).default;
		const _command = new Command(this._bot);
		_command._classloc = commandLocation;
		this._bot.commands.register(_command, _command.name, true);
		console.log(`Command '${_command.name}' reloaded.`); // eslint-disable-line no-console
		return true;
	}
}
<|endoftext|>


<commit_before>'use strict';

var Promise = require('bluebird');
var Joi = require('joi');
var Message = require('./Message');
var Handler = require('./Handler');
var schema = require('./schema');

function Route(server, config) {
  this.server = server;

  Joi.assert(config, schema.route, 'route');

  this.channel = null;

  this.method = config.method;
  this.handler = config.config.handler;
  this.validation = config.config.validate;
  this.concurrency = config.concurrency || 1;

  if (! this.validation) {
    this.validation = Joi.any().optional();
  }
}

Route.validate = function(config) {
  return Joi.validate(config, schema, 'route');
};

Route.prototype.validate = function(params) {
  return Joi.validate(
    params,
    this.validation.options({
      language : { root : 'params' }
    }),
    {
      abortEarly   : false,
      stripUnknown : true,
      presence     : 'required'
    }
  );
};

Route.prototype.sendResponse = function(request, response) {
  var payload = response.toMessage().format();
  this.channel.sendToQueue(request.properties.replyTo, payload.content, payload.properties);
};

Route.prototype.onMessage = function(message, callback) {
  var request = new Message(message);

  var handler = new Handler(this, request, callback);
  handler.handle().bind(this).then(function(response) {

    if (response.disposition === 'nack') {
      return this.channel.nack(message, false, false);
    } else if (response.disposition === 'requeue') {
      return this.channel.nack(message, false, true);
    }

    response.properties.correlationId = message.properties.correlationId;

    if (request.properties.replyTo) {
      response.properties.persistent = true;
      this.sendResponse(request, response);
    }

    this.channel.ack(message);
  });
};

Route.prototype._createChannel = function() {
  return Promise.resolve(this.server.connection.createChannel())
    .bind(this)
    .then(function(channel) {
      this.channel = channel;
      this.channel.prefetch(this.concurrency);
    });
};

Route.prototype.start = function() {
  return this._createChannel().bind(this)
    .then(function() {
      return Promise.resolve(this.channel.assertQueue(this.method), {
        durable : true
      });
    })
    .then(function() {
      return Promise.resolve(this.channel.consume(this.method, this.onMessage.bind(this), {
        noAck : false
      })).then(function(queue) {
        this.consumerTag = queue.consumerTag;
      }.bind(this));
    });
};

module.exports = Route;
<commit_msg>remove callback from Route.onMessage<commit_after>'use strict';

var Promise = require('bluebird');
var Joi = require('joi');
var Message = require('./Message');
var Handler = require('./Handler');
var schema = require('./schema');

function Route(server, config) {
  this.server = server;

  Joi.assert(config, schema.route, 'route');

  this.channel = null;

  this.method = config.method;
  this.handler = config.config.handler;
  this.validation = config.config.validate;
  this.concurrency = config.concurrency || 1;

  if (! this.validation) {
    this.validation = Joi.any().optional();
  }
}

Route.validate = function(config) {
  return Joi.validate(config, schema, 'route');
};

Route.prototype.validate = function(params) {
  return Joi.validate(
    params,
    this.validation.options({
      language : { root : 'params' }
    }),
    {
      abortEarly   : false,
      stripUnknown : true,
      presence     : 'required'
    }
  );
};

Route.prototype.sendResponse = function(request, response) {
  var payload = response.toMessage().format();
  this.channel.sendToQueue(request.properties.replyTo, payload.content, payload.properties);
};

Route.prototype.onMessage = function(message) {
  var request = new Message(message);

  var handler = new Handler(this, request);
  handler.handle().bind(this).then(function(response) {

    if (response.disposition === 'nack') {
      return this.channel.nack(message, false, false);
    } else if (response.disposition === 'requeue') {
      return this.channel.nack(message, false, true);
    }

    response.properties.correlationId = message.properties.correlationId;

    if (request.properties.replyTo) {
      response.properties.persistent = true;
      this.sendResponse(request, response);
    }

    this.channel.ack(message);
  });
};

Route.prototype._createChannel = function() {
  return Promise.resolve(this.server.connection.createChannel())
    .bind(this)
    .then(function(channel) {
      this.channel = channel;
      this.channel.prefetch(this.concurrency);
    });
};

Route.prototype.start = function() {
  return this._createChannel().bind(this)
    .then(function() {
      return Promise.resolve(this.channel.assertQueue(this.method), {
        durable : true
      });
    })
    .then(function() {
      return Promise.resolve(this.channel.consume(this.method, this.onMessage.bind(this), {
        noAck : false
      })).then(function(queue) {
        this.consumerTag = queue.consumerTag;
      }.bind(this));
    });
};

module.exports = Route;
<|endoftext|>


<commit_before>const keyMirror = require('keymirror');

const Types = keyMirror({
    SET_SEARCH_TERM: null
});

module.exports.navigationReducer = (state, action) => {
    if (typeof state === 'undefined') {
        state = '';
    }
    switch (action.type) {
    case Types.SET_SEARCH_TERM:
        return action.searchTerm;
    default:
        return state;
    }
};

module.exports.setSearchTerm = searchTerm => ({
    type: Types.SET_SEARCH_TERM,
    searchTerm: searchTerm
});
<commit_msg>Add mode<commit_after>const keyMirror = require('keymirror');

const Types = keyMirror({
    SET_MODE: null,
    SET_SEARCH_TERM: null
});

module.exports.navigationReducer = (state, action) => {
    if (typeof state === 'undefined') {
        state = '';
    }
    switch (action.type) {
    case Types.SET_MODE:
        return action.mode;
    case Types.SET_SEARCH_TERM:
        return action.searchTerm;
    default:
        return state;
    }
};

module.exports.setMode = mode => ({
    type: Types.SET_MODE,
    mode: mode
});

module.exports.setSearchTerm = searchTerm => ({
    type: Types.SET_SEARCH_TERM,
    searchTerm: searchTerm
});
<|endoftext|>


<commit_before>import BLP from 'blizzardry/lib/blp';
import * as DBC from 'blizzardry/lib/dbc/entities';
import { DecodeStream } from 'blizzardry/lib/restructure';
import { PNG } from 'pngjs';
import express from 'express';
import find from 'array-find';

import Archive from './archive';
import ServerConfig from '../utils/server-config';

class Pipeline {

  static get DATA_DIR() {
    return ServerConfig.db.get('clientData');
  }

  constructor() {
    this.router = express();
    this.router.param('resource', ::this.resource);
    this.router.get('/:resource(*.blp).png', ::this.blp);
    this.router.get('/:resource(*.dbc)/:id(*)?.json', ::this.dbc);
    this.router.get('/find/:query', ::this.find);
    this.router.get('/:resource', ::this.serve);
  }

  get archive() {
    this._archive = this._archive || Archive.build(this.constructor.DATA_DIR);
    return this._archive;
  }

  resource(req, _res, next, path) {
    req.resourcePath = path;
    req.resource = this.archive.files.get(path);
    if (req.resource) {
      next();

      // Ensure file is closed in StormLib.
      req.resource.close();
    } else {
      const err = new Error('resource not found');
      err.status = 404;
      throw err;
    }
  }

  blp(req, res) {
    BLP.from(req.resource.data, function(blp) {
      const mipmap = blp.largest;

      const png = new PNG({ width: mipmap.width, height: mipmap.height });
      png.data = mipmap.rgba;

      res.set('Content-Type', 'image/png');
      png.pack().pipe(res);
    });
  }

  dbc(req, res) {
    const name = req.resourcePath.match(/(\w+)\.dbc/)[1];
    const definition = DBC[name];
    if (definition) {
      const dbc = definition.dbc.decode(new DecodeStream(req.resource.data));
      const id = req.params.id;
      if (id) {
        const match = find(dbc.records, function(entity) {
          return String(entity.id) === id;
        });
        if (match) {
          res.send(match);
        } else {
          const err = new Error('entity not found');
          err.status = 404;
          throw err;
        }
      } else {
        res.send(dbc.records);
      }
    } else {
      const err = new Error('entity definition not found');
      err.status = 404;
      throw err;
    }
  }

  find(req, res) {
    res.send(this.archive.files.find(req.params.query));
  }

  serve(req, res) {
    res.send(req.resource.data);
  }

}

export default Pipeline;
<commit_msg>Fix incorrect ServerConfig path<commit_after>import BLP from 'blizzardry/lib/blp';
import * as DBC from 'blizzardry/lib/dbc/entities';
import { DecodeStream } from 'blizzardry/lib/restructure';
import { PNG } from 'pngjs';
import express from 'express';
import find from 'array-find';

import Archive from './archive';
import ServerConfig from '../config';

class Pipeline {

  static get DATA_DIR() {
    return ServerConfig.db.get('clientData');
  }

  constructor() {
    this.router = express();
    this.router.param('resource', ::this.resource);
    this.router.get('/:resource(*.blp).png', ::this.blp);
    this.router.get('/:resource(*.dbc)/:id(*)?.json', ::this.dbc);
    this.router.get('/find/:query', ::this.find);
    this.router.get('/:resource', ::this.serve);
  }

  get archive() {
    this._archive = this._archive || Archive.build(this.constructor.DATA_DIR);
    return this._archive;
  }

  resource(req, _res, next, path) {
    req.resourcePath = path;
    req.resource = this.archive.files.get(path);
    if (req.resource) {
      next();

      // Ensure file is closed in StormLib.
      req.resource.close();
    } else {
      const err = new Error('resource not found');
      err.status = 404;
      throw err;
    }
  }

  blp(req, res) {
    BLP.from(req.resource.data, function(blp) {
      const mipmap = blp.largest;

      const png = new PNG({ width: mipmap.width, height: mipmap.height });
      png.data = mipmap.rgba;

      res.set('Content-Type', 'image/png');
      png.pack().pipe(res);
    });
  }

  dbc(req, res) {
    const name = req.resourcePath.match(/(\w+)\.dbc/)[1];
    const definition = DBC[name];
    if (definition) {
      const dbc = definition.dbc.decode(new DecodeStream(req.resource.data));
      const id = req.params.id;
      if (id) {
        const match = find(dbc.records, function(entity) {
          return String(entity.id) === id;
        });
        if (match) {
          res.send(match);
        } else {
          const err = new Error('entity not found');
          err.status = 404;
          throw err;
        }
      } else {
        res.send(dbc.records);
      }
    } else {
      const err = new Error('entity definition not found');
      err.status = 404;
      throw err;
    }
  }

  find(req, res) {
    res.send(this.archive.files.find(req.params.query));
  }

  serve(req, res) {
    res.send(req.resource.data);
  }

}

export default Pipeline;
<|endoftext|>


<commit_before>// ==UserScript==
// @name        Swearify
// @description Adds a number of enhancements to your experience on AIM games.
// @namespace   kaffeinition@gmail.com
// @include     http://aimgames.forummotion.com/*
// @version     beta 0.trash.5
// @grant       none
// @icon        http://i.imgur.com/HlEs1B4.png
// @license     MIT License (Expat); opensource.org/licenses/MIT
// @require     https://raw.githubusercontent.com/RadLikeWhoa/Countable/master/Countable.js
// @require     https://raw.githubusercontent.com/HulaSamsquanch/aimgames/master/swearify/textUtils.js
// @require     https://raw.githubusercontent.com/arasatasaygin/is.js/master/is.js
// ==/UserScript==

window.addEventListener('load', function() { /* shit goes down in here */
  if (is.ie() || is.safari() || is.opera()) alert('This browser is unsupported by Swearify.');
  else {
    var chatFrame = document.getElementById('frame_chatbox').contentWindow.document.getElementsByTagName('script');
    var chatScript;
    for (var i = 0; i < chatFrame.length; i++)
    	if (chatFrame[i].src == 'http://illiweb.com/rsc/18/frm/chatbox/chatbox8.js')
    		chatScript = chatFrame[i];
    //remove old script since we can't edit src directly
    var chatScriptParent = chatScript.parentNode;
    chatScriptParent.removeChild(chatScript);
    //make a new script with the right src
    chatScript = document.createElement('script');
    chatScript.type = 'text/javascript';
    chatScript.src = 'https://cdn.rawgit.com/HulaSamsquanch/aimgames/hijack-cbox/swearify/hchat.js';
    chatScriptParent.appendChild(chatScript);
  }
  //debugg(); // get info and shit
}, false);
<commit_msg>Update swearify.user.js<commit_after>// ==UserScript==
// @name        Swearify
// @description Adds a number of enhancements to your experience on AIM games.
// @namespace   kaffeinition@gmail.com
// @include     http://aimgames.forummotion.com/*
// @version     beta 0.trash.6
// @grant       none
// @icon        http://i.imgur.com/HlEs1B4.png
// @license     MIT License (Expat); opensource.org/licenses/MIT
// @require     https://raw.githubusercontent.com/RadLikeWhoa/Countable/master/Countable.js
// @require     https://raw.githubusercontent.com/HulaSamsquanch/aimgames/master/swearify/textUtils.js
// @require     https://raw.githubusercontent.com/arasatasaygin/is.js/master/is.js
// ==/UserScript==

window.addEventListener('load', function() { /* shit goes down in here */
  if (is.ie() || is.safari() || is.opera()) alert('This browser is unsupported by Swearify.');
  else {
    var chatFrame = document.getElementById('frame_chatbox').contentWindow.document.getElementsByTagName('script');
    var chatScript;
    for (var i = 0; i < chatFrame.length; i++)
    	if (chatFrame[i].src == 'http://illiweb.com/rsc/18/frm/chatbox/chatbox8.js')
    		chatScript = chatFrame[i];
    console.log(chatScript);
    //remove old script since we can't edit src directly
    var chatScriptParent = chatScript.parentNode;
    chatScriptParent.removeChild(chatScript);
    //make a new script with the right src
    chatScript = document.createElement('script');
    chatScript.type = 'text/javascript';
    chatScript.src = 'https://cdn.rawgit.com/HulaSamsquanch/aimgames/hijack-cbox/swearify/hchat.js';
    chatScriptParent.appendChild(chatScript);
    console.log(chatScript);
    console.log(chatScriptParent);
  }
  //debugg(); // get info and shit
}, false);
<|endoftext|>


<commit_before>/**
 * Copyright 2009 RedHog, Egil Möller <egil.moller@piratpartiet.se>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import("faststatic");
import("fileutils");
import("dispatch.{Dispatcher,PrefixMatcher,forward}");

import("etherpad.utils.*");
import("etherpad.globals.*");
import("etherpad.log");
import("fastJSON");

jimport("java.io.File",
        "java.io.DataInputStream",
        "java.io.FileInputStream",
        "java.lang.Byte",
        "java.io.FileReader",
        "java.io.BufferedReader",
        "java.security.MessageDigest",
        "java.lang.Runtime",
        "net.appjet.common.util.BetterFile",
	"java.lang.ProcessBuilder",
	"java.lang.Process",
	"java.io.InputStreamReader"
	);

function getImageSize(filename, page) {
  var proc;
  if (filename.split(".").pop().toLowerCase() == 'pdf') {
    proc = ProcessBuilder("src/plugins/imageConvert/identifyImage.sh",
			  filename, page).start();
  } else {
    proc = ProcessBuilder("identify",
			  "-format",
			  "%[fx:w]\n%[fx:h]",
			  filename + "[" + page + "]").start();
  }
  var procStdout = BufferedReader(new InputStreamReader(proc.getInputStream()));
  var w = parseFloat(procStdout.readLine());
  var h = parseFloat(procStdout.readLine());
  proc.waitFor();
  return {w:w, h:h}
}

function convertImage(inFileName, page, outFileName, offset, size, pixelSize) {
  var proc;
  if (inFileName.split(".").pop().toLowerCase() == 'pdf') {
    var dpix = pixelSize.w * 72.0 / size.w;
    var dpiy = pixelSize.h * 72.0 / size.h;
    proc = ProcessBuilder("src/plugins/imageConvert/convertImage.sh",
			  inFileName,
			  outFileName,
			  page,
			  dpix, dpiy,
			  offset.x, offset.y,
			  offset.x + size.w, offset.y + size.h);
  } else {
    proc = ProcessBuilder("convert",
			  "-crop",
			  "" + size.w + "x" + size.h + "+" + offset.x + "+" + offset.y,
			  "-scale",
			  "" + pixelSize.w + "x" + pixelSize.w,
			  inFileName + "["+page+"]",
			  outFileName);
  }
  proc.start().waitFor();
}

function onRequest() {
  var path = "src/plugins/fileUpload/upload/" + request.path.toString().slice("/ep/imageConvert/".length);

  if (request.params.action == "getSize") {
    var imageSize = getImageSize(path, 0);
    response.setContentType("text/plain");
    response.write(fastJSON.stringify(imageSize));
  } else {
    var page = request.params.p === undefined ? 0 : parseInt(request.params.p);
    var offset = {x:(request.params.x === undefined) ? 0 : parseInt(request.params.x),
		  y:(request.params.y === undefined) ? 0 : parseInt(request.params.y)};
    var size = {w:(request.params.w === undefined) ? 0 : parseInt(request.params.w),
		h:(request.params.h === undefined) ? 0 : parseInt(request.params.h)};
    var pixelSize = {w:(request.params.pw === undefined) ? 0 : parseInt(request.params.pw),
		     h:(request.params.ph === undefined) ? 0 : parseInt(request.params.ph)};

    var outFileName = path.split(".");
    var extension = outFileName.pop();
    outFileName.push("" + page + ":" + offset.x + "," +  offset.y + ":" + size.w + "," +  size.h + ":" + pixelSize.w + "," +  pixelSize.h);
    outFileName.push("png");
    outFileName = outFileName.join(".");

    convertImage(path, page, outFileName, offset, size, pixelSize);

    response.setContentType("image/png");

    var file = FileInputStream(File(outFileName));
    response.writeBytes(BetterFile.getStreamBytes(file));
    file.close();

  }

  if (request.acceptsGzip) {
    response.setGzip(true);
  }
  return true;
}
<commit_msg>Bugfix for pdf page handling<commit_after>/**
 * Copyright 2009 RedHog, Egil Möller <egil.moller@piratpartiet.se>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import("faststatic");
import("fileutils");
import("dispatch.{Dispatcher,PrefixMatcher,forward}");

import("etherpad.utils.*");
import("etherpad.globals.*");
import("etherpad.log");
import("fastJSON");

jimport("java.io.File",
        "java.io.DataInputStream",
        "java.io.FileInputStream",
        "java.lang.Byte",
        "java.io.FileReader",
        "java.io.BufferedReader",
        "java.security.MessageDigest",
        "java.lang.Runtime",
        "net.appjet.common.util.BetterFile",
	"java.lang.ProcessBuilder",
	"java.lang.Process",
	"java.io.InputStreamReader"
	);

function getImageSize(filename, page) {
  var proc;
  if (filename.split(".").pop().toLowerCase() == 'pdf') {
    proc = ProcessBuilder("src/plugins/imageConvert/identifyImage.sh",
			  filename, page + 1).start();
  } else {
    proc = ProcessBuilder("identify",
			  "-format",
			  "%[fx:w]\n%[fx:h]",
			  filename + "[" + page + "]").start();
  }
  var procStdout = BufferedReader(new InputStreamReader(proc.getInputStream()));
  var w = parseFloat(procStdout.readLine());
  var h = parseFloat(procStdout.readLine());
  proc.waitFor();
  return {w:w, h:h}
}

function convertImage(inFileName, page, outFileName, offset, size, pixelSize) {
  var proc;
  if (inFileName.split(".").pop().toLowerCase() == 'pdf') {
    var dpix = pixelSize.w * 72.0 / size.w;
    var dpiy = pixelSize.h * 72.0 / size.h;
    proc = ProcessBuilder("src/plugins/imageConvert/convertImage.sh",
			  inFileName,
			  outFileName,
			  page + 1,
			  dpix, dpiy,
			  offset.x, offset.y,
			  offset.x + size.w, offset.y + size.h);
  } else {
    proc = ProcessBuilder("convert",
			  "-crop",
			  "" + size.w + "x" + size.h + "+" + offset.x + "+" + offset.y,
			  "-scale",
			  "" + pixelSize.w + "x" + pixelSize.w,
			  inFileName + "["+page+"]",
			  outFileName);
  }
  proc.start().waitFor();
}

function onRequest() {
  var path = "src/plugins/fileUpload/upload/" + request.path.toString().slice("/ep/imageConvert/".length);
  var page = request.params.p === undefined ? 0 : parseInt(request.params.p);
  var offset = {x:(request.params.x === undefined) ? 0 : parseInt(request.params.x),
		y:(request.params.y === undefined) ? 0 : parseInt(request.params.y)};
  var size = {w:(request.params.w === undefined) ? 0 : parseInt(request.params.w),
	      h:(request.params.h === undefined) ? 0 : parseInt(request.params.h)};
  var pixelSize = {w:(request.params.pw === undefined) ? 0 : parseInt(request.params.pw),
		   h:(request.params.ph === undefined) ? 0 : parseInt(request.params.ph)};

  if (request.params.action == "getSize") {
    var imageSize = getImageSize(path, page);
    response.setContentType("text/plain");
    response.write(fastJSON.stringify(imageSize));
  } else {
    var outFileName = path.split(".");
    var extension = outFileName.pop();
    outFileName.push("" + page + ":" + offset.x + "," +  offset.y + ":" + size.w + "," +  size.h + ":" + pixelSize.w + "," +  pixelSize.h);
    outFileName.push("png");
    outFileName = outFileName.join(".");

    convertImage(path, page, outFileName, offset, size, pixelSize);

    response.setContentType("image/png");

    var file = FileInputStream(File(outFileName));
    response.writeBytes(BetterFile.getStreamBytes(file));
    file.close();

  }

  if (request.acceptsGzip) {
    response.setGzip(true);
  }
  return true;
}
<|endoftext|>


<commit_before>import React, { Component, PropTypes } from 'react'
import _ from '../util'

const { setValueByPath, getValueByPath, debounce } = _

export default class Input extends Component {
  static contextTypes = {
    state: PropTypes.object,
    actions: PropTypes.object,
    handleInputChange: PropTypes.func
  };
  static defaultProps = {
    as: 'input',
    type: 'text',
    name: '',
    actionType: 'UPDATE_INPUT_VALUE',
    wait: 50
  };
  constructor(props, context) {
    super(props, context)
    this.state = {
      value: this.getValue()
    }
    this.lock = false
    this.timer = null
  }
  componentWillReceiveProps(nextProps, nextContext) {
    if (!this.lock) {
      this.setState({
        value: this.getValue(nextProps, nextContext)
      })
    }
  }
  render () {
    let { state, props } = this
    let { as, name, value, check, actionType, transformer, wait, ...subProps } = props
    let Tag = as

    subProps.value = value != null ? value : state.value
    subProps.name = name
    subProps.onChange = this.handleChange
    subProps.onFocus = this.handleFocus
    subProps.onBlur = this.handleBlur

    return <Tag {...subProps} />
  }
  handleChange = event => {
    let { onChange } = this.props
    let currentValue = event.currentTarget.value
    this.setState({
      value: currentValue,
    })
    onChange && onChange(event)
    this.sync(currentValue)
  }
  handleFocus = event => {
    let { onFocus, check } = this.props

    this.lock = true

    if (typeof check === 'function') {
      this.clearValidStatus()
    }

    onFocus && onFocus(event)
  };
  handleBlur = event => {
    let { onBlur, check } = this.props

    this.lock = false

    if (typeof check === 'function') {
      this.checkValidStatus()
    }

    this.updateValue()

    onBlur && onBlur(event)
  };
  updateValue = () => {
    let { context, props } = this
    let { name, check, transformer } = props
    let path = check ? `${name}.value` : name
    let currentValue = this.state.value
    let oldValue = getValueByPath(context.state, path)

    if (typeof transformer === 'function') {
      currentValue = transformer(currentValue, oldValue)
    }

    let { handleInputChange } = context

    if (handleInputChange) {
      currentValue = handleInputChange(path, currentValue, oldValue)
    }

    if (currentValue === oldValue) {
      return
    }

    this.setState({
      value: currentValue
    })

    let newGlobalState = setValueByPath(context.state, path, currentValue)
    this.setGlobalState(newGlobalState)
  }
  sync(currentValue) {
    clearTimeout(this.timer)
    this.timer = setTimeout(
      this.updateValue,
      this.props.wait
    )
  }
  getValue(props, context) {
    let { state } = context || this.context
    let { check, name } = props || this.props
    let path = check ? `${name}.value` : name
    return getValueByPath(state, path)
  }
  getAction () {
    return this.context.actions[this.props.actionType]
  }
  setGlobalState (newState) {
    let CALL_ACTION = this.getAction()
    CALL_ACTION(newState)
  }
  checkValidStatus() {
    let { name, onBlur, check } = this.props
    let currentValue = this.state.value
    let pathOfValidState = `${name}.isValid`
    let pathOfWranState = `${name}.isWarn`
    let isValidValue = check(currentValue)
    let newState = this.context.state

    newState = setValueByPath(newState, pathOfValidState, isValidValue)
    newState = setValueByPath(newState, pathOfWranState, !isValidValue)
    this.setGlobalState(newState)
  }
  clearValidStatus() {
    let { state } = this.context
    let { name } = this.props
    let path = `${name}.isWarn`
    let isWarn = getValueByPath(state, path)
    if (!isWarn) {
      return
    }

    let newState = setValueByPath(state, path, false)
    this.setGlobalState(newState)
  }
}
<commit_msg>revert Input.js<commit_after>import React, { Component, PropTypes } from 'react'
import _ from '../util'

const { setValueByPath, getValueByPath } = _

export default class Input extends Component {
  static contextTypes = {
    state: PropTypes.object,
    actions: PropTypes.object,
    handleInputChange: PropTypes.func
  };
  static defaultProps = {
    as: 'input',
    type: 'text',
    name: '',
    actionType: 'UPDATE_INPUT_VALUE'
  };
  render () {
    let { state } = this.context
    let { as, name, value, check, actionType, transformer, ...subProps } = this.props
    let Tag = as

    let path = check ? `${name}.value` : name

    if (value === undefined) {
      value = getValueByPath(state, path)
    }

    subProps.value = value
    subProps.name = name
    subProps.onChange = this.handleChange
    if (check) {
      subProps.onFocus = this.handleFocus
      subProps.onBlur = this.handleBlur
    }

    return <Tag {...subProps} />
  }
  getAction () {
    return this.context.actions[this.props.actionType]
  }
  setGlobalState (newState) {
    let CALL_ACTION = this.getAction()
    CALL_ACTION(newState)
  }
  handleChange = event => {
    let { state, handleInputChange } = this.context
    let { name, onChange, check, transformer } = this.props
    let currentValue = event.currentTarget.value
    let path = check ? `${name}.value` : name
    let oldValue = getValueByPath(state, path)

    if (typeof transformer === 'function') {
      currentValue = transformer(currentValue)
    }
    if (handleInputChange) {
      currentValue = handleInputChange(path, currentValue, oldValue)
    }

    let newState = setValueByPath(state, path, currentValue)

    this.setGlobalState(newState)
    onChange && onChange(event)
  };
  handleFocus = event => {
    let { state } = this.context
    let { name, onFocus } = this.props
    let path = `${name}.isWarn`
    let isWarn = getValueByPath(state, path)
    if (!isWarn) {
      return
    }
    let newState = setValueByPath(state, path, false)

    this.setGlobalState(newState)
    onFocus && onFocus(event)
  };
  handleBlur = event => {
    let state = this.context.state
    let { name, onBlur, check } = this.props
    let pathOfValidState = `${name}.isValid`
    let pathOfWranState = `${name}.isWarn`

    let isValidValue = check(event.currentTarget.value)
    let newState = state

    newState = setValueByPath(newState, pathOfValidState, isValidValue)
    newState = setValueByPath(newState, pathOfWranState, !isValidValue)

    this.setGlobalState(newState)
    onBlur && onBlur(event)
  };
}
<|endoftext|>