255 lines
5.5 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 = {
async upload(ctx) {
2018-02-27 16:53:06 +01:00
// Retrieve provider configuration.
const config = await strapi
.store({
environment: strapi.config.environment,
type: 'plugin',
name: 'upload',
})
.get({ key: 'provider' });
2018-02-20 17:10:25 +01:00
2018-02-27 16:53:06 +01:00
// Verify if the file upload is enable.
if (config.enabled === false) {
return ctx.badRequest(
null,
ctx.request.admin
? [{ messages: [{ id: 'Upload.status.disabled' }] }]
: 'File upload is disabled'
);
2018-02-20 17:10:25 +01:00
}
2018-02-27 16:53:06 +01:00
// Extract optional relational data.
const { refId, ref, source, field, path } = ctx.request.body.fields || {};
const { files = {} } = ctx.request.body.files || {};
if (_.isEmpty(files)) {
return ctx.badRequest(
null,
ctx.request.admin
? [{ messages: [{ id: 'Upload.status.empty' }] }]
: 'Files are empty'
);
}
2018-02-27 16:53:06 +01:00
// Transform stream files to buffer
const buffers = await strapi.plugins.upload.services.upload.bufferize(
ctx.request.body.files.files
);
const enhancedFiles = buffers.map(file => {
2018-04-30 18:00:01 +02:00
if (file.size > config.sizeLimit) {
return ctx.badRequest(
null,
ctx.request.admin
? [
{
messages: [
{
id: 'Upload.status.sizeLimit',
values: { file: file.name },
},
],
},
]
: `${file.name} file is bigger than limit size!`
);
2018-04-30 18:00:01 +02:00
}
// Add details to the file to be able to create the relationships.
if (refId && ref && field) {
Object.assign(file, {
related: [
{
refId,
ref,
source,
field,
},
],
2018-04-30 18:00:01 +02:00
});
}
2018-08-23 11:43:05 -07:00
// Update uploading folder path for the file.
if (path) {
Object.assign(file, {
path,
2018-08-23 11:43:05 -07:00
});
}
2018-04-30 18:00:01 +02:00
return file;
});
2018-02-27 16:53:06 +01:00
// Something is wrong (size limit)...
if (ctx.status === 400) {
return;
2018-02-21 14:46:10 +01:00
}
const uploadedFiles = await strapi.plugins.upload.services.upload.upload(
enhancedFiles,
config
);
// Send 200 `ok`
ctx.send(
uploadedFiles.map(file => {
// If is local server upload, add backend host as prefix
if (file.url && file.url[0] === '/') {
file.url = strapi.config.url + file.url;
}
if (_.isArray(file.related)) {
file.related = file.related.map(obj => obj.ref || obj);
}
return file;
})
);
},
async getEnvironments(ctx) {
const environments = _.map(
_.keys(strapi.config.environments),
environment => {
return {
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',
})
.get({ key: 'provider' });
2018-02-20 17:10:25 +01:00
ctx.send({
providers: strapi.plugins.upload.config.providers,
config,
2018-02-20 17:10:25 +01:00
});
},
async updateSettings(ctx) {
await strapi
.store({
environment: ctx.params.environment,
type: 'plugin',
name: 'upload',
})
.set({ key: 'provider', value: ctx.request.body });
2018-02-20 17:10:25 +01:00
ctx.send({ ok: true });
2018-02-20 17:10:25 +01:00
},
async find(ctx) {
const data = await strapi.plugins['upload'].services.upload.fetchAll(
ctx.query
);
// Send 200 `ok`
ctx.send(
data.map(file => {
// if is local server upload, add backend host as prefix
if (file.url[0] === '/') {
file.url = strapi.config.url + file.url;
}
return file;
})
);
},
2018-02-19 16:00:37 +01:00
async findOne(ctx) {
const data = await strapi.plugins['upload'].services.upload.fetch(
ctx.params
);
if (!data) {
return ctx.notFound('file.notFound');
}
data.url = strapi.config.url + data.url;
ctx.send(data);
},
async count(ctx) {
const data = await strapi.plugins['upload'].services.upload.count(
ctx.query
);
2018-02-19 19:54:45 +01:00
ctx.send({ count: data });
2018-02-19 19:54:45 +01:00
},
async destroy(ctx) {
const { id } = ctx.params;
const config = await strapi
.store({
environment: strapi.config.environment,
type: 'plugin',
name: 'upload',
})
.get({ key: 'provider' });
2018-02-20 17:10:25 +01:00
const file = await strapi.plugins['upload'].services.upload.fetch({ id });
2018-02-19 16:00:37 +01:00
if (!file) {
return ctx.notFound('file.notFound');
}
await strapi.plugins['upload'].services.upload.remove(file, config);
ctx.send(file);
2018-02-22 17:12:03 +01:00
},
async search(ctx) {
const { id } = ctx.params;
const data = await strapi.query('file', 'upload').custom(searchQueries)({
id,
});
2018-02-22 17:12:03 +01:00
ctx.send(data);
},
};
const searchQueries = {
bookshelf({ model }) {
return ({ id }) => {
return model
.query(qb => {
qb.whereRaw('LOWER(hash) LIKE ?', [`%${id}%`]).orWhereRaw(
'LOWER(name) LIKE ?',
[`%${id}%`]
);
})
.fetchAll()
.then(results => results.toJSON());
};
},
mongoose({ model }) {
return ({ id }) => {
const re = new RegExp(id, 'i');
return model.find({
$or: [{ hash: re }, { name: re }],
});
};
},
};