2018-02-08 12:01:06 +01:00
|
|
|
'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');
|
|
|
|
|
2018-02-08 12:01:06 +01:00
|
|
|
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);
|
2018-02-08 12:01:06 +01:00
|
|
|
|
|
|
|
// Send 200 `ok`
|
2018-02-19 14:26:20 +01:00
|
|
|
ctx.send(files.map((file) => {
|
2018-02-19 15:41:26 +01:00
|
|
|
delete file.buffer;
|
2018-02-21 14:06:57 +01:00
|
|
|
|
|
|
|
if (_.startsWith(file.url, '/')) {
|
|
|
|
file.url = strapi.config.url + file.url;
|
|
|
|
}
|
2018-02-19 15:41:26 +01:00
|
|
|
|
|
|
|
// Static data
|
|
|
|
file.updatedAt = new Date();
|
|
|
|
file.relatedTo = 'John Doe';
|
|
|
|
|
|
|
|
return file;
|
2018-02-19 14:26:20 +01:00
|
|
|
}));
|
2018-02-19 15:41:26 +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});
|
|
|
|
},
|
|
|
|
|
2018-02-19 15:41:26 +01:00
|
|
|
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 15:41:26 +01:00
|
|
|
},
|
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);
|
|
|
|
}
|
2018-02-08 12:01:06 +01:00
|
|
|
};
|