all files / src/meta/ sounds.js

12.96% Statements 7/54
0% Branches 0/20
4.76% Functions 1/21
12.96% Lines 7/54
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133                                                                                                                                                                                                                                                           
'use strict';
 
var path = require('path'),
	fs = require('fs'),
	nconf = require('nconf'),
	winston = require('winston'),
	rimraf = require('rimraf'),
	mkdirp = require('mkdirp'),
	async = require('async'),
 
	plugins = require('../plugins'),
	db = require('../database');
 
module.exports = function(Meta) {
 
	Meta.sounds = {};
 
	Meta.sounds.init = function(callback) {
		if (nconf.get('isPrimary') === 'true') {
			setupSounds(callback);
		} else {
			if (typeof callback === 'function') {
				callback();
			}
		}
	};
 
	Meta.sounds.getFiles = function(callback) {
		async.waterfall([
			function(next) {
				fs.readdir(path.join(__dirname, '../../public/sounds'), next);
			},
			function(sounds, next) {
				fs.readdir(path.join(__dirname, '../../public/uploads/sounds'), function(err, uploaded) {
					next(err, sounds.concat(uploaded));
				});
			}
		], function(err, files) {
			var	localList = {};
 
			// Filter out hidden files
			files = files.filter(function(filename) {
				return !filename.startsWith('.');
			});
 
			if (err) {
				winston.error('Could not get local sound files:' + err.message);
				console.log(err.stack);
				return callback(null, []);
			}
 
			// Return proper paths
			files.forEach(function(filename) {
				localList[filename] = nconf.get('relative_path') + '/sounds/' + filename;
			});
 
			callback(null, localList);
		});
	};
 
	Meta.sounds.getMapping = function(callback) {
		db.getObject('settings:sounds', function(err, sounds) {
			if (err || !sounds) {
				// Send default sounds
				var	defaults = {
						'notification': 'notification.mp3',
						'chat-incoming': 'waterdrop-high.mp3',
						'chat-outgoing': undefined
					};
 
				return callback(null, defaults);
			}
 
			callback(null, sounds);
		});
	};
 
	function setupSounds(callback) {
		var	soundsPath = path.join(__dirname, '../../public/sounds');
 
		async.waterfall([
			function(next) {
				fs.readdir(path.join(__dirname, '../../public/uploads/sounds'), next);
			},
			function(uploaded, next) {
				uploaded = uploaded.map(function(filename) {
					return path.join(__dirname, '../../public/uploads/sounds', filename);
				});
 
				plugins.fireHook('filter:sounds.get', uploaded, function(err, filePaths) {
					if (err) {
						winston.error('Could not initialise sound files:' + err.message);
						return;
					}
 
					// Clear the sounds directory
					async.series([
						function(next) {
							rimraf(soundsPath, next);
						},
						function(next) {
							mkdirp(soundsPath, next);
						}
					], function(err) {
						if (err) {
							winston.error('Could not initialise sound files:' + err.message);
							return;
						}
 
						// Link paths
						async.each(filePaths, function(filePath, next) {
							if (process.platform === 'win32') {
								fs.link(filePath, path.join(soundsPath, path.basename(filePath)), next);
							} else {
								fs.symlink(filePath, path.join(soundsPath, path.basename(filePath)), 'file', next);
							}
						}, function(err) {
							if (!err) {
								winston.verbose('[sounds] Sounds OK');
							} else {
								winston.error('[sounds] Could not initialise sounds: ' + err.message);
							}
 
							if (typeof callback === 'function') {
								callback();
							}
						});
					});
				});
			}
		], callback);
	}
};