mirror of
				https://github.com/strapi/strapi.git
				synced 2025-10-31 18:08:11 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			130 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			130 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| 'use strict';
 | |
| 
 | |
| const _ = require('lodash');
 | |
| 
 | |
| module.exports = async ({ strapi }) => {
 | |
|   // set plugin store
 | |
|   const configurator = strapi.store({ type: 'plugin', name: 'upload', key: 'settings' });
 | |
| 
 | |
|   strapi.plugin('upload').provider = createProvider(strapi.config.get('plugin.upload', {}));
 | |
| 
 | |
|   // if provider config does not exist set one by default
 | |
|   const config = await configurator.get();
 | |
| 
 | |
|   if (!config) {
 | |
|     await configurator.set({
 | |
|       value: {
 | |
|         sizeOptimization: true,
 | |
|         responsiveDimensions: true,
 | |
|         autoOrientation: false,
 | |
|       },
 | |
|     });
 | |
|   }
 | |
| 
 | |
|   await registerPermissionActions();
 | |
| };
 | |
| 
 | |
| const createProvider = config => {
 | |
|   const { providerOptions, actionOptions = {} } = config;
 | |
| 
 | |
|   const providerName = _.toLower(config.provider);
 | |
|   let provider;
 | |
| 
 | |
|   let modulePath;
 | |
|   try {
 | |
|     modulePath = require.resolve(`@strapi/provider-upload-${providerName}`);
 | |
|   } catch (error) {
 | |
|     if (error.code === 'MODULE_NOT_FOUND') {
 | |
|       modulePath = providerName;
 | |
|     } else {
 | |
|       throw error;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   try {
 | |
|     provider = require(modulePath);
 | |
|   } catch (err) {
 | |
|     throw new Error(`Could not load upload provider "${providerName}".`);
 | |
|   }
 | |
| 
 | |
|   const providerInstance = provider.init(providerOptions);
 | |
| 
 | |
|   if (!providerInstance.delete) {
 | |
|     throw new Error(`The upload provider "${providerName}" doesn't implement the delete method.`);
 | |
|   }
 | |
| 
 | |
|   if (!providerInstance.upload && !providerInstance.uploadStream) {
 | |
|     throw new Error(
 | |
|       `The upload provider "${providerName}" doesn't implement the uploadStream nor the upload method.`
 | |
|     );
 | |
|   }
 | |
| 
 | |
|   if (!providerInstance.uploadStream) {
 | |
|     process.emitWarning(
 | |
|       `The upload provider "${providerName}" doesn't implement the uploadStream function. Strapi will fallback on the upload method. Some performance issues may occur.`
 | |
|     );
 | |
|   }
 | |
| 
 | |
|   const wrappedProvider = _.mapValues(providerInstance, (method, methodName) => {
 | |
|     return async function(file, options = actionOptions[methodName]) {
 | |
|       return providerInstance[methodName](file, options);
 | |
|     };
 | |
|   });
 | |
| 
 | |
|   return Object.assign(Object.create(baseProvider), wrappedProvider);
 | |
| };
 | |
| 
 | |
| const baseProvider = {
 | |
|   extend(obj) {
 | |
|     Object.assign(this, obj);
 | |
|   },
 | |
| };
 | |
| 
 | |
| const registerPermissionActions = async () => {
 | |
|   const actions = [
 | |
|     {
 | |
|       section: 'plugins',
 | |
|       displayName: 'Access the Media Library',
 | |
|       uid: 'read',
 | |
|       pluginName: 'upload',
 | |
|     },
 | |
|     {
 | |
|       section: 'plugins',
 | |
|       displayName: 'Create (upload)',
 | |
|       uid: 'assets.create',
 | |
|       subCategory: 'assets',
 | |
|       pluginName: 'upload',
 | |
|     },
 | |
|     {
 | |
|       section: 'plugins',
 | |
|       displayName: 'Update (crop, details, replace) + delete',
 | |
|       uid: 'assets.update',
 | |
|       subCategory: 'assets',
 | |
|       pluginName: 'upload',
 | |
|     },
 | |
|     {
 | |
|       section: 'plugins',
 | |
|       displayName: 'Download',
 | |
|       uid: 'assets.download',
 | |
|       subCategory: 'assets',
 | |
|       pluginName: 'upload',
 | |
|     },
 | |
|     {
 | |
|       section: 'plugins',
 | |
|       displayName: 'Copy link',
 | |
|       uid: 'assets.copy-link',
 | |
|       subCategory: 'assets',
 | |
|       pluginName: 'upload',
 | |
|     },
 | |
|     {
 | |
|       section: 'settings',
 | |
|       displayName: 'Access the Media Library settings page',
 | |
|       uid: 'settings.read',
 | |
|       category: 'media library',
 | |
|       pluginName: 'upload',
 | |
|     },
 | |
|   ];
 | |
| 
 | |
|   await strapi.admin.services.permission.actionProvider.registerMany(actions);
 | |
| };
 | 
