diff --git a/packages/strapi-plugin-upload/config/functions/__tests__/bootstrap.test.js b/packages/strapi-plugin-upload/config/functions/__tests__/bootstrap.test.js new file mode 100644 index 0000000000..c81914275c --- /dev/null +++ b/packages/strapi-plugin-upload/config/functions/__tests__/bootstrap.test.js @@ -0,0 +1,36 @@ +const bootstrap = require('../bootstrap'); + +describe('Upload plugin bootstrap function', () => { + test('Sets default config if id does not exist', async () => { + const setStore = jest.fn(() => {}); + + const strapi = { + config: { + info: { + dependencies: {}, + }, + }, + plugins: { + upload: { config: {} }, + }, + store() { + return { + get() { + return null; + }, + set: setStore, + }; + }, + }; + + await bootstrap(strapi); + + expect(setStore).toHaveBeenCalledWith({ + value: { + sizeOptimization: true, + videoPreview: true, + responsiveDimensions: true, + }, + }); + }); +}); diff --git a/packages/strapi-plugin-upload/config/functions/bootstrap.js b/packages/strapi-plugin-upload/config/functions/bootstrap.js index 307eced151..fc5c7b9386 100644 --- a/packages/strapi-plugin-upload/config/functions/bootstrap.js +++ b/packages/strapi-plugin-upload/config/functions/bootstrap.js @@ -9,15 +9,20 @@ */ const _ = require('lodash'); -module.exports = async () => { +module.exports = async strapi => { // set plugin store - const pluginStore = strapi.store({ - environment: strapi.config.environment, + const configurator = strapi.store({ type: 'plugin', name: 'upload', + key: 'settings', }); - strapi.plugins.upload.config.providers = []; + Object.assign(strapi.plugins.upload.config, { + enabled: true, + provider: 'local', + sizeLimit: 1000000, + providers: [], + }); const installedProviders = Object.keys(strapi.config.info.dependencies) .filter(d => d.includes('strapi-provider-upload-')) @@ -28,19 +33,15 @@ module.exports = async () => { } // if provider config does not exist set one by default - const config = await pluginStore.get({ key: 'provider' }); + const config = await configurator.get(); if (!config) { - const provider = _.find(strapi.plugins.upload.config.providers, { - provider: 'local', + await configurator.set({ + value: { + sizeOptimization: true, + responsiveDimensions: true, + videoPreview: true, + }, }); - - const value = _.assign({}, provider, { - enabled: true, - // by default limit size to 1 GB - sizeLimit: 1000000, - }); - - await pluginStore.set({ key: 'provider', value }); } }; diff --git a/packages/strapi-plugin-upload/config/routes.json b/packages/strapi-plugin-upload/config/routes.json index 6f8962eb2e..07af8d59e1 100644 --- a/packages/strapi-plugin-upload/config/routes.json +++ b/packages/strapi-plugin-upload/config/routes.json @@ -15,15 +15,7 @@ }, { "method": "GET", - "path": "/environments", - "handler": "Upload.getEnvironments", - "config": { - "policies": [] - } - }, - { - "method": "GET", - "path": "/settings/:environment", + "path": "/settings", "handler": "Upload.getSettings", "config": { "policies": [] @@ -31,7 +23,7 @@ }, { "method": "PUT", - "path": "/settings/:environment", + "path": "/settings", "handler": "Upload.updateSettings", "config": { "policies": [] diff --git a/packages/strapi-plugin-upload/config/schema.graphql b/packages/strapi-plugin-upload/config/schema.graphql index 8db4d8ed05..89fa6c1cdc 100644 --- a/packages/strapi-plugin-upload/config/schema.graphql +++ b/packages/strapi-plugin-upload/config/schema.graphql @@ -40,13 +40,7 @@ module.exports = { resolver: async (obj, { file: upload, ...fields }) => { const file = await formatFile(upload, fields); - const config = await strapi - .store({ - environment: strapi.config.environment, - type: 'plugin', - name: 'upload', - }) - .get({ key: 'provider' }); + const config = strapi.plugins.upload.config; const uploadedFiles = await strapi.plugins.upload.services.upload.upload( [file], @@ -66,13 +60,7 @@ module.exports = { uploads.map(upload => formatFile(upload, fields)) ); - const config = await strapi - .store({ - environment: strapi.config.environment, - type: 'plugin', - name: 'upload', - }) - .get({ key: 'provider' }); + const config = strapi.plugins.upload.config; const uploadedFiles = await strapi.plugins.upload.services.upload.upload( files, diff --git a/packages/strapi-plugin-upload/controllers/Upload.js b/packages/strapi-plugin-upload/controllers/Upload.js index 54e1bd3e65..ce5ca1c184 100644 --- a/packages/strapi-plugin-upload/controllers/Upload.js +++ b/packages/strapi-plugin-upload/controllers/Upload.js @@ -3,17 +3,17 @@ /** * Upload.js controller * - * @description: A set of functions called "actions" of the `upload` plugin. */ const _ = require('lodash'); +const validateSettings = require('./validation/settings'); module.exports = { async upload(ctx) { const uploadService = strapi.plugins.upload.services.upload; // Retrieve provider configuration. - const config = await uploadService.getConfig(); + const config = strapi.plugins.upload.config; // Verify if the file upload is enable. if (config.enabled === false) { @@ -98,48 +98,32 @@ module.exports = { ctx.send(uploadedFiles); }, - async getEnvironments(ctx) { - const environments = Object.keys(strapi.config.environments).map( - environment => ({ - name: environment, - active: strapi.config.environment === environment, - }) - ); - - ctx.send({ environments }); - }, - async getSettings(ctx) { const config = await strapi .store({ - environment: ctx.params.environment, type: 'plugin', name: 'upload', + key: 'settings', }) - .get({ key: 'provider' }); + .get(); ctx.send({ - providers: strapi.plugins.upload.config.providers, - config, + data: config, }); }, async updateSettings(ctx) { - const { - request: { body: newSettings }, - } = ctx; - await strapi - .store({ - environment: ctx.params.environment, - type: 'plugin', - name: 'upload', - }) - .set({ - key: 'provider', - value: { ...newSettings, sizeLimit: parseFloat(newSettings.sizeLimit) }, - }); + const configurator = strapi.store({ + type: 'plugin', + name: 'upload', + key: 'settings', + }); - ctx.send({ ok: true }); + const data = await validateSettings(ctx.request.body); + + await configurator.set({ key: 'settings', value: data }); + + ctx.body = { data }; }, async find(ctx) { @@ -173,13 +157,7 @@ module.exports = { async destroy(ctx) { const { id } = ctx.params; - const config = await strapi - .store({ - environment: strapi.config.environment, - type: 'plugin', - name: 'upload', - }) - .get({ key: 'provider' }); + const config = strapi.plugins.upload.config; const file = await strapi.plugins['upload'].services.upload.fetch({ id }); diff --git a/packages/strapi-plugin-upload/controllers/validation/settings.js b/packages/strapi-plugin-upload/controllers/validation/settings.js new file mode 100644 index 0000000000..642089eb72 --- /dev/null +++ b/packages/strapi-plugin-upload/controllers/validation/settings.js @@ -0,0 +1,23 @@ +'use strict'; + +const { yup, formatYupErrors } = require('strapi-utils'); + +const settingsSchema = yup.object({ + sizeOptimization: yup.boolean().required(), + responsiveDimensions: yup.boolean().required(), + videoPreview: yup.boolean().required(), +}); + +const validateSettings = data => { + return settingsSchema + .validate(data, { + abortEarly: false, + }) + .catch(error => { + throw strapi.errors.badRequest('ValidationError', { + errors: formatYupErrors(error), + }); + }); +}; + +module.exports = validateSettings; diff --git a/packages/strapi-plugin-upload/services/Upload.js b/packages/strapi-plugin-upload/services/Upload.js index 822d5ae312..2ca9e690aa 100644 --- a/packages/strapi-plugin-upload/services/Upload.js +++ b/packages/strapi-plugin-upload/services/Upload.js @@ -139,13 +139,7 @@ module.exports = { async uploadToEntity(params, files, source) { // Retrieve provider settings from database. - const config = await strapi - .store({ - environment: strapi.config.environment, - type: 'plugin', - name: 'upload', - }) - .get({ key: 'provider' }); + const config = strapi.plugins.upload.config; const model = strapi.getModel(params.model, source); diff --git a/packages/strapi-plugin-upload/test/upload.test.e2e.js b/packages/strapi-plugin-upload/test/upload.test.e2e.js index 249eaffcb9..7f0cd1263f 100644 --- a/packages/strapi-plugin-upload/test/upload.test.e2e.js +++ b/packages/strapi-plugin-upload/test/upload.test.e2e.js @@ -8,85 +8,22 @@ const { createAuthRequest } = require('../../../test/helpers/request'); let rq; -const defaultProviderConfig = { - provider: 'local', - name: 'Local server', - enabled: true, - sizeLimit: 1000000, -}; - -const resetProviderConfigToDefault = () => { - return setConfigOptions(defaultProviderConfig); -}; - -const setConfigOptions = assign => { - return rq.put('/upload/settings/development', { - body: { - ...defaultProviderConfig, - ...assign, - }, - }); -}; - describe('Upload plugin end to end tests', () => { beforeAll(async () => { const token = await registerAndLogin(); rq = createAuthRequest(token); }, 60000); - afterEach(async () => { - await resetProviderConfigToDefault(); - }); - - describe('GET /upload/environments => List available environments', () => { - test('Returns the list of envrionments and which one is currently active', async () => { - const res = await rq.get('/upload/environments'); + describe('GET /upload/settings => Get settings for an environment', () => { + test('Returns the settings', async () => { + const res = await rq.get('/upload/settings'); expect(res.statusCode).toBe(200); expect(res.body).toEqual({ - environments: expect.arrayContaining([ - { - name: 'development', - active: true, - }, - { - name: 'staging', - active: false, - }, - { - name: 'production', - active: false, - }, - ]), - }); - }); - }); - - describe('GET /upload/settings/:environment => Get settings for an environment', () => { - test('Lists the available providers', async () => { - const res = await rq.get('/upload/settings/development'); - - expect(res.statusCode).toBe(200); - expect(res.body).toMatchObject({ - providers: [ - { - provider: 'local', - name: 'Local server', - }, - ], - }); - }); - - test('Return the default provider config', async () => { - const res = await rq.get('/upload/settings/development'); - - expect(res.statusCode).toBe(200); - expect(res.body).toMatchObject({ - config: { - provider: 'local', - name: 'Local server', - enabled: true, - sizeLimit: 1000000, + data: { + sizeOptimization: true, + videoPreview: true, + responsiveDimensions: true, }, }); }); @@ -94,24 +31,32 @@ describe('Upload plugin end to end tests', () => { describe('PUT /upload/settings/:environment', () => { test('Updates an envrionment config correctly', async () => { - const updateRes = await rq.put('/upload/settings/development', { + const updateRes = await rq.put('/upload/settings', { body: { - provider: 'test', - enabled: false, - sizeLimit: 1000, + sizeOptimization: true, + videoPreview: false, + responsiveDimensions: true, }, }); expect(updateRes.statusCode).toBe(200); - expect(updateRes.body).toEqual({ ok: true }); + expect(updateRes.body).toEqual({ + data: { + sizeOptimization: true, + videoPreview: false, + responsiveDimensions: true, + }, + }); - const getRes = await rq.get('/upload/settings/development'); + const getRes = await rq.get('/upload/settings'); expect(getRes.statusCode).toBe(200); - expect(getRes.body.config).toEqual({ - provider: 'test', - enabled: false, - sizeLimit: 1000, + expect(getRes.body).toEqual({ + data: { + sizeOptimization: true, + videoPreview: false, + responsiveDimensions: true, + }, }); }); }); @@ -142,21 +87,6 @@ describe('Upload plugin end to end tests', () => { ); }); - test('Rejects when provider is not enabled', async () => { - await setConfigOptions({ enabled: false }); - - const res = await rq.post('/upload', { - formData: { - files: fs.createReadStream(__dirname + '/rec.jpg'), - }, - }); - - expect(res.statusCode).toBe(400); - expect(res.body).toMatchObject({ - message: [{ messages: [{ message: 'File upload is disabled' }] }], - }); - }); - test('Rejects when no files are provided', async () => { const res = await rq.post('/upload', { formData: {}, @@ -167,27 +97,6 @@ describe('Upload plugin end to end tests', () => { message: [{ messages: [{ message: 'Files are empty' }] }], }); }); - - test('Rejects when any file if over the configured size limit', async () => { - await setConfigOptions({ - sizeLimit: 0, - }); - - const res = await rq.post('/upload', { - formData: { - files: fs.createReadStream(__dirname + '/rec.jpg'), - }, - }); - - expect(res.statusCode).toBe(400); - expect(res.body).toMatchObject({ - message: [ - { - messages: [{ message: 'rec.jpg file is bigger than limit size!' }], - }, - ], - }); - }); }); describe('GET /upload/files => Find files', () => {}); diff --git a/packages/strapi/lib/Strapi.js b/packages/strapi/lib/Strapi.js index cff440af39..3e8fed526c 100644 --- a/packages/strapi/lib/Strapi.js +++ b/packages/strapi/lib/Strapi.js @@ -471,16 +471,16 @@ class Strapi extends EventEmitter { this.log.warn('Make sure you call it?'); }, timeoutMs); - async function execBootstrap(fn) { + const execBootstrap = async fn => { if (!fn) return; const timer = warnOnTimeout(); try { - await fn(); + await fn(this); } finally { clearTimeout(timer); } - } + }; const pluginBoostraps = Object.keys(this.plugins).map(plugin => { return execBootstrap( diff --git a/packages/strapi/lib/load/load-files.js b/packages/strapi/lib/load/load-files.js index 4dab6aef39..849cbe6cea 100644 --- a/packages/strapi/lib/load/load-files.js +++ b/packages/strapi/lib/load/load-files.js @@ -25,7 +25,11 @@ const loadFiles = async ( } = {} ) => { const root = {}; - const files = await glob(pattern, { cwd: dir, ...globArgs }); + const files = await glob(pattern, { + cwd: dir, + ...globArgs, + ignore: ['**/__tests__/**'], + }); for (let file of files) { const absolutePath = path.resolve(dir, file);