110 lines
2.4 KiB
JavaScript
Raw Normal View History

'use strict';
/**
* Upload.js controller
*
* @description: A set of functions called "actions" of the `upload` plugin.
*/
2018-02-21 14:06:57 +01:00
const _ = require('lodash');
module.exports = {
/**
* Default action.
*
* @return {Object}
*/
index: async (ctx) => {
2018-02-20 17:10:25 +01:00
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!');
}
2018-02-19 14:26:20 +01:00
const Service = strapi.plugins['upload'].services.upload;
2018-02-21 14:23:12 +01:00
const files = await Service.bufferize(ctx.request.body.files);
2018-02-19 14:26:20 +01:00
2018-02-20 17:10:25 +01:00
await Service.upload(files, config);
// Send 200 `ok`
2018-02-19 14:26:20 +01:00
ctx.send(files.map((file) => {
delete file.buffer;
2018-02-21 14:06:57 +01:00
if (_.startsWith(file.url, '/')) {
file.url = strapi.config.url + file.url;
}
// Static data
file.updatedAt = new Date();
file.relatedTo = 'John Doe';
return file;
2018-02-19 14:26:20 +01:00
}));
},
2018-02-20 17:10:25 +01:00
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'
2018-02-21 14:06:57 +01:00
}).set({key: 'provider', value: ctx.request.body});
2018-02-20 17:10:25 +01:00
ctx.send({ok: true});
},
find: async (ctx) => {
const data = await strapi.plugins['upload'].services.upload.fetchAll(ctx.query);
// Send 200 `ok`
2018-02-19 16:00:37 +01:00
ctx.send(data.map((file) => {
2018-02-21 14:06:57 +01:00
if (_.startsWith(file.url, '/')) {
file.url = strapi.config.url + file.url;
}
2018-02-19 16:00:37 +01:00
return file;
}));
},
2018-02-19 16:00:37 +01:00
2018-02-19 19:54:45 +01:00
count: async (ctx, next) => {
const data = await strapi.plugins['upload'].services.upload.count(ctx.query);
// Send 200 `ok`
ctx.send({
count: data
});
},
2018-02-19 16:00:37 +01:00
destroy: async (ctx, next) => {
2018-02-20 17:10:25 +01:00
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);
2018-02-19 16:00:37 +01:00
// Send 200 `ok`
ctx.send(data);
}
};