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 = {
|
2019-07-15 18:28:56 +02:00
|
|
|
async upload(ctx) {
|
2019-08-01 07:48:52 +02:00
|
|
|
const uploadService = strapi.plugins.upload.services.upload;
|
|
|
|
|
2018-02-27 16:53:06 +01:00
|
|
|
// Retrieve provider configuration.
|
2019-07-15 18:28:56 +02:00
|
|
|
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) {
|
2019-07-15 18:28:56 +02:00
|
|
|
return ctx.badRequest(
|
|
|
|
null,
|
2019-08-21 12:10:23 +02:00
|
|
|
|
|
|
|
[
|
|
|
|
{
|
|
|
|
messages: [
|
|
|
|
{
|
|
|
|
id: 'Upload.status.disabled',
|
|
|
|
message: 'File upload is disabled',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]
|
2019-07-15 18:28:56 +02:00
|
|
|
);
|
2018-02-20 17:10:25 +01:00
|
|
|
}
|
|
|
|
|
2018-02-27 16:53:06 +01:00
|
|
|
// Extract optional relational data.
|
2019-08-01 07:48:52 +02:00
|
|
|
const { refId, ref, source, field, path } = ctx.request.body || {};
|
|
|
|
const { files = {} } = ctx.request.files || {};
|
2018-03-07 17:48:40 +01:00
|
|
|
|
|
|
|
if (_.isEmpty(files)) {
|
2019-08-21 12:10:23 +02:00
|
|
|
return ctx.badRequest(null, [
|
|
|
|
{
|
|
|
|
messages: [{ id: 'Upload.status.empty', message: 'Files are empty' }],
|
|
|
|
},
|
|
|
|
]);
|
2018-03-07 17:48:40 +01:00
|
|
|
}
|
2018-02-27 16:53:06 +01:00
|
|
|
|
|
|
|
// Transform stream files to buffer
|
2019-08-01 07:48:52 +02:00
|
|
|
const buffers = await uploadService.bufferize(files);
|
|
|
|
|
2018-03-07 17:48:40 +01:00
|
|
|
const enhancedFiles = buffers.map(file => {
|
2018-04-30 18:00:01 +02:00
|
|
|
if (file.size > config.sizeLimit) {
|
2019-08-21 12:10:23 +02:00
|
|
|
return ctx.badRequest(null, [
|
|
|
|
{
|
|
|
|
messages: [
|
|
|
|
{
|
|
|
|
id: 'Upload.status.sizeLimit',
|
|
|
|
message: `${file.name} file is bigger than limit size!`,
|
|
|
|
values: { file: file.name },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]);
|
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, {
|
2019-07-15 18:28:56 +02:00
|
|
|
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, {
|
2019-07-15 18:28:56 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-08-01 07:48:52 +02:00
|
|
|
const uploadedFiles = await uploadService.upload(enhancedFiles, config);
|
2018-02-08 12:01:06 +01:00
|
|
|
|
|
|
|
// Send 200 `ok`
|
2019-12-24 17:49:17 +01:00
|
|
|
ctx.send(uploadedFiles);
|
2018-02-19 15:41:26 +01:00
|
|
|
},
|
|
|
|
|
2019-07-15 18:28:56 +02:00
|
|
|
async getEnvironments(ctx) {
|
2019-08-05 09:10:00 +02:00
|
|
|
const environments = Object.keys(strapi.config.environments).map(
|
|
|
|
environment => ({
|
|
|
|
name: environment,
|
|
|
|
active: strapi.config.environment === environment,
|
|
|
|
})
|
2019-07-15 18:28:56 +02:00
|
|
|
);
|
2018-02-27 17:45:12 +01:00
|
|
|
|
|
|
|
ctx.send({ environments });
|
|
|
|
},
|
|
|
|
|
2019-07-15 18:28:56 +02:00
|
|
|
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,
|
2019-07-15 18:28:56 +02:00
|
|
|
config,
|
2018-02-20 17:10:25 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-07-15 18:28:56 +02: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
|
|
|
|
2019-07-15 18:28:56 +02:00
|
|
|
ctx.send({ ok: true });
|
2018-02-20 17:10:25 +01:00
|
|
|
},
|
|
|
|
|
2019-07-15 18:28:56 +02:00
|
|
|
async find(ctx) {
|
|
|
|
const data = await strapi.plugins['upload'].services.upload.fetchAll(
|
|
|
|
ctx.query
|
|
|
|
);
|
2018-02-19 15:41:26 +01:00
|
|
|
|
|
|
|
// Send 200 `ok`
|
2019-12-24 17:49:17 +01:00
|
|
|
ctx.send(data);
|
2018-02-19 15:41:26 +01:00
|
|
|
},
|
2018-02-19 16:00:37 +01:00
|
|
|
|
2019-07-15 18:28:56 +02:00
|
|
|
async findOne(ctx) {
|
|
|
|
const data = await strapi.plugins['upload'].services.upload.fetch(
|
|
|
|
ctx.params
|
|
|
|
);
|
2018-04-09 19:31:49 +02:00
|
|
|
|
2019-07-15 18:28:56 +02:00
|
|
|
if (!data) {
|
|
|
|
return ctx.notFound('file.notFound');
|
|
|
|
}
|
2018-04-09 19:31:49 +02:00
|
|
|
|
|
|
|
ctx.send(data);
|
|
|
|
},
|
|
|
|
|
2019-07-15 18:28:56 +02:00
|
|
|
async count(ctx) {
|
|
|
|
const data = await strapi.plugins['upload'].services.upload.count(
|
|
|
|
ctx.query
|
|
|
|
);
|
2018-02-19 19:54:45 +01:00
|
|
|
|
2019-07-15 18:28:56 +02:00
|
|
|
ctx.send({ count: data });
|
2018-02-19 19:54:45 +01:00
|
|
|
},
|
|
|
|
|
2019-07-15 18:28:56 +02: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
|
|
|
|
2019-07-15 18:28:56 +02:00
|
|
|
const file = await strapi.plugins['upload'].services.upload.fetch({ id });
|
2018-02-19 16:00:37 +01:00
|
|
|
|
2019-07-15 18:28:56 +02: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
|
|
|
},
|
|
|
|
|
2019-07-15 18:28:56 +02: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);
|
|
|
|
},
|
2018-02-08 12:01:06 +01:00
|
|
|
};
|
2019-07-15 18:28:56 +02:00
|
|
|
|
|
|
|
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');
|
|
|
|
|
2019-12-24 17:49:17 +01:00
|
|
|
return model
|
|
|
|
.find({
|
|
|
|
$or: [{ hash: re }, { name: re }],
|
|
|
|
})
|
|
|
|
.lean();
|
2019-07-15 18:28:56 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|