<commit_before>var async = require('async');
var keystone = require('keystone');
var exec = require('child_process').exec;

var FileData = keystone.list('FileUpload');
var User = keystone.list('User');

/**
 * List Files
 */
exports.list = function(req, res) {
    FileData.model.find(function(err, items) {

        if (err) return res.apiError('database error', err);

        res.apiResponse({
            collections: items
        });

    });
}

/**
 * Get File by ID
 */
exports.get = function(req, res) {

    FileData.model.findById(req.params.id).exec(function(err, item) {

        if (err) return res.apiError('database error', err);
        if (!item) return res.apiError('not found');

        res.apiResponse({
            collection: item
        });

    });
}


/**
 * Update File by ID
 */
exports.update = function(req, res) {
    FileData.model.findById(req.params.id).exec(function(err, item) {
        if (err) return res.apiError('database error', err);
        if (!item) return res.apiError('not found');

        var data = (req.method == 'POST') ? req.body : req.query;

        item.getUpdateHandler(req).process(data, function(err) {

            if (err) return res.apiError('create error', err);

            res.apiResponse({
                collection: item
            });

        });
    });
}

/**
 * Upload a New File
 */
exports.create = function(req, res) {

    var item = new FileData.model(),
        data = (req.method == 'POST') ? req.body : req.query;

    var user_id = req.user._id;

    if (req.files.file_upload.mimetype !== 'image/jpeg' && req.files.file_upload.mimetype !== 'image/png') {
        req.flash('warning', 'File not supported');

        res.apiResponse({
            unsuported_file: "true"
        });

    } else if (req.files.file_upload.size > 500000) {
        req.flash('warning', 'Max file size is 500kb');

        res.apiResponse({
            unsuported_file: "true"
        });
    } else {
        item.getUpdateHandler(req).process(req.files, function(err) {

            if (err) return res.apiError('error', err);

            var new_path = "/uploads/members/" + item.file.filename;

            User.model.update({_id: user_id}, {$set : {photo_path : new_path}},
              function(err, affected, resp) {
                if(!err){

                    req.flash('success', 'Foto de perfil atualizada com sucesso!');
                    res.apiResponse({
                        file_upload: item
                    });

                } else {
                  req.flash('error', 'Ocorreu um erro, tenta mais tarde!');
                }
              });
        });
    }
}

/**
 * Delete File by ID
 */
exports.remove = function(req, res) {
    var fileId = req.params.id;
    FileData.model.findById(req.params.id).exec(function(err, item) {

        if (err) return res.apiError('database error', err);

        if (!item) return res.apiError('not found');

        item.remove(function(err) {

            if (err) return res.apiError('database error', err);

            exec('rm public/uploads/files/' + fileId + '.*', function(err, stdout, stderr) {
                if (err) {
                    console.log('child process exited with error code ' + err.code);
                    return;
                }
                console.log(stdout);
            });
<commit_msg>Commit for Marcelo<commit_after>var async = require('async');
var keystone = require('keystone');
var exec = require('child_process').exec;

var FileData = keystone.list('FileUpload');
var User = keystone.list('User');

/**
 * List Files
 */
exports.list = function(req, res) {
    FileData.model.find(function(err, items) {

        if (err) return res.apiError('database error', err);

        res.apiResponse({
            collections: items
        });

    });
}

/**
 * Get File by ID
 */
exports.get = function(req, res) {

    FileData.model.findById(req.params.id).exec(function(err, item) {

        if (err) return res.apiError('database error', err);
        if (!item) return res.apiError('not found');

        res.apiResponse({
            collection: item
        });

    });
}


/**
 * Update File by ID
 */
exports.update = function(req, res) {
    FileData.model.findById(req.params.id).exec(function(err, item) {
        if (err) return res.apiError('database error', err);
        if (!item) return res.apiError('not found');

        var data = (req.method == 'POST') ? req.body : req.query;

        item.getUpdateHandler(req).process(data, function(err) {

            if (err) return res.apiError('create error', err);

            res.apiResponse({
                collection: item
            });

        });
    });
}

/**
 * Upload a New File
 */
exports.create = function(req, res) {

    var item = new FileData.model(),
        data = (req.method == 'POST') ? req.body : req.query;

    var user_id = req.user._id;

    if (req.files.file_upload.mimetype !== 'image/jpeg' && req.files.file_upload.mimetype !== 'image/png') {
        req.flash('warning', 'File not supported');

        res.apiResponse({
            unsuported_file: "true"
        });

    } else if (req.files.file_upload.size > 500000) {
        req.flash('warning', 'Max file size is 500kb');

        res.apiResponse({
            unsuported_file: "true"
        });
    } else {
        item.getUpdateHandler(req).process(req.files, function(err) {

            if (err) return res.apiError('error', err);

            var new_path = "/uploads/members/" + item.file.filename;

            User.model.update({_id: user_id}, {$set : {photo_path : new_path}},
              function(err, affected, resp) {
                if(!err){

                    req.flash('success', 'Foto de perfil atualizada com sucesso!');
                    res.apiResponse({
                        file_upload: item
                    });

                } else {
                  req.flash('error', 'Ocorreu um erro, tenta mais tarde!');
                }
              });
        });
    }
}

/**
 * Delete File by ID
 */
exports.remove = function(req, res) {
    var fileId = req.params.id;
    FileData.model.findById(req.params.id).exec(function(err, item) {

        if (err) return res.apiError('database error', err);

        if (!item) return res.apiError('not found');

        item.remove(function(err) {

            if (err) return res.apiError('database error', err);

            //FIXME Mudar isto para 'fs'
            exec('rm public/uploads/files/' + fileId + '.*', function(err, stdout, stderr) {
                if (err) {
                    console.log('child process exited with error code ' + err.code);
                    return;
                }
                console.log(stdout);
            });

{'fraction_non_alphanumeric': 0.1502651738361815, 'fraction_numerical': 0.0026517383618149676, 'mean_word_length': 1.9491746307558644, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5415371', 'n_tokens_mistral': 2094, 'n_tokens_neox': 1978, 'n_words': 449}