salix/back/models/dms.js

131 lines
4.2 KiB
JavaScript

const UserError = require('vn-loopback/util/user-error');
const fs = require('fs-extra');
const path = require('path');
module.exports = Self => {
require('../methods/dms/downloadFile')(Self);
require('../methods/dms/uploadFile')(Self);
require('../methods/dms/removeFile')(Self);
require('../methods/dms/updateFile')(Self);
require('../methods/dms/deleteTrashFiles')(Self);
Self.checkRole = async function(ctx, id) {
const models = Self.app.models;
const dms = await Self.findById(id);
return await models.DmsType.hasReadRole(ctx, dms.dmsTypeFk);
};
Self.getFile = async function(id, container = 'DmsContainer') {
const Container = Self.app.models.models[container];
const dms = await Self.findById(id);
const pathHash = Container.getHash(dms.id);
try {
await Container.getFile(pathHash, dms.file);
} catch (e) {
if (e.code != 'ENOENT')
throw e;
const error = new UserError(`File doesn't exists`);
error.statusCode = 404;
throw error;
}
const stream = Container.downloadStream(pathHash, dms.file);
return [stream, dms.contentType, `filename="${dms.file}"`];
};
Self.getPath = async function(dms) {
const models = Self.app.models;
const pathHash = await models.DmsContainer.getHash(dms.id);
const dmsContainer = await models.DmsContainer.container(pathHash);
const dstFile = path.join(dmsContainer.client.root, pathHash, dms.file);
return dstFile;
};
Self.createWithExtension = async function(data, extension, options) {
const newDms = await Self.create(data, options);
return newDms.updateAttribute('file', `${newDms.id}.${extension}`, options);
};
Self.createFromFile = async function(data, extension, srcFile, options) {
const dms = await Self.createWithExtension(data, extension, options);
const dstFile = await Self.getPath(dms);
await fs.move(srcFile, dstFile, {overwrite: true});
return dms;
};
Self.createFromStream = async function(data, extension, stream, options) {
const dms = await Self.createWithExtension(data, extension, options);
const dstFile = await Self.getPath(dms);
const writeStream = await fs.createWriteStream(dstFile);
await stream.pipe(writeStream);
return dms;
};
Self.uploadFileTemplate = async(ctx, id, model, foreignKey, options) => {
const {Dms, [model]: modelDms} = Self.app.models;
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const uploadedFiles = await Dms.uploadFile(ctx, myOptions);
const promises = uploadedFiles.map(dms =>
modelDms.create({
[foreignKey]: id,
dmsFk: dms.id
}, myOptions)
);
await Promise.all(promises);
if (tx) await tx.commit();
return uploadedFiles;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
Self.removeFileTemplate = async(ctx, id, currentSelf, options) => {
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await currentSelf.beginTransaction({});
myOptions.transaction = tx;
}
try {
const targetModelDms = await currentSelf.findById(id, null, myOptions);
const targetDms = await Self.removeFile(ctx, targetModelDms.dmsFk, myOptions);
if (!targetDms || !targetModelDms)
throw new UserError('Try again');
const modelDmsDestroyed = await targetModelDms.destroy(myOptions);
if (tx) await tx.commit();
return modelDmsDestroyed;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};