salix/back/models/dms.js

38 lines
1.2 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 storageConnector = Self.app.dataSources.storage.connector;
const models = Self.app.models;
const dms = await Self.findById(id);
const pathHash = storageConnector.getPathHash(dms.id);
try {
await models.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 = models.Container.downloadStream(pathHash, dms.file);
return [stream, dms.contentType, `filename="${dms.file}"`];
};
2019-05-01 16:49:39 +00:00
};