62 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-02-20 15:57:34 +01:00
'use strict';
/**
* An asynchronous bootstrap function that runs before
* your application gets started.
*
* This gives you an opportunity to set up your data model,
* run jobs, or perform some special logic.
*/
const path = require('path');
const _ = require('lodash');
const fs = require('fs');
module.exports = async cb => {
2018-02-21 17:18:33 +01:00
// set plugin store
2018-02-20 15:57:34 +01:00
const pluginStore = strapi.store({
environment: strapi.config.environment,
type: 'plugin',
name: 'upload'
});
2018-02-23 14:57:58 +01:00
fs.readdir(path.join(strapi.config.appPath, 'plugins', 'upload', 'node_modules'), async (err, node_modules) => {
2018-02-21 17:18:33 +01:00
// get all upload provider
2018-02-20 15:57:34 +01:00
const uploads = _.filter(node_modules, (node_module) => {
2018-02-23 14:57:58 +01:00
return _.startsWith(node_module, ('strapi-upload'));
2018-02-20 15:57:34 +01:00
});
strapi.plugins.upload.config.providers = [];
2018-02-22 15:41:24 +01:00
// mount all providers to get configs
2018-02-20 15:57:34 +01:00
_.forEach(uploads, (node_module) => {
strapi.plugins.upload.config.providers.push(
2018-02-23 14:57:58 +01:00
require(path.join(`${strapi.config.appPath}/plugins/upload/node_modules/${node_module}`))
2018-02-20 15:57:34 +01:00
);
});
try {
2018-02-23 14:57:58 +01:00
// if provider config not exit set one by default
2018-02-20 15:57:34 +01:00
const config = await pluginStore.get({key: 'provider'});
if (!config) {
2018-02-23 14:57:58 +01:00
const provider = _.find(strapi.plugins.upload.config.providers, {provider: 'local'});
2018-02-20 15:57:34 +01:00
2018-02-21 14:23:12 +01:00
const value = _.assign({}, provider, {
2018-02-21 14:46:10 +01:00
enabled: true,
2018-02-22 15:41:24 +01:00
// by default limit size to 1 GB
2018-02-21 14:46:10 +01:00
sizeLimit: 1000000
2018-02-21 14:23:12 +01:00
});
2018-02-20 15:57:34 +01:00
await pluginStore.set({key: 'provider', value});
}
} catch (err) {
2018-02-23 14:57:58 +01:00
strapi.log.error(`Can't load ${config.provider} upload provider.`);
strapi.log.warn(`Please install strapi-upload-${config.provider} --save in ${path.join(strapi.config.appPath, 'plugins', 'upload')} folder.`);
2018-02-20 15:57:34 +01:00
strapi.stop();
}
cb();
});
};