<commit_before>/**
 *
 *	Automated Tasks
 *	These tasks watch the media directory for new, changed, or deleted images
 *	and videos. It reads exif data from new media, converts the data into 
 *	something usable, adds it to the database, and passes the passes it to the
 *	media controller for processing. Deleted media is removed from the database.
 *
 **/

var path		= require('path'),
		hound		= require('hound'),
		exif		= require('exif2'),
		http 		= require('http');

module.exports = {
	watchDirectory: function (directory, watcher, createCallback, changeCallback, deleteCallback) {
		'use strict';

		createCallback = createCallback || this.readExif.bind(this);
		watcher = watcher || hound.watch(directory);

		watcher.on('create', function(file, stats) {
			createCallback(file);
		});

		watcher.on('change', function(file, stats) {
			if (changeCallback !== undefined) {
				changeCallback(file);
			}
		});

		watcher.on('delete', function(file) {
			if (deleteCallback !== undefined) {
				deleteCallback(file);
			}
		});
	},

	readExif: function (file, callback) {
		'use strict';

		callback = callback || this.prepareData.bind(this);

		exif(file, function (err, obj) {
			if (err) throw err;

			callback(obj);
		});
	},

	prepareData: function (data, callback) {
		'use strict';

		var preparedData = {},
				createDate, 
				file, 
				lastindex, 
				sizearray;

		callback = callback || this.submitMedia.bind(this);

		// Convert date into a string usable by JavaScript's new Date()
		createDate = data['create date'].split(' ');
		createDate[0] = createDate[0].replace(/[:]/g, '-');
		createDate = createDate.join('T');

		// Set up file name to be split into filename and extension
		file = data['file name'];
		lastindex = file.lastIndexOf('.');

		// Creates an array for width and height
		sizearray = (data['image size']) ? data['image size'].split('x') : [];

		preparedData.extension = file.substr(lastindex + 1);
		preparedData.filename = file.substr(0, file.length - preparedData.extension.length - 1);
		preparedData.title = preparedData.filename;
		preparedData.tags = [];
		preparedData.date = new Date(createDate);
		preparedData.description = '';
		preparedData.dimensions = data['image size'];
		preparedData.width = Number(sizearray[0]);
		preparedData.height = Number(sizearray[1]);
		preparedData.layout = (preparedData.width > preparedData.height) ? 'landscape' : 'portrait';

		if (preparedData.extension.match(/^(jpg|jpeg|png|gif)$/)) {
			preparedData.type = 'photo';
		} 
		else if (preparedData.extension.match(/^(mp4|m4v|mov|wmv|avi|mpg|ogv|3gp|3g2|webm)$/)) {
			preparedData.type = 'video';
		} 
		else {
			preparedData.type = 'not allowed';
		}

		callback(preparedData);
	},

	submitMedia: function (data, host, path, port, callback) {
		'use strict';

		var options, 
				req;

		data = JSON.stringify(data);

		host = host || 'localhost';
		path = path || '/api/v1/media';
		port = port || 8080;

		options = {
			host: host,
			path: path,
			port: port,
			method: 'POST',
			headers: {
				'Content-Type': 'application/json'
			}
		};

		req = http.request(options, function (response) {
			var str = '';
			response.on('data', function (chunk) {
				// A piece of data has been recieved
			});

			response.on('end', function () {
				// Response has been recieved
				console.log(str);
				if (callback) {
					callback(str);
				}
			});
		});

		req.write(data);
		req.end();
	},

	deleteMedia: function (data, host, path, port, callback) {
		'use strict';

		var options, 
				req;

		data = JSON.stringify(data);

		host = host || 'localhost';
		path = path || '/api/v1/media';
		port = port || 8080;

		options = {
			host: host,
			path: path,
			port: port,
			method: 'DELETE',
			headers: {
				'Content-Type': 'application/json'
			}
		};

		http.request(options, function (response) {
			var str = '';
			response.on('data', function (chunk) {
				// A piece of data has been recieved
			});

			response.on('end', function () {
				// Response has been recieved
				console.log(str);<commit_msg>Setting up deleteMedia method to delete media from database when it is removed from the appropriate folder<commit_after>/**
 *
 *	Automated Tasks
 *	These tasks watch the media directory for new, changed, or deleted images
 *	and videos. It reads exif data from new media, converts the data into 
 *	something usable, adds it to the database, and passes the passes it to the
 *	media controller for processing. Deleted media is removed from the database.
 *
 **/

var path		= require('path'),
		hound		= require('hound'),
		exif		= require('exif2'),
		http 		= require('http');

module.exports = {
	watchDirectory: function (directory, watcher, createCallback, changeCallback, deleteCallback) {
		'use strict';

		createCallback = createCallback || this.readExif.bind(this);
		deleteCallback = deleteCallback || this.deleteMedia.bind(this);
		watcher = watcher || hound.watch(directory);

		watcher.on('create', function(file, stats) {
			createCallback(file);
		});

		watcher.on('change', function(file, stats) {
			if (changeCallback !== undefined) {
				changeCallback(file);
			}
		});

		watcher.on('delete', function(file) {
			if (deleteCallback !== undefined) {
				console.log('file: ' + file);
				deleteCallback(file);
			}
		});
	},

	readExif: function (file, callback) {
		'use strict';

		callback = callback || this.prepareData.bind(this);

		exif(file, function (err, obj) {
			if (err) throw err;

			callback(obj);
		});
	},

	prepareData: function (data, callback) {
		'use strict';

		var preparedData = {},
				createDate, 
				file, 
				lastindex, 
				sizearray;

		callback = callback || this.submitMedia.bind(this);

		// Convert date into a string usable by JavaScript's new Date()
		createDate = data['create date'].split(' ');
		createDate[0] = createDate[0].replace(/[:]/g, '-');
		createDate = createDate.join('T');

		// Set up file name to be split into filename and extension
		file = data['file name'];
		lastindex = file.lastIndexOf('.');

		// Creates an array for width and height
		sizearray = (data['image size']) ? data['image size'].split('x') : [];

		preparedData.extension = file.substr(lastindex + 1);
		preparedData.filename = file.substr(0, file.length - preparedData.extension.length - 1);
		preparedData.title = preparedData.filename;
		preparedData.tags = [];
		preparedData.date = new Date(createDate);
		preparedData.description = '';
		preparedData.dimensions = data['image size'];
		preparedData.width = Number(sizearray[0]);
		preparedData.height = Number(sizearray[1]);
		preparedData.layout = (preparedData.width > preparedData.height) ? 'landscape' : 'portrait';

		if (preparedData.extension.match(/^(jpg|jpeg|png|gif)$/)) {
			preparedData.type = 'photo';
		} 
		else if (preparedData.extension.match(/^(mp4|m4v|mov|wmv|avi|mpg|ogv|3gp|3g2|webm)$/)) {
			preparedData.type = 'video';
		} 
		else {
			preparedData.type = 'not allowed';
		}

		callback(preparedData);
	},

	submitMedia: function (data, host, path, port, callback) {
		'use strict';

		var options, 
				req;

		data = JSON.stringify(data);

		host = host || 'localhost';
		path = path || '/api/v1/media';
		port = port || 8080;

		options = {
			host: host,
			path: path,
			port: port,
			method: 'POST',
			headers: {
				'Content-Type': 'application/json'
			}
		};

		req = http.request(options, function (response) {
			var str = '';
			response.on('data', function (chunk) {
				// A piece of data has been recieved
			});

			response.on('end', function () {
				// Response has been recieved
				console.log(str);
				if (callback) {
					callback(str);
				}
			});
		});

		req.write(data);
		req.end();
	},

	deleteMedia: function (file, host, path, port, callback) {
		'use strict';

		var options, 
				req,
				filename = '';

		filename = file.substring(file.lastIndexOf('/') + 1);
		filename = filename.substring(0, filename.length - 4);

		host = host || 'localhost';
		path = path || '/api/v1/media/' + filename;
		port = port || 8080;

		options = {
			host: host,
			path: path,
			port: port,
			method: 'DELETE',
			headers: {
				'Content-Type': 'application/json'
			}
		};

		http.request(options, function (response) {
			var str = '';
			response.on('data', function (chunk) {
				// A piece of data has been recieved
			});

			response.on('end', function () {
				// Response has been recieved
				console.log(str);
{'fraction_non_alphanumeric': 0.15339966832504145, 'fraction_numerical': 0.005804311774461028, 'mean_word_length': 3.415794979079498, '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': '627630', 'n_tokens_mistral': 3211, 'n_tokens_neox': 2694, 'n_words': 695}