refs #2051 perf: uploadFile after review
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Javier Segarra 2023-12-13 15:38:46 +01:00
parent 7b9e3b0985
commit 06bae44824
6 changed files with 64 additions and 108 deletions

View File

@ -54,7 +54,7 @@ module.exports = Self => {
});
Self.uploadFile = async(ctx, id, options) => {
const models = Self.app.models;
const {Dms, ClaimDms} = Self.app.models;
const myOptions = {};
let tx;
@ -66,56 +66,14 @@ module.exports = Self => {
myOptions.transaction = tx;
}
// const promises = [];
// const TempContainer = models.TempContainer;
// const ClaimContainer = models.ClaimContainer;
// const fileOptions = {};
// const args = ctx.args;
// let srcFile;
try {
myOptions.model = 'ClaimDms';
myOptions.create = {claimFk: id};
const uploadedFiles = await models.Dms.uploadFile(ctx, myOptions);
const uploadedFiles = await Dms.uploadFile(ctx, myOptions);
// const hasWriteRole = await models.DmsType.hasWriteRole(ctx, args.dmsTypeId, myOptions);
// if (!hasWriteRole)
// throw new UserError(`You don't have enough privileges`);
// // Upload file to temporary path
// const tempContainer = await TempContainer.container('dms');
// const uploaded = await TempContainer.upload(tempContainer.name, ctx.req, ctx.result, fileOptions);
// const files = Object.values(uploaded.files).map(file => {
// return file[0];
// });
// const addedDms = [];
// for (const uploadedFile of files) {
// const newDms = await createDms(ctx, uploadedFile, myOptions);
// const pathHash = ClaimContainer.getHash(newDms.id);
// const file = await TempContainer.getFile(tempContainer.name, uploadedFile.name);
// srcFile = path.join(file.client.root, file.container, file.name);
// const claimContainer = await ClaimContainer.container(pathHash);
// const dstFile = path.join(claimContainer.client.root, pathHash, newDms.file);
// await fs.move(srcFile, dstFile, {
// overwrite: true
// });
// addedDms.push(newDms);
// }
// addedDms.forEach(dms => {
// const newClaimDms = models.ClaimDms.create({
// claimFk: id,
// dmsFk: dms.id
// }, myOptions);
// promises.push(newClaimDms);
// });
// const resolvedPromises = await Promise.all(promises);
const promises = uploadedFiles.map(dms => ClaimDms.create({
claimFk: id,
dmsFk: dms.id
}, myOptions));
await Promise.all(promises);
if (tx) await tx.commit();

View File

@ -55,7 +55,7 @@ module.exports = Self => {
});
Self.uploadFile = async(ctx, id, options) => {
const models = Self.app.models;
const {Dms, ClientDms} = Self.app.models;
const myOptions = {};
let tx;
@ -67,21 +67,16 @@ module.exports = Self => {
myOptions.transaction = tx;
}
// const promises = [];
try {
myOptions.model = 'ClientDms';
myOptions.create = {clientFk: id};
const uploadedFiles = await models.Dms.uploadFile(ctx, myOptions);
// uploadedFiles.forEach(dms => {
// const newClientDms = models.ClientDms.create({
// clientFk: id,
// dmsFk: dms.id
// }, myOptions);
const uploadedFiles = await Dms.uploadFile(ctx, myOptions);
const promises = uploadedFiles.map(dms =>
ClientDms.create({
clientFk: id,
dmsFk: dms.id
}, myOptions)
// promises.push(newClientDms);
// });
// const resolvedPromises = await Promise.all(promises);
);
await Promise.all(promises);
if (tx) await tx.commit();

View File

@ -47,7 +47,7 @@ module.exports = Self => {
});
Self.uploadFile = async(ctx, id, options) => {
const models = Self.app.models;
const {Dms, TicketDms} = Self.app.models;
const myOptions = {};
let tx;
@ -59,21 +59,15 @@ module.exports = Self => {
myOptions.transaction = tx;
}
// const promises = [];
try {
myOptions.model = 'TicketDms';
myOptions.create = {ticketFk: id};
const uploadedFiles = await models.Dms.uploadFile(ctx, myOptions);
/* uploadedFiles.forEach(dms => {
const newTicketDms = models.TicketDms.create({
const uploadedFiles = await Dms.uploadFile(ctx, myOptions);
const promises = uploadedFiles.map(dms => TicketDms.create({
ticketFk: id,
dmsFk: dms.id
}, myOptions);
}, myOptions));
await Promise.all(promises);
promises.push(newTicketDms);
});
const resolvedPromises = await Promise.all(promises);
*/
if (tx) await tx.commit();
return uploadedFiles;

View File

@ -30,9 +30,15 @@ module.exports = Self => {
}
try {
const WorkerDms = await Self.findById(id, null, myOptions);
// const dms = await Self.app.models.Dms.findById(id, null, myOptions);
const WorkerDms = await Self.findOne({
where: {document: id}
}, myOptions);
const targetDms = await Self.app.models.Dms.removeFile(ctx, WorkerDms.dmsFk, myOptions);
// const targetDms = await Self.app.models.Dms.removeFile(ctx, id, myOptions);
// const WorkerDms = await Self.findById(+targetDms.reference, null, myOptions);
const targetDms = await Self.app.models.Dms.removeFile(ctx, id, myOptions);
if (!targetDms || !WorkerDms)
throw new UserError('Try again');

View File

@ -47,10 +47,9 @@ module.exports = Self => {
});
Self.uploadFile = async(ctx, id) => {
const models = Self.app.models;
const {Dms, WorkerDms} = Self.app.models;
const myOptions = {};
let tx;
// const promises = [];
if (typeof options == 'object')
Object.assign(myOptions, options);
@ -61,20 +60,14 @@ module.exports = Self => {
}
try {
myOptions.model = 'WorkerDms';
myOptions.create = {workerFk: id};
const uploadedFiles = await models.Dms.uploadFile(ctx, myOptions);
/* uploadedFiles.forEach(dms => {
const newWorkerDms = models.WorkerDms.create({
const uploadedFiles = await Dms.uploadFile(ctx, myOptions);
const promises = uploadedFiles.map(dms =>
WorkerDms.create({
workerFk: id,
dmsFk: dms.id
}, options);
}, myOptions));
await Promise.all(promises);
promises.push(newWorkerDms);
});
const resolvedPromises = await Promise.all(promises);
*/
if (tx) await tx.commit();
return uploadedFiles;

View File

@ -1,8 +1,8 @@
import ngModule from '../../module';
import SectionDms from 'salix/components/section-dms';
import Section from 'salix/components/section';
import './style.scss';
class Controller extends SectionDms {
class Controller extends Section {
get worker() {
return this._worker;
}
@ -11,19 +11,28 @@ class Controller extends SectionDms {
this._worker = value;
if (value) {
this.getDms(this.$params.dmsId).then(handleDefaultParams);
this.getAllowedContentTypes().then(data => this.allowedContentTypes = data);
this.setDefaultParams();
this.getAllowedContentTypes();
}
}
getAllowedContentTypes() {
this.$http.get('DmsContainers/allowedContentTypes').then(res => {
const contentTypes = res.data.join(', ');
this.allowedContentTypes = contentTypes;
});
}
get contentTypesInfo() {
return this.$t('ContentTypesInfo', {
allowedContentTypes: this.allowedContentTypes
});
}
handleDefaultParams({data: dms}) {
setDefaultParams() {
const path = `Dms/${this.$params.dmsId}`;
this.$http.get(path).then(res => {
const dms = res.data && res.data;
this.dms = {
reference: dms.reference,
warehouseId: dms.warehouseFk,
@ -34,6 +43,7 @@ class Controller extends SectionDms {
hasFileAttached: false,
files: []
};
});
}
onSubmit() {