Fix upload update

Co-authored-by: Aurelsicoko <aurelsicoko@gmail.com>
This commit is contained in:
cyril lopez 2018-03-07 17:48:40 +01:00
parent 7fe96d3875
commit bb65ff70da
2 changed files with 170 additions and 3 deletions

View File

@ -464,7 +464,7 @@ module.exports = function (strapi) {
return result;
},
manageRelations: async function (model, params) {
manageRelations: async function (model, params, source) {
const models = _.assign(_.clone(strapi.models), Object.keys(strapi.plugins).reduce((acc, current) => {
_.assign(acc, strapi.plugins[current].models);
return acc;
@ -487,6 +487,7 @@ module.exports = function (strapi) {
if (_.get(Model._attributes, `${current}.isVirtual`) !== true && _.isUndefined(association)) {
acc[current] = params.values[current];
} else {
console.log(association);
switch (association.nature) {
case 'oneToOne':
if (response[current] !== params.values[current]) {
@ -592,6 +593,84 @@ module.exports = function (strapi) {
acc[current] = params.values[current];
}
break;
case 'manyMorphToMany':
case 'manyMorphToOne':
// Update the relational array.
acc[current] = params.values[current].map(obj => {
const globalId = obj.source && obj.source !== 'content-manager' ?
strapi.plugins[obj.source].models[obj.ref].globalId:
strapi.models[obj.ref].globalId;
// Define the object stored in database.
// The shape is this object is defined by the strapi-mongoose connector.
return {
ref: obj.refId,
kind: globalId,
[association.filter]: obj.field
}
});
break;
case 'oneToManyMorph':
case 'manyToManyMorph':
const transformToArrayID = (array) => {
if (_.isArray(array)) {
return array.map(value => {
if (_.isObject(value)) {
return value._id || value.id;
}
return value;
})
}
if (association.type === 'model') {
return _.isEmpty(array) ? [] : transformToArrayID([array]);
}
return [];
};
//
// console.log(response[current]);
// console.log(params.values[current]);
// Compare array of ID to find deleted files.
const currentValue = transformToArrayID(response[current]).map(id => id.toString());
const storedValue = transformToArrayID(params.values[current]).map(id => id.toString());
//
console.log(currentValue);
console.log(storedValue);
const toAdd = _.difference(storedValue, currentValue);
const toRemove = _.difference(currentValue, storedValue);
console.log("ADD", toAdd);
console.log("REMOVE", toRemove)
// Remove relations in the other side.
toAdd.forEach(id => {
virtualFields.push(this.addRelationMorph(details.model || details.collection, {
id,
alias: association.via,
ref: Model.globalId,
refId: response._id,
field: association.alias
}, details.plugin));
});
// Remove relations in the other side.
toRemove.forEach(id => {
virtualFields.push(this.removeRelationMorph(details.model || details.collection, {
id,
alias: association.via,
ref: Model.globalId,
refId: response._id,
field: association.alias
}, details.plugin));
});
break;
case 'oneMorphToOne':
case 'oneMorphToMany':
break;
default:
}
@ -611,6 +690,89 @@ module.exports = function (strapi) {
const process = await Promise.all(virtualFields);
return process[process.length - 1];
},
addRelationMorph: async function (model, params, source) {
const models = _.assign(_.clone(strapi.models), Object.keys(strapi.plugins).reduce((acc, current) => {
_.assign(acc, strapi.plugins[current].models);
return acc;
}, {}));
const Model = models[model];
/*
TODO:
Test this part because it has been coded during the development of the upload feature.
However the upload doesn't need this method. It only uses the `removeRelationMorph`.
*/
const entry = await Model.findOne({
[Model.primaryKey]: params.id
});
const value = entry[params.alias] || [];
// Retrieve association.
const association = Model.associations.find(association => association.via === params.alias)[0];
if (!association) {
throw Error(`Impossible to create relationship with ${params.ref} (${params.refId})`);
}
// Resolve if the association is already existing.
const isExisting = entry[params.alias].find(obj => {
if (obj.kind === params.ref && obj.ref.toString() === params.refId.toString() && obj.field === params.field) {
return true;
}
return false;
});
// Avoid duplicate.
if (isExisting) {
return Promise.resolve();
}
// Push new relation to the association array.
value.push({
ref: params.refId,
kind: params.ref,
field: association.filter
});
entry[params.alias] = value;
return Model.update({
[Model.primaryKey]: params.id
}, entry, {
strict: false
});
},
removeRelationMorph: async function (model, params, source) {
const models = _.assign(_.clone(strapi.models), Object.keys(strapi.plugins).reduce((acc, current) => {
_.assign(acc, strapi.plugins[current].models);
return acc;
}, {}));
const Model = models[model];
const entry = await Model.findOne({
[Model.primaryKey]: params.id
});
// Filter the association array and remove the association.
entry[params.alias] = entry[params.alias].filter(obj => {
if (obj.kind === params.ref && obj.ref.toString() === params.refId.toString() && obj.field === params.field) {
return false;
}
return true;
});
return Model.update({
[Model.primaryKey]: params.id
}, entry, {
strict: false
});
}
};

View File

@ -32,10 +32,15 @@ module.exports = {
// Extract optional relational data.
const { refId, ref, source, field } = ctx.request.body.fields;
const { files = {} } = ctx.request.body.files;
if (_.isEmpty(files)) {
return ctx.send(true);
}
// Transform stream files to buffer
const buffers = await strapi.plugins.upload.services.upload.bufferize(ctx.request.body.files.files);
const files = buffers.map(file => {
const enhancedFiles = buffers.map(file => {
if (file.size > config.sizeLimit) {
return ctx.badRequest(null, ctx.request.admin ? [{ messages: [{ id: 'Upload.status.sizeLimit', values: {file: file.name} }] }] : `${file.name} file is bigger than limit size!`);
}
@ -60,7 +65,7 @@ module.exports = {
return;
}
const uploadedFiles = await strapi.plugins.upload.services.upload.upload(files, config);
const uploadedFiles = await strapi.plugins.upload.services.upload.upload(enhancedFiles, config);
// Send 200 `ok`
ctx.send(uploadedFiles.map((file) => {