WIP: 7151-dms_refactor_methods #3164

Draft
alexm wants to merge 4 commits from 7151-dms_refactor_methods into dev
5 changed files with 37 additions and 59 deletions
Showing only changes of commit 379e7437f6 - Show all commits

View File

@ -16,12 +16,13 @@ module.exports = Self => {
return await models.DmsType.hasReadRole(ctx, dms.dmsTypeFk);
};
Self.getFile = async function(id, container = 'DmsContainer') {
const Container = Self.app.models.models[container];
Self.getFile = async function(id) {
const models = Self.app.models;
const DmsContainer = models.DmsContainer;
const dms = await Self.findById(id);
const pathHash = Container.getHash(dms.id);
const pathHash = DmsContainer.getHash(dms.id);
try {
await Container.getFile(pathHash, dms.file);
await DmsContainer.getFile(pathHash, dms.file);
} catch (e) {
if (e.code != 'ENOENT')
throw e;
@ -32,7 +33,7 @@ module.exports = Self => {
throw error;
}
const stream = Container.downloadStream(pathHash, dms.file);
const stream = DmsContainer.downloadStream(pathHash, dms.file);
return [stream, dms.contentType, `filename="${dms.file}"`];
};
@ -127,4 +128,26 @@ module.exports = Self => {
throw e;
}
};
Self.downloadFileTemplate = async(id, container) => {
const models = Self.app.models;
const Container = models[container];
const dms = await models.Dms.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}"`];
};
};

View File

@ -1,7 +1,5 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('downloadFile', {
Self.remoteMethod('downloadFile', {
description: 'Get the claim file',
accessType: 'READ',
accepts: [
@ -36,25 +34,6 @@ module.exports = Self => {
accessScopes: ['DEFAULT', 'read:multimedia']
});
Self.downloadFile = async function(ctx, id) {
const models = Self.app.models;
const ClaimContainer = models.ClaimContainer;
const dms = await models.Dms.findById(id);
const pathHash = ClaimContainer.getHash(dms.id);
try {
await ClaimContainer.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 = ClaimContainer.downloadStream(pathHash, dms.file);
return [stream, dms.contentType, `filename="${dms.file}"`];
};
Self.downloadFile = async id =>
Self.app.models.Dms.downloadFileTemplate(id, 'ClaimContainer');
};

View File

@ -1,12 +1,9 @@
const app = require('vn-loopback/server/server');
describe('claim downloadFile()', () => {
const dmsId = 7;
it('should return a response for an employee with image content-type', async() => {
const workerId = 1107;
const ctx = {req: {accessToken: {userId: workerId}}};
const result = await app.models.Claim.downloadFile(ctx, dmsId);
const dmsId = 7;
const result = await app.models.Claim.downloadFile(dmsId);
expect(result[1]).toEqual('image/jpeg');
});

View File

@ -1,7 +1,5 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('downloadFile', {
Self.remoteMethod('downloadFile', {
description: 'Get the entry file',
accessType: 'READ',
accepts: [
@ -35,25 +33,6 @@ module.exports = Self => {
}
});
Self.downloadFile = async function(ctx, id) {
const models = Self.app.models;
const EntryContainer = models.EntryContainer;
const dms = await models.Dms.findById(id);
const pathHash = EntryContainer.getHash(dms.id);
try {
await EntryContainer.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 = EntryContainer.downloadStream(pathHash, dms.file);
return [stream, dms.contentType, `filename="${dms.file}"`];
};
Self.downloadFile = async id =>
Self.app.models.Dms.downloadFileTemplate(id, 'EntryContainer');
};

View File

@ -46,6 +46,6 @@ module.exports = Self => {
}
});
Self.uploadFile = async(ctx, id) =>
Self.uploadFile = async(ctx, id, options) =>
Self.app.models.Dms.uploadFileTemplate(ctx, id, 'WorkerDms', 'workerFk', options);
};