From 90907640520294229ccc8f1d07c7fb611010bc20 Mon Sep 17 00:00:00 2001 From: Harry Wolff Date: Sun, 8 Dec 2013 19:49:02 -0500 Subject: [PATCH] Standardize file path access throughout ghost resolves #1390 update all string based references to file paths to use the ./core/server/config/paths file so that it is the single source of truth --- core/server/api/db.js | 7 ++++--- core/server/config/paths.js | 16 +++++++++++----- core/server/errorHandling.js | 18 ++++++++---------- core/server/index.js | 2 +- core/server/middleware/index.js | 2 +- core/server/storage/localfilesystem.js | 8 ++++---- 6 files changed, 29 insertions(+), 24 deletions(-) diff --git a/core/server/api/db.js b/core/server/api/db.js index b767a1e1c8..65096d7af1 100644 --- a/core/server/api/db.js +++ b/core/server/api/db.js @@ -8,6 +8,7 @@ var dataExport = require('../data/export'), _ = require('underscore'), schema = require('../data/schema'), config = require('../config'), + debugPath = config.paths().webroot + '/ghost/debug/', db; @@ -34,7 +35,7 @@ db = { id: 'per-' + (notifications.length + 1) }; return api.notifications.add(notification).then(function () { - res.redirect(config.paths().webroot + '/ghost/debug/'); + res.redirect(debugPath); }); }); }); @@ -59,7 +60,7 @@ db = { id: 'per-' + (notifications.length + 1) }; return api.notifications.add(notification).then(function () { - res.redirect(config.paths().webroot + '/ghost/debug/'); + res.redirect(debugPath); }); }); } @@ -153,7 +154,7 @@ db = { }; return api.notifications.add(notification).then(function () { - res.redirect(config.paths().webroot + '/ghost/debug/'); + res.redirect(debugPath); }); }); }); diff --git a/core/server/config/paths.js b/core/server/config/paths.js index bea7adcc35..65f38c2f19 100644 --- a/core/server/config/paths.js +++ b/core/server/config/paths.js @@ -6,8 +6,10 @@ var path = require('path'), url = require('url'), requireTree = require('../require-tree'), appRoot = path.resolve(__dirname, '../../../'), - themePath = path.resolve(appRoot + '/content/themes'), - pluginPath = path.resolve(appRoot + '/content/plugins'), + corePath = path.resolve(appRoot, 'core/'), + contentPath = path.resolve(appRoot, 'content/'), + themePath = path.resolve(contentPath + '/themes'), + pluginPath = path.resolve(contentPath + '/plugins'), themeDirectories = requireTree(themePath), pluginDirectories = requireTree(pluginPath), localPath = '', @@ -23,11 +25,15 @@ function getPaths() { 'webroot': localPath === '/' ? '' : localPath, 'config': path.join(appRoot, 'config.js'), 'configExample': path.join(appRoot, 'config.example.js'), + 'contentPath': contentPath, + 'corePath': corePath, 'themePath': themePath, 'pluginPath': pluginPath, - 'adminViews': path.join(appRoot, '/core/server/views/'), - 'helperTemplates': path.join(appRoot, '/core/server/helpers/tpl/'), - 'lang': path.join(appRoot, '/core/shared/lang/'), + 'imagesPath': path.resolve(contentPath, 'images/'), + 'imagesRelPath': 'content/images', + 'adminViews': path.join(corePath, '/server/views/'), + 'helperTemplates': path.join(corePath, '/server/helpers/tpl/'), + 'lang': path.join(corePath, '/shared/lang/'), 'availableThemes': availableThemes, 'availablePlugins': availablePlugins }; diff --git a/core/server/errorHandling.js b/core/server/errorHandling.js index df1c20fb90..6c5a52c8df 100644 --- a/core/server/errorHandling.js +++ b/core/server/errorHandling.js @@ -1,16 +1,14 @@ /*jslint regexp: true */ -var _ = require('underscore'), - colors = require('colors'), - fs = require('fs'), - path = require('path'), +var _ = require('underscore'), + colors = require('colors'), + fs = require('fs'), + configPaths = require('./config/paths'), + path = require('path'), errors, // Paths for views - appRoot = path.resolve(__dirname, '..', '..'), - themePath = path.resolve(appRoot, 'content', 'themes'), - adminTemplatePath = path.resolve(appRoot, 'core', 'server', 'views'), - defaultErrorTemplatePath = path.resolve(adminTemplatePath, 'user-error.hbs'), - userErrorTemplatePath = path.resolve(themePath, 'error.hbs'), + defaultErrorTemplatePath = path.resolve(configPaths().adminViews, 'user-error.hbs'), + userErrorTemplatePath = path.resolve(configPaths().themePath, 'error.hbs'), userErrorTemplateExists; /** @@ -18,7 +16,7 @@ var _ = require('underscore'), */ errors = { updateActiveTheme: function (activeTheme) { - userErrorTemplatePath = path.resolve(themePath, activeTheme, 'error.hbs'); + userErrorTemplatePath = path.resolve(configPaths().themePath, activeTheme, 'error.hbs'); userErrorTemplateExists = undefined; }, diff --git a/core/server/index.js b/core/server/index.js index c18952ecd5..c14a8ee579 100644 --- a/core/server/index.js +++ b/core/server/index.js @@ -150,7 +150,7 @@ function setup(server) { // Are we using sockets? Custom socket or the default? function getSocket() { if (config().server.hasOwnProperty('socket')) { - return _.isString(config().server.socket) ? config().server.socket : path.join(__dirname, '../content/', process.env.NODE_ENV + '.socket'); + return _.isString(config().server.socket) ? config().server.socket : path.join(config.path().contentPath, process.env.NODE_ENV + '.socket'); } return false; } diff --git a/core/server/middleware/index.js b/core/server/middleware/index.js index 2b1e3843a2..4c0ae16422 100644 --- a/core/server/middleware/index.js +++ b/core/server/middleware/index.js @@ -164,7 +164,7 @@ function redirectToSignup(req, res, next) { module.exports = function (server, dbHash) { var oneYear = 31536000000, root = config.paths().webroot, - corePath = path.join(config.paths().appRoot, 'core'); + corePath = config.paths().corePath; // Cache express server instance expressServer = server; diff --git a/core/server/storage/localfilesystem.js b/core/server/storage/localfilesystem.js index 9b5bfd6521..4819bb844a 100644 --- a/core/server/storage/localfilesystem.js +++ b/core/server/storage/localfilesystem.js @@ -8,10 +8,10 @@ var _ = require('underscore'), path = require('path'), when = require('when'), errors = require('../errorHandling'), + config = require('../config'), baseStore = require('./base'), - localFileStore, - localDir = 'content/images'; + localFileStore; localFileStore = _.extend(baseStore, { // ### Save @@ -20,7 +20,7 @@ localFileStore = _.extend(baseStore, { // - returns a promise which ultimately returns the full url to the uploaded image 'save': function (image) { var saved = when.defer(), - targetDir = this.getTargetDir(localDir); + targetDir = this.getTargetDir(config.paths().imagesRelPath); this.getUniqueFileName(this, image, targetDir).then(function (filename) { nodefn.call(fs.mkdirs, targetDir).then(function () { @@ -55,7 +55,7 @@ localFileStore = _.extend(baseStore, { // middleware for serving the files 'serve': function () { - return express['static'](path.join(__dirname, '/../../../', localDir)); + return express['static'](config.paths().imagesPath); } });