<commit_before>'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
	errorHandler = require('./errors.server.controller'),
	Project = mongoose.model('Project'),
	_ = require('lodash');

var fs = require('fs');
var path = require('path');
var aws = require('aws-sdk');
var AWS_ACCESS_KEY = process.env.AWS_ACCESS_KEY;
var AWS_SECRET_KEY = process.env.AWS_SECRET_KEY;
var S3_BUCKET = 'starthub.img';

/**
 * Create a Project
 */
exports.create = function(req, res) {
	var project = new Project(req.body);
	project.user = req.user;

	project.save(function(err) {
		if (err) {
			return res.status(400).send({
				message: errorHandler.getErrorMessage(err)
			});
		} else {
			res.jsonp(project);
		}
	});
};

/**
 * Show the current Project
 */
exports.read = function(req, res) {
	res.jsonp(req.project);
};

/**
 * Update a Project
 */
exports.update = function(req, res) {
	var project = req.project ;

	project = _.extend(project , req.body);

	project.save(function(err) {
		if (err) {
			return res.status(400).send({
				message: errorHandler.getErrorMessage(err)
			});
		} else {
			res.jsonp(project);
		}
	});
};

/**
 * Delete an Project
 */
exports.delete = function(req, res) {
	var project = req.project ;

	project.remove(function(err) {
		if (err) {
			return res.status(400).send({
				message: errorHandler.getErrorMessage(err)
			});
		} else {
			res.jsonp(project);
		}
	});
};

/**
 * List of Projects
 */
exports.list = function(req, res) { 
	Project.find().sort('-created').populate('user', 'displayName username').exec(function(err, projects) {
		if (err) {
			return res.status(400).send({
				message: errorHandler.getErrorMessage(err)
			});
		} else {
			res.jsonp(projects);
		}
	});
};

/**
 * Project middleware
 */
exports.projectByID = function(req, res, next, id) { 
	Project.findById(id).populate('user', 'displayName username').exec(function(err, project) {
		if (err) return next(err);
		if (! project) return next(new Error('Failed to load Project ' + id));
		req.project = project ;
		next();
	});
};

/**
 * Project authorization middleware
 */
exports.hasAuthorization = function(req, res, next) {
	if (req.project.user.id !== req.user.id) {
		return res.status(403).send('User is not authorized');
	}
	next();
};

exports.saveImg = function(req, res){

    var mkdirSync = function (path) {
        try {
            fs.mkdirSync(path);
        } catch(e) {
            if ( e.code !== 'EEXIST' ) throw e;
        }
    };

    console.log(req.files.file.path);
    var projectId = req.project._id;
    var img_dir = path.join(__dirname, '../img/' + projectId + '/');
    mkdirSync(img_dir);
    var img_path = path.join(img_dir, 'logo.jpg');
    fs.rename(req.files.file.path, img_path, function(err){
        if(err){
            console.log('ERROr ' + err);
            res.sendStatus(400);
        }
        else {
            console.log('completed');
            res.sendStatus(200);
        }
    });
};

exports.getImg =  function(req, res){
    var projectId = req.project._id;
    fs.exists(path.join(__dirname, '../img/' + projectId + '/logo.jpg'), function(exists){
        if(exists){
            res.statusCode= 200;
            res.sendFile(path.join(__dirname, '../img/' + projectId + '/logo.jpg'));
        }
        else{
            //we want the default logo
            res.statusCode = 200;
            res.sendFile(path.join(__dirname, '../img/default.jpg'));
        }
    });
};


/*exports.get_aws_sign = function(req, res){
        aws.config.update({accessKeyId: AWS_ACCESS_KEY, secretAccessKey: AWS_SECRET_KEY});
        var s3 = new aws.S3();
        var s3_params = {
            Bucket: S3_BUCKET,
            Key: req.query.s3_object_name,
            Expires: 60,
            ContentType: req.query.s3_object_type,
            ACL: 'public-read'
        };
        s3.getSignedUrl('putObject', s3_params, function(err, data){
            if(err){
                console.log(err);
            }
            else{
                var return_data = {
                    signed_request: data,
                    url: 'https://'+S3_BUCKET+'.s3.amazonaws.com/'+req.query.s3_object_name
                };
                res.write(JSON.stringify(return_data));
                res.end();
            }
        });
});*/<commit_msg>Removed old aws code<commit_after>'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
	errorHandler = require('./errors.server.controller'),
	Project = mongoose.model('Project'),
	_ = require('lodash');

var fs = require('fs');
var path = require('path');

/**
 * Create a Project
 */
exports.create = function(req, res) {
	var project = new Project(req.body);
	project.user = req.user;

	project.save(function(err) {
		if (err) {
			return res.status(400).send({
				message: errorHandler.getErrorMessage(err)
			});
		} else {
			res.jsonp(project);
		}
	});
};

/**
 * Show the current Project
 */
exports.read = function(req, res) {
	res.jsonp(req.project);
};

/**
 * Update a Project
 */
exports.update = function(req, res) {
	var project = req.project ;

	project = _.extend(project , req.body);

	project.save(function(err) {
		if (err) {
			return res.status(400).send({
				message: errorHandler.getErrorMessage(err)
			});
		} else {
			res.jsonp(project);
		}
	});
};

/**
 * Delete an Project
 */
exports.delete = function(req, res) {
	var project = req.project ;

	project.remove(function(err) {
		if (err) {
			return res.status(400).send({
				message: errorHandler.getErrorMessage(err)
			});
		} else {
			res.jsonp(project);
		}
	});
};

/**
 * List of Projects
 */
exports.list = function(req, res) { 
	Project.find().sort('-created').populate('user', 'displayName username').exec(function(err, projects) {
		if (err) {
			return res.status(400).send({
				message: errorHandler.getErrorMessage(err)
			});
		} else {
			res.jsonp(projects);
		}
	});
};

/**
 * Project middleware
 */
exports.projectByID = function(req, res, next, id) { 
	Project.findById(id).populate('user', 'displayName username').exec(function(err, project) {
		if (err) return next(err);
		if (! project) return next(new Error('Failed to load Project ' + id));
		req.project = project ;
		next();
	});
};

/**
 * Project authorization middleware
 */
exports.hasAuthorization = function(req, res, next) {
	if (req.project.user.id !== req.user.id) {
		return res.status(403).send('User is not authorized');
	}
	next();
};

exports.saveImg = function(req, res){

    var mkdirSync = function (path) {
        try {
            fs.mkdirSync(path);
        } catch(e) {
            if ( e.code !== 'EEXIST' ) throw e;
        }
    };

    console.log(req.files.file.path);
    var projectId = req.project._id;
    var img_dir = path.join(__dirname, '../img/' + projectId + '/');
    mkdirSync(img_dir);
    var img_path = path.join(img_dir, 'logo.jpg');
    fs.rename(req.files.file.path, img_path, function(err){
        if(err){
            console.log('ERROr ' + err);
            res.sendStatus(400);
        }
        else {
            console.log('completed');
            res.sendStatus(200);
        }
    });
};

exports.getImg =  function(req, res){
    var projectId = req.project._id;
    fs.exists(path.join(__dirname, '../img/' + projectId + '/logo.jpg'), function(exists){
        if(exists){
            res.statusCode= 200;
            res.sendFile(path.join(__dirname, '../img/' + projectId + '/logo.jpg'));
        }
        else{
            //we want the default logo
            res.statusCode = 200;
            res.sendFile(path.join(__dirname, '../img/default.jpg'));
        }
    });
};


{'fraction_non_alphanumeric': 0.16988265971316818, 'fraction_numerical': 0.008865710560625815, 'mean_word_length': 2.5645910780669143, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, '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': '361087', 'n_tokens_mistral': 2700, 'n_tokens_neox': 2528, 'n_words': 481}