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'}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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'
|
|
|
|
}, {
|
|
|
|
arg: 'hasFile',
|
|
|
|
type: 'Boolean',
|
2019-07-16 11:37:25 +00:00
|
|
|
description: 'True if has original file'
|
|
|
|
}, {
|
|
|
|
arg: 'hasAttachedFile',
|
|
|
|
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'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.updateFile = async(ctx, id, warehouseId, companyId,
|
2019-07-16 11:37:25 +00:00
|
|
|
dmsTypeId, reference, description, hasFile, hasAttachedFile, options) => {
|
2019-07-15 09:40:11 +00:00
|
|
|
const models = Self.app.models;
|
|
|
|
|
|
|
|
let tx;
|
|
|
|
let myOptions = {};
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const hasWriteRole = await models.DmsType.hasWriteRole(ctx, dmsTypeId);
|
|
|
|
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({
|
|
|
|
dmsTypeFk: dmsTypeId,
|
|
|
|
companyFk: companyId,
|
|
|
|
warehouseFk: warehouseId,
|
|
|
|
reference: reference,
|
|
|
|
description: description,
|
|
|
|
hasFile: hasFile
|
|
|
|
}, myOptions);
|
2019-07-15 09:40:11 +00:00
|
|
|
|
2019-07-16 11:37:25 +00:00
|
|
|
if (hasAttachedFile)
|
|
|
|
updatedDms = await uploadNewFile(ctx, dms, myOptions);
|
2019-07-15 09:40:11 +00:00
|
|
|
|
|
|
|
if (tx) await tx.commit();
|
2019-07-16 11:37:25 +00:00
|
|
|
return updatedDms;
|
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;
|
|
|
|
}
|
|
|
|
};
|