mirror of
https://github.com/strapi/strapi.git
synced 2025-11-02 19:04:38 +00:00
Support dynamic zone upload file
This commit is contained in:
parent
a71f5c6830
commit
6478e53ab8
@ -31,14 +31,6 @@ module.exports = {
|
|||||||
return strapi.query(params.model, source).count(filters);
|
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 } = {}) {
|
async create(data, { files, model, source } = {}) {
|
||||||
const entry = await strapi.query(model, source).create(data);
|
const entry = await strapi.query(model, source).create(data);
|
||||||
|
|
||||||
|
|||||||
@ -12,21 +12,31 @@ module.exports = async (entry, files, { model, source }) => {
|
|||||||
const findModelFromUploadPath = path => {
|
const findModelFromUploadPath = path => {
|
||||||
if (path.length === 0) return { model, source };
|
if (path.length === 0) return { model, source };
|
||||||
|
|
||||||
// exclude array indexes from path
|
let currentPath = [];
|
||||||
const parts = path.filter(p => !_.isFinite(_.toNumber(p)));
|
|
||||||
|
|
||||||
let tmpModel = entity;
|
let tmpModel = entity;
|
||||||
let modelName = model;
|
let modelName = model;
|
||||||
let sourceName;
|
let sourceName;
|
||||||
for (let part of parts) {
|
for (let i = 0; i < path.length; i++) {
|
||||||
if (!tmpModel) return {};
|
if (!tmpModel) return {};
|
||||||
|
const part = path[i];
|
||||||
const attr = tmpModel.attributes[part];
|
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) return {};
|
||||||
|
|
||||||
if (attr.type === 'component') {
|
if (attr.type === 'component') {
|
||||||
modelName = attr.component;
|
modelName = attr.component;
|
||||||
tmpModel = strapi.components[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')) {
|
} else if (_.has(attr, 'model') || _.has(attr, 'collection')) {
|
||||||
sourceName = attr.plugin;
|
sourceName = attr.plugin;
|
||||||
modelName = attr.model || attr.collection;
|
modelName = attr.model || attr.collection;
|
||||||
@ -48,7 +58,7 @@ module.exports = async (entry, files, { model, source }) => {
|
|||||||
if (model) {
|
if (model) {
|
||||||
const id = _.get(entry, path.concat('id'));
|
const id = _.get(entry, path.concat('id'));
|
||||||
return uploadService.uploadToEntity(
|
return uploadService.uploadToEntity(
|
||||||
{ id, model: model },
|
{ id, model },
|
||||||
{ [field]: files },
|
{ [field]: files },
|
||||||
source
|
source
|
||||||
);
|
);
|
||||||
|
|||||||
@ -12,21 +12,31 @@ module.exports = async (entry, files, { model, source }) => {
|
|||||||
const findModelFromUploadPath = path => {
|
const findModelFromUploadPath = path => {
|
||||||
if (path.length === 0) return { model, source };
|
if (path.length === 0) return { model, source };
|
||||||
|
|
||||||
// exclude array indexes from path
|
let currentPath = [];
|
||||||
const parts = path.filter(p => !_.isFinite(_.toNumber(p)));
|
|
||||||
|
|
||||||
let tmpModel = entity;
|
let tmpModel = entity;
|
||||||
let modelName = model;
|
let modelName = model;
|
||||||
let sourceName;
|
let sourceName;
|
||||||
for (let part of parts) {
|
for (let i = 0; i < path.length; i++) {
|
||||||
if (!tmpModel) return {};
|
if (!tmpModel) return {};
|
||||||
|
const part = path[i];
|
||||||
const attr = tmpModel.attributes[part];
|
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) return {};
|
||||||
|
|
||||||
if (attr.type === 'component') {
|
if (attr.type === 'component') {
|
||||||
modelName = attr.component;
|
modelName = attr.component;
|
||||||
tmpModel = strapi.components[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')) {
|
} else if (_.has(attr, 'model') || _.has(attr, 'collection')) {
|
||||||
sourceName = attr.plugin;
|
sourceName = attr.plugin;
|
||||||
modelName = attr.model || attr.collection;
|
modelName = attr.model || attr.collection;
|
||||||
@ -46,8 +56,9 @@ module.exports = async (entry, files, { model, source }) => {
|
|||||||
const { model, source } = findModelFromUploadPath(path);
|
const { model, source } = findModelFromUploadPath(path);
|
||||||
|
|
||||||
if (model) {
|
if (model) {
|
||||||
|
const id = _.get(entry, path.concat('id'));
|
||||||
return uploadService.uploadToEntity(
|
return uploadService.uploadToEntity(
|
||||||
{ id: _.get(entry, path.concat('id')), model: model },
|
{ id, model },
|
||||||
{ [field]: files },
|
{ [field]: files },
|
||||||
source
|
source
|
||||||
);
|
);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user