refs #2051 feat: uploadFile approach
This commit is contained in:
parent
ae76776245
commit
6b70f8b3eb
|
@ -1,6 +1,3 @@
|
||||||
const UserError = require('vn-loopback/util/user-error');
|
|
||||||
const fs = require('fs-extra');
|
|
||||||
const path = require('path');
|
|
||||||
|
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.remoteMethodCtx('uploadFile', {
|
Self.remoteMethodCtx('uploadFile', {
|
||||||
|
@ -57,96 +54,75 @@ module.exports = Self => {
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.uploadFile = async(ctx, id, options) => {
|
Self.uploadFile = async(ctx, id, options) => {
|
||||||
const tx = await Self.beginTransaction({});
|
const models = Self.app.models;
|
||||||
const myOptions = {};
|
const myOptions = {};
|
||||||
|
let tx;
|
||||||
|
|
||||||
if (typeof options == 'object')
|
if (typeof options == 'object')
|
||||||
Object.assign(myOptions, options);
|
Object.assign(myOptions, options);
|
||||||
|
|
||||||
if (!myOptions.transaction)
|
if (!myOptions.transaction) {
|
||||||
|
tx = await Self.beginTransaction({});
|
||||||
myOptions.transaction = tx;
|
myOptions.transaction = tx;
|
||||||
|
|
||||||
const models = Self.app.models;
|
|
||||||
const promises = [];
|
|
||||||
const TempContainer = models.TempContainer;
|
|
||||||
const ClaimContainer = models.ClaimContainer;
|
|
||||||
const fileOptions = {};
|
|
||||||
const args = ctx.args;
|
|
||||||
|
|
||||||
let srcFile;
|
|
||||||
try {
|
|
||||||
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 promises = [];
|
||||||
const newClaimDms = models.ClaimDms.create({
|
// const TempContainer = models.TempContainer;
|
||||||
claimFk: id,
|
// const ClaimContainer = models.ClaimContainer;
|
||||||
dmsFk: dms.id
|
// const fileOptions = {};
|
||||||
}, myOptions);
|
// const args = ctx.args;
|
||||||
|
|
||||||
promises.push(newClaimDms);
|
// let srcFile;
|
||||||
});
|
try {
|
||||||
const resolvedPromises = await Promise.all(promises);
|
myOptions.model = 'ClaimDms';
|
||||||
|
myOptions.create = {claimFk: id};
|
||||||
|
const uploadedFiles = await models.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);
|
||||||
|
|
||||||
if (tx) await tx.commit();
|
if (tx) await tx.commit();
|
||||||
|
|
||||||
return resolvedPromises;
|
return uploadedFiles;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (tx) await tx.rollback();
|
if (tx) await tx.rollback();
|
||||||
|
|
||||||
if (fs.existsSync(srcFile))
|
|
||||||
await fs.unlink(srcFile);
|
|
||||||
|
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
async function createDms(ctx, file, myOptions) {
|
|
||||||
const models = Self.app.models;
|
|
||||||
const myUserId = ctx.req.accessToken.userId;
|
|
||||||
const args = ctx.args;
|
|
||||||
|
|
||||||
const newDms = await models.Dms.create({
|
|
||||||
workerFk: myUserId,
|
|
||||||
dmsTypeFk: args.dmsTypeId,
|
|
||||||
companyFk: args.companyId,
|
|
||||||
warehouseFk: args.warehouseId,
|
|
||||||
reference: args.reference,
|
|
||||||
description: args.description,
|
|
||||||
contentType: file.type,
|
|
||||||
hasFile: args.hasFile
|
|
||||||
}, myOptions);
|
|
||||||
|
|
||||||
let fileName = file.name;
|
|
||||||
const extension = models.DmsContainer.getFileExtension(fileName);
|
|
||||||
fileName = `${newDms.id}.${extension}`;
|
|
||||||
|
|
||||||
return newDms.updateAttribute('file', fileName, myOptions);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -56,8 +56,8 @@ module.exports = Self => {
|
||||||
|
|
||||||
Self.uploadFile = async(ctx, id, options) => {
|
Self.uploadFile = async(ctx, id, options) => {
|
||||||
const models = Self.app.models;
|
const models = Self.app.models;
|
||||||
let tx;
|
|
||||||
const myOptions = {};
|
const myOptions = {};
|
||||||
|
let tx;
|
||||||
|
|
||||||
if (typeof options == 'object')
|
if (typeof options == 'object')
|
||||||
Object.assign(myOptions, options);
|
Object.assign(myOptions, options);
|
||||||
|
@ -67,23 +67,25 @@ module.exports = Self => {
|
||||||
myOptions.transaction = tx;
|
myOptions.transaction = tx;
|
||||||
}
|
}
|
||||||
|
|
||||||
const promises = [];
|
// const promises = [];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
myOptions.model = 'ClientDms';
|
||||||
|
myOptions.create = {clientFk: id};
|
||||||
const uploadedFiles = await models.Dms.uploadFile(ctx, myOptions);
|
const uploadedFiles = await models.Dms.uploadFile(ctx, myOptions);
|
||||||
uploadedFiles.forEach(dms => {
|
// uploadedFiles.forEach(dms => {
|
||||||
const newClientDms = models.ClientDms.create({
|
// const newClientDms = models.ClientDms.create({
|
||||||
clientFk: id,
|
// clientFk: id,
|
||||||
dmsFk: dms.id
|
// dmsFk: dms.id
|
||||||
}, myOptions);
|
// }, myOptions);
|
||||||
|
|
||||||
promises.push(newClientDms);
|
// promises.push(newClientDms);
|
||||||
});
|
// });
|
||||||
const resolvedPromises = await Promise.all(promises);
|
// const resolvedPromises = await Promise.all(promises);
|
||||||
|
|
||||||
if (tx) await tx.commit();
|
if (tx) await tx.commit();
|
||||||
|
|
||||||
return resolvedPromises;
|
return uploadedFiles;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (tx) await tx.rollback();
|
if (tx) await tx.rollback();
|
||||||
throw e;
|
throw e;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
const models = require('vn-loopback/server/server').models;
|
const models = require('vn-loopback/server/server').models;
|
||||||
|
// f
|
||||||
describe('Ticket uploadFile()', () => {
|
fdescribe('Ticket uploadFile()', () => {
|
||||||
it(`should return an error for a user without enough privileges`, async() => {
|
it(`should return an error for a user without enough privileges`, async() => {
|
||||||
const tx = await models.Ticket.beginTransaction({});
|
const tx = await models.Ticket.beginTransaction({});
|
||||||
|
|
||||||
|
@ -23,4 +23,27 @@ describe('Ticket uploadFile()', () => {
|
||||||
|
|
||||||
expect(error.message).toEqual(`You don't have enough privileges`);
|
expect(error.message).toEqual(`You don't have enough privileges`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// fit(`should uploadFile`, async() => {
|
||||||
|
// const tx = await models.Ticket.beginTransaction({});
|
||||||
|
|
||||||
|
// let error;
|
||||||
|
// try {
|
||||||
|
// const options = {transaction: tx};
|
||||||
|
|
||||||
|
// const ticketId = 15;
|
||||||
|
// const currentUserId = 9;
|
||||||
|
// const ticketTypeId = 14;
|
||||||
|
// const ctx = {req: {accessToken: {userId: currentUserId}}, args: {dmsTypeId: ticketTypeId}, result: new ArrayBuffer(20000)};
|
||||||
|
|
||||||
|
// await models.Ticket.uploadFile(ctx, ticketId, options);
|
||||||
|
|
||||||
|
// await tx.rollback();
|
||||||
|
// } catch (e) {
|
||||||
|
// await tx.rollback();
|
||||||
|
// error = e;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// expect(error.message).toEqual(`You don't have enough privileges`);
|
||||||
|
// });
|
||||||
});
|
});
|
||||||
|
|
|
@ -59,10 +59,12 @@ module.exports = Self => {
|
||||||
myOptions.transaction = tx;
|
myOptions.transaction = tx;
|
||||||
}
|
}
|
||||||
|
|
||||||
const promises = [];
|
// const promises = [];
|
||||||
try {
|
try {
|
||||||
|
myOptions.model = 'TicketDms';
|
||||||
|
myOptions.create = {ticketFk: id};
|
||||||
const uploadedFiles = await models.Dms.uploadFile(ctx, myOptions);
|
const uploadedFiles = await models.Dms.uploadFile(ctx, myOptions);
|
||||||
uploadedFiles.forEach(dms => {
|
/* uploadedFiles.forEach(dms => {
|
||||||
const newTicketDms = models.TicketDms.create({
|
const newTicketDms = models.TicketDms.create({
|
||||||
ticketFk: id,
|
ticketFk: id,
|
||||||
dmsFk: dms.id
|
dmsFk: dms.id
|
||||||
|
@ -71,10 +73,10 @@ module.exports = Self => {
|
||||||
promises.push(newTicketDms);
|
promises.push(newTicketDms);
|
||||||
});
|
});
|
||||||
const resolvedPromises = await Promise.all(promises);
|
const resolvedPromises = await Promise.all(promises);
|
||||||
|
*/
|
||||||
if (tx) await tx.commit();
|
if (tx) await tx.commit();
|
||||||
|
|
||||||
return resolvedPromises;
|
return uploadedFiles;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (tx) await tx.rollback();
|
if (tx) await tx.rollback();
|
||||||
throw e;
|
throw e;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
const app = require('vn-loopback/server/server');
|
const app = require('vn-loopback/server/server');
|
||||||
|
// f
|
||||||
describe('Worker uploadFile()', () => {
|
fdescribe('Worker uploadFile()', () => {
|
||||||
it(`should return an error for a user without enough privileges`, async() => {
|
it(`should return an error for a user without enough privileges`, async() => {
|
||||||
let workerId = 1106;
|
let workerId = 1106;
|
||||||
let currentUserId = 1102;
|
let currentUserId = 1102;
|
||||||
|
|
|
@ -48,14 +48,24 @@ module.exports = Self => {
|
||||||
|
|
||||||
Self.uploadFile = async(ctx, id) => {
|
Self.uploadFile = async(ctx, id) => {
|
||||||
const models = Self.app.models;
|
const models = Self.app.models;
|
||||||
const promises = [];
|
const myOptions = {};
|
||||||
const tx = await Self.beginTransaction({});
|
let tx;
|
||||||
|
// const promises = [];
|
||||||
|
|
||||||
|
if (typeof options == 'object')
|
||||||
|
Object.assign(myOptions, options);
|
||||||
|
|
||||||
|
if (!myOptions.transaction) {
|
||||||
|
tx = await Self.beginTransaction({});
|
||||||
|
myOptions.transaction = tx;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const options = {transaction: tx};
|
myOptions.model = 'WorkerDms';
|
||||||
|
myOptions.create = {workerFk: id};
|
||||||
|
|
||||||
const uploadedFiles = await models.Dms.uploadFile(ctx, options);
|
const uploadedFiles = await models.Dms.uploadFile(ctx, myOptions);
|
||||||
uploadedFiles.forEach(dms => {
|
/* uploadedFiles.forEach(dms => {
|
||||||
const newWorkerDms = models.WorkerDms.create({
|
const newWorkerDms = models.WorkerDms.create({
|
||||||
workerFk: id,
|
workerFk: id,
|
||||||
dmsFk: dms.id
|
dmsFk: dms.id
|
||||||
|
@ -64,13 +74,13 @@ module.exports = Self => {
|
||||||
promises.push(newWorkerDms);
|
promises.push(newWorkerDms);
|
||||||
});
|
});
|
||||||
const resolvedPromises = await Promise.all(promises);
|
const resolvedPromises = await Promise.all(promises);
|
||||||
|
*/
|
||||||
|
if (tx) await tx.commit();
|
||||||
|
|
||||||
await tx.commit();
|
return uploadedFiles;
|
||||||
|
} catch (e) {
|
||||||
return resolvedPromises;
|
if (tx) await tx.rollback();
|
||||||
} catch (err) {
|
throw e;
|
||||||
await tx.rollback();
|
|
||||||
throw err;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue