Merge remote-tracking branch 'origin/master' into webserver.js-refactor

This commit is contained in:
psychobunny
2014-02-28 16:42:05 -05:00
8 changed files with 94 additions and 56 deletions

View File

@@ -1,35 +1,45 @@
var db = require('./../database'),
'use strict';
var async = require('async'),
db = require('./../database'),
utils = require('./../../public/src/utils'),
categories = require('./../categories');
(function(CategoriesAdmin) {
CategoriesAdmin.update = function(modified, socket) {
var updated = [];
CategoriesAdmin.update = function(modified, socket, callback) {
for (var cid in modified) {
function updateCategory(cid, next) {
var category = modified[cid];
var fields = Object.keys(category);
for (var key in category) {
db.setObjectField('category:' + cid, key, category[key]);
if (key === 'name') {
// reset slugs if name is updated
var slug = cid + '/' + utils.slugify(category[key]);
db.setObjectField('category:' + cid, 'slug', slug);
} else if (key === 'order') {
db.sortedSetAdd('categories:cid', category[key], cid);
}
}
updated.push(cid);
async.each(fields, function(key, next) {
updateCategoryField(cid, key, category[key], next);
}, next);
}
socket.emit('event:alert', {
title: 'Updated Categories',
message: 'Category IDs ' + updated.join(', ') + ' was successfully updated.',
type: 'success',
timeout: 2000
function updateCategoryField(cid, key, value, next) {
db.setObjectField('category:' + cid, key, value, function(err) {
if(err) {
return next(err);
}
if (key === 'name') {
var slug = cid + '/' + utils.slugify(value);
db.setObjectField('category:' + cid, 'slug', slug, next);
} else if (key === 'order') {
db.sortedSetAdd('categories:cid', value, cid, next);
} else {
next();
}
});
}
var cids = Object.keys(modified);
async.each(cids, updateCategory, function(err) {
callback(err, cids);
});
};

View File

@@ -174,7 +174,7 @@ var fs = require('fs'),
(function(staticDir) {
fs.exists(staticDir, function(exists) {
if (exists) {
Plugins.staticDirs[mappedPath] = staticDir;
Plugins.staticDirs[path.join(pluginData.id, mappedPath)] = staticDir;
} else {
winston.warn('[plugins/' + pluginData.id + '] Mapped path \'' + mappedPath + ' => ' + staticDir + '\' not found.');
}

View File

@@ -4,6 +4,8 @@ var nconf = require('nconf'),
path = require('path'),
fs = require('fs'),
validator = require('validator'),
_ = require('underscore'),
async = require('async'),
plugins = require('../plugins'),
PluginRoutes = function(app) {
@@ -31,16 +33,34 @@ var nconf = require('nconf'),
// Static Assets
app.get('/plugins/:id/*', function(req, res) {
var relPath = req._parsedUrl.pathname.replace(nconf.get('relative_path') + '/plugins/' + req.params.id, '');
var relPath = req._parsedUrl.pathname.replace(nconf.get('relative_path') + '/plugins/', ''),
matches = _.map(plugins.staticDirs, function(realPath, mappedPath) {
if (relPath.match(mappedPath)) {
return mappedPath;
} else {
return null;
}
}).filter(function(a) { return a; });
if (plugins.staticDirs[req.params.id]) {
var fullPath = path.join(plugins.staticDirs[req.params.id], decodeURIComponent(relPath));
if (matches) {
async.map(matches, function(mappedPath, next) {
var filePath = path.join(plugins.staticDirs[mappedPath], relPath.slice(mappedPath.length));
fs.exists(fullPath, function(exists) {
if (exists) {
res.sendfile(fullPath, {
maxAge: app.enabled('cache') ? 5184000000 : 0
});
fs.exists(filePath, function(exists) {
if (exists) {
next(null, filePath);
} else {
next();
}
});
}, function(err, matches) {
// Filter out the nulls
matches = matches.filter(function(a) {
return a;
});
if (matches.length) {
res.sendfile(matches[0]);
} else {
res.redirect('/404');
}

View File

@@ -118,12 +118,12 @@ SocketAdmin.categories.create = function(socket, data, callback) {
categories.create(data, callback);
};
SocketAdmin.categories.update = function(socket, data) {
SocketAdmin.categories.update = function(socket, data, callback) {
if(!data) {
throw new Error('invalid data');
return callback(new Error('invalid data'));
}
admin.categories.update(data, socket);
admin.categories.update(data, socket, callback);
};
SocketAdmin.categories.search = function(socket, data, callback) {