0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Initial implementation for custom storage engines

closes #4600
- implemented as suggested in #4600
- loads a custom storage defined in config from the /content/storage directory
This commit is contained in:
Blaine Bublitz 2015-01-17 20:54:56 -07:00
parent e07ec3ef84
commit 5c640e95f5
3 changed files with 58 additions and 5 deletions

View file

@ -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(/\/$/, '') : ''

View file

@ -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];
}

View file

@ -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();