From 6478e53ab83f66d459cdb0469c38d7b5e4ef1fd0 Mon Sep 17 00:00:00 2001 From: Alexandre Bodin Date: Thu, 7 Nov 2019 18:11:23 +0100 Subject: [PATCH] Support dynamic zone upload file --- .../services/ContentManager.js | 8 ------- .../utils/upload-files.js | 20 +++++++++++++----- .../strapi/lib/core-api/utils/upload-files.js | 21 ++++++++++++++----- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/packages/strapi-plugin-content-manager/services/ContentManager.js b/packages/strapi-plugin-content-manager/services/ContentManager.js index 827a9e053a..669b12fd28 100644 --- a/packages/strapi-plugin-content-manager/services/ContentManager.js +++ b/packages/strapi-plugin-content-manager/services/ContentManager.js @@ -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); diff --git a/packages/strapi-plugin-content-manager/utils/upload-files.js b/packages/strapi-plugin-content-manager/utils/upload-files.js index 47009cd14e..1ad149493f 100644 --- a/packages/strapi-plugin-content-manager/utils/upload-files.js +++ b/packages/strapi-plugin-content-manager/utils/upload-files.js @@ -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 ); diff --git a/packages/strapi/lib/core-api/utils/upload-files.js b/packages/strapi/lib/core-api/utils/upload-files.js index fa17b3fbf9..1ad149493f 100644 --- a/packages/strapi/lib/core-api/utils/upload-files.js +++ b/packages/strapi/lib/core-api/utils/upload-files.js @@ -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 );