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-08-22 20:55:28 +03:00
|
|
|
var storageChoice = config.storage.active[type],
|
|
|
|
storageConfig = config.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)
|
|
|
|
// CASE: load adapter from default path (.../server/storage)
|
2013-11-07 20:00:39 +04:00
|
|
|
try {
|
2016-08-22 20:55:28 +03:00
|
|
|
storage[storageChoice] = require(config.paths.storagePath.custom + storageChoice);
|
|
|
|
} catch (err1) {
|
|
|
|
try {
|
|
|
|
storage[storageChoice] = require(config.paths.storagePath.default + storageChoice);
|
|
|
|
} catch (err2) {
|
|
|
|
throw err2;
|
|
|
|
}
|
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;
|