From e069150625705651a36227e568d8a3a526a59e22 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Tue, 23 Feb 2016 16:06:02 -0500 Subject: [PATCH] closes #2165 --- public/src/admin/settings.js | 7 +- src/controllers/admin/uploads.js | 29 ++++++++ src/meta/sounds.js | 111 ++++++++++++++++------------ src/routes/admin.js | 1 + src/views/admin/general/sounds.tpl | 112 +++++++++++++++++------------ 5 files changed, 169 insertions(+), 91 deletions(-) diff --git a/public/src/admin/settings.js b/public/src/admin/settings.js index 117857d5ec..273b7be0a3 100644 --- a/public/src/admin/settings.js +++ b/public/src/admin/settings.js @@ -116,7 +116,12 @@ define('admin/settings', ['uploader', 'sounds'], function(uploader, sounds) { fileSize: 0, showHelp: uploadBtn.attr('data-help') ? uploadBtn.attr('data-help') === 1 : undefined }, function(image) { - $('#' + uploadBtn.attr('data-target')).val(image); + // need to move these into template, ex data-callback + if (ajaxify.currentPage === 'admin/general/sounds') { + ajaxify.refresh(); + } else { + $('#' + uploadBtn.attr('data-target')).val(image); + } }); }); }); diff --git a/src/controllers/admin/uploads.js b/src/controllers/admin/uploads.js index eb6a4a59f4..5f582bc117 100644 --- a/src/controllers/admin/uploads.js +++ b/src/controllers/admin/uploads.js @@ -93,6 +93,33 @@ uploadsController.uploadLogo = function(req, res, next) { upload('site-logo', req, res, next); }; +uploadsController.uploadSound = function(req, res, next) { + var uploadedFile = req.files.files[0]; + + file.saveFileToLocal(uploadedFile.name, 'sounds', uploadedFile.path, function(err) { + if (err) { + return next(err); + } + + var soundsPath = path.join(__dirname, '../../../public/sounds'), + filePath = path.join(__dirname, '../../../public/uploads/sounds', uploadedFile.name); + + if (process.platform === 'win32') { + fs.link(filePath, path.join(soundsPath, path.basename(filePath))); + } else { + fs.symlink(filePath, path.join(soundsPath, path.basename(filePath)), 'file'); + } + + fs.unlink(uploadedFile.path, function(err) { + if (err) { + return next(err); + } + + res.json([{}]); + }); + }); +}; + uploadsController.uploadDefaultAvatar = function(req, res, next) { upload('avatar-default', req, res, next); }; @@ -131,7 +158,9 @@ function uploadImage(filename, folder, uploadedFile, req, res, next) { if (err) { return next(err); } + res.json([{name: uploadedFile.name, url: image.url.startsWith('http') ? image.url : nconf.get('relative_path') + image.url}]); + next(); } if (plugins.hasListeners('filter:uploadImage')) { diff --git a/src/meta/sounds.js b/src/meta/sounds.js index 30f1f9e165..d13ecd70cd 100644 --- a/src/meta/sounds.js +++ b/src/meta/sounds.js @@ -17,48 +17,7 @@ module.exports = function(Meta) { Meta.sounds.init = function(callback) { if (nconf.get('isPrimary') === 'true') { - var soundsPath = path.join(__dirname, '../../public/sounds'); - - plugins.fireHook('filter:sounds.get', [], 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(); - } - }); - }); - }); + setupSounds(callback); } else { if (typeof callback === 'function') { callback(); @@ -67,8 +26,16 @@ module.exports = function(Meta) { }; Meta.sounds.getFiles = function(callback) { - // todo: Possibly move these into a bundled module? - fs.readdir(path.join(__dirname, '../../public/sounds'), function(err, files) { + 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 = {}; if (err) { @@ -102,4 +69,60 @@ module.exports = function(Meta) { 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); + } }; \ No newline at end of file diff --git a/src/routes/admin.js b/src/routes/admin.js index e5121ac0f9..9424a1cda3 100644 --- a/src/routes/admin.js +++ b/src/routes/admin.js @@ -15,6 +15,7 @@ function apiRoutes(router, middleware, controllers) { router.post('/uploadfavicon', middlewares, controllers.admin.uploads.uploadFavicon); router.post('/uploadTouchIcon', middlewares, controllers.admin.uploads.uploadTouchIcon); router.post('/uploadlogo', middlewares, controllers.admin.uploads.uploadLogo); + router.post('/upload/sound', middlewares, controllers.admin.uploads.uploadSound); router.post('/uploadDefaultAvatar', middlewares, controllers.admin.uploads.uploadDefaultAvatar); } diff --git a/src/views/admin/general/sounds.tpl b/src/views/admin/general/sounds.tpl index 13156c9ce4..b0e4ce071e 100644 --- a/src/views/admin/general/sounds.tpl +++ b/src/views/admin/general/sounds.tpl @@ -1,62 +1,82 @@
-
-
-
Notifications
-
- -
-
- -
-
- +
+ +
+
Notifications
+
+ +
+
+ +
+
+ +
-
-
-
Chat Messages
-
- -
-
- +
+
Chat Messages
+
+ +
+
+ +
+
+ +
-
- + + +
+
+ +
+
+ +
+
+ +
- -
-
- -
-
- -
+
+
+
+
+ + +
- +
\ No newline at end of file + + + \ No newline at end of file