2019-06-06 11:59:11 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2019-06-20 05:46:30 +00:00
|
|
|
const fs = require('fs-extra');
|
2019-06-06 11:59:11 +00:00
|
|
|
|
|
|
|
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',
|
2019-10-21 06:31:14 +00:00
|
|
|
description: 'The warehouse id',
|
|
|
|
required: true
|
2019-06-13 07:21:36 +00:00
|
|
|
}, {
|
|
|
|
arg: 'companyId',
|
|
|
|
type: 'Number',
|
2019-10-21 06:31:14 +00:00
|
|
|
description: 'The company id',
|
|
|
|
required: true
|
2019-06-13 07:21:36 +00:00
|
|
|
}, {
|
|
|
|
arg: 'dmsTypeId',
|
|
|
|
type: 'Number',
|
2019-10-21 06:31:14 +00:00
|
|
|
description: 'The dms type id',
|
|
|
|
required: true
|
2019-06-13 07:21:36 +00:00
|
|
|
}, {
|
|
|
|
arg: 'reference',
|
2019-10-21 06:31:14 +00:00
|
|
|
type: 'String',
|
|
|
|
required: true
|
2019-06-13 07:21:36 +00:00
|
|
|
}, {
|
|
|
|
arg: 'description',
|
2019-10-21 06:31:14 +00:00
|
|
|
type: 'String',
|
|
|
|
required: true
|
2019-06-13 07:21:36 +00:00
|
|
|
}, {
|
|
|
|
arg: 'hasFile',
|
|
|
|
type: 'Boolean',
|
2019-10-21 06:31:14 +00:00
|
|
|
description: 'True if has an attached file',
|
|
|
|
required: true
|
2019-06-20 05:46:30 +00:00
|
|
|
}],
|
2019-06-06 11:59:11 +00:00
|
|
|
returns: {
|
|
|
|
type: 'Object',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/uploadFile`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-06-20 07:23:55 +00:00
|
|
|
Self.uploadFile = async(ctx, options) => {
|
2019-06-06 11:59:11 +00:00
|
|
|
const storageConnector = Self.app.dataSources.storage.connector;
|
2019-06-20 05:46:30 +00:00
|
|
|
const models = Self.app.models;
|
2019-06-06 11:59:11 +00:00
|
|
|
const fileOptions = {};
|
2019-06-20 05:46:30 +00:00
|
|
|
const args = ctx.args;
|
2019-06-06 11:59:11 +00:00
|
|
|
|
2019-06-13 07:21:36 +00:00
|
|
|
let tx;
|
2019-06-20 07:23:55 +00:00
|
|
|
let 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
|
|
|
}
|
2019-06-06 11:59:11 +00:00
|
|
|
|
|
|
|
try {
|
2019-11-22 12:46:38 +00:00
|
|
|
const hasWriteRole = await models.DmsType.hasWriteRole(ctx, args.dmsTypeId, myOptions);
|
2019-06-06 11:59:11 +00:00
|
|
|
if (!hasWriteRole)
|
|
|
|
throw new UserError(`You don't have enough privileges`);
|
|
|
|
|
2019-06-19 05:36:36 +00:00
|
|
|
// Upload file to temporary path
|
2019-06-20 05:46:30 +00:00
|
|
|
const tempContainer = await getContainer('temp');
|
|
|
|
const uploaded = await models.Container.upload(tempContainer.name, ctx.req, ctx.result, fileOptions);
|
2019-06-06 11:59:11 +00:00
|
|
|
const files = Object.values(uploaded.files).map(file => {
|
|
|
|
return file[0];
|
|
|
|
});
|
|
|
|
|
2019-06-20 05:46:30 +00:00
|
|
|
const addedDms = [];
|
|
|
|
for (const file of files) {
|
2019-07-15 11:16:02 +00:00
|
|
|
const newDms = await createDms(ctx, file, myOptions);
|
2019-06-20 05:46:30 +00:00
|
|
|
const pathHash = storageConnector.getPathHash(newDms.id);
|
|
|
|
const container = await getContainer(pathHash);
|
2019-06-06 11:59:11 +00:00
|
|
|
|
2019-06-20 05:46:30 +00:00
|
|
|
const originPath = `${tempContainer.client.root}/${tempContainer.name}/${file.name}`;
|
|
|
|
const destinationPath = `${container.client.root}/${pathHash}/${newDms.file}`;
|
2019-06-06 11:59:11 +00:00
|
|
|
|
2019-11-22 12:46:38 +00:00
|
|
|
await fs.rename(originPath, destinationPath);
|
2019-06-20 05:46:30 +00:00
|
|
|
|
|
|
|
addedDms.push(newDms);
|
|
|
|
}
|
2019-06-06 11:59:11 +00:00
|
|
|
|
2019-06-13 07:21:36 +00:00
|
|
|
if (tx) await tx.commit();
|
2019-06-20 05:46:30 +00:00
|
|
|
return addedDms;
|
2019-06-06 11:59:11 +00:00
|
|
|
} catch (e) {
|
2019-06-13 07:21:36 +00:00
|
|
|
if (tx) await tx.rollback();
|
2019-06-06 11:59:11 +00:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
2019-06-20 05:46:30 +00:00
|
|
|
|
2019-07-15 11:16:02 +00:00
|
|
|
async function createDms(ctx, file, myOptions) {
|
2019-06-20 05:46:30 +00:00
|
|
|
const models = Self.app.models;
|
|
|
|
const storageConnector = Self.app.dataSources.storage.connector;
|
|
|
|
const myUserId = ctx.req.accessToken.userId;
|
2019-10-10 06:33:22 +00:00
|
|
|
const myWorker = await models.Worker.findOne({where: {userFk: myUserId}}, myOptions);
|
2019-06-20 05:46:30 +00:00
|
|
|
const args = ctx.args;
|
|
|
|
|
|
|
|
const newDms = await Self.create({
|
|
|
|
workerFk: myWorker.id,
|
|
|
|
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,
|
2019-06-20 05:46:30 +00:00
|
|
|
hasFile: args.hasFile
|
2019-06-20 07:23:55 +00:00
|
|
|
}, myOptions);
|
2019-06-20 05:46:30 +00:00
|
|
|
|
2019-07-15 11:16:02 +00:00
|
|
|
let fileName = file.name;
|
2019-06-20 05:46:30 +00:00
|
|
|
const extension = storageConnector.getFileExtension(fileName);
|
|
|
|
fileName = `${newDms.id}.${extension}`;
|
|
|
|
|
2019-06-20 07:23:55 +00:00
|
|
|
return newDms.updateAttribute('file', fileName, myOptions);
|
2019-06-20 05:46:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a container instance
|
|
|
|
* If doesn't exists creates a new one
|
|
|
|
*
|
|
|
|
* @param {String} name Container name
|
|
|
|
* @return {Object} Container instance
|
|
|
|
*/
|
|
|
|
async function getContainer(name) {
|
|
|
|
const models = Self.app.models;
|
|
|
|
let container;
|
|
|
|
try {
|
|
|
|
container = await models.Container.getContainer(name);
|
|
|
|
} catch (err) {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
container = await models.Container.createContainer({
|
|
|
|
name: name
|
|
|
|
});
|
|
|
|
} else throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return container;
|
|
|
|
}
|
2019-06-06 11:59:11 +00:00
|
|
|
};
|