salix/back/models/dms.js

38 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-04-03 15:52:47 +00:00
const UserError = require('vn-loopback/util/user-error');
2019-05-01 16:49:39 +00:00
module.exports = Self => {
require('../methods/dms/downloadFile')(Self);
require('../methods/dms/uploadFile')(Self);
require('../methods/dms/removeFile')(Self);
2019-07-15 09:40:11 +00:00
require('../methods/dms/updateFile')(Self);
2020-04-03 15:52:47 +00:00
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) {
const models = Self.app.models;
2020-12-18 16:23:04 +00:00
const DmsContainer = models.DmsContainer;
2020-04-03 15:52:47 +00:00
const dms = await Self.findById(id);
2020-12-18 16:23:04 +00:00
const pathHash = DmsContainer.getHash(dms.id);
2020-04-03 15:52:47 +00:00
try {
2020-12-18 16:23:04 +00:00
await DmsContainer.getFile(pathHash, dms.file);
2020-04-03 15:52:47 +00:00
} catch (e) {
if (e.code != 'ENOENT')
throw e;
const error = new UserError(`File doesn't exists`);
error.statusCode = 404;
throw error;
}
2020-12-18 16:23:04 +00:00
const stream = DmsContainer.downloadStream(pathHash, dms.file);
2020-04-03 15:52:47 +00:00
return [stream, dms.contentType, `filename="${dms.file}"`];
};
2019-05-01 16:49:39 +00:00
};