Get and update configs

This commit is contained in:
Jim Laurie 2018-02-20 17:10:25 +01:00
parent ab93e7113f
commit f12b0503ff
8 changed files with 79 additions and 21 deletions

View File

@ -41,14 +41,7 @@ module.exports = async cb => {
const value = _.assign({}, provider, {enabled: true}); const value = _.assign({}, provider, {enabled: true});
await pluginStore.set({key: 'provider', value}); await pluginStore.set({key: 'provider', value});
strapi.plugins.upload.config.provider = provider;
} else {
strapi.plugins.upload.config.provider = _.find(strapi.plugins.upload.config.providers, {provider: config.provider});
_.assign(strapi.plugins.upload.config.provider, config);
} }
strapi.plugins.upload.config.provider.init(strapi);
} catch (err) { } catch (err) {
strapi.log.error(`Can't laod ${config.provider} upload provider`); strapi.log.error(`Can't laod ${config.provider} upload provider`);
strapi.log.warn(`Please install strapi-upload-${config.provider} --save`); strapi.log.warn(`Please install strapi-upload-${config.provider} --save`);

View File

@ -8,6 +8,22 @@
"policies": [] "policies": []
} }
}, },
{
"method": "GET",
"path": "/settings/:environment",
"handler": "Upload.getSettings",
"config": {
"policies": []
}
},
{
"method": "PUT",
"path": "/settings/:environment",
"handler": "Upload.updateSettings",
"config": {
"policies": []
}
},
{ {
"method": "GET", "method": "GET",
"path": "/files", "path": "/files",

View File

@ -15,11 +15,21 @@ module.exports = {
*/ */
index: async (ctx) => { index: async (ctx) => {
const config = await strapi.store({
environment: strapi.config.environment,
type: 'plugin',
name: 'upload'
}).get({key: 'provider'});
if (!config.enabled) {
return ctx.badRequest(null, ctx.request.admin ? [{ messages: [{ id: 'Upload.status.disabled' }] }] : 'Upload is disabled!');
}
const Service = strapi.plugins['upload'].services.upload; const Service = strapi.plugins['upload'].services.upload;
const files = await Service.buffurize(ctx.request.body.files); const files = await Service.buffurize(ctx.request.body.files);
await Service.upload(files); await Service.upload(files, config);
// Send 200 `ok` // Send 200 `ok`
ctx.send(files.map((file) => { ctx.send(files.map((file) => {
@ -34,6 +44,29 @@ module.exports = {
})); }));
}, },
getSettings: async (ctx) => {
const config = await strapi.store({
environment: ctx.params.environment,
type: 'plugin',
name: 'upload'
}).get({key: 'provider'});
ctx.send({
providers: strapi.plugins.upload.config.providers,
config
});
},
updateSettings: async (ctx) => {
await strapi.store({
environment: ctx.params.environment,
type: 'plugin',
name: 'upload'
}).get({key: 'provider'});
ctx.send({ok: true});
},
find: async (ctx) => { find: async (ctx) => {
const data = await strapi.plugins['upload'].services.upload.fetchAll(ctx.query); const data = await strapi.plugins['upload'].services.upload.fetchAll(ctx.query);
@ -54,7 +87,13 @@ module.exports = {
}, },
destroy: async (ctx, next) => { destroy: async (ctx, next) => {
const data = await strapi.plugins['upload'].services.upload.remove(ctx.params); const config = await strapi.store({
environment: strapi.config.environment,
type: 'plugin',
name: 'upload'
}).get({key: 'provider'});
const data = await strapi.plugins['upload'].services.upload.remove(ctx.params, config);
// Send 200 `ok` // Send 200 `ok`
ctx.send(data); ctx.send(data);

View File

@ -1,6 +1,6 @@
{ {
"name": "strapi-plugin-upload", "name": "strapi-plugin-upload",
"version": "3.0.0-alpha.10.1", "version": "3.0.0-alpha.10.2",
"description": "This is the description of the plugin.", "description": "This is the description of the plugin.",
"strapi": { "strapi": {
"name": "upload", "name": "upload",
@ -27,7 +27,7 @@
"uuid": "^3.2.1" "uuid": "^3.2.1"
}, },
"devDependencies": { "devDependencies": {
"strapi-helper-plugin": "3.0.0-alpha.10.1" "strapi-helper-plugin": "3.0.0-alpha.10.2"
}, },
"author": { "author": {
"name": "A Strapi developer", "name": "A Strapi developer",

View File

@ -39,10 +39,14 @@ module.exports = {
); );
}, },
upload: (files) => { upload: async (files, config) => {
const provider = _.cloneDeep(_.find(strapi.plugins.upload.config.providers, {provider: config.provider}));
_.assign(provider, config);
const actions = provider.init(strapi, config);
return Promise.all( return Promise.all(
files.map(async file => { files.map(async file => {
await strapi.plugins.upload.services.provider.upload(file); await actions.upload(file);
delete file.buffer; delete file.buffer;
@ -86,10 +90,14 @@ module.exports = {
return await strapi.query('file', 'upload').count(); return await strapi.query('file', 'upload').count();
}, },
remove: async params => { remove: async (params, config) => {
const file = await strapi.plugins['upload'].services.upload.fetch(params); const file = await strapi.plugins['upload'].services.upload.fetch(params);
await strapi.plugins.upload.services.provider.delete(file); const provider = _.cloneDeep(_.find(strapi.plugins.upload.config.providers, {provider: config.provider}));
_.assign(provider, config);
const actions = provider.init(strapi, config);
await actions.delete(file);
// Use Content Manager business logic to handle relation. // Use Content Manager business logic to handle relation.
if (strapi.plugins['content-manager']) { if (strapi.plugins['content-manager']) {

View File

@ -12,8 +12,8 @@ const path = require('path');
module.exports = { module.exports = {
provider: 'local', provider: 'local',
name: 'Local server', name: 'Local server',
init: (strapi) => { init: (strapi, config) => {
strapi.plugins.upload.services.provider = { return {
upload: (file) => { upload: (file) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fs.writeFile(path.join(strapi.config.appPath, 'public', `uploads/${file.hash}.${file.ext}`), file.buffer, (err) => { fs.writeFile(path.join(strapi.config.appPath, 'public', `uploads/${file.hash}.${file.ext}`), file.buffer, (err) => {

View File

@ -48,8 +48,8 @@ module.exports = {
type: 'text' type: 'text'
} }
}, },
init: (strapi) => { init: (strapi, config) => {
strapi.plugins.upload.services.provider = { return {
upload: (buffers) => { upload: (buffers) => {
}, },

View File

@ -1,10 +1,12 @@
{ {
"name": "strapi-upload-local", "name": "strapi-upload-s3",
"version": "3.0.0-alpha.10.2", "version": "3.0.0-alpha.10.2",
"description": "Local provider for strapi upload", "description": "AWS S3 provider for strapi upload",
"homepage": "http://strapi.io", "homepage": "http://strapi.io",
"keywords": [ "keywords": [
"upload", "upload",
"aws",
"s3",
"strapi" "strapi"
], ],
"directories": { "directories": {