2016-08-22 20:55:28 +03:00
|
|
|
var errors = require('../errors'),
|
|
|
|
config = require('../config'),
|
2016-08-23 00:51:42 +03:00
|
|
|
Base = require('./base'),
|
|
|
|
_ = require('lodash'),
|
2014-08-29 07:48:49 +04:00
|
|
|
storage = {};
|
2013-11-07 20:00:39 +04:00
|
|
|
|
2016-08-22 20:55:28 +03:00
|
|
|
/**
|
|
|
|
* type: images|themes
|
|
|
|
*/
|
|
|
|
function getStorage(type) {
|
|
|
|
type = type || 'images';
|
2015-01-18 06:54:56 +03:00
|
|
|
|
2016-09-13 18:41:14 +03:00
|
|
|
var storageChoice = config.get('storage').active[type],
|
2016-09-13 19:20:44 +03:00
|
|
|
storageConfig;
|
|
|
|
|
|
|
|
// CASE: we only allow local-file-storage for themes
|
|
|
|
// @TODO: https://github.com/TryGhost/Ghost/issues/7246
|
|
|
|
if (type === 'themes') {
|
|
|
|
storageChoice = 'local-file-store';
|
|
|
|
}
|
|
|
|
|
|
|
|
storageConfig = config.get('storage')[storageChoice];
|
2013-11-07 20:00:39 +04:00
|
|
|
|
2016-08-22 20:55:28 +03:00
|
|
|
// CASE: type does not exist
|
|
|
|
if (!storageChoice) {
|
|
|
|
throw new errors.IncorrectUsage('No adapter found for type: ' + type);
|
|
|
|
}
|
|
|
|
|
|
|
|
// cache?
|
2014-08-29 07:48:49 +04:00
|
|
|
if (storage[storageChoice]) {
|
|
|
|
return storage[storageChoice];
|
2013-11-07 20:00:39 +04:00
|
|
|
}
|
|
|
|
|
2016-08-22 20:55:28 +03:00
|
|
|
// CASE: load adapter from custom path (.../content/storage)
|
2013-11-07 20:00:39 +04:00
|
|
|
try {
|
2016-09-13 18:41:14 +03:00
|
|
|
storage[storageChoice] = require(config.get('paths').storagePath.custom + storageChoice);
|
2016-08-24 15:32:29 +03:00
|
|
|
} catch (err) {
|
|
|
|
// CASE: only throw error if module does exist
|
|
|
|
if (err.code !== 'MODULE_NOT_FOUND') {
|
|
|
|
throw new errors.IncorrectUsage(err.message);
|
|
|
|
}
|
|
|
|
// CASE: if module not found it can be an error within the adapter (cannot find bluebird for example)
|
2016-09-13 18:41:14 +03:00
|
|
|
else if (err.code === 'MODULE_NOT_FOUND' && err.message.indexOf(config.get('paths').storagePath.custom + storageChoice) === -1) {
|
2016-08-24 15:32:29 +03:00
|
|
|
throw new errors.IncorrectUsage(err.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: either storage[storageChoice] is already set or why check for in the default storage path
|
|
|
|
try {
|
2016-09-13 18:41:14 +03:00
|
|
|
storage[storageChoice] = storage[storageChoice] || require(config.get('paths').storagePath.default + storageChoice);
|
2016-08-24 15:32:29 +03:00
|
|
|
} catch (err) {
|
|
|
|
if (err.code === 'MODULE_NOT_FOUND') {
|
2016-09-13 18:41:14 +03:00
|
|
|
throw new errors.IncorrectUsage('We cannot find your adpter in: ' + config.get('paths').storagePath.custom + ' or: ' + config.get('paths').storagePath.default);
|
2016-08-24 15:32:29 +03:00
|
|
|
} else {
|
|
|
|
throw new errors.IncorrectUsage(err.message);
|
2016-08-22 20:55:28 +03:00
|
|
|
}
|
2013-11-07 20:00:39 +04:00
|
|
|
}
|
2014-08-29 07:48:49 +04:00
|
|
|
|
2015-01-18 06:54:56 +03:00
|
|
|
storage[storageChoice] = new storage[storageChoice](storageConfig);
|
2014-08-29 07:48:49 +04:00
|
|
|
|
2016-08-23 00:51:42 +03:00
|
|
|
if (!(storage[storageChoice] instanceof Base)) {
|
|
|
|
throw new errors.IncorrectUsage('Your storage adapter does not inherit from the Storage Base.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!storage[storageChoice].requiredFns) {
|
|
|
|
throw new errors.IncorrectUsage('Your storage adapter does not provide the minimum required functions.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_.xor(storage[storageChoice].requiredFns, Object.keys(_.pick(Object.getPrototypeOf(storage[storageChoice]), storage[storageChoice].requiredFns))).length) {
|
|
|
|
throw new errors.IncorrectUsage('Your storage adapter does not provide the minimum required functions.');
|
|
|
|
}
|
|
|
|
|
2014-08-29 07:48:49 +04:00
|
|
|
return storage[storageChoice];
|
2013-11-07 20:00:39 +04:00
|
|
|
}
|
|
|
|
|
2014-09-10 08:06:24 +04:00
|
|
|
module.exports.getStorage = getStorage;
|