<commit_before>var Memory = require('../models/memoryModel');
var User = require('../models/userModel');
var awsClient = require('../../server/aws');
var fs = require('fs');
var clarifai = require('../../api/clarifai');

exports.upload = function(req, res) {
  console.log('POST: /api/memories/upload');
  // console.log('req.file is now', req.file);
  awsClient.upload('uploads/' + req.file.filename, {}, function(err, versions, meta) {
		if (err) { 
			console.log('s3 upload error: ', err); 
		} 

		versions.forEach(function(image) {
			if (image.original) {
				Memory.create({
					title: req.file.filename,
					filePath: image.url, 
					createdAt: Date.now()
				}).then(function(memory) {
					fs.unlink('uploads/' + req.file.filename, function(err, success) {
						if (err) {
							console.log('Error deleting file,', err);
						}
					});

					clarifai(image.url).then(function(tags) {
						results = {
							api: 'clarifai',
							tags: tags
						};

						memory.analyses.push(JSON.stringify(results));
						memory.save();
					});

					User.findOne({username: req.user.username}).then(function(user) {
						user.memories.push(memory._id);		
						user.save(function(err) {
  						res.status(201).send(memory._id);
						});
					});
				}).catch(function(err) {
					console.log('Error creating memory,', err);
					res.status(404).send();
				});
			}
		});
	});

};

exports.fetchMemories = function(req, res) {
	User.findOne({ username: req.user.username }).populate('memories').then(function(user) {
		res.status(200).send(user.memories);
	}).catch(function(err) {
		console.log('Error retrieving user,', err);
		res.status(404).send();
	});
};

exports.fetchOne = function(req, res) {
	Memory.findOne({ _id: req.params.id }).then(function(memory) {
		res.status(200).send(memory);
	}).catch(function(err) {
		res.status(404).send();
	});
};

var addOne = function(req, res) {

};<commit_msg>Remove analysis stringification (#40)<commit_after>var Memory = require('../models/memoryModel');
var User = require('../models/userModel');
var awsClient = require('../../server/aws');
var fs = require('fs');
var clarifai = require('../../api/clarifai');

exports.upload = function(req, res) {
	if (!req.file) {
		console.log('Multer failed to save file');
		res.status(404).send();
	} else {
		  awsClient.upload('uploads/' + req.file.filename, {}, function(err, versions, meta) {
				if (err) { 
					console.log('s3 upload error: ', err); 
				} 

				versions.forEach(function(image) {
					if (image.original) {
						Memory.create({
							title: req.file.filename,
							filePath: image.url, 
							createdAt: Date.now()
						}).then(function(memory) {
							fs.unlink('uploads/' + req.file.filename, function(err, success) {
								if (err) {
									console.log('Error deleting file,', err);
								}
							});

							clarifai(image.url).then(function(tags) {
								results = {
									api: 'clarifai',
									tags: tags
								};

								memory.analyses.push(results);
								memory.save();
							});

							User.findOne({username: req.user.username}).then(function(user) {
								user.memories.push(memory._id);		
								user.save(function(err) {
		  						res.status(201).send(memory._id);
								});
							});
						}).catch(function(err) {
							console.log('Error creating memory,', err);
							res.status(404).send();
						});
					}
				});
			});
	}
};

exports.fetchMemories = function(req, res) {
	User.findOne({ username: req.user.username }).populate('memories').then(function(user) {
		res.status(200).send(user.memories);
	}).catch(function(err) {
		console.log('Error retrieving user,', err);
		res.status(404).send();
	});
};

exports.fetchOne = function(req, res) {
	Memory.findOne({ _id: req.params.id }).then(function(memory) {
		res.status(200).send(memory);
	}).catch(function(err) {
		res.status(404).send();
	});
};

var addOne = function(req, res) {

};
{'fraction_non_alphanumeric': 0.19507239014478028, 'fraction_numerical': 0.010922021844043688, 'mean_word_length': 2.9538152610441766, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '953595', 'n_tokens_mistral': 1706, 'n_tokens_neox': 1307, 'n_words': 212}