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 => {
|
|
|
|
Self.remoteMethodCtx('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-01-09 14:11:53 +00:00
|
|
|
description: 'The file cabinet',
|
2022-02-21 07:48:53 +00:00
|
|
|
http: {source: 'path'}
|
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-01-11 14:19:09 +00:00
|
|
|
path: `/:id/download/:fileCabinet`,
|
2022-02-16 13:57:47 +00:00
|
|
|
verb: 'GET'
|
2022-02-15 15:48:37 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-01-11 14:19:09 +00:00
|
|
|
Self.download = async function(ctx, id, fileCabinet) {
|
2022-02-15 15:48:37 +00:00
|
|
|
const models = Self.app.models;
|
2023-01-11 14:19:09 +00:00
|
|
|
const docuwareFile = await models.Docuware.checkFile(ctx, id, fileCabinet);
|
|
|
|
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
|
|
|
};
|
|
|
|
};
|