mirror of
https://github.com/strapi/strapi.git
synced 2025-12-27 07:03:38 +00:00
Add upload route in content-manager and remove formdata for data creation
This commit is contained in:
parent
c8f72df4d5
commit
c5e50665cd
@ -24,59 +24,10 @@ module.exports = async (ctx, next) => {
|
||||
'actions',
|
||||
ctx.request.route.action,
|
||||
],
|
||||
[],
|
||||
[]
|
||||
).split('.');
|
||||
|
||||
if (controller && action) {
|
||||
// Redirect to specific controller.
|
||||
if (
|
||||
ctx.request.body.hasOwnProperty('fields') &&
|
||||
ctx.request.body.hasOwnProperty('files')
|
||||
) {
|
||||
let { files, fields } = ctx.request.body;
|
||||
|
||||
const parser = value => {
|
||||
try {
|
||||
value = JSON.parse(value);
|
||||
} catch (e) {
|
||||
// Silent.
|
||||
}
|
||||
|
||||
return _.isArray(value) ? value.map(obj => parser(obj)) : value;
|
||||
};
|
||||
|
||||
fields = Object.keys(fields).reduce((acc, current) => {
|
||||
acc[current] = parser(fields[current]);
|
||||
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
ctx.request.body = fields;
|
||||
|
||||
await target.controllers[controller.toLowerCase()][action](ctx);
|
||||
const resBody = ctx.body;
|
||||
|
||||
await Promise.all(
|
||||
Object.keys(files).map(async field => {
|
||||
ctx.request.body = {
|
||||
files: {
|
||||
files: files[field],
|
||||
},
|
||||
fields: {
|
||||
refId: resBody.id || resBody._id,
|
||||
ref: ctx.params.model,
|
||||
source,
|
||||
field,
|
||||
},
|
||||
};
|
||||
|
||||
return strapi.plugins.upload.controllers.upload.upload(ctx);
|
||||
}),
|
||||
);
|
||||
|
||||
return ctx.send(resBody);
|
||||
}
|
||||
|
||||
return await target.controllers[controller.toLowerCase()][action](ctx);
|
||||
}
|
||||
}
|
||||
|
||||
@ -64,6 +64,14 @@
|
||||
"policies": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"method": "POST",
|
||||
"path": "/explorer/upload",
|
||||
"handler": "ContentManager.uploadFile",
|
||||
"config": {
|
||||
"policies": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/explorer/:model",
|
||||
|
||||
@ -144,4 +144,15 @@ module.exports = {
|
||||
ctx.request.query
|
||||
);
|
||||
},
|
||||
|
||||
/**
|
||||
* Handle uploads in the explorer
|
||||
*/
|
||||
async uploadFile(ctx) {
|
||||
if (!strapi.plugins.upload) {
|
||||
ctx.send({ error: 'uploadPlugin.notInstalled' }, 400);
|
||||
}
|
||||
|
||||
return strapi.plugins.upload.controllers.upload.upload(ctx);
|
||||
},
|
||||
};
|
||||
|
||||
@ -5,27 +5,6 @@ const _ = require('lodash');
|
||||
/**
|
||||
* A set of functions called "actions" for `ContentManager`
|
||||
*/
|
||||
|
||||
const parseFormInput = value => {
|
||||
try {
|
||||
const parsed = JSON.parse(value);
|
||||
// do not modify initial value if it is string except 'null'
|
||||
if (typeof parsed !== 'string') {
|
||||
value = parsed;
|
||||
}
|
||||
} catch (e) {
|
||||
// Silent.
|
||||
}
|
||||
|
||||
return _.isArray(value) ? value.map(parseFormInput) : value;
|
||||
};
|
||||
|
||||
const parseFormData = fields =>
|
||||
Object.keys(fields).reduce((acc, current) => {
|
||||
acc[current] = parseFormInput(fields[current]);
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
module.exports = {
|
||||
fetch(params, source, populate) {
|
||||
return strapi
|
||||
@ -53,61 +32,10 @@ module.exports = {
|
||||
},
|
||||
|
||||
async add(params, values, source) {
|
||||
// Multipart/form-data.
|
||||
if (values.hasOwnProperty('fields') && values.hasOwnProperty('files')) {
|
||||
const data = parseFormData(values.fields);
|
||||
const entry = await strapi.query(params.model, source).create(data);
|
||||
|
||||
// Then, request plugin upload.
|
||||
if (strapi.plugins.upload && Object.keys(values.files).length > 0) {
|
||||
// Upload new files and attach them to this entity.
|
||||
await strapi.plugins.upload.services.upload.uploadToEntity(
|
||||
{
|
||||
id: entry.id || entry._id,
|
||||
model: params.model,
|
||||
},
|
||||
values.files,
|
||||
source
|
||||
);
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
// Create an entry using `queries` system
|
||||
return await strapi.query(params.model, source).create(values);
|
||||
},
|
||||
|
||||
async edit(params, values, source) {
|
||||
// Multipart/form-data.
|
||||
if (values.hasOwnProperty('fields') && values.hasOwnProperty('files')) {
|
||||
// set empty attributes if old values was cleared
|
||||
_.difference(
|
||||
Object.keys(values.files),
|
||||
Object.keys(values.fields)
|
||||
).forEach(attr => {
|
||||
values.fields[attr] = [];
|
||||
});
|
||||
|
||||
const data = parseFormData(values.fields);
|
||||
const updatedEntity = await strapi
|
||||
.query(params.model, source)
|
||||
.update({ id: params.id }, data);
|
||||
|
||||
// Then, request plugin upload.
|
||||
if (strapi.plugins.upload) {
|
||||
// Upload new files and attach them to this entity.
|
||||
await strapi.plugins.upload.services.upload.uploadToEntity(
|
||||
params,
|
||||
values.files,
|
||||
source
|
||||
);
|
||||
}
|
||||
|
||||
return updatedEntity;
|
||||
}
|
||||
|
||||
// Raw JSON.
|
||||
return strapi.query(params.model, source).update({ id: params.id }, values);
|
||||
},
|
||||
|
||||
|
||||
@ -57,7 +57,7 @@ describe('Content Manager End to End', () => {
|
||||
let { body } = await rq({
|
||||
url: '/content-manager/explorer/tag/?source=content-manager',
|
||||
method: 'POST',
|
||||
formData: {
|
||||
body: {
|
||||
name: 'tag1',
|
||||
},
|
||||
});
|
||||
@ -73,7 +73,7 @@ describe('Content Manager End to End', () => {
|
||||
let { body } = await rq({
|
||||
url: '/content-manager/explorer/tag/?source=content-manager',
|
||||
method: 'POST',
|
||||
formData: {
|
||||
body: {
|
||||
name: 'tag2',
|
||||
},
|
||||
});
|
||||
@ -89,7 +89,7 @@ describe('Content Manager End to End', () => {
|
||||
let { body } = await rq({
|
||||
url: '/content-manager/explorer/tag/?source=content-manager',
|
||||
method: 'POST',
|
||||
formData: {
|
||||
body: {
|
||||
name: 'tag3',
|
||||
},
|
||||
});
|
||||
@ -110,7 +110,7 @@ describe('Content Manager End to End', () => {
|
||||
let { body } = await rq({
|
||||
url: '/content-manager/explorer/article/?source=content-manager',
|
||||
method: 'POST',
|
||||
formData: entry,
|
||||
body: entry,
|
||||
});
|
||||
|
||||
data.articles.push(body);
|
||||
@ -132,7 +132,7 @@ describe('Content Manager End to End', () => {
|
||||
let { body } = await rq({
|
||||
url: '/content-manager/explorer/article/?source=content-manager',
|
||||
method: 'POST',
|
||||
formData: entry,
|
||||
body: entry,
|
||||
});
|
||||
|
||||
data.articles.push(body);
|
||||
@ -155,7 +155,7 @@ describe('Content Manager End to End', () => {
|
||||
let { body } = await rq({
|
||||
url: `/content-manager/explorer/article/${entry.id}?source=content-manager`,
|
||||
method: 'PUT',
|
||||
formData: entry,
|
||||
body: entry,
|
||||
});
|
||||
|
||||
data.articles[0] = body;
|
||||
@ -178,7 +178,7 @@ describe('Content Manager End to End', () => {
|
||||
let { body } = await rq({
|
||||
url: `/content-manager/explorer/article/${entry.id}?source=content-manager`,
|
||||
method: 'PUT',
|
||||
formData: entry,
|
||||
body: entry,
|
||||
});
|
||||
|
||||
data.articles[0] = body;
|
||||
@ -199,7 +199,7 @@ describe('Content Manager End to End', () => {
|
||||
let { body } = await rq({
|
||||
url: `/content-manager/explorer/article/${entry.id}?source=content-manager`,
|
||||
method: 'PUT',
|
||||
formData: entry,
|
||||
body: entry,
|
||||
});
|
||||
|
||||
data.articles[0] = body;
|
||||
@ -221,7 +221,7 @@ describe('Content Manager End to End', () => {
|
||||
let { body } = await rq({
|
||||
url: `/content-manager/explorer/article/${entry.id}?source=content-manager`,
|
||||
method: 'PUT',
|
||||
formData: entry,
|
||||
body: entry,
|
||||
});
|
||||
|
||||
data.articles[0] = body;
|
||||
@ -237,7 +237,7 @@ describe('Content Manager End to End', () => {
|
||||
const { body: createdTag } = await rq({
|
||||
url: '/content-manager/explorer/tag/?source=content-manager',
|
||||
method: 'POST',
|
||||
formData: {
|
||||
body: {
|
||||
name: 'tag11',
|
||||
},
|
||||
});
|
||||
@ -245,7 +245,7 @@ describe('Content Manager End to End', () => {
|
||||
const { body: article12 } = await rq({
|
||||
url: '/content-manager/explorer/article/?source=content-manager',
|
||||
method: 'POST',
|
||||
formData: {
|
||||
body: {
|
||||
title: 'article12',
|
||||
content: 'Content',
|
||||
tags: [createdTag],
|
||||
@ -260,7 +260,7 @@ describe('Content Manager End to End', () => {
|
||||
const { body: article13 } = await rq({
|
||||
url: '/content-manager/explorer/article/?source=content-manager',
|
||||
method: 'POST',
|
||||
formData: {
|
||||
body: {
|
||||
title: 'article13',
|
||||
content: 'Content',
|
||||
tags: [updatedTag],
|
||||
@ -308,7 +308,7 @@ describe('Content Manager End to End', () => {
|
||||
let { body } = await rq({
|
||||
url: '/content-manager/explorer/articlewithtag/?source=content-manager',
|
||||
method: 'POST',
|
||||
formData: entry,
|
||||
body: entry,
|
||||
});
|
||||
|
||||
expect(body.id);
|
||||
@ -330,7 +330,7 @@ describe('Content Manager End to End', () => {
|
||||
let { body } = await rq({
|
||||
url: '/content-manager/explorer/category/?source=content-manager',
|
||||
method: 'POST',
|
||||
formData: {
|
||||
body: {
|
||||
name: 'cat1',
|
||||
},
|
||||
});
|
||||
@ -346,7 +346,7 @@ describe('Content Manager End to End', () => {
|
||||
let { body } = await rq({
|
||||
url: '/content-manager/explorer/category/?source=content-manager',
|
||||
method: 'POST',
|
||||
formData: {
|
||||
body: {
|
||||
name: 'cat2',
|
||||
},
|
||||
});
|
||||
@ -368,7 +368,7 @@ describe('Content Manager End to End', () => {
|
||||
let { body } = await rq({
|
||||
url: '/content-manager/explorer/article/?source=content-manager',
|
||||
method: 'POST',
|
||||
formData: entry,
|
||||
body: entry,
|
||||
});
|
||||
|
||||
data.articles.push(body);
|
||||
@ -390,7 +390,7 @@ describe('Content Manager End to End', () => {
|
||||
let { body } = await rq({
|
||||
url: `/content-manager/explorer/article/${entry.id}?source=content-manager`,
|
||||
method: 'PUT',
|
||||
formData: entry,
|
||||
body: entry,
|
||||
});
|
||||
|
||||
data.articles[0] = body;
|
||||
@ -411,7 +411,7 @@ describe('Content Manager End to End', () => {
|
||||
let { body } = await rq({
|
||||
url: '/content-manager/explorer/article?source=content-manager',
|
||||
method: 'POST',
|
||||
formData: entry,
|
||||
body: entry,
|
||||
});
|
||||
|
||||
data.articles.push(body);
|
||||
@ -432,7 +432,7 @@ describe('Content Manager End to End', () => {
|
||||
let { body } = await rq({
|
||||
url: `/content-manager/explorer/article/${entry.id}?source=content-manager`,
|
||||
method: 'PUT',
|
||||
formData: entry,
|
||||
body: entry,
|
||||
});
|
||||
|
||||
data.articles[1] = body;
|
||||
@ -453,7 +453,7 @@ describe('Content Manager End to End', () => {
|
||||
let { body } = await rq({
|
||||
url: `/content-manager/explorer/category/${entry.id}?source=content-manager`,
|
||||
method: 'PUT',
|
||||
formData: entry,
|
||||
body: entry,
|
||||
});
|
||||
|
||||
data.categories[0] = body;
|
||||
@ -473,7 +473,7 @@ describe('Content Manager End to End', () => {
|
||||
let { body } = await rq({
|
||||
url: '/content-manager/explorer/category/?source=content-manager',
|
||||
method: 'POST',
|
||||
formData: entry,
|
||||
body: entry,
|
||||
});
|
||||
|
||||
data.categories.push(body);
|
||||
@ -549,7 +549,7 @@ describe('Content Manager End to End', () => {
|
||||
let { body } = await rq({
|
||||
url: '/content-manager/explorer/reference/?source=content-manager',
|
||||
method: 'POST',
|
||||
formData: {
|
||||
body: {
|
||||
name: 'ref1',
|
||||
},
|
||||
});
|
||||
@ -569,7 +569,7 @@ describe('Content Manager End to End', () => {
|
||||
let { body } = await rq({
|
||||
url: '/content-manager/explorer/article?source=content-manager',
|
||||
method: 'POST',
|
||||
formData: entry,
|
||||
body: entry,
|
||||
});
|
||||
|
||||
data.articles.push(body);
|
||||
@ -589,7 +589,7 @@ describe('Content Manager End to End', () => {
|
||||
let { body } = await rq({
|
||||
url: `/content-manager/explorer/article/${entry.id}?source=content-manager`,
|
||||
method: 'PUT',
|
||||
formData: entry,
|
||||
body: entry,
|
||||
});
|
||||
|
||||
data.articles[0] = body;
|
||||
@ -610,7 +610,7 @@ describe('Content Manager End to End', () => {
|
||||
let { body } = await rq({
|
||||
url: '/content-manager/explorer/article?source=content-manager',
|
||||
method: 'POST',
|
||||
formData: entry,
|
||||
body: entry,
|
||||
});
|
||||
|
||||
data.articles.push(body);
|
||||
@ -627,7 +627,7 @@ describe('Content Manager End to End', () => {
|
||||
const { body: tagToCreate } = await rq({
|
||||
url: '/content-manager/explorer/tag/?source=content-manager',
|
||||
method: 'POST',
|
||||
formData: {
|
||||
body: {
|
||||
name: 'tag111',
|
||||
},
|
||||
});
|
||||
@ -635,7 +635,7 @@ describe('Content Manager End to End', () => {
|
||||
const { body: referenceToCreate } = await rq({
|
||||
url: '/content-manager/explorer/reference/?source=content-manager',
|
||||
method: 'POST',
|
||||
formData: {
|
||||
body: {
|
||||
name: 'cat111',
|
||||
tag: tagToCreate,
|
||||
},
|
||||
@ -648,7 +648,7 @@ describe('Content Manager End to End', () => {
|
||||
const { body: tagToCreate } = await rq({
|
||||
url: '/content-manager/explorer/tag/?source=content-manager',
|
||||
method: 'POST',
|
||||
formData: {
|
||||
body: {
|
||||
name: 'tag111',
|
||||
},
|
||||
});
|
||||
@ -656,7 +656,7 @@ describe('Content Manager End to End', () => {
|
||||
const { body: referenceToCreate } = await rq({
|
||||
url: '/content-manager/explorer/reference/?source=content-manager',
|
||||
method: 'POST',
|
||||
formData: {
|
||||
body: {
|
||||
name: 'cat111',
|
||||
tag: tagToCreate,
|
||||
},
|
||||
@ -667,7 +667,7 @@ describe('Content Manager End to End', () => {
|
||||
const { body: referenceToUpdate } = await rq({
|
||||
url: `/content-manager/explorer/reference/${referenceToCreate.id}?source=content-manager`,
|
||||
method: 'PUT',
|
||||
formData: {
|
||||
body: {
|
||||
tag: null,
|
||||
},
|
||||
});
|
||||
@ -679,7 +679,7 @@ describe('Content Manager End to End', () => {
|
||||
const { body: tagToCreate } = await rq({
|
||||
url: '/content-manager/explorer/tag/?source=content-manager',
|
||||
method: 'POST',
|
||||
formData: {
|
||||
body: {
|
||||
name: 'tag111',
|
||||
},
|
||||
});
|
||||
@ -687,7 +687,7 @@ describe('Content Manager End to End', () => {
|
||||
const { body: referenceToCreate } = await rq({
|
||||
url: '/content-manager/explorer/reference/?source=content-manager',
|
||||
method: 'POST',
|
||||
formData: {
|
||||
body: {
|
||||
name: 'cat111',
|
||||
tag: tagToCreate,
|
||||
},
|
||||
|
||||
@ -1,25 +1,13 @@
|
||||
const request = require('request-promise-native');
|
||||
|
||||
const createRequest = (defaults = {}) => {
|
||||
const client = request.defaults({
|
||||
return request.defaults({
|
||||
baseUrl: 'http://localhost:1337',
|
||||
json: true,
|
||||
resolveWithFullResponse: true,
|
||||
simple: false,
|
||||
...defaults,
|
||||
});
|
||||
|
||||
return async options => {
|
||||
const params = JSON.parse(JSON.stringify(options));
|
||||
|
||||
for (let key in params.formData) {
|
||||
if (typeof params.formData[key] === 'object') {
|
||||
params.formData[key] = JSON.stringify(params.formData[key]);
|
||||
}
|
||||
}
|
||||
|
||||
return client(params);
|
||||
};
|
||||
};
|
||||
|
||||
const createAuthRequest = token => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user