salix/back/methods/dms/updateFile.js

150 lines
4.6 KiB
JavaScript
Raw Normal View History

2019-07-15 09:40:11 +00:00
const UserError = require('vn-loopback/util/user-error');
const fs = require('fs-extra');
module.exports = Self => {
Self.remoteMethodCtx('updateFile', {
description: 'updates a file properties or file',
accessType: 'WRITE',
accepts: [{
arg: 'id',
type: 'Number',
description: 'The document id',
http: {source: 'path'}
2020-02-28 13:18:55 +00:00
}, {
2019-07-15 09:40:11 +00:00
arg: 'warehouseId',
type: 'Number',
description: 'The warehouse id'
}, {
arg: 'companyId',
type: 'Number',
description: 'The company id'
}, {
arg: 'dmsTypeId',
type: 'Number',
description: 'The dms type id'
}, {
arg: 'reference',
type: 'String'
}, {
arg: 'description',
type: 'String'
2019-07-16 11:37:25 +00:00
}, {
2019-07-16 12:12:58 +00:00
arg: 'hasFileAttached',
2019-07-16 11:37:25 +00:00
type: 'Boolean',
2019-07-15 09:40:11 +00:00
description: 'True if has an attached file'
}],
returns: {
type: 'Object',
root: true
},
http: {
path: `/:id/updateFile`,
verb: 'POST'
}
});
2020-02-28 13:18:55 +00:00
Self.updateFile = async(ctx, id, options) => {
2019-07-15 09:40:11 +00:00
const models = Self.app.models;
2020-02-28 13:18:55 +00:00
const args = ctx.args;
2019-07-15 09:40:11 +00:00
let tx;
let myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
2020-02-28 13:18:55 +00:00
const hasWriteRole = await models.DmsType.hasWriteRole(ctx, args.dmsTypeId);
2019-07-15 09:40:11 +00:00
if (!hasWriteRole)
throw new UserError(`You don't have enough privileges`);
2019-07-16 11:37:25 +00:00
const dms = await Self.findById(id, null, myOptions);
await dms.updateAttributes({
2020-02-28 13:18:55 +00:00
dmsTypeFk: args.dmsTypeId,
companyFk: args.companyId,
warehouseFk: args.warehouseId,
reference: args.reference,
description: args.description
2019-07-16 11:37:25 +00:00
}, myOptions);
2019-07-15 09:40:11 +00:00
2020-02-28 13:18:55 +00:00
if (args.hasFileAttached)
2019-07-16 11:55:24 +00:00
await uploadNewFile(ctx, dms, myOptions);
2019-07-15 09:40:11 +00:00
if (tx) await tx.commit();
2019-07-16 11:55:24 +00:00
return dms;
2019-07-15 09:40:11 +00:00
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
2019-07-16 11:37:25 +00:00
async function uploadNewFile(ctx, dms, myOptions) {
2019-07-15 09:40:11 +00:00
const storageConnector = Self.app.dataSources.storage.connector;
2019-07-16 11:37:25 +00:00
const models = Self.app.models;
const fileOptions = {};
2019-07-15 09:40:11 +00:00
2019-07-16 11:37:25 +00:00
const tempContainer = await getContainer('temp');
const makeUpload = await models.Container.upload(tempContainer.name, ctx.req, ctx.result, fileOptions);
const keys = Object.values(makeUpload.files);
const files = keys.map(file => file[0]);
const file = files[0];
2019-07-15 09:40:11 +00:00
2019-07-16 11:37:25 +00:00
if (file) {
const oldExtension = storageConnector.getFileExtension(dms.file);
const newExtension = storageConnector.getFileExtension(file.name);
const fileName = `${dms.id}.${newExtension}`;
2019-07-15 09:40:11 +00:00
2019-07-16 11:37:25 +00:00
try {
if (oldExtension != newExtension) {
const pathHash = storageConnector.getPathHash(dms.id);
2019-07-15 09:40:11 +00:00
2019-07-16 11:37:25 +00:00
await models.Container.removeFile(pathHash, dms.file);
}
} catch (err) {}
const updatedDms = await dms.updateAttributes({
contentType: file.type,
file: fileName
}, myOptions);
2019-07-15 09:40:11 +00:00
2019-07-16 11:37:25 +00:00
const pathHash = storageConnector.getPathHash(updatedDms.id);
const container = await getContainer(pathHash);
const originPath = `${tempContainer.client.root}/${tempContainer.name}/${file.name}`;
const destinationPath = `${container.client.root}/${pathHash}/${updatedDms.file}`;
fs.rename(originPath, destinationPath);
return updatedDms;
}
}
2019-07-15 09:40:11 +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;
}
};