Support dynamic zone upload file

This commit is contained in:
Alexandre Bodin 2019-11-07 18:11:23 +01:00
parent a71f5c6830
commit 6478e53ab8
3 changed files with 31 additions and 18 deletions

View File

@ -31,14 +31,6 @@ module.exports = {
return strapi.query(params.model, source).count(filters);
},
async createMultipart(data, { files = {}, model, source } = {}) {
const entry = await strapi.query(model, source).create(data);
await uploadFiles(entry, files, { model, source });
return strapi.query(model, source).findOne({ id: entry.id });
},
async create(data, { files, model, source } = {}) {
const entry = await strapi.query(model, source).create(data);

View File

@ -12,21 +12,31 @@ module.exports = async (entry, files, { model, source }) => {
const findModelFromUploadPath = path => {
if (path.length === 0) return { model, source };
// exclude array indexes from path
const parts = path.filter(p => !_.isFinite(_.toNumber(p)));
let currentPath = [];
let tmpModel = entity;
let modelName = model;
let sourceName;
for (let part of parts) {
for (let i = 0; i < path.length; i++) {
if (!tmpModel) return {};
const part = path[i];
const attr = tmpModel.attributes[part];
currentPath.push(part);
// ignore array indexes => handled in the dynamic zone section
if (_.isFinite(_.toNumber(path[i]))) {
continue;
}
if (!attr) return {};
if (attr.type === 'component') {
modelName = attr.component;
tmpModel = strapi.components[attr.component];
} else if (attr.type === 'dynamiczone') {
const entryIdx = path[i + 1]; // get component index
modelName = _.get(entry, [...currentPath, entryIdx]).__component; // get component type
tmpModel = strapi.components[modelName];
} else if (_.has(attr, 'model') || _.has(attr, 'collection')) {
sourceName = attr.plugin;
modelName = attr.model || attr.collection;
@ -48,7 +58,7 @@ module.exports = async (entry, files, { model, source }) => {
if (model) {
const id = _.get(entry, path.concat('id'));
return uploadService.uploadToEntity(
{ id, model: model },
{ id, model },
{ [field]: files },
source
);

View File

@ -12,21 +12,31 @@ module.exports = async (entry, files, { model, source }) => {
const findModelFromUploadPath = path => {
if (path.length === 0) return { model, source };
// exclude array indexes from path
const parts = path.filter(p => !_.isFinite(_.toNumber(p)));
let currentPath = [];
let tmpModel = entity;
let modelName = model;
let sourceName;
for (let part of parts) {
for (let i = 0; i < path.length; i++) {
if (!tmpModel) return {};
const part = path[i];
const attr = tmpModel.attributes[part];
currentPath.push(part);
// ignore array indexes => handled in the dynamic zone section
if (_.isFinite(_.toNumber(path[i]))) {
continue;
}
if (!attr) return {};
if (attr.type === 'component') {
modelName = attr.component;
tmpModel = strapi.components[attr.component];
} else if (attr.type === 'dynamiczone') {
const entryIdx = path[i + 1]; // get component index
modelName = _.get(entry, [...currentPath, entryIdx]).__component; // get component type
tmpModel = strapi.components[modelName];
} else if (_.has(attr, 'model') || _.has(attr, 'collection')) {
sourceName = attr.plugin;
modelName = attr.model || attr.collection;
@ -46,8 +56,9 @@ module.exports = async (entry, files, { model, source }) => {
const { model, source } = findModelFromUploadPath(path);
if (model) {
const id = _.get(entry, path.concat('id'));
return uploadService.uploadToEntity(
{ id: _.get(entry, path.concat('id')), model: model },
{ id, model },
{ [field]: files },
source
);