2022-02-21 07:48:53 +00:00
|
|
|
/* eslint max-len: ["error", { "code": 180 }]*/
|
2023-01-12 14:16:38 +00:00
|
|
|
const axios = require('axios');
|
2022-02-15 15:48:37 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
|
|
|
|
module.exports = Self => {
|
2023-05-30 13:34:11 +00:00
|
|
|
Self.remoteMethod('download', {
|
2022-02-16 13:57:47 +00:00
|
|
|
description: 'Download an docuware PDF',
|
2022-02-15 15:48:37 +00:00
|
|
|
accessType: 'READ',
|
|
|
|
accepts: [
|
|
|
|
{
|
2022-02-16 13:57:47 +00:00
|
|
|
arg: 'id',
|
2022-02-15 15:48:37 +00:00
|
|
|
type: 'number',
|
2023-01-09 14:11:53 +00:00
|
|
|
description: 'The ticket id',
|
2022-02-21 07:48:53 +00:00
|
|
|
http: {source: 'path'}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'fileCabinet',
|
|
|
|
type: 'string',
|
2023-05-30 13:34:11 +00:00
|
|
|
description: 'The file cabinet'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'filter',
|
|
|
|
type: 'object',
|
|
|
|
description: 'The filter'
|
2022-02-16 13:57:47 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
returns: [
|
|
|
|
{
|
|
|
|
arg: 'body',
|
|
|
|
type: 'file',
|
|
|
|
root: true
|
|
|
|
}, {
|
|
|
|
arg: 'Content-Type',
|
2022-02-21 07:48:53 +00:00
|
|
|
type: 'string',
|
2022-02-16 13:57:47 +00:00
|
|
|
http: {target: 'header'}
|
|
|
|
}, {
|
|
|
|
arg: 'Content-Disposition',
|
2022-02-21 07:48:53 +00:00
|
|
|
type: 'string',
|
2022-02-16 13:57:47 +00:00
|
|
|
http: {target: 'header'}
|
|
|
|
}
|
2022-02-15 15:48:37 +00:00
|
|
|
],
|
|
|
|
http: {
|
2023-05-30 13:34:11 +00:00
|
|
|
path: `/:id/download`,
|
2022-02-16 13:57:47 +00:00
|
|
|
verb: 'GET'
|
2024-02-26 05:57:17 +00:00
|
|
|
},
|
2024-04-19 08:49:57 +00:00
|
|
|
accessScopes: ['DEFAULT', 'read:multimedia']
|
2022-02-15 15:48:37 +00:00
|
|
|
});
|
|
|
|
|
2023-05-30 13:34:11 +00:00
|
|
|
Self.download = async function(id, fileCabinet, filter) {
|
2022-02-15 15:48:37 +00:00
|
|
|
const models = Self.app.models;
|
2023-05-29 13:21:37 +00:00
|
|
|
|
2023-05-30 13:34:11 +00:00
|
|
|
const docuwareFile = await models.Docuware.checkFile(id, fileCabinet, filter);
|
2023-01-11 14:19:09 +00:00
|
|
|
if (!docuwareFile) throw new UserError('The DOCUWARE PDF document does not exists');
|
2022-02-15 15:48:37 +00:00
|
|
|
|
2023-01-11 14:19:09 +00:00
|
|
|
const fileCabinetId = await Self.getFileCabinet(fileCabinet);
|
|
|
|
const options = await Self.getOptions();
|
2023-01-12 14:16:38 +00:00
|
|
|
options.headers.responseType = 'stream';
|
2022-02-21 07:48:53 +00:00
|
|
|
|
2023-01-11 14:19:09 +00:00
|
|
|
const fileName = `filename="${id}.pdf"`;
|
|
|
|
const contentType = 'application/pdf';
|
|
|
|
const downloadUri = `${options.url}/FileCabinets/${fileCabinetId}/Documents/${docuwareFile.id}/FileDownload?targetFileType=Auto&keepAnnotations=false`;
|
2022-02-21 07:48:53 +00:00
|
|
|
|
2023-01-12 14:16:38 +00:00
|
|
|
const stream = await axios.get(downloadUri, options.headers);
|
2022-02-16 13:57:47 +00:00
|
|
|
|
2023-01-12 14:16:38 +00:00
|
|
|
return [stream.data, contentType, fileName];
|
2022-02-15 15:48:37 +00:00
|
|
|
};
|
|
|
|
};
|