Add allowedType in ctb and init getMediaTypes api

Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
Alexandre Bodin 2020-03-03 18:35:57 +01:00
parent f1de079d77
commit 196160dc1c
9 changed files with 153 additions and 16 deletions

View File

@ -238,4 +238,74 @@ describe('Type validators', () => {
expect(validator.isValidSync(attributes.slug)).toBe(false);
});
});
describe('media type', () => {
test('Validates allowedTypes', () => {
const attributes = {
img: {
type: 'media',
allowedTypes: ['nonexistent'],
},
};
const validator = getTypeValidator(attributes.img, {
types: ['media'],
modelType: 'collectionType',
attributes,
});
expect(validator.isValidSync(attributes.img)).toBe(false);
});
test('Cannot set all with other allowedTypes', () => {
const attributes = {
img: {
type: 'media',
allowedTypes: ['all', 'videos'],
},
};
const validator = getTypeValidator(attributes.img, {
types: ['media'],
modelType: 'collectionType',
attributes,
});
expect(validator.isValidSync(attributes.img)).toBe(false);
});
test('Can set multiple allowedTypes', () => {
const attributes = {
img: {
type: 'media',
allowedTypes: ['files', 'videos'],
},
};
const validator = getTypeValidator(attributes.img, {
types: ['media'],
modelType: 'collectionType',
attributes,
});
expect(validator.isValidSync(attributes.img)).toBe(true);
});
test.each(['all', 'images', 'files', 'videos'])('%s is an allowed types', type => {
const attributes = {
img: {
type: 'media',
allowedTypes: [type],
},
};
const validator = getTypeValidator(attributes.img, {
types: ['media'],
modelType: 'collectionType',
attributes,
});
expect(validator.isValidSync(attributes.img)).toBe(true);
});
});
});

View File

@ -49,6 +49,20 @@ const getTypeShape = (attribute, { modelType, attributes } = {}) => {
multiple: yup.boolean(),
required: validators.required,
unique: validators.unique,
allowedTypes: yup.lazy(value => {
if (Array.isArray(value) && value.includes('all')) {
return yup
.array()
.of(yup.string().oneOf(['all']))
.min(1)
.max(1);
}
return yup
.array()
.of(yup.string().oneOf(['images', 'videos', 'files']))
.min(1);
}),
};
}

View File

@ -60,18 +60,12 @@ function createSchemaBuilder({ components, contentTypes }) {
// init temporary ContentTypes
Object.keys(contentTypes).forEach(key => {
tmpContentTypes.set(
contentTypes[key].uid,
createSchemaHandler(contentTypes[key])
);
tmpContentTypes.set(contentTypes[key].uid, createSchemaHandler(contentTypes[key]));
});
// init temporary components
Object.keys(components).forEach(key => {
tmpComponents.set(
components[key].uid,
createSchemaHandler(components[key])
);
tmpComponents.set(components[key].uid, createSchemaHandler(components[key]));
});
return {
@ -97,6 +91,7 @@ function createSchemaBuilder({ components, contentTypes }) {
acc[key] = {
[attribute.multiple ? 'collection' : 'model']: 'file',
via,
allowedTypes: attribute.allowedTypes,
plugin: 'upload',
required: attribute.required ? true : false,
configurable: configurable === false ? false : undefined,

View File

@ -2,7 +2,6 @@
"enabled": true,
"provider": "local",
"providerOptions": {
"sizeLimit": 1000
},
"providers": []
"sizeLimit": 1000000
}
}

View File

@ -1,5 +1,9 @@
'use strict';
const db = require('mime-db');
const mime = require('mime-type')(db);
const _ = require('lodash');
/**
* An asynchronous bootstrap function that runs before
* your application gets started.
@ -16,9 +20,14 @@ module.exports = async () => {
key: 'settings',
});
strapi.plugins.upload.provider = createProvider(
strapi.plugins.upload.config || {}
);
_.defaults(strapi.plugins.upload.config, {
mediaTypes: {
images: mime.glob('image/*'),
videos: mime.glob('video/*'),
},
});
strapi.plugins.upload.provider = createProvider(strapi.plugins.upload.config || {});
// if provider config does not exist set one by default
const config = await configurator.get();

View File

@ -29,6 +29,14 @@
"policies": []
}
},
{
"method": "GET",
"path": "/media-types",
"handler": "Upload.getMediaTypes",
"config": {
"policies": []
}
},
{
"method": "GET",
"path": "/files/count",

View File

@ -106,6 +106,12 @@ module.exports = {
ctx.body = { data };
},
async getMediaTypes(ctx) {
const data = _.get(strapi.plugins.upload, 'config.mediaTypes');
ctx.body = { data };
},
async find(ctx) {
const data = await strapi.plugins['upload'].services.upload.fetchAll(ctx.query);

View File

@ -16,6 +16,8 @@
"immutable": "^3.8.2",
"invariant": "^2.2.1",
"lodash": "^4.17.11",
"mime-db": "1.43.0",
"mime-type": "3.0.7",
"react": "^16.9.0",
"react-copy-to-clipboard": "^5.0.1",
"react-dom": "^16.9.0",

View File

@ -6945,7 +6945,7 @@ escape-string-regexp@1.0.2:
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.2.tgz#4dbc2fe674e71949caf3fb2695ce7f2dc1d9a8d1"
integrity sha1-Tbwv5nTnGUnK8/smlc5/LcHZqNE=
escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.5:
escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.3, escape-string-regexp@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=
@ -9218,6 +9218,13 @@ inflight@^1.0.4:
once "^1.3.0"
wrappy "1"
inherits-ex@^1.1.2:
version "1.2.3"
resolved "https://registry.yarnpkg.com/inherits-ex/-/inherits-ex-1.2.3.tgz#ba0da6258e9b855f98429e82ea97c4cc0e4e459d"
integrity sha512-DCZqD7BpjXqaha8IKcoAE3ZZr6Hi12ropV1h+3pBnirE14mNRwLuYySvYxUSBemTQ40SjAxPL8BTk2Xw/3IF9w==
dependencies:
xtend "^4.0.0"
inherits@2, inherits@2.0.4, inherits@^2.0.1, inherits@^2.0.3, inherits@~2.0.1, inherits@~2.0.3, inherits@~2.0.4:
version "2.0.4"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
@ -11618,7 +11625,7 @@ mdn-data@2.0.4:
resolved "https://registry.yarnpkg.com/mdn-data/-/mdn-data-2.0.4.tgz#699b3c38ac6f1d728091a64650b65d388502fd5b"
integrity sha512-iV3XNKw06j5Q7mi6h+9vbx23Tv7JkjEVgKHW4pimwyDGWm0OIQntJJ+u1C6mg6mK1EaTv42XQ7w76yuzH7M2cA==
media-typer@0.3.0:
media-typer@0.3.0, media-typer@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748"
integrity sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=
@ -11774,6 +11781,16 @@ mime-db@1.43.0, "mime-db@>= 1.43.0 < 2":
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.43.0.tgz#0a12e0502650e473d735535050e7c8f4eb4fae58"
integrity sha512-+5dsGEEovYbT8UY9yD7eE4XTc4UwJ1jBYlgaQQF38ENsKR3wj/8q8RFZrF9WIZpB2V1ArTVFUva8sAul1NzRzQ==
mime-type@3.0.7:
version "3.0.7"
resolved "https://registry.yarnpkg.com/mime-type/-/mime-type-3.0.7.tgz#15c21e4833edd1ac5ddf04fffb49164d17bef57b"
integrity sha512-NyWtbAKERuLQIv+1jjEdWGrWepVlubZEW0fTs4K9T6UWW45iMBpgrwpP5GIl8/5trHLviOcQfA6zEth3T8WhNA==
dependencies:
media-typer "^0.3.0"
minimatch "^3.0.4"
path.js "^1.0.7"
util-ex "^0.3.15"
mime-types@^2.0.8, mime-types@^2.1.12, mime-types@^2.1.18, mime-types@~2.1.17, mime-types@~2.1.19, mime-types@~2.1.24:
version "2.1.26"
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.26.tgz#9c921fc09b7e149a65dfdc0da4d20997200b0a06"
@ -13293,6 +13310,15 @@ path-type@^4.0.0:
resolved "https://registry.yarnpkg.com/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b"
integrity sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==
path.js@^1.0.7:
version "1.0.7"
resolved "https://registry.yarnpkg.com/path.js/-/path.js-1.0.7.tgz#7d136b607de19bfd98ba068874926287e6534939"
integrity sha1-fRNrYH3hm/2YugaIdJJih+ZTSTk=
dependencies:
escape-string-regexp "^1.0.3"
inherits-ex "^1.1.2"
util-ex "^0.3.10"
pbkdf2@^3.0.3:
version "3.0.17"
resolved "https://registry.yarnpkg.com/pbkdf2/-/pbkdf2-3.0.17.tgz#976c206530617b14ebb32114239f7b09336e93a6"
@ -17729,6 +17755,14 @@ util-deprecate@^1.0.1, util-deprecate@~1.0.1:
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
integrity sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=
util-ex@^0.3.10, util-ex@^0.3.15:
version "0.3.15"
resolved "https://registry.yarnpkg.com/util-ex/-/util-ex-0.3.15.tgz#f9261cda13c4327d0740cbe67be1227ded8b0058"
integrity sha1-+SYc2hPEMn0HQMvme+Eife2LAFg=
dependencies:
inherits-ex "^1.1.2"
xtend "^4.0.0"
util-promisify@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/util-promisify/-/util-promisify-2.1.0.tgz#3c2236476c4d32c5ff3c47002add7c13b9a82a53"