salix/back/methods/dms/updateFile.js

144 lines
4.3 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');
2020-12-18 16:23:04 +00:00
const path = require('path');
2019-07-15 09:40:11 +00:00
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'}
2021-02-05 11:09:48 +00:00
},
{
2019-07-15 09:40:11 +00:00
arg: 'warehouseId',
type: 'Number',
description: 'The warehouse id'
2021-02-05 11:09:48 +00:00
},
{
2019-07-15 09:40:11 +00:00
arg: 'companyId',
type: 'Number',
description: 'The company id'
2021-02-05 11:09:48 +00:00
},
{
2019-07-15 09:40:11 +00:00
arg: 'dmsTypeId',
type: 'Number',
description: 'The dms type id'
2021-02-05 11:09:48 +00:00
},
{
2019-07-15 09:40:11 +00:00
arg: 'reference',
type: 'String'
2021-02-05 11:09:48 +00:00
},
{
2019-07-15 09:40:11 +00:00
arg: 'description',
type: 'String'
2021-02-05 11:09:48 +00:00
},
{
arg: 'hasFile',
type: 'Boolean',
2021-02-08 06:28:00 +00:00
description: 'True if has an attached file'
2021-02-05 11:09:48 +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,
2021-02-05 11:09:48 +00:00
description: args.description,
hasFile: args.hasFile
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) {
const models = Self.app.models;
2020-12-18 16:23:04 +00:00
const TempContainer = models.TempContainer;
const DmsContainer = models.DmsContainer;
2019-07-16 11:37:25 +00:00
const fileOptions = {};
2020-12-18 16:23:04 +00:00
const tempContainer = await TempContainer.container('dms');
const makeUpload = await TempContainer.upload(tempContainer.name, ctx.req, ctx.result, fileOptions);
2019-07-16 11:37:25 +00:00
const keys = Object.values(makeUpload.files);
const files = keys.map(file => file[0]);
2020-12-18 16:23:04 +00:00
const uploadedFile = files[0];
2019-07-15 09:40:11 +00:00
2020-12-18 16:23:04 +00:00
if (uploadedFile) {
const oldExtension = DmsContainer.getFileExtension(dms.file);
const newExtension = DmsContainer.getFileExtension(uploadedFile.name);
2019-07-16 11:37:25 +00:00
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) {
2020-12-18 16:23:04 +00:00
const pathHash = DmsContainer.getHash(dms.id);
2019-07-15 09:40:11 +00:00
2020-12-18 16:23:04 +00:00
await DmsContainer.removeFile(pathHash, dms.file);
2019-07-16 11:37:25 +00:00
}
} catch (err) {}
const updatedDms = await dms.updateAttributes({
2020-12-18 16:23:04 +00:00
contentType: uploadedFile.type,
2019-07-16 11:37:25 +00:00
file: fileName
}, myOptions);
2019-07-15 09:40:11 +00:00
2020-12-18 16:23:04 +00:00
const file = await TempContainer.getFile(tempContainer.name, uploadedFile.name);
const srcFile = path.join(file.client.root, file.container, file.name);
2019-07-16 11:37:25 +00:00
2020-12-18 16:23:04 +00:00
const pathHash = DmsContainer.getHash(updatedDms.id);
const dmsContainer = await DmsContainer.container(pathHash);
const dstFile = path.join(dmsContainer.client.root, pathHash, updatedDms.file);
2019-07-16 11:37:25 +00:00
2020-12-18 16:23:04 +00:00
await fs.move(srcFile, dstFile, {
overwrite: true
});
2019-07-16 11:37:25 +00:00
return updatedDms;
}
}
2019-07-15 09:40:11 +00:00
};