diff --git a/core/server/config/index.js b/core/server/config/index.js index a6be1155e0..b7744030f3 100644 --- a/core/server/config/index.js +++ b/core/server/config/index.js @@ -108,7 +108,10 @@ function configureDriver(client) { */ ConfigManager.prototype.set = function (config) { var localPath = '', + defaultStorage = 'local-file-store', contentPath, + activeStorage, + storagePath, subdir, assetHash; @@ -146,6 +149,18 @@ ConfigManager.prototype.set = function (config) { knexInstance = knex(this._config.database); } + // Protect against accessing a non-existant object. + // This ensures there's always at least a storage object + // because it's referenced in multiple places. + this._config.storage = this._config.storage || {}; + activeStorage = this._config.storage.active || defaultStorage; + + if (activeStorage === defaultStorage) { + storagePath = path.join(corePath, '/server/storage/'); + } else { + storagePath = path.join(contentPath, 'storage'); + } + _.merge(this._config, { database: { knex: knexInstance @@ -158,6 +173,8 @@ ConfigManager.prototype.set = function (config) { configExample: path.join(appRoot, 'config.example.js'), corePath: corePath, + storage: path.join(storagePath, activeStorage), + contentPath: contentPath, themePath: path.resolve(contentPath, 'themes'), appPath: path.resolve(contentPath, 'apps'), @@ -173,6 +190,9 @@ ConfigManager.prototype.set = function (config) { availableApps: this._config.paths.availableApps || {}, builtScriptPath: path.join(corePath, 'built/scripts/') }, + storage: { + active: activeStorage + }, theme: { // normalise the URL by removing any trailing slash url: this._config.url ? this._config.url.replace(/\/$/, '') : '' diff --git a/core/server/storage/index.js b/core/server/storage/index.js index 6f8cf211a0..4827d084e4 100644 --- a/core/server/storage/index.js +++ b/core/server/storage/index.js @@ -1,10 +1,14 @@ var errors = require('../errors'), + config = require('../config'), storage = {}; function getStorage(storageChoice) { - // TODO: this is where the check for storage apps should go - // Local file system is the default. Fow now that is all we support. - storageChoice = 'local-file-store'; + var storagePath, + storageConfig; + + storageChoice = config.storage.active; + storagePath = config.paths.storage; + storageConfig = config.storage[storageChoice]; if (storage[storageChoice]) { return storage[storageChoice]; @@ -12,13 +16,13 @@ function getStorage(storageChoice) { try { // TODO: determine if storage has all the necessary methods. - storage[storageChoice] = require('./' + storageChoice); + storage[storageChoice] = require(storagePath); } catch (e) { errors.logError(e); } // Instantiate and cache the storage module instance. - storage[storageChoice] = new storage[storageChoice](); + storage[storageChoice] = new storage[storageChoice](storageConfig); return storage[storageChoice]; } diff --git a/core/test/unit/config_spec.js b/core/test/unit/config_spec.js index 50d8717865..21b23388e9 100644 --- a/core/test/unit/config_spec.js +++ b/core/test/unit/config_spec.js @@ -82,6 +82,7 @@ describe('Config', function () { 'subdir', 'config', 'configExample', + 'storage', 'contentPath', 'corePath', 'themePath', @@ -147,6 +148,34 @@ describe('Config', function () { }); }); + describe('Storage', function () { + afterEach(function () { + resetConfig(); + }); + + it('should default to local-file-store', function () { + var storagePath = path.join(config.paths.corePath, '/server/storage/', 'local-file-store'); + + config.paths.should.have.property('storage', storagePath); + config.storage.should.have.property('active', 'local-file-store'); + }); + + it('should allow setting a custom active storage', function () { + var storagePath = path.join(config.paths.contentPath, 'storage', 's3'); + + config.set({ + storage: { + active: 's3', + s3: {} + } + }); + + config.paths.should.have.property('storage', storagePath); + config.storage.should.have.property('active', 's3'); + config.storage.should.have.property('s3', {}); + }); + }); + describe('urlFor', function () { before(function () { resetConfig();