salix/back/methods/dms/uploadFile.js

134 lines
4.2 KiB
JavaScript
Raw Normal View History

const UserError = require('vn-loopback/util/user-error');
const fs = require('fs-extra');
2020-12-18 16:23:04 +00:00
const path = require('path');
module.exports = Self => {
Self.remoteMethodCtx('uploadFile', {
description: 'Uploads a file and inserts into dms model',
accessType: 'WRITE',
2019-06-13 07:21:36 +00:00
accepts: [
{
arg: 'warehouseId',
type: 'Number',
description: 'The warehouse id',
required: true
2019-06-13 07:21:36 +00:00
}, {
arg: 'companyId',
type: 'Number',
description: 'The company id',
required: true
2019-06-13 07:21:36 +00:00
}, {
arg: 'dmsTypeId',
type: 'Number',
description: 'The dms type id',
required: true
2019-06-13 07:21:36 +00:00
}, {
arg: 'reference',
type: 'String',
required: true
2019-06-13 07:21:36 +00:00
}, {
arg: 'description',
type: 'String',
required: true
2019-06-13 07:21:36 +00:00
}, {
arg: 'hasFile',
type: 'Boolean',
description: 'True if has an attached file',
required: true
}],
returns: {
type: 'Object',
root: true
},
http: {
path: `/uploadFile`,
verb: 'POST'
}
});
2019-06-20 07:23:55 +00:00
Self.uploadFile = async(ctx, options) => {
const models = Self.app.models;
2020-12-18 16:23:04 +00:00
const TempContainer = models.TempContainer;
const DmsContainer = models.DmsContainer;
const fileOptions = {};
const args = ctx.args;
2019-06-13 07:21:36 +00:00
let tx;
const myOptions = {};
2019-06-13 07:21:36 +00:00
if (typeof options == 'object')
2019-06-20 07:23:55 +00:00
Object.assign(myOptions, options);
2019-06-13 07:21:36 +00:00
2019-06-20 07:23:55 +00:00
if (!myOptions.transaction) {
2019-06-13 07:21:36 +00:00
tx = await Self.beginTransaction({});
2019-06-20 07:23:55 +00:00
myOptions.transaction = tx;
2019-06-13 07:21:36 +00:00
}
2020-12-18 16:23:04 +00:00
let srcFile;
try {
2019-11-22 12:46:38 +00:00
const hasWriteRole = await models.DmsType.hasWriteRole(ctx, args.dmsTypeId, myOptions);
if (!hasWriteRole)
throw new UserError(`You don't have enough privileges`);
2019-06-19 05:36:36 +00:00
// Upload file to temporary path
2020-12-18 16:23:04 +00:00
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 = [];
2020-12-18 16:23:04 +00:00
for (const uploadedFile of files) {
const newDms = await createDms(ctx, uploadedFile, myOptions);
const pathHash = DmsContainer.getHash(newDms.id);
2020-12-18 16:23:04 +00:00
const file = await TempContainer.getFile(tempContainer.name, uploadedFile.name);
srcFile = path.join(file.client.root, file.container, file.name);
2020-12-18 16:23:04 +00:00
const dmsContainer = await DmsContainer.container(pathHash);
const dstFile = path.join(dmsContainer.client.root, pathHash, newDms.file);
await fs.move(srcFile, dstFile, {
overwrite: true
});
addedDms.push(newDms);
}
2019-06-13 07:21:36 +00:00
if (tx) await tx.commit();
return addedDms;
} catch (e) {
2019-06-13 07:21:36 +00:00
if (tx) await tx.rollback();
2020-12-18 16:23:04 +00:00
if (fs.existsSync(srcFile))
await fs.unlink(srcFile);
throw e;
}
};
2019-07-15 11:16:02 +00:00
async function createDms(ctx, file, myOptions) {
const models = Self.app.models;
const myUserId = ctx.req.accessToken.userId;
const args = ctx.args;
const newDms = await Self.create({
2021-02-12 12:18:02 +00:00
workerFk: myUserId,
dmsTypeFk: args.dmsTypeId,
companyFk: args.companyId,
warehouseFk: args.warehouseId,
reference: args.reference,
description: args.description,
2019-07-15 11:16:02 +00:00
contentType: file.type,
hasFile: args.hasFile
2019-06-20 07:23:55 +00:00
}, myOptions);
2019-07-15 11:16:02 +00:00
let fileName = file.name;
2020-12-18 16:23:04 +00:00
const extension = models.DmsContainer.getFileExtension(fileName);
fileName = `${newDms.id}.${extension}`;
2019-06-20 07:23:55 +00:00
return newDms.updateAttribute('file', fileName, myOptions);
}
};