diff --git a/back/models/dms.js b/back/models/dms.js index b21a91a8a..adebd2bb3 100644 --- a/back/models/dms.js +++ b/back/models/dms.js @@ -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}"`]; + }; }; diff --git a/modules/claim/back/methods/claim/downloadFile.js b/modules/claim/back/methods/claim/downloadFile.js index ffcf51367..f15d6ae92 100644 --- a/modules/claim/back/methods/claim/downloadFile.js +++ b/modules/claim/back/methods/claim/downloadFile.js @@ -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'); }; diff --git a/modules/claim/back/methods/claim/specs/downloadFile.spec.js b/modules/claim/back/methods/claim/specs/downloadFile.spec.js index 3e46a9bc3..eb1c3c79e 100644 --- a/modules/claim/back/methods/claim/specs/downloadFile.spec.js +++ b/modules/claim/back/methods/claim/specs/downloadFile.spec.js @@ -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'); }); diff --git a/modules/entry/back/methods/entry-dms/downloadFile.js b/modules/entry/back/methods/entry-dms/downloadFile.js index a4f10f9dc..6ab7154a0 100644 --- a/modules/entry/back/methods/entry-dms/downloadFile.js +++ b/modules/entry/back/methods/entry-dms/downloadFile.js @@ -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'); }; diff --git a/modules/worker/back/methods/worker/uploadFile.js b/modules/worker/back/methods/worker/uploadFile.js index b853806d8..5c00a804f 100644 --- a/modules/worker/back/methods/worker/uploadFile.js +++ b/modules/worker/back/methods/worker/uploadFile.js @@ -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); };