From 83a2fa831f4d1aac89c30b09303bd3d6838fa7f6 Mon Sep 17 00:00:00 2001 From: alexm Date: Tue, 5 Nov 2024 16:56:20 +0100 Subject: [PATCH 1/3] refactor(dms): refs #7151 removeFile --- back/models/dms.js | 74 +++++++++++++++++-- .../back/methods/claim-dms/removeFile.js | 34 +-------- .../back/methods/client-dms/removeFile.js | 32 +------- .../back/methods/entry-dms/removeFile.js | 33 +-------- .../supplier/supplier-dms/removeFile.js | 33 +-------- .../back/methods/ticket-dms/removeFile.js | 32 +------- .../back/methods/worker-dms/removeFile.js | 32 +------- 7 files changed, 80 insertions(+), 190 deletions(-) diff --git a/back/models/dms.js b/back/models/dms.js index d6eab4fe4..b21a91a8a 100644 --- a/back/models/dms.js +++ b/back/models/dms.js @@ -16,13 +16,12 @@ module.exports = Self => { return await models.DmsType.hasReadRole(ctx, dms.dmsTypeFk); }; - Self.getFile = async function(id) { - const models = Self.app.models; - const DmsContainer = models.DmsContainer; + Self.getFile = async function(id, container = 'DmsContainer') { + const Container = Self.app.models.models[container]; const dms = await Self.findById(id); - const pathHash = DmsContainer.getHash(dms.id); + const pathHash = Container.getHash(dms.id); try { - await DmsContainer.getFile(pathHash, dms.file); + await Container.getFile(pathHash, dms.file); } catch (e) { if (e.code != 'ENOENT') throw e; @@ -33,7 +32,7 @@ module.exports = Self => { throw error; } - const stream = DmsContainer.downloadStream(pathHash, dms.file); + const stream = Container.downloadStream(pathHash, dms.file); return [stream, dms.contentType, `filename="${dms.file}"`]; }; @@ -65,4 +64,67 @@ module.exports = Self => { 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; + } + }; }; diff --git a/modules/claim/back/methods/claim-dms/removeFile.js b/modules/claim/back/methods/claim-dms/removeFile.js index 28e78c9d7..eb3adea31 100644 --- a/modules/claim/back/methods/claim-dms/removeFile.js +++ b/modules/claim/back/methods/claim-dms/removeFile.js @@ -1,5 +1,3 @@ -const UserError = require('vn-loopback/util/user-error'); - module.exports = Self => { Self.remoteMethodCtx('removeFile', { description: 'Removes a claim document', @@ -20,35 +18,7 @@ module.exports = Self => { } }); - Self.removeFile = async(ctx, id, options) => { - const myOptions = {}; - let tx; - - if (typeof options == 'object') - Object.assign(myOptions, options); - - if (!myOptions.transaction) { - tx = await Self.beginTransaction({}); - myOptions.transaction = tx; - } - - try { - const claimDms = await Self.findById(id, null, myOptions); - - const targetDms = await Self.app.models.Dms.removeFile(ctx, claimDms.dmsFk, myOptions); - - if (!targetDms || ! claimDms) - throw new UserError('Try again'); - - const claimDmsDestroyed = await claimDms.destroy(myOptions); - - if (tx) await tx.commit(); - - return claimDmsDestroyed; - } catch (e) { - if (tx) await tx.rollback(); - throw e; - } - }; + Self.removeFile = async(ctx, id, options) => + Self.app.models.Dms.removeFileTemplate(ctx, id, Self, options); }; diff --git a/modules/client/back/methods/client-dms/removeFile.js b/modules/client/back/methods/client-dms/removeFile.js index bc9a0f719..2cab64d28 100644 --- a/modules/client/back/methods/client-dms/removeFile.js +++ b/modules/client/back/methods/client-dms/removeFile.js @@ -18,35 +18,7 @@ module.exports = Self => { } }); - Self.removeFile = async(ctx, id, options) => { - const myOptions = {}; - let tx; - - if (typeof options == 'object') - Object.assign(myOptions, options); - - if (!myOptions.transaction) { - tx = await Self.beginTransaction({}); - myOptions.transaction = tx; - } - - try { - const clientDms = await Self.findById(id, null, myOptions); - - const targetDms = await Self.app.models.Dms.removeFile(ctx, clientDms.dmsFk, myOptions); - - if (!targetDms || !clientDms) - throw new UserError('Try again'); - - const clientDmsDestroyed = await clientDms.destroy(myOptions); - - if (tx) await tx.commit(); - - return clientDmsDestroyed; - } catch (e) { - if (tx) await tx.rollback(); - throw e; - } - }; + Self.removeFile = async(ctx, id, options) => + Self.app.models.Dms.removeFileTemplate(ctx, id, Self, options); }; diff --git a/modules/entry/back/methods/entry-dms/removeFile.js b/modules/entry/back/methods/entry-dms/removeFile.js index 89a87755c..f28c3fa2b 100644 --- a/modules/entry/back/methods/entry-dms/removeFile.js +++ b/modules/entry/back/methods/entry-dms/removeFile.js @@ -1,5 +1,3 @@ -const UserError = require('vn-loopback/util/user-error'); - module.exports = Self => { Self.remoteMethodCtx('removeFile', { description: 'Removes a entry document', @@ -20,34 +18,7 @@ module.exports = Self => { } }); - Self.removeFile = async(ctx, id, options) => { - const myOptions = {}; - let tx; - - if (typeof options == 'object') - Object.assign(myOptions, options); - - if (!myOptions.transaction) { - tx = await Self.beginTransaction({}); - myOptions.transaction = tx; - } - - try { - const targetEntryDms = await Self.findById(id, null, myOptions); - const targetDms = await Self.app.models.Dms.removeFile(ctx, targetEntryDms.dmsFk, myOptions); - - if (!targetDms) - throw new UserError('Try again'); - - const entryDmsDestroyed = await targetEntryDms.destroy(myOptions); - - if (tx) await tx.commit(); - - return entryDmsDestroyed; - } catch (e) { - if (tx) await tx.rollback(); - throw e; - } - }; + Self.removeFile = async(ctx, id, options) => + Self.app.models.Dms.removeFileTemplate(ctx, id, Self, options); }; diff --git a/modules/supplier/back/methods/supplier/supplier-dms/removeFile.js b/modules/supplier/back/methods/supplier/supplier-dms/removeFile.js index 04a9011f1..3671a747c 100644 --- a/modules/supplier/back/methods/supplier/supplier-dms/removeFile.js +++ b/modules/supplier/back/methods/supplier/supplier-dms/removeFile.js @@ -1,5 +1,3 @@ -const UserError = require('vn-loopback/util/user-error'); - module.exports = Self => { Self.remoteMethodCtx('removeFile', { description: 'Removes a entry document', @@ -20,34 +18,7 @@ module.exports = Self => { } }); - Self.removeFile = async(ctx, id, options) => { - const myOptions = {}; - let tx; - - if (typeof options == 'object') - Object.assign(myOptions, options); - - if (!myOptions.transaction) { - tx = await Self.beginTransaction({}); - myOptions.transaction = tx; - } - - try { - const targetSupplierDms = await Self.findById(id, null, myOptions); - const targetDms = await Self.app.models.Dms.removeFile(ctx, targetSupplierDms.dmsFk, myOptions); - - if (!targetDms) - throw new UserError('Try again'); - - const supplierDmsDestroyed = await targetSupplierDms.destroy(myOptions); - - if (tx) await tx.commit(); - - return supplierDmsDestroyed; - } catch (e) { - if (tx) await tx.rollback(); - throw e; - } - }; + Self.removeFile = async(ctx, id, options) => + Self.app.models.Dms.removeFileTemplate(ctx, id, Self, options); }; diff --git a/modules/ticket/back/methods/ticket-dms/removeFile.js b/modules/ticket/back/methods/ticket-dms/removeFile.js index 6fba4c552..54fc5ff56 100644 --- a/modules/ticket/back/methods/ticket-dms/removeFile.js +++ b/modules/ticket/back/methods/ticket-dms/removeFile.js @@ -18,35 +18,7 @@ module.exports = Self => { } }); - Self.removeFile = async(ctx, id, options) => { - const myOptions = {}; - let tx; - - if (typeof options == 'object') - Object.assign(myOptions, options); - - if (!myOptions.transaction) { - tx = await Self.beginTransaction({}); - myOptions.transaction = tx; - } - - try { - const ticketDms = await Self.findById(id, null, myOptions); - - const targetDms = await Self.app.models.Dms.removeFile(ctx, ticketDms.dmsFk, myOptions); - - if (!targetDms || !ticketDms) - throw new UserError('Try again'); - - const ticketDmsDestroyed = await ticketDms.destroy(myOptions); - - if (tx) await tx.commit(); - - return ticketDmsDestroyed; - } catch (e) { - if (tx) await tx.rollback(); - throw e; - } - }; + Self.removeFile = async(ctx, id, options) => + Self.app.models.Dms.removeFileTemplate(ctx, id, Self, options); }; diff --git a/modules/worker/back/methods/worker-dms/removeFile.js b/modules/worker/back/methods/worker-dms/removeFile.js index 8eb6c05fd..540b8ff84 100644 --- a/modules/worker/back/methods/worker-dms/removeFile.js +++ b/modules/worker/back/methods/worker-dms/removeFile.js @@ -18,35 +18,7 @@ module.exports = Self => { } }); - Self.removeFile = async(ctx, dmsFk, options) => { - const myOptions = {}; - let tx; - if (typeof options == 'object') - Object.assign(myOptions, options); - - if (!myOptions.transaction) { - tx = await Self.beginTransaction({}); - myOptions.transaction = tx; - } - - try { - const WorkerDms = await Self.findOne({ - where: {document: dmsFk} - }, myOptions); - - const targetDms = await Self.app.models.Dms.removeFile(ctx, dmsFk, myOptions); - - if (!targetDms || !WorkerDms) - throw new UserError('Try again'); - - const workerDmsDestroyed = await WorkerDms.destroy(myOptions); - if (tx) await tx.commit(); - - return workerDmsDestroyed; - } catch (e) { - await tx.rollback(); - throw e; - } - }; + Self.removeFile = async(ctx, id, options) => + Self.app.models.Dms.removeFileTemplate(ctx, id, Self, options); }; -- 2.40.1 From 382309752f89aae7abbb2fcb764a4c960513d910 Mon Sep 17 00:00:00 2001 From: alexm Date: Tue, 5 Nov 2024 16:56:33 +0100 Subject: [PATCH 2/3] refactor(dms): refs #7151 uploadFile --- .../claim/back/methods/claim/uploadFile.js | 32 ++--------------- .../client/back/methods/client/uploadFile.js | 34 ++----------------- .../back/methods/entry-dms/uploadFile.js | 32 ++--------------- .../supplier/supplier-dms/uploadFile.js | 32 ++--------------- .../ticket/back/methods/ticket/uploadFile.js | 33 ++---------------- .../worker/back/methods/worker/uploadFile.js | 32 ++--------------- 6 files changed, 12 insertions(+), 183 deletions(-) diff --git a/modules/claim/back/methods/claim/uploadFile.js b/modules/claim/back/methods/claim/uploadFile.js index 4fd041456..a5fd9b405 100644 --- a/modules/claim/back/methods/claim/uploadFile.js +++ b/modules/claim/back/methods/claim/uploadFile.js @@ -53,34 +53,6 @@ module.exports = Self => { } }); - Self.uploadFile = async(ctx, id, options) => { - const {Dms, ClaimDms} = 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 => ClaimDms.create({ - claimFk: 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.uploadFile = async(ctx, id, options) => + Self.app.models.Dms.uploadFileTemplate(ctx, id, 'ClaimDms', 'claimFk', options); }; diff --git a/modules/client/back/methods/client/uploadFile.js b/modules/client/back/methods/client/uploadFile.js index 1a5965955..5f7bb7f9a 100644 --- a/modules/client/back/methods/client/uploadFile.js +++ b/modules/client/back/methods/client/uploadFile.js @@ -54,36 +54,6 @@ module.exports = Self => { } }); - Self.uploadFile = async(ctx, id, options) => { - const {Dms, ClientDms} = 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 => - ClientDms.create({ - clientFk: 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.uploadFile = async(ctx, id, options) => + Self.app.models.Dms.uploadFileTemplate(ctx, id, 'ClientDms', 'clientFk', options); }; diff --git a/modules/entry/back/methods/entry-dms/uploadFile.js b/modules/entry/back/methods/entry-dms/uploadFile.js index 54b56fed4..f29506aa7 100644 --- a/modules/entry/back/methods/entry-dms/uploadFile.js +++ b/modules/entry/back/methods/entry-dms/uploadFile.js @@ -53,34 +53,6 @@ module.exports = Self => { } }); - Self.uploadFile = async(ctx, id, options) => { - const {Dms, EntryDms} = 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 => EntryDms.create({ - entryFk: 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.uploadFile = async(ctx, id, options) => + Self.app.models.Dms.uploadFileTemplate(ctx, id, 'EntryDms', 'entryFk', options); }; diff --git a/modules/supplier/back/methods/supplier/supplier-dms/uploadFile.js b/modules/supplier/back/methods/supplier/supplier-dms/uploadFile.js index a9ff2d125..9b6ec2d14 100644 --- a/modules/supplier/back/methods/supplier/supplier-dms/uploadFile.js +++ b/modules/supplier/back/methods/supplier/supplier-dms/uploadFile.js @@ -53,34 +53,6 @@ module.exports = Self => { } }); - Self.uploadFile = async(ctx, id, options) => { - const {Dms, SupplierDms} = 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 => SupplierDms.create({ - supplierFk: 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.uploadFile = async(ctx, id, options) => + Self.app.models.Dms.uploadFileTemplate(ctx, id, 'SupplierDms', 'supplierFk', options); }; diff --git a/modules/ticket/back/methods/ticket/uploadFile.js b/modules/ticket/back/methods/ticket/uploadFile.js index 5b79aa973..07d4c8971 100644 --- a/modules/ticket/back/methods/ticket/uploadFile.js +++ b/modules/ticket/back/methods/ticket/uploadFile.js @@ -46,35 +46,6 @@ module.exports = Self => { } }); - Self.uploadFile = async(ctx, id, options) => { - const {Dms, TicketDms} = 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 => TicketDms.create({ - ticketFk: 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.uploadFile = async(ctx, id, options) => + Self.app.models.Dms.uploadFileTemplate(ctx, id, 'TicketDms', 'ticketFk', options); }; diff --git a/modules/worker/back/methods/worker/uploadFile.js b/modules/worker/back/methods/worker/uploadFile.js index c3526cbb1..b853806d8 100644 --- a/modules/worker/back/methods/worker/uploadFile.js +++ b/modules/worker/back/methods/worker/uploadFile.js @@ -46,34 +46,6 @@ module.exports = Self => { } }); - Self.uploadFile = async(ctx, id) => { - const {Dms, WorkerDms} = 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 => - WorkerDms.create({ - workerFk: 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.uploadFile = async(ctx, id) => + Self.app.models.Dms.uploadFileTemplate(ctx, id, 'WorkerDms', 'workerFk', options); }; -- 2.40.1 From 379e7437f653803f5b9fe15821d2cdebaf938e9d Mon Sep 17 00:00:00 2001 From: alexm Date: Mon, 11 Nov 2024 14:08:52 +0100 Subject: [PATCH 3/3] refactor(dms): refs #7151 downloadFile --- back/models/dms.js | 33 ++++++++++++++++--- .../claim/back/methods/claim/downloadFile.js | 27 ++------------- .../methods/claim/specs/downloadFile.spec.js | 7 ++-- .../back/methods/entry-dms/downloadFile.js | 27 ++------------- .../worker/back/methods/worker/uploadFile.js | 2 +- 5 files changed, 37 insertions(+), 59 deletions(-) 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); }; -- 2.40.1